52. Stack Smashing.pdf
(
399 KB
)
Pobierz
.oO Phrack 49 Oo.
Volume Seven, Issue FortyNine File 14 of 16
BugTraq, r00t, and Underground.Org
bring you
Smashing The Stack For Fun And Profit
Aleph One
aleph1@underground.org
`smash the stack` [C programming] n. On many C implementations it is possible to corrupt the
execution stack by writing past the end of an array declared auto in a routine. Code that does this is
said to smash the stack, and can cause return from the routine to jump to a random address. This
can produce some of the most insidious datadependent bugs known to mankind. Variants include
trash the stack, scribble the stack, mangle the stack; the term mung the stack is not used, as this is
never done intentionally. See spam; see also alias bug, fandango on core, memory leak, precedence
lossage, overrun screw.
Introduction
Over the last few months there has been a large increase of buffer overflow vulnerabilities being both
discovered and exploited. Examples of these are syslog, splitvt, sendmail 8.7.5, Linux/FreeBSD mount, Xt
library, at, etc. This paper attempts to explain what buffer overflows are, and how their exploits work. Basic
knowledge of assembly is required. An understanding of virtual memory concepts, and experience with gdb are
very helpful but not necessary. We also assume we are working with an Intel x86 CPU, and that the operating
system is Linux. Some basic definitions before we begin: A buffer is simply a contiguous block of computer
memory that holds multiple instances of the same data type. C programmers normally associate with the word
buffer arrays. Most commonly, character arrays. Arrays, like all variables in C, can be declared either static or
dynamic. Static variables are allocated at load time on the data segment. Dynamic variables are allocated at run
time on the stack. To overflow is to flow, or fill over the top, brims, or bounds. We will concern ourselves only
with the overflow of dynamic buffers, otherwise known as stackbased buffer overflows.
Process Memory Organization
To understand what stack buffers are we must first understand how a process is organized in memory. Processes
are divided into three regions: Text, Data, and Stack. We will concentrate on the stack region, but first a small
overview of the other regions is in order. The text region is fixed by the program and includes code
(instructions) and readonly data. This region corresponds to the text section of the executable file. This region
is normally marked readonly and any attempt to write to it will result in a segmentation violation. The data
region contains initialized and uninitialized data. Static variables are stored in this region. The data region
corresponds to the databss sections of the executable file. Its size can be changed with the brk(2) system call. If
the expansion of the bss data or the user stack exhausts available memory, the process is blocked and is
rescheduled to run again with a larger memory space. New memory is added between the data and stack
segments.
/------------------\
|
|
|
Text
|
|
|
|------------------|
|
(Initialized) |
|
Data
|
| (Uninitialized) |
|------------------|
|
|
|
Stack
|
|
|
\------------------/
lower
memory
addresses
higher
memory
addresses
Fig. 1 Process Memory Regions
What Is A Stack?
A stack is an abstract data type frequently used in computer science. A stack of objects has the property that the
last object placed on the stack will be the first object removed. This property is commonly referred to as last in,
first out queue, or a LIFO. Several operations are defined on stacks. Two of the most important are PUSH and
POP. PUSH adds an element at the top of the stack. POP, in contrast, reduces the stack size by one by removing
the last element at the top of the stack.
Why Do We Use A Stack?
Modern computers are designed with the need of highlevel languages in mind. The most important technique
for structuring programs introduced by highlevel languages is the procedure or function. From one point of
view, a procedure call alters the flow of control just as a jump does, but unlike a jump, when finished
performing its task, a function returns control to the statement or instruction following the call. This highlevel
abstraction is implemented with the help of the stack. The stack is also used to dynamically allocate the local
variables used in functions, to pass parameters to the functions, and to return values from the function.
The Stack Region
A stack is a contiguous block of memory containing data. A register called the stack pointer (SP) points to the
top of the stack. The bottom of the stack is at a fixed address. Its size is dynamically adjusted by the kernel at
run time. The CPU implements instructions to PUSH onto and POP off of the stack. The stack consists of
logical stack frames that are pushed when calling a function and popped when re
turning. A stack frame
contains the parameters to a function, its local variables, and the data necessary to recover the previous stack
frame, including the value of the instruction pointer at the time of the function call. Depending on the
implementation the stack will either grow down (towards lower memory addresses), or up. In our examples
we'll use a stack that grows down. This is the way the stack grows on many computers including the Intel,
Motorola, SPARC and MIPS processors. The stack pointer (SP) is also implementation dependent. It may point
to the last address on the stack, or to the next free available address after the stack. For our discussion we'll
assume it points to the last address on the stack. In addition to the stack pointer, which points to the top of the
stack (lowest numerical address), it is often convenient to have a frame pointer (FP) which points to a fixed
location within a frame. Some texts also refer to it as a local base pointer (LB). In principle, local variables
could be referenced by giving their offsets from SP. However, as words are pushed onto the stack and popped
from the stack, these offsets change. Although in some cases the compiler can keep track of the number of
words on the stack and thus correct the offsets, in some cases it cannot, and in all cases considerable
administration is required. Furthermore, on some machines, such as Intelbased processors, accessing a variable
at a known distance from SP requires multiple instructions. Consequently, many compilers use a second
register, FP, for referencing both local variables and parameters because their distances from FP do not change
with PUSHes and POPs. On Intel CPUs, BP (EBP) is used for this purpose. On the Motorola CPUs, any
address register except A7 (the stack pointer) will do. Because the way our stack grows, actual parameters have
positive offsets and local variables have negative offsets from FP. The first thing a procedure must do when
called is save the previous FP (so it can be restored at procedure exit). Then it copies SP into FP to create the
new FP, and advances SP to reserve space for the local variables. This code is called the procedure prolog.
Upon procedure exit, the stack must be cleaned up again, something called the procedure epilog. The Intel
ENTER and LEAVE instructions and the Motorola LINK and UNLINK instructions, have been provided to do
most of the procedure prolog and epilog work efficiently. Let us see what the stack looks like in a simple
example:
example1.c:
void function(int a, int b, int c) {
char buffer1[5];
char buffer2[10];
}
void main() {
function(1,2,3);
}
To understand what the program does to call function() we compile it with gcc using the S switch to generate
assembly code output:
$ gcc -S -o example1.s example1.c
By looking at the assembly language output we see that the call to function() is translated to:
pushl $3
pushl $2
pushl $1
call function
This pushes the 3 arguments to function backwards into the stack, and calls function(). The instruction 'call' will
push the instruction pointer (IP) onto the stack. We'll call the saved IP the return address (RET). The first thing
done in function is the procedure prolog:
pushl %ebp
movl %esp,%ebp
subl $20,%esp
This pushes EBP, the frame pointer, onto the stack. It then copies the current SP onto EBP, making it the new
FP pointer. We'll call the saved FP pointer SFP. It then allocates space for the local variables by subtracting
their size from SP.
We must remember that memory can only be addressed in multiples of the word size. A word in our case is 4
bytes, or 32 bits. So our 5 byte buffer is really going to take 8 bytes (2 words) of memory, and our 10 byte
buffer is going to take 12 bytes (3 words) of memory. That is why SP is being subtracted by 20. With that in
mind our stack looks like this when function() is called (each space represents a byte):
bottom of
memory
<------
top of
stack
buffer2
[
buffer1
sfp
ret
a
][
][
][
][
b
][
c
][
]
bottom of
stack
top of
memory
Buffer Overflows
A buffer overflow is the result of stuffing more data into a buffer than it can handle. How can this often found
programming error can be taken advantage to execute arbitrary code? Lets look at another example:
example2.c
void function(char *str) {
char buffer[16];
}
strcpy(buffer,str);
void main() {
char large_string[256];
int i;
for( i = 0; i < 255; i++)
large_string[i] = 'A';
}
function(large_string);
This program has a function with a typical buffer overflow coding error. The function copies a supplied string
without bounds checking by using strcpy() instead of strncpy(). If you run this program you will get a
segmentation violation. Lets see what its stack looks [like] when we call function:
bottom of
memory
<------
top of
stack
buffer
[
sfp
ret
*str
][
][
][
]
top of
memory
bottom of
stack
What is going on here? Why do we get a segmentation violation? Simple. strcpy() is copying the contents of
*str (larger_string[]) into buffer[] until a null character is found on the string. As we can see buffer[] is much
smaller than *str. buffer[] is 16 bytes long, and we are trying to stuff it with 256 bytes. This means that all 250
[240] bytes after buffer in the stack are being overwritten. This includes the SFP, RET, and even *str! We had
filled large_string with the character 'A'. It's hex character value is 0x41. That means that the return
address is now 0x41414141. This is outside of the process address space. That is why when the function returns
and tries to read the next instruction from that address you get a segmentation violation. So a buffer overflow
allows us to change the return address of a function. In this way we can change the flow of execution of the
program. Lets go back to our first example and recall what the stack looked like:
bottom of
memory
<------
top of
stack
buffer2
[
buffer1
sfp
ret
a
][
][
][
][
b
][
c
][
]
bottom of
stack
top of
memory
Lets try to modify our first example so that it overwrites the return address, and demonstrate how we can make
it execute arbitrary code. Just before buffer1[] on the stack is SFP, and before it, the return address. That is 4
bytes pass the end of buffer1[]. But remember that buffer1[] is really 2 word so its 8 bytes long. So the return
address is 12 bytes from the start of buffer1[]. We'll modify the return value in such a way that the assignment
statement 'x = 1;' after the function call will be jumped. To do so we add 8 bytes to the return address.
Our code is now: example3.c:
void function(int a, int b, int c) {
char buffer1[5];
char buffer2[10];
int *ret;
ret = buffer1 + 12;
(*ret) += 8;
}
void main() {
int x;
x = 0;
function(1,2,3);
x = 1;
printf("%d\n",x);
}
Plik z chomika:
WMatrixie
Inne pliki z tego folderu:
53. Syngress - Sockets, Shellcode, Porting & Coding - Reverse Engineering Exploits And Tool Coding For Security Professionals.pdf
(115030 KB)
53. Syngress - Sockets, Shellcode, Porting & Coding - Reverse Engineering Exploits And Tool Coding For Security Professionals_2.pdf
(115030 KB)
26. Hacker School.pdf
(29911 KB)
24. Network Attacks and Exploitation.pdf
(25752 KB)
18. CompTIA Cybersecurit 2017.pdf
(16795 KB)
Inne foldery tego chomika:
Cracking_For_The_Beginner
Forbidden Knowledge
General
Guide_on_Cracking
Kali tuts
Zgłoś jeśli
naruszono regulamin