26.07.2018 Views

hacking-the-art-of-exploitation

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Back in <strong>the</strong> debugging terminal, <strong>the</strong> first breakpoint is encountered.<br />

Some important stack registers are displayed, which show <strong>the</strong> stack setup<br />

before (and after) <strong>the</strong> handle_connection() call. Then, execution continues<br />

to <strong>the</strong> int3 instruction in <strong>the</strong> shellcode, which acts like a breakpoint. Then<br />

<strong>the</strong>se stack registers are checked again to view <strong>the</strong>ir state at <strong>the</strong> moment <strong>the</strong><br />

shellcode begins to execute.<br />

Breakpoint 1, 0x08048fb2 in main () at tinywebd.c:72<br />

72 handle_connection(new_sockfd, &client_addr, logfd);<br />

(gdb) i r esp ebx ebp<br />

esp 0xbffff7e0 0xbffff7e0<br />

ebx 0xb7fd5ff4 -1208131596<br />

ebp 0xbffff848 0xbffff848<br />

(gdb) cont<br />

Continuing.<br />

Program received signal SIGTRAP, Trace/breakpoint trap.<br />

0xbffff753 in ?? ()<br />

(gdb) i r esp ebx ebp<br />

esp 0xbffff7e0 0xbffff7e0<br />

ebx 0x6 6<br />

ebp 0xbffff624 0xbffff624<br />

(gdb)<br />

This output shows that EBX and EBP are changed at <strong>the</strong> point <strong>the</strong> shellcode<br />

begins execution. However, an inspection <strong>of</strong> <strong>the</strong> instructions in main()’s<br />

disassembly shows that EBX isn’t actually used. The compiler probably saved<br />

this register to <strong>the</strong> stack due to some rule about calling convention, even<br />

though it isn’t really used. EBP, however, is used heavily, since it’s <strong>the</strong> point<br />

<strong>of</strong> reference for all local stack variables. Because <strong>the</strong> original saved value <strong>of</strong><br />

EBP was overwritten by our exploit, <strong>the</strong> original value must be recreated.<br />

When EBP is restored to its original value, <strong>the</strong> shellcode should be able<br />

to do its dirty work and <strong>the</strong>n return back into main() as usual. Since computers<br />

are deterministic, <strong>the</strong> assembly instructions will clearly explain how<br />

to do all this.<br />

(gdb) set dis intel<br />

(gdb) x/5i main<br />

0x8048d93 : push ebp<br />

0x8048d94 : mov ebp,esp<br />

0x8048d96 : sub esp,0x68<br />

0x8048d99 : and esp,0xfffffff0<br />

0x8048d9c : mov eax,0x0<br />

(gdb) x/5i main+533<br />

0x8048fa8 : mov DWORD PTR [esp+4],eax<br />

0x8048fac : mov eax,DWORD PTR [ebp-12]<br />

0x8048faf : mov DWORD PTR [esp],eax<br />

0x8048fb2 : call 0x8048fb9 <br />

0x8048fb7 : jmp 0x8048f65 <br />

(gdb)<br />

344 0x600

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!