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.

functions are shared, so any program that uses <strong>the</strong> printf() function directs<br />

execution into <strong>the</strong> appropriate location in libc. An exploit can do <strong>the</strong> exact<br />

same thing and direct a program’s execution into a certain function in libc.<br />

The functionality <strong>of</strong> such an exploit is limited by <strong>the</strong> functions in libc, which<br />

is a significant restriction when compared to arbitrary shellcode. However,<br />

nothing is ever executed on <strong>the</strong> stack.<br />

0x6b2<br />

Returning into system()<br />

One <strong>of</strong> <strong>the</strong> simplest libc functions to return into is system(). As you recall, this<br />

function takes a single argument and executes that argument with /bin/sh.<br />

This function only needs a single argument, which makes it a useful target.<br />

For this example, a simple vulnerable program will be used.<br />

vuln.c<br />

int main(int argc, char *argv[])<br />

{<br />

char buffer[5];<br />

strcpy(buffer, argv[1]);<br />

return 0;<br />

}<br />

Of course, this program must be compiled and setuid root before it’s truly<br />

vulnerable.<br />

reader@<strong>hacking</strong>:~/booksrc $ gcc -o vuln vuln.c<br />

reader@<strong>hacking</strong>:~/booksrc $ sudo chown root ./vuln<br />

reader@<strong>hacking</strong>:~/booksrc $ sudo chmod u+s ./vuln<br />

reader@<strong>hacking</strong>:~/booksrc $ ls -l ./vuln<br />

-rwsr-xr-x 1 root reader 6600 2007-09-30 22:43 ./vuln<br />

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

The general idea is to force <strong>the</strong> vulnerable program to spawn a shell,<br />

without executing anything on <strong>the</strong> stack, by returning into <strong>the</strong> libc function<br />

system(). If this function is supplied with <strong>the</strong> argument <strong>of</strong> /bin/sh, this should<br />

spawn a shell.<br />

First, <strong>the</strong> location <strong>of</strong> <strong>the</strong> system() function in libc must be determined.<br />

This will be different for every system, but once <strong>the</strong> location is known, it will<br />

remain <strong>the</strong> same until libc is recompiled. One <strong>of</strong> <strong>the</strong> easiest ways to find <strong>the</strong><br />

location <strong>of</strong> a libc function is to create a simple dummy program and debug it,<br />

like this:<br />

reader@<strong>hacking</strong>:~/booksrc $ cat > dummy.c<br />

int main()<br />

{ system(); }<br />

reader@<strong>hacking</strong>:~/booksrc $ gcc -o dummy dummy.c<br />

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

Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".<br />

Countermeasures 377

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

Saved successfully!

Ooh no, something went wrong!