Buffer Overflow Ex
Buffer Overflow Ex
S4.1. How are the addresses decided for the following variables a and x, i.e., during the run-
time, how does the program know the address of these two variables?
void foo(int a)
{
int x;
}
S4.2. In which memory segments are the variables in the following code located?
int i = 0;
void func(char *str)
{
char *ptr = malloc(sizeof(int));
char buf[1024];
int j;
static int y;
}
S4.3. Please draw the function stack frame for the following C function.
int bof(char *str)
{
char buffer[24];
strcpy(buffer,str);
return 1;
}
S4.4. A student proposes to change how the stack grows. Instead of growing from high address
to low address, the student proposes to let the stack grow from low address to high
address. This way, the buffer will be allocated above the return address, so overflowing
the buffer will not be able to affect the return address. Please comment on this proposal.
S4.5. In the buffer overflow example shown in the book (i.e., the vulnerable stack.c pro-
gram), the buffer overflow occurs inside the strcpy() function, so the jumping to the
malicious code occurs when strcpy() returns, not when foo() returns. Is this true
or false? Please explain.
S4.6. The buffer overflow example was fixed as below. Is this safe ?
int bof(char *str, int size)
{
char *buffer = (char *) malloc(size);
strcpy(buffer, str);
return 1;
}
S4.7. Several students had issue with the buffer overflow attack. Their badfile was constructed
properly, with the shellcode being put at the end of badfile. However when they try
different return addresses, they get the following observations. Can you explain why
some addresses work and some do not?
buffer address : 0xbffff180
case 1 : retAddr = 0xbffff250 -> Able to get shell access
case 2 : retAddr = 0xbffff280 -> Able to get shell access
case 3 : retAddr = 0xbffff300 -> Cannot get shell access
case 4 : retAddr = 0xbffff310 -> Able to get shell access
case 5: retAddr = 0xbffff400 -> Cannot get shell access
S4.8. The following function is called in a privileged program. The argument str points to a
string that is entirely provided by users (the size of the string is up to 300 bytes). When
this function is invoked, the address of the buffer array is 0xAABB0010, while the
return address is stored in 0xAABB0050. Please write down the string that you would
feed into the program, so when this string is copied to buffer and when the bof()
function returns, the privileged program will run your code. In your answer, you don’t
need to write down the injected code, but the offsets of the key elements in your string
need to be correct. Note: there is a trap in this problem; some people may be lucky and
step over it, but some people may fall into it. Be careful.
int bof(char *str)
{
char buffer[24];
strcpy(buffer,str);
return 1;
}
S4.9. H H H
In this problem, we will figure out how overflowing a buffer on the heap can lead to the
execution of malicious code. The following is a snippet of a code execution sequence
(not all code in this sequence is shown here). During the execution of this sequence,
the memory locations of buffer and the node p, which are allocated on the heap, are
depicted in Figure 1. You can provide your input (up to 300 bytes) in user input,
which will be copied to buffer. Your job is to overflow the buffer, so when the target
program gets to Line Ì, it will jump to the code that you have injected into the heap
memory (assuming that the heap memory is executable). The return address is stored at
location 0xBBFFAACC.
Buffer Overflow Attack 3
pre
next
p Address B: 0x804B22C0
struct Node
{
struct Node *next;
struct Node *pre;
};
return; Ì
Hint: You still want to place the starting address of your malicious code into the re-
turn address field located at 0xBBFFAACC. Unlike stack-back buffer overflows, where
you can naturally reach the return address field via overflowing, now the buffer is on
the heap, but the return address is on the stack; you cannot reach the stack by overflow-
ing something on the heap. You should take advantage of the operations on the linked
4 Buffer Overflow Attack
char buffer[X];
strcpy(buffer,str);
return 1;
}