diff --git a/lib/.gitignore b/lib/.gitignore new file mode 100644 index 0000000..2fb2e91 --- /dev/null +++ b/lib/.gitignore @@ -0,0 +1,3 @@ +test_avec_cipher +test_sans_cipher +test \ No newline at end of file diff --git a/lib/Makefile b/lib/Makefile index 000d6f2..3c88325 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,2 +1,10 @@ -test: test_chiffrement.c rondoudouPatch.c rondoudouPatch.h - gcc -Wall -g test_chiffrement.c rondoudouPatch.c -o test \ No newline at end of file +all: test + +test_sans_cipher: test_chiffrement.c rondoudouPatch.c rondoudouPatch.h + gcc -Wall -g test_chiffrement.c rondoudouPatch.c -DNOCIPHER -o test_sans_cipher + +test_avec_cipher: test_chiffrement.c rondoudouPatch.c rondoudouPatch.h + gcc -Wall -g test_chiffrement.c rondoudouPatch.c -o test_avec_cipher + +clean: + rm -f test \ No newline at end of file diff --git a/lib/rondoudouPatch.c b/lib/rondoudouPatch.c index d1ebb4d..87b6640 100644 --- a/lib/rondoudouPatch.c +++ b/lib/rondoudouPatch.c @@ -5,15 +5,23 @@ //WARNING SUR TOUT LES TYPES !!! +#ifdef NOCIPHER + +void cipher(void *address) {} +void decipher() {} +void rondoudou_patch_init() {} + +#else + struct t_pile_addrs { int index; - void ** tab[TAB_SIZE]; - struct t_pile_addrs * next; + void **tab[TAB_SIZE]; + struct t_pile_addrs *next; }; uintptr_t rondoudou_patch_key; -struct t_pile_addrs * pile_addrs; +struct t_pile_addrs *pile_addrs; void rondoudou_patch_init(void) { pile_addrs = (struct t_pile_addrs *)malloc(sizeof(struct t_pile_addrs)); @@ -34,7 +42,7 @@ int print_pile_aux(int profondeur, struct t_pile_addrs * pile) { max = TAB_SIZE; } for (i = 0; i %p\n", (prof_max - profondeur)*TAB_SIZE + i, pile->tab[i]); +// printf("%d -> %p\n", (prof_max - profondeur)*TAB_SIZE + i, pile->tab[i]); } return prof_max; } else { @@ -81,7 +89,7 @@ void cipher(void * address) { pile_addrs->index++; } pile_addrs->tab[pile_addrs->index] = addr; - printf("APPEL A CIPHER\n"); +// printf("APPEL A CIPHER\n"); print_pile(); } } @@ -89,7 +97,7 @@ void cipher(void * address) { void decipher(void) { if (pile_addrs->index == -1) { if (pile_addrs->next == NULL) { - printf("Ouille ouille ouille qu'est ce que j'ai mal aux nouilles ! \n"); +// printf("Ouille ouille ouille qu'est ce que j'ai mal aux nouilles ! \n"); exit(2); } else { struct t_pile_addrs * aux = pile_addrs; @@ -98,10 +106,13 @@ void decipher(void) { } } - *((pile_addrs->tab)[pile_addrs->index]) = (void *)((uintptr_t)(*((pile_addrs->tab)[pile_addrs->index])) ^ (uintptr_t)rondoudou_patch_key); + *((pile_addrs->tab)[pile_addrs->index]) = (void *) ((uintptr_t) (*((pile_addrs->tab)[pile_addrs->index])) ^ + (uintptr_t) rondoudou_patch_key); pile_addrs->index--; - printf("APPEL A DECIPHER\n"); +// printf("APPEL A DECIPHER\n"); print_pile(); } +#endif // NOCIPHER + void changekey(void); diff --git a/lib/test_chiffrement.c b/lib/test_chiffrement.c index f6c49bf..ce9bdb9 100644 --- a/lib/test_chiffrement.c +++ b/lib/test_chiffrement.c @@ -1,36 +1,34 @@ #include "rondoudouPatch.h" #include +#include -void f() { + +int h(int a) { cipher(__builtin_return_address(0)); - printf("Dans f()\n"); + if (a > 0) { + int ret = h(a - 1); + decipher(); + return ret; + } decipher(); + return 5; } -void g() { - cipher(__builtin_return_address(0)); - printf("Dans g()\n"); - f(); - decipher(); -} - -void h() { - cipher(__builtin_return_address(0)); - printf("Dans h()\n"); - g(); - decipher(); -} - -void i() { - cipher(__builtin_return_address(0)); - printf("Dans i()\n"); - h(); - decipher(); -} +char *ebp; +char *esp; int main() { + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + rondoudou_patch_init(); cipher(__builtin_return_address(0)); - i(); + for (int i = 0; i < 1000; ++i) { + h(25); + } decipher(); + + clock_gettime(CLOCK_MONOTONIC, &end); + unsigned long ns = (end.tv_sec - start.tv_sec) * 1000000000ul + end.tv_nsec - start.tv_nsec; + printf("%.3lfμs\n", (double) ns / 1000.0); } diff --git a/lib/test_perf.sh b/lib/test_perf.sh new file mode 100755 index 0000000..034d2a2 --- /dev/null +++ b/lib/test_perf.sh @@ -0,0 +1,7 @@ +#!/bin/bash +make test_avec_cipher +make test_sans_cipher +echo "-- Sans cipher --" +./test_sans_cipher +echo "-- Avec cipher --" +./test_avec_cipher \ No newline at end of file diff --git a/patch2/.gitignore b/patch2/.gitignore new file mode 100644 index 0000000..2fb2e91 --- /dev/null +++ b/patch2/.gitignore @@ -0,0 +1,3 @@ +test_avec_cipher +test_sans_cipher +test \ No newline at end of file diff --git a/patch2/CMakeLists.txt b/patch2/CMakeLists.txt index fe1e751..97c1ca6 100644 --- a/patch2/CMakeLists.txt +++ b/patch2/CMakeLists.txt @@ -3,4 +3,6 @@ project(patch2 C) set(CMAKE_C_STANDARD 99) -add_executable(patch2 main.c rondoudou_patch2.h rondoudou_patch2.c) \ No newline at end of file +add_executable(test_avec_cipher main.c rondoudou_patch2.h rondoudou_patch2.c) +add_executable(test_sans_cipher main.c rondoudou_patch2.h rondoudou_patch2.c) +target_compile_definitions(test_sans_cipher PRIVATE NOCIPHER) diff --git a/patch2/Makefile b/patch2/Makefile index 5b53fae..7674834 100644 --- a/patch2/Makefile +++ b/patch2/Makefile @@ -1,2 +1,6 @@ -test_patch2: main.c rondoudou_patch2.c rondoudou_patch2.h - gcc -Wall -g main.c rondoudou_patch2.c -o test_patch2 \ No newline at end of file +test_avec_cipher: main.c rondoudou_patch2.c rondoudou_patch2.h + gcc -g main.c rondoudou_patch2.c -o test_avec_cipher + +test_sans_cipher: main.c rondoudou_patch2.c rondoudou_patch2.h + gcc -g main.c rondoudou_patch2.c -DNOCIPHER -o test_sans_cipher + diff --git a/patch2/main.c b/patch2/main.c index 45ae50d..daa6e91 100644 --- a/patch2/main.c +++ b/patch2/main.c @@ -1,4 +1,6 @@ +#include #include "rondoudou_patch2.h" +#include "time.h" void f() { cipher; @@ -15,7 +17,12 @@ void g(int a) { int h(int a) { cipher; print_log("Dans h(%d)\n", a); - change_key(1516531); + if (a > 0) { + int ret = h(a - 1); + decipher; + return ret; + } + change_key(15165314561313153217ul); decipher; return 5; @@ -23,13 +30,19 @@ int h(int a) { int main() { + struct timespec start, end; + + clock_gettime(CLOCK_MONOTONIC, &start); + cipher; - print_log("Appel de f\n"); - f(); - print_log("Appel de g(3)\n"); - g(3); - print_log("Appel de f(6)\n"); - h(6); + for (int i = 0; i < 1000; ++i) { + h(25); + } decipher; + + clock_gettime(CLOCK_MONOTONIC, &end); + unsigned long ns = (end.tv_sec - start.tv_sec) * 1000000000ul + end.tv_nsec - start.tv_nsec; + printf("%.3lfμs\n", (double) ns / 1000.0); + return 0; } diff --git a/patch2/rondoudou_patch2.c b/patch2/rondoudou_patch2.c index 6487fe6..d59a416 100644 --- a/patch2/rondoudou_patch2.c +++ b/patch2/rondoudou_patch2.c @@ -7,7 +7,7 @@ uintptr_t rondoudou_patch_key = 0xffffffffffffffff; uintptr_t rondoudou_patch_offset = 1; uintptr_t *rondoudou_patch_return_addr_addr = 0; -int print_debug = 1; +int print_debug = 0; int print_log(const char *format, ...) { if (print_debug) { diff --git a/patch2/rondoudou_patch2.h b/patch2/rondoudou_patch2.h index 7813174..120a399 100644 --- a/patch2/rondoudou_patch2.h +++ b/patch2/rondoudou_patch2.h @@ -10,6 +10,15 @@ extern uintptr_t *rondoudou_patch_return_addr_addr; int print_log(const char *format, ...); +#ifdef NOCIPHER + +#define cipher +#define decipher +#define change_one_address(i, new_key) +#define change_key(new_key) + +#else + #define cipher \ do { \ rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(0) + rondoudou_patch_offset; \ @@ -32,138 +41,52 @@ int print_log(const char *format, ...); rondoudou_patch_call_level--; \ } while(0) +#define change_one_address(i, new_key) \ + if (rondoudou_patch_call_level > (i)) { \ + rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(i) + rondoudou_patch_offset; \ + print_log("%d: encrypted ret address = %018p\n", i, *rondoudou_patch_return_addr_addr); \ + *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ + print_log("%d: decrypted ret address = %018p\n", i, *rondoudou_patch_return_addr_addr); \ + *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ (new_key); \ + print_log("%d: reencrypted ret address = %018p\n", i, *rondoudou_patch_return_addr_addr); \ + } \ + #define change_key(new_key) \ do { \ print_log("\n--- Changing key ---\n"); \ print_log("Call_level = %d\n", rondoudou_patch_call_level); \ - \ - if (rondoudou_patch_call_level > 0) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(0) + rondoudou_patch_offset; \ - print_log("0: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("0: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("0: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - if (rondoudou_patch_call_level > 1) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(1) + rondoudou_patch_offset; \ - print_log("1: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("1: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("1: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - \ - if (rondoudou_patch_call_level > 2) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(2) + rondoudou_patch_offset; \ - print_log("2: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("2: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("2: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - \ - if (rondoudou_patch_call_level > 3) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(3) + rondoudou_patch_offset; \ - print_log("3: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("3: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("3: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - \ - \ - if (rondoudou_patch_call_level > 4) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(4) + rondoudou_patch_offset; \ - print_log("4: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("4: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("4: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - \ - if (rondoudou_patch_call_level > 6) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(6) + rondoudou_patch_offset; \ - print_log("6: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("6: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("6: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - if (rondoudou_patch_call_level > 7) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(7) + rondoudou_patch_offset; \ - print_log("7: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("7: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("7: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - if (rondoudou_patch_call_level > 8) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(8) + rondoudou_patch_offset; \ - print_log("8: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("8: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("8: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - if (rondoudou_patch_call_level > 9) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(9) + rondoudou_patch_offset; \ - print_log("9: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("9: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("9: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - if (rondoudou_patch_call_level > 10) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(10) + rondoudou_patch_offset; \ - print_log("10: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("10: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("10: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - if (rondoudou_patch_call_level > 11) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(11) + rondoudou_patch_offset; \ - print_log("11: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("11: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("11: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - if (rondoudou_patch_call_level > 12) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(12) + rondoudou_patch_offset; \ - print_log("12: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("12: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("12: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - if (rondoudou_patch_call_level > 13) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(13) + rondoudou_patch_offset; \ - print_log("13: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("13: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("13: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - if (rondoudou_patch_call_level > 14) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(14) + rondoudou_patch_offset; \ - print_log("14: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("14: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("14: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ - if (rondoudou_patch_call_level > 15) { \ - rondoudou_patch_return_addr_addr = (uintptr_t *)__builtin_frame_address(15) + rondoudou_patch_offset; \ - print_log("15: encrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ rondoudou_patch_key; \ - print_log("15: decrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - *rondoudou_patch_return_addr_addr = *rondoudou_patch_return_addr_addr ^ new_key; \ - print_log("15: reencrypted ret address = %018p\n", *rondoudou_patch_return_addr_addr); \ - } \ + \ + change_one_address(0, new_key); \ + change_one_address(1, new_key); \ + change_one_address(2, new_key); \ + change_one_address(3, new_key); \ + change_one_address(4, new_key); \ + change_one_address(5, new_key); \ + change_one_address(6, new_key); \ + change_one_address(7, new_key); \ + change_one_address(8, new_key); \ + change_one_address(9, new_key); \ + change_one_address(10, new_key); \ + change_one_address(11, new_key); \ + change_one_address(12, new_key) \ + change_one_address(13, new_key); \ + change_one_address(14, new_key); \ + change_one_address(15, new_key); \ + change_one_address(16, new_key); \ + change_one_address(17, new_key); \ + change_one_address(18, new_key); \ + change_one_address(19, new_key); \ + change_one_address(20, new_key); \ + change_one_address(21, new_key); \ + change_one_address(22, new_key); \ + change_one_address(23, new_key); \ + change_one_address(24, new_key); \ + change_one_address(25, new_key); \ + change_one_address(26, new_key); \ + \ print_log("\n"); \ rondoudou_patch_key = new_key; \ } while (0) +#endif // NOCIPHER #endif //PATCH2_RONDOUDOU_PATCH2_H diff --git a/patch2/test_perf.sh b/patch2/test_perf.sh new file mode 100755 index 0000000..034d2a2 --- /dev/null +++ b/patch2/test_perf.sh @@ -0,0 +1,7 @@ +#!/bin/bash +make test_avec_cipher +make test_sans_cipher +echo "-- Sans cipher --" +./test_sans_cipher +echo "-- Avec cipher --" +./test_avec_cipher \ No newline at end of file