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.

There are also operations that are used to control <strong>the</strong> flow <strong>of</strong> execution.<br />

The cmp operation is used to compare values, and basically any operation<br />

beginning with j is used to jump to a different p<strong>art</strong> <strong>of</strong> <strong>the</strong> code (depending<br />

on <strong>the</strong> result <strong>of</strong> <strong>the</strong> comparison). The example below first compares a 4-byte<br />

value located at EBP minus 4 with <strong>the</strong> number 9. The next instruction is shorthand<br />

for jump if less than or equal to, referring to <strong>the</strong> result <strong>of</strong> <strong>the</strong> previous<br />

comparison. If that value is less than or equal to 9, execution jumps to <strong>the</strong><br />

instruction at 0x8048393. O<strong>the</strong>rwise, execution flows to <strong>the</strong> next instruction<br />

with an unconditional jump. If <strong>the</strong> value isn’t less than or equal to 9, execution<br />

will jump to 0x80483a6.<br />

804838b: 83 7d fc 09 cmp DWORD PTR [ebp-4],0x9<br />

804838f: 7e 02 jle 8048393 <br />

8048391: eb 13 jmp 80483a6 <br />

These examples have been from our previous disassembly, and we have<br />

our debugger configured to use Intel syntax, so let’s use <strong>the</strong> debugger to step<br />

through <strong>the</strong> first program at <strong>the</strong> assembly instruction level.<br />

The -g flag can be used by <strong>the</strong> GCC compiler to include extra debugging<br />

information, which will give GDB access to <strong>the</strong> source code.<br />

reader@<strong>hacking</strong>:~/booksrc $ gcc -g firstprog.c<br />

reader@<strong>hacking</strong>:~/booksrc $ ls -l a.out<br />

-rwxr-xr-x 1 matrix users 11977 Jul 4 17:29 a.out<br />

reader@<strong>hacking</strong>:~/booksrc $ gdb -q ./a.out<br />

Using host libthread_db library "/lib/libthread_db.so.1".<br />

(gdb) list<br />

1 #include <br />

2<br />

3 int main()<br />

4 {<br />

5 int i;<br />

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

7 {<br />

8 printf("Hello, world!\n");<br />

9 }<br />

10 }<br />

(gdb) disassemble main<br />

Dump <strong>of</strong> assembler code for function main():<br />

0x08048384 : push ebp<br />

0x08048385 : mov ebp,esp<br />

0x08048387 : sub esp,0x8<br />

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

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

0x08048392 : sub esp,eax<br />

0x08048394 : mov DWORD PTR [ebp-4],0x0<br />

0x0804839b : cmp DWORD PTR [ebp-4],0x9<br />

0x0804839f : jle 0x80483a3 <br />

0x080483a1 : jmp 0x80483b6 <br />

26 0x200

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

Saved successfully!

Ooh no, something went wrong!