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.

Amazingly, <strong>the</strong>se instructions, combined with <strong>the</strong> AND eax instruction,<br />

are sufficient to build loader code that will inject <strong>the</strong> shellcode onto <strong>the</strong> stack<br />

and <strong>the</strong>n execute it. The general technique is, first, to set ESP back behind <strong>the</strong><br />

executing loader code (in higher memory addresses), and <strong>the</strong>n to build <strong>the</strong><br />

shellcode from end to st<strong>art</strong> by pushing values onto <strong>the</strong> stack, as shown here.<br />

Since <strong>the</strong> stack grows up (from higher memory addresses to lower memory<br />

addresses), <strong>the</strong> ESP will move backward as values are pushed to <strong>the</strong> stack,<br />

and <strong>the</strong> EIP will move forward as <strong>the</strong> loader code executes. Eventually,<br />

EIP and ESP will meet up, and <strong>the</strong> EIP will continue executing into <strong>the</strong><br />

freshly built shellcode.<br />

1)<br />

Loader Code<br />

EIP<br />

ESP<br />

2)<br />

Loader Code<br />

Shellcode<br />

EIP<br />

ESP<br />

3)<br />

Loader Code<br />

Shellcode being built<br />

EIP<br />

ESP<br />

o<br />

First, ESP must be set behind <strong>the</strong> printable loader shellcode. A little<br />

debugging with GDB shows that after gaining control <strong>of</strong> program execution,<br />

ESP is 555 bytes before <strong>the</strong> st<strong>art</strong> <strong>of</strong> <strong>the</strong> overflow buffer (which will contain <strong>the</strong><br />

loader code). The ESP register must be moved so it’s after <strong>the</strong> loader code,<br />

while still leaving room for <strong>the</strong> new shellcode and for <strong>the</strong> loader shellcode<br />

itself. About 300 bytes should be enough room for this, so let’s add 860 bytes<br />

to ESP to put it 305 bytes past <strong>the</strong> st<strong>art</strong> <strong>of</strong> <strong>the</strong> loader code. This value doesn’t<br />

need to be exact, since provisions will be made later to allow for some slop.<br />

Since <strong>the</strong> only usable instruction is subtraction, addition can be simulated by<br />

subtracting so much from <strong>the</strong> register that it wraps around. The register only<br />

has 32 bits <strong>of</strong> space, so adding 860 to a register is <strong>the</strong> same as subtracting 860<br />

from 2 32 , or 4,294,966,436. However, this subtraction must only use printable<br />

values, so we split it up across three instructions that all use printable operands.<br />

sub eax, 0x39393333 ; Assembles into -3399<br />

sub eax, 0x72727550 ; Assembles into -Purr<br />

sub eax, 0x54545421 ; Assembles into -!TTT<br />

As <strong>the</strong> GDB output confirms, subtracting <strong>the</strong>se three values from a 32-bit<br />

number is <strong>the</strong> same as adding 860 to it.<br />

Countermeasures 367

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

Saved successfully!

Ooh no, something went wrong!