Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #include <fcntl.h>
  5. #include <sys/mman.h>
  6. #include <unistd.h>
  7.  
  8. // Alternatively, /dev/shm/test, /var/tmp/test, /home/username/test, /home/.local/share/appname/test, etc.
  9. static const char *const path = "/tmp/test";
  10. static const char code[] = "\xeb\x1e\x5e\x48\x31\xc0\xb0\x01\x48\x89\xc7\x48\x89\xfa\x48\x83\xc2\x0e\x0f\x05\x48\x31\xc0\x48\x83\xc0\x3c\x48\x31\xff\x0f\x05\xe8\xdd\xff\xff\xff\x48\x65\x6c\x6c\x6f\x2c\x20\x77\x6f\x72\x6c\x64\x21\x0a";
  11.  
  12. int main(void) {
  13. size_t len = 4096;
  14. int fd = open("/tmp/test", O_RDWR|O_CREAT, 0700);
  15. if (fd == -1) {
  16. perror("shm_open");
  17. return 1;
  18. }
  19. if (ftruncate(fd, len) == -1) {
  20. perror("ftruncate");
  21. return 1;
  22. }
  23. void *rw = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  24. if (rw == MAP_FAILED) {
  25. perror("mmap");
  26. return 1;
  27. }
  28.  
  29. void *rx = mmap(NULL, len, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0);
  30. if (rx == MAP_FAILED) {
  31. perror("mmap");
  32. return 1;
  33. }
  34.  
  35. memcpy(rw, code, sizeof(code));
  36. (*(void(*)()) rx)();
  37.  
  38. close(fd);
  39. if (unlink("/tmp/test") == -1) {
  40. perror("unlink");
  41. return 1;
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement