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.

auth_overflow2.c<br />

#include <br />

#include <br />

#include <br />

int check_au<strong>the</strong>ntication(char *password) {<br />

char password_buffer[16];<br />

int auth_flag = 0;<br />

strcpy(password_buffer, password);<br />

if(strcmp(password_buffer, "brillig") == 0)<br />

auth_flag = 1;<br />

if(strcmp(password_buffer, "outgrabe") == 0)<br />

auth_flag = 1;<br />

}<br />

return auth_flag;<br />

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

if(argc < 2) {<br />

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

exit(0);<br />

}<br />

if(check_au<strong>the</strong>ntication(argv[1])) {<br />

printf("\n-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");<br />

printf(" Access Granted.\n");<br />

printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");<br />

} else {<br />

printf("\nAccess Denied.\n");<br />

}<br />

}<br />

This simple change puts <strong>the</strong> auth_flag variable before <strong>the</strong> password_buffer<br />

in memory. This eliminates <strong>the</strong> use <strong>of</strong> <strong>the</strong> return_value variable as an execution<br />

control point, since it can no longer be corrupted by an overflow.<br />

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

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

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

(gdb) list 1<br />

1 #include <br />

2 #include <br />

3 #include <br />

4<br />

5 int check_au<strong>the</strong>ntication(char *password) {<br />

6 char password_buffer[16];<br />

7 int auth_flag = 0;<br />

8<br />

9 strcpy(password_buffer, password);<br />

10<br />

(gdb)<br />

126 0x300

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

Saved successfully!

Ooh no, something went wrong!