I am trying to simulate buffer overflow on my mac, but it keeps getting segmentation fault even with -fno-stack-protector.
Below is the output I get.
Vulnerable function executed!
data:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
zsh: segmentation fault
I compiled and ran using the below command.
gcc -o sql_slammer slammer.c -fno-stack-protector -D_FORTIFY_SOURCE=0 -Wl && ./sql_slammer
Below is my code for reference.
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 27
void malicious_function()
{
printf("Malicious code executed!\n");
// Insert your malicious code here
}
void vulnerable_function(char *data)
{
char buffer[BUFFER_SIZE];
printf("Vulnerable function executed!\n");
printf("data:%s\n", data);
sprintf(buffer, "%s", data); // Vulnerable sprintf() call
// Create a function pointer and set it to the address of the malicious function
void (*function_ptr)() = &malicious_function;
// Overwrite the return address with the address of the malicious function
// This assumes little-endian architecture where addresses are stored in reverse order
memcpy(buffer + BUFFER_SIZE - sizeof(void *), &function_ptr, sizeof(void *));
}
int main()
{
char packet[] = "\x04"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41"
"\x41";
vulnerable_function(packet);
return 0;
}
I read other stackoverflow posts related to this and tried the solution, but it didn't work for me. I included compiler flags such as -O0 and -fno-stack-protector. I also tried running it on Windows.
packet, which becomesdata, of length 44, was too long forbuffer, of length 27. Why are you surprised?