Skip to content

Instantly share code, notes, and snippets.

@FedericoPonzi
Created March 1, 2015 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FedericoPonzi/8ebdd9ec285dae0cf929 to your computer and use it in GitHub Desktop.
Save FedericoPonzi/8ebdd9ec285dae0cf929 to your computer and use it in GitHub Desktop.
A buffer overflow example. Check an example on how to exploit this program: https://youtu.be/1I_zu37YJFI
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
char greeting[] = "Hello there\n1. Receive wisdom\n2. Add wisdom\nSelection >";
char prompt[] = "Enter some wisdom\n";
char pat[] = "Achievement unlocked!\n";
char secret[] = "secret key";
int infd = 0; /* stdin */
int outfd = 1; /* stdout */
#define DATA_SIZE 128
typedef struct _WisdomList {
struct _WisdomList *next;
char data[DATA_SIZE];
} WisdomList;
struct _WisdomList *head = NULL;
typedef void (*fptr)(void);
void write_secret(void) {
write(outfd, secret, sizeof(secret));
return;
}
void pat_on_back(void) {
write(outfd, pat, sizeof(pat));
return;
}
void get_wisdom(void) {
char buf[] = "no wisdom\n";
if(head == NULL) {
write(outfd, buf, sizeof(buf)-sizeof(char));
} else {
WisdomList *l = head;
while(l != NULL) {
write(outfd, l->data, strlen(l->data));
write(outfd, "\n", 1);
l = l->next;
}
}
return;
}
void put_wisdom(void) {
char wis[DATA_SIZE] = {0};
int r;
r = write(outfd, prompt, sizeof(prompt)-sizeof(char));
if(r < 0) {
return;
}
r = (int)gets(wis);
if (r == 0)
return;
WisdomList *l = malloc(sizeof(WisdomList));
if(l != NULL) {
memset(l, 0, sizeof(WisdomList));
strcpy(l->data, wis);
if(head == NULL) {
head = l;
} else {
WisdomList *v = head;
while(v->next != NULL) {
v = v->next;
}
v->next = l;
}
}
return;
}
fptr ptrs[3] = { NULL, get_wisdom, put_wisdom };
int main(int argc, char *argv[]) {
while(1) {
char buf[1024] = {0};
int r;
fptr p = pat_on_back; // A pointer to a function that prints a message
r = write(outfd, greeting, sizeof(greeting)-sizeof(char));
if(r < 0) {
break;
}
r = read(infd, buf, sizeof(buf)-sizeof(char));
if(r > 0) {
buf[r] = '\0';
int s = atoi(buf);
/**
* 1 : get_wisdom
* 2 : put_wisdom
* No input check!
**/
fptr tmp = ptrs[s];
tmp(); //run the function pointed from tmp
} else {
break;
}
}
return 0;
}
@jemacom
Copy link

jemacom commented Nov 14, 2015

In order to have buffer overflow vulnerability in the executable , you need to disable the stack protector of the gcc compiler like this :

gcc -fno-stack-protector -ggdb -m32 wisdom-alt.c -o wisdom-alt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment