Buffer Overflow
An absolute essential to master, this topic guarantees 25 points in the OSCP. Credits to the Cyber Mentor for his outstanding playlist.
Last updated
An absolute essential to master, this topic guarantees 25 points in the OSCP. Credits to the Cyber Mentor for his outstanding playlist.
Last updated
if buffer space properly sanitizes inputs, inputs reach the EBP and stops, do not reach the EIP
EIP can be used to point to directions tat we instruct > point to malicious codes that we inject
Reaching EIP controls Stack > control pointer > reverse shell
Spiking - to find vulnerable part of programme
Fuzzing - send bunch of random characters to vulnerability found
Find the offset - at what point did the program break
Overwrite EIP (Pointer) - use offset to overwrite EIP
Find bad characters
Find the right module
Generate Shellcode
Point EIP to shellcode
find the valid commands on the vuln server
Tool used for spike:
spike script used to readline() then enter command along with the spiking variables
write a script to fuzz the vulnerability found by spiking in step 2, then logging the amount of bytes or length of buffer sent to crash the program
info for connecting via sockets TCP/IP: https://pymotw.com/2/socket/tcp.html
Basically looking for where we overwrite the EIP, so we can control the stack.
Tool used will be : pattern_create by metasploit
copy the generated pattern of specified length
Now we check the immunity debugger and run the script with offset pattern
Notice the offset has went beyond the EIP and even to the ESP (too far)
Base on above, we can see the pattern for EIP is 386F4337
Go back to linux attack machine and search for this pattern in the pattern create
Now it tells you where the EXACT EIP is, at 2003 bytes!
So what we have gathered so far: There is 2003 bytes before you get to EIP, and the EIP is exactly 4 bytes long (always). So we need to overwrite bytes 2004 to 2007.
Now modify our same python script, no need offset anymore
Check the Immunity Debugger, note that all registers output are in HEX
AAAA will be represented by 41414141 and BBBB by 42424242 and so on
Here we have the representation in immunity debugger
Notice that EBP overwritten with AAAA and EIP with BBBB, EIP controlled!!
When we generate shellcode, some chars are bad for shellcode (act up) etc. NULL char x00
google badcharacters immunity debugger, PASTE into our shell script
now look at debugger hexdump (right click ESP > follow dump), check that all characters under badchar are present (nothing wrong)
If there are badchar, they will turn up missing (etc. taken as a command)
No badchar, scan till FF the last char xff
an example with bad characters is shown below
looking for DLL or similar inside a program with no memory protections (no safeSEH, no ASLR etc)
Tool: Mona.py on git hub
Download Mona.py into the folder > Local Disk (C:)/Program Files x86/Immunity Inc/Immunity Debugger/PyCommands
go to type "!mona module" in immunity debugger (the type bar at the very bottom), hit enter
The goal is to find DLL with all FALSE (no memory protection), in the above example it is essFunc.dll
(To convert assembly language to hexcode)
Jump command is "JMP ESP", type it into Nasm interactive > hexcode equivalent will be given, in this case, "FFE4"
go back immunity debugger, type into typebar <!mona find -s "\xff\xe4" -m essfunc.dll>
-s tag searches for string for jump code in hex
-m selects module with no protection
NOTE : string FFE4 converted to hex must have "\x" -prefix!
Note the numerous return addresses listed, note them down
debugger returns the return addresses with search string \xff\xe4 in the module essfunc.dll (etc. 0x625011af)
In the space of EIP, right after the 2003 bytes of buffer
In reversed order, hex form
0x625011af becomes \xaf\x11\x05\x62, 0x is the initializer for every hex code
This is due to x86 computer architecture using Little-Endian format, least significant bit (LSB) at lowest address and vice versa.
https://geosn0w.github.io/An-Introduction-To-Intel-x86-Assembly/
The CPU has what is called “registers”. They are pretty much a very fast but limited memory on the microprocessor. The microprocessor can access these registers and retrieve the data way faster than it could do from RAM. The problem is, there are only a few of them and some registers are not general purpose, meaning they can’t really be used for everything. What can’t be fitted in the registers, lies in the RAM.
The IA-32 CPUs have 8 general purpose 32-Bit registers. Of course, these CPUs have way more registers than that, but only eight of them are truly general purpose. These registers are very important and you will find them scattered all over the place in the assembly output of any binary disassembler. A list of the available registers and a brief description is provided below.
EAX, EBX, ECX, and EDX are general purpose registers used for various operations. ECX is sometimes used as a counter by repetitive instructions.
EBP and ESP are the Stack Base Pointer and the Stack Pointer. Together, these two create the Stack Frame.
EDI and ESI are the Destination Index and Source Index registers and is used when dealing with the memory copying.
go back to immunity debugger, click top button arrow > to follow the expression of the return address
highlight this line and hit F2 to make breakpoint, so the program stops at JUMPCODE
now run your JUMPCODE SHELL SCRIPT
Perfect now we have breakpoint at the EIP return address, now we ready to enter our shellcode as we have control of the pointer now!
Flags for MSFVENOM
-b flag is for bad characters, by default is the NULL byte \x00, add on your other bad characters if necessary
-p for payload (windows machine, shell )
-a for assembly architecture
-f for file type
After generating it, copy it into python shell script we using, do take note of payload size (etc limited bytes left)
add the overflow payload behind the return address with a little padding "\x90" * 32 (NOPs - no operation, might need to play around with padding size, this prevents interference between return address and payload)
set up netcat <nc -lvnp 4444>
run payload script! Get reverse shell :)