Skip to content

Instantly share code, notes, and snippets.

@Aenigma
Created April 15, 2014 07:05
Show Gist options
  • Save Aenigma/d1bad8fd7f6ad1b8ba38 to your computer and use it in GitHub Desktop.
Save Aenigma/d1bad8fd7f6ad1b8ba38 to your computer and use it in GitHub Desktop.
Simple buffer overflow example program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void display_args(int size, char *start);
int main(int argc, char **argv)
{
char pass[16] = "password123";
char user[8] = "root";
char buffer[8] = {};
if (argc < 2) {
fprintf(stderr,"You must enter a single argument!\n");
return -1;
}
strncpy(buffer, argv[1], strlen(argv[1]));
#ifdef DEBUG
if (argc > 2) {
display_args(atoi(argv[2]), buffer);
fprintf(stderr,
"WARNING: Debug mode is on and arguments"
" were passed to dump memory.\n");
return 0;
}
#endif
printf("You entered: %s\n", buffer);
printf("You are user: %s\n", user);
return 0;
}
void display_args(int size, char *start)
{
int i = 0;
while (i < size) {
putchar(start[i++]);
}
}
CC:=$(shell which cc)
CC_WIN_32:=$(shell which i686-w64-mingw32-gcc)
CC_WIN_64:=$(shell which x86_64-w64-mingw32-gcc)
CFLAGS=
DEFINES=-DDEBUG
CFILES=example.c
OUTPUT=example
BIN_DIR=bin
.PHONY: all clean winbins posixbins
all: winbins posixbins
clean:
@rm -r $(BIN_DIR)
winbins: $(BIN_DIR)/example32.exe $(BIN_DIR)/example64.exe
posixbins: $(BIN_DIR)/example32 $(BIN_DIR)/example64
$(BIN_DIR)/example32:
@mkdir -p $(BIN_DIR)
$(CC) $(DEFINES) $(CFLAGS) $(CFILES) -o $@ -m32
$(BIN_DIR)/example64:
@mkdir -p $(BIN_DIR)
$(CC) $(DEFINES) $(CFLAGS) $(CFILES) -o $@ -m64
$(BIN_DIR)/example32.exe:
@mkdir -p $(BIN_DIR)
$(CC_WIN_32) $(DEFINES) $(CFLAGS) $(CFILES) -o $@ -m32
$(BIN_DIR)/example64.exe:
@mkdir -p $(BIN_DIR)
$(CC_WIN_64) $(DEFINES) $(CFLAGS) $(CFILES) -o $@ -m64
$ ./bin/example32 0123456789ABCDEF 64 | hexdump -C
WARNING: Debug mode is on and arguments were passed to dump memory.
00000000 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 |0123456789ABCDEF|
00000010 70 61 73 73 77 6f 72 64 31 32 33 00 00 00 00 00 |password123.....|
00000020 c0 86 04 08 00 00 00 00 58 db fc ff 46 ee 64 f7 |........X...F.d.|
00000030 03 00 00 00 84 db fc ff 94 db fc ff 00 c0 79 f7 |..............y.|
00000040
$ ./bin/example32 0123456789ABCDEF
You entered: 0123456789ABCDEFpassword123
You are user: 89ABCDEFpassword123
$ ./bin/example64 0123456789ABCDEFGHIJKLMNOPQRSTUV 64 | hexdump -C
WARNING: Debug mode is on and arguments were passed to dump memory.
00000000 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 |0123456789ABCDEF|
00000010 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 |GHIJKLMNOPQRSTUV|
00000020 70 61 73 73 77 6f 72 64 31 32 33 00 00 00 00 00 |password123.....|
00000030 00 00 00 00 00 00 00 00 ad ee 02 1f fc 7f 00 00 |................|
00000040
$ ./bin/example64 0123456789ABCDEFGHIJKLMNOPQRSTUV
You entered: 0123456789ABCDEFGHIJKLMNOPQRSTUVpassword123
You are user: GHIJKLMNOPQRSTUVpassword123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment