BOF
Buffer overflow
Fuzzing
Send increasingly long string to vulnerable field until the application crashes because the EIP registry has been overwritten. The string length will be the size of the payload.
Find EIP address
Generate and send a string to the vulnerable application to replicate the crash. Once the application crashes use the debugger to read the bytes stored in the EIP register and use them to find the offset of the register from the injection point
Find writable area offset
It is needed to use a register to store the code to be executed at crash time. To do so we have to find a register that references an area of memory we have reserved for our script (the โCโ area). After submitting the following payload:
Find a register that points to an area written with โCโs:
Check for space
After calculating the starting offset, see how much space you have available by trying to inject several extra lines of โDโs that represent your actual payload position:
If the available space is not enough to store the exploit see Bof with first stage payload
Check for bad chars
String for tests
known bad chars:
Procedure:
Send the badchars string to the target machine.
When the program crashes use the debugger to access the memory dump and read at which char the execution stopped.
The character next to the last in memory is a bad one, remove it from the badchars string and send it again
Repeat steps 1-3 until the application crashes after processing the whole string.
Redirect execution
Use Immunity's mona shell to find the position of an exploitable JMP ESP instruction (we want ESP register because it points to the top of the stack). A suitable address must not contain bad chars, and be stored in a library without memory protection systems in place and without bad chars in its base address.
When storing the address check for endianness: if the system is little endian invert the bytes' order.
Get opcodes
Assemble payload
Generate shellcode
If msfvenom is not able to generate a payload, remove the encoder option (-e) in this way the payload generator will automatically pick the best encoder to work with the given badchars.
Assemble payload
BoF with first stage payload
If the ESP register is too small to store the whole payload it will store the first stage payload and from there the execution will be redirected to the actual exploit code stored in another register pointing at the top of the stack.
First stage payload
Once a suitable register is found we have to add our instructions after the data stored. The following instructions will be executed from the ESP register in order to redirect the execution to the exploit
payload:
The payload is formed by the generated hex strings joined together
Assemble payload
The procedure for the rest of the exploit is the same as before, the only difference is that you have to account for the first stage payload when assembling the exploit
Last updated