Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
907 views
in Technique[技术] by (71.8m points)

c - 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("[!] Correct usage: %s <password>
", argv[0]);
    exit(0);

}

if(check_authentication(argv[1])){

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

} else {

    printf("
Access Denied.
");

}


   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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...