fork download
  1. #include <iostream>
  2. #include <sys/mman.h>
  3. #include <string.h>
  4.  
  5. typedef unsigned char UC;
  6. const static UC v[]={0x55,0x48,0x89,0xe5,0xb8,0x4b,0x0,0x0,0x0,0x5d,0xc3};
  7. const static UC z[]={0x55,0x48,0x89,0xe5,0x89,0xf8,0x88,0x45,0xfc,0x80,0x7d,0xfc,0x0,0x74,0x7,0xb8,0x14,0x0,0x0,0x0,0xeb,0x5,0xb8,0x1e,0x0,0x0,0x0,0x5d,0xc3};
  8.  
  9. int main(){
  10. void* w=mmap(0,100,PROT_EXEC|PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0);
  11. int n=11;
  12. memcpy(w,v,n);
  13. int(*a)()=(int(*)())w;
  14. std::cout<<a()<<std::endl;
  15. n=sizeof(z);
  16. memcpy(w,z,n);
  17. int(*x)(bool)=(int(*)(bool))w;
  18. std::cout<<x(1)<<std::endl;
  19. std::cout<<x(0)<<std::endl;
  20. return 0;
  21. }
  22.  
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
75
30
30