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.

0x080483a3 : mov DWORD PTR [esp],0x80484d4<br />

0x080483aa : call 0x80482a8 <br />

0x080483af : lea eax,[ebp-4]<br />

0x080483b2 : inc DWORD PTR [eax]<br />

0x080483b4 : jmp 0x804839b <br />

0x080483b6 : leave<br />

0x080483b7 : ret<br />

End <strong>of</strong> assembler dump.<br />

(gdb) break main<br />

Breakpoint 1 at 0x8048394: file firstprog.c, line 6.<br />

(gdb) run<br />

St<strong>art</strong>ing program: /<strong>hacking</strong>/a.out<br />

Breakpoint 1, main() at firstprog.c:6<br />

6 for(i=0; i < 10; i++)<br />

(gdb) info register eip<br />

eip 0x8048394 0x8048394<br />

(gdb)<br />

First, <strong>the</strong> source code is listed and <strong>the</strong> disassembly <strong>of</strong> <strong>the</strong> main() function<br />

is displayed. Then a breakpoint is set at <strong>the</strong> st<strong>art</strong> <strong>of</strong> main(), and <strong>the</strong> program is<br />

run. This breakpoint simply tells <strong>the</strong> debugger to pause <strong>the</strong> execution <strong>of</strong> <strong>the</strong><br />

program when it gets to that point. Since <strong>the</strong> breakpoint has been set at <strong>the</strong><br />

st<strong>art</strong> <strong>of</strong> <strong>the</strong> main() function, <strong>the</strong> program hits <strong>the</strong> breakpoint and pauses<br />

before actually executing any instructions in main(). Then <strong>the</strong> value <strong>of</strong> EIP<br />

(<strong>the</strong> Instruction Pointer) is displayed.<br />

Notice that EIP contains a memory address that points to an instruction in<br />

<strong>the</strong> main() function’s disassembly (shown in bold). The instructions before this<br />

(shown in italics) are collectively known as <strong>the</strong> function prologue and are generated<br />

by <strong>the</strong> compiler to set up memory for <strong>the</strong> rest <strong>of</strong> <strong>the</strong> main() function’s<br />

local variables. P<strong>art</strong> <strong>of</strong> <strong>the</strong> reason variables need to be declared in C is to aid<br />

<strong>the</strong> construction <strong>of</strong> this section <strong>of</strong> code. The debugger knows this p<strong>art</strong> <strong>of</strong> <strong>the</strong><br />

code is automatically generated and is sm<strong>art</strong> enough to skip over it. We’ll talk<br />

more about <strong>the</strong> function prologue later, but for now we can take a cue from<br />

GDB and skip it.<br />

The GDB debugger provides a direct method to examine memory, using<br />

<strong>the</strong> command x, which is short for examine. Examining memory is a critical<br />

skill for any hacker. Most hacker exploits are a lot like magic tricks—<strong>the</strong>y<br />

seem amazing and magical, unless you know about sleight <strong>of</strong> hand and<br />

misdirection. In both magic and <strong>hacking</strong>, if you were to look in just <strong>the</strong> right<br />

spot, <strong>the</strong> trick would be obvious. That’s one <strong>of</strong> <strong>the</strong> reasons a good magician<br />

never does <strong>the</strong> same trick twice. But with a debugger like GDB, every aspect<br />

<strong>of</strong> a program’s execution can be deterministically examined, paused, stepped<br />

through, and repeated as <strong>of</strong>ten as needed. Since a running program is mostly<br />

just a processor and segments <strong>of</strong> memory, examining memory is <strong>the</strong> first way<br />

to look at what’s really going on.<br />

The examine command in GDB can be used to look at a certain address<br />

<strong>of</strong> memory in a variety <strong>of</strong> ways. This command expects two arguments when<br />

it’s used: <strong>the</strong> location in memory to examine and how to display that memory.<br />

Programming 27

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

Saved successfully!

Ooh no, something went wrong!