Ajout de tests de performance pour les deux versions du patch rondoudou

This commit is contained in:
Yohan Simard 2021-04-07 20:05:46 +02:00
parent 239e8ba670
commit 32cd3068f3
12 changed files with 149 additions and 170 deletions

3
lib/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
test_avec_cipher
test_sans_cipher
test

View file

@ -1,2 +1,10 @@
test: test_chiffrement.c rondoudouPatch.c rondoudouPatch.h
gcc -Wall -g test_chiffrement.c rondoudouPatch.c -o test
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

View file

@ -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<max; i++) {
printf("%d -> %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);

View file

@ -1,36 +1,34 @@
#include "rondoudouPatch.h"
#include <stdio.h>
#include <time.h>
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);
}

7
lib/test_perf.sh Executable file
View file

@ -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

3
patch2/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
test_avec_cipher
test_sans_cipher
test

View file

@ -3,4 +3,6 @@ project(patch2 C)
set(CMAKE_C_STANDARD 99)
add_executable(patch2 main.c rondoudou_patch2.h rondoudou_patch2.c)
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)

View file

@ -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
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

View file

@ -1,4 +1,6 @@
#include <stdio.h>
#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;
}

View file

@ -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) {

View file

@ -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

7
patch2/test_perf.sh Executable file
View file

@ -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