Writing shellcode for 32-bit Linux
I was doing some practice with stack based buffer overflows in 32-bit linux (Kali) and wanted to dig a bit deeper into how the shellcode payloads work. So instead of just reaching for msfvenom
I decided to try to roll my own simple shellcode. It wasn’t to difficult in the end, just have to take it a step at a time:
- Write a simple assembly program
- Test it out
- Extract the code in a hexadecimal format
I already had a vulnerable program I could test the payload at (that whole process is another story), so most of the hard work was done. A simple test was to see if I could write my own netcat bind shell, by using the execve
system call to run /bin/nc -lvp 4444 -e /bin/sh
.
I’m not an assembly expert, and the code below could be improved by using variables for the hard coded strings instead of building everything on the stack, but this was short and sweet. The tricky part is keeping track of what is pushed onto the stack, and using the correct registers for the arguments passed to execve
.
Now that we have some assembly written, we need to assemble and link it, with the following two commands:
Time to test it out and see if our bind shell works (I’m connecting from the same host).
Running the program with sftrace
we can see the call to netcat
.
Then test connecting to the bind shell, and success!
The hexadecimal values needed for the shellcode can be easily extracted with objdump
.
For the shellcode payload we only need the contents of start_netcat
, starting with the
xor eax, eax
instruction and ending with the system interrupt int 0x80
. So the final payload for
the netcat bind shell is: