Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
914 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...