Buffer Overflow not working

喜欢而已 提交于 2019-12-18 08:30:09

问题


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.


回答1:


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




回答2:


If compiling with gcc, add -fno-stack-protector flag. The message you received is meant to protect you from your bad code :)




回答3:


The reason is stack smashing is actually a protection mechanism used by some compilers to detect buffer overflow attacks. You are trying to put the 29 A's into a shorter character array (16 bytes).




回答4:


Most modern OS have protective mechanisms built in. Almost any good OS does not allow direct low level memory access to any program. It only allows programs to access the adress space allocated to them. Linux based OS automatically kill the processes that try to access beyond their allocated memory space.

Other than this, OS also have protective mechanisms that prevent a program from crashing the system by allocating large amounts of memory, in an attempt to severely deplete the resources available to the OS.



来源:https://stackoverflow.com/questions/14144216/buffer-overflow-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!