8

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.

jndok
  • 909
  • 3
  • 14
  • 28

4 Answers4

10

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

Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
  • Nice post, mate! Do you maybe know a solution for Cygwin environments as well? `-fno-stack-protector` seems not to make any difference. – Powerslave Jul 18 '15 at 23:50
3

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

Betagan
  • 121
  • 3
1

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).

Bob Balfe
  • 48
  • 5
0

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.

asheeshr
  • 4,088
  • 6
  • 31
  • 50