hoping someone can help me understand a vulnerability I'm studying in college.
In the c code there is an unbound strcat
strcat(buffer, argv[1]);
The aim is to overflow this buffer into the saved EIP and have it display the "magic" string which is called in the below funtion.
if (geteuid() == 0) {
printf("%s\n", magic);
} else {
printf("Forget it. You do not have access to the magic string.\n");
return (-1);
}
The difference from the buffer to the saved EIP is 52 bytes and my idea was to overflow the buffer with the address of the printf function that will display the magic string but cannot get this to work I keep geting Seg faults with memory addresses different to what I am inputting.
Any help greatly appreciated.
EDIT: Complete code below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
enum { SIZE = 40 };
/* The magic string */
char *magic = "This is the magic string";
int
main(int argc, char *argv[])
{
char buffer[SIZE];
if (argc != 2) {
printf("Usage: %s name\n", argv[0]);
return (-1);
}
snprintf(buffer, sizeof (buffer), "%s", "Hello ");
strcat(buffer, argv[1]);
printf("%s\n", buffer);
if (geteuid() == 0) {
printf("%s\n", magic);
} else {
printf("Access Denied \n");
return (-1);
}
return (0);
}