From 0142fb19905808101878148ca188161f016eba5e Mon Sep 17 00:00:00 2001 From: spacehen Date: Fri, 30 Dec 2022 12:05:55 +0200 Subject: CDL: Compact Detour Library Initial commit for the CDL x86_64 detour library. --- tests/Makefile | 16 ++++++++++++++++ tests/basic_jmp.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/basic_swbp.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 tests/Makefile create mode 100644 tests/basic_jmp.c create mode 100644 tests/basic_swbp.c (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..d28b133 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,16 @@ +IDIR=../ +CC=gcc +CFLAGS=-I$(IDIR) +CFILES_CDL=../cdl.c + +all: basic_jmp basic_swbp + +basic_jmp: basic_jmp.c + $(CC) $(CFLAGS) basic_jmp.c $(CFILES_CDL) -o basic_jmp +basic_swbp: basic_swbp.c + $(CC) $(CFLAGS) basic_swbp.c $(CFILES_CDL) -o basic_swbp + +.PHONY: clean +clean: + rm -f basic_jmp + rm -f basic_swbp diff --git a/tests/basic_jmp.c b/tests/basic_jmp.c new file mode 100644 index 0000000..e8aa85a --- /dev/null +++ b/tests/basic_jmp.c @@ -0,0 +1,52 @@ +#include "cdl.h" + +typedef int add_t( + __in int x, + __in int y +); + +add_t* addo = NULL; + +int add( + __in int x, + __in int y +) +{ + printf("Inside original function\n"); + return x + y; +} + +int add_detour( + __in int x, + __in int y +) +{ + printf("Inside detour function\n"); + return addo(5,5); +} + +int main( + __in void +) +{ + struct cdl_jmp_patch jmp_patch = {}; + addo = (add_t*)add; + + printf("Before attach: \n"); + printf("add(1,1) = %i\n\n", add(1,1)); + + jmp_patch = cdl_jmp_attach((void**)&addo, add_detour); + if(jmp_patch.active) + { + printf("After attach: \n"); + printf("add(1,1) = %i\n\n", add(1,1)); + printf("== DEBUG INFO ==\n"); + cdl_jmp_dbg(&jmp_patch); + } + + cdl_jmp_detach(&jmp_patch); + printf("\nAfter detach: \n"); + printf("add(1,1) = %i\n\n", add(1,1)); + + return 0; +} diff --git a/tests/basic_swbp.c b/tests/basic_swbp.c new file mode 100644 index 0000000..684f71d --- /dev/null +++ b/tests/basic_swbp.c @@ -0,0 +1,52 @@ +#include "cdl.h" + +typedef int add_t( + __in int x, + __in int y +); + +add_t* addo = NULL; + +int add( + __in int x, + __in int y +) +{ + printf("Inside original function\n"); + return x + y; +} + +int add_detour( + __in int x, + __in int y +) +{ + printf("Inside detour function\n"); + return addo(5,5); +} + +int main( + __in void +) +{ + struct cdl_swbp_patch swbp_patch = {}; + addo = (add_t*)add; + + printf("Before attach: \n"); + printf("add(1,1) = %i\n\n", add(1,1)); + + swbp_patch = cdl_swbp_attach((void**)&addo, add_detour); + if(swbp_patch.active) + { + printf("After attach: \n"); + printf("add(1,1) = %i\n\n", add(1,1)); + printf("== DEBUG INFO ==\n"); + cdl_swbp_dbg(&swbp_patch); + } + + cdl_swbp_detach(&swbp_patch); + printf("\nAfter detach: \n"); + printf("add(1,1) = %i\n\n", add(1,1)); + + return 0; +} \ No newline at end of file -- cgit v1.2.3-70-g09d2