Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer Overflow not working

I was trying to do a buffer overflow (I'm using Linux) on a simple program that requires a password. Here's the program code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int check_authentication(char *password){

int auth_flag = 0;
char password_buffer[16];

strcpy(password_buffer, password);

if(strcmp(password_buffer, "pass1") == 0)
    auth_flag = 1;
if(strcmp(password_buffer, "pass2") == 0)
    auth_flag = 1;

return auth_flag;

}

int main(int argc, char **argv)
{

if(argc < 2){

    printf("\t[!] Correct usage: %s <password>\n", argv[0]);
    exit(0);

}

if(check_authentication(argv[1])){

    printf("\n-=-=-=-=-=-=-=-=\n");
    printf("  Access granted.\n");
    printf("-=-=-=-=-=-=-=-=\n");

} else {

    printf("\nAccess Denied.\n");

}


   return 0;

}

OK, now I compiled it, no errors, and saved it as overflow.c.

Now I opened the Terminal, I moved into the file directory (Desktop) and then wrote:

./overflow.c AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

The Terminal said: "Stack smashing detected" (or something like that) and then quit the program execution.

Now, I'm reading a book, called "Hacking - The Art Of Exploitation" by Jon Erickson. In a chapter, he explains this type of exploit (I took the code from the book) and does the same command I've done. The memory overflows and the program prints "Access granted.". Now, why my OS is detecting I'm trying to exploit the program? I've done something wrong?

I also tried the exploit on Mac OS X. Same thing happened. Please, can someone help me? Thanks in advance.

like image 462
jndok Avatar asked Jan 03 '13 17:01

jndok


1 Answers

In modern linux distributions buffer overflow is detected and the process is killed. In order to disable that mode simply compile your application with such flags (gcc):

-fno-stack-protector -fno-stack-protector-all

like image 105
Adam Sznajder Avatar answered Nov 03 '22 16:11

Adam Sznajder