From c9d07e22a9ad6c18fd3891e2b30ac6cb7371aaf0 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sat, 15 Jun 2024 12:43:59 +0200 Subject: [PATCH] feat(print.s): decypher blocks --- Makefile | 10 ++-- includes/rsa.h | 29 ++++++++++++ includes/woody.h | 7 ++- print.s | 118 ++++++++++++++++++++++++++++++++++++++--------- srcs/encrypt.c | 33 +++++++++++-- srcs/primes.c | 74 +++++++++++++++++++++++++++++ srcs/rsa.c | 53 +++++++++++++++++++++ srcs/woody.c | 35 +++++++++++--- zreset_woody.sh | 1 + 9 files changed, 319 insertions(+), 41 deletions(-) create mode 100644 includes/rsa.h create mode 100644 srcs/primes.c create mode 100644 srcs/rsa.c create mode 100755 zreset_woody.sh diff --git a/Makefile b/Makefile index 6ecb028..f51ff5f 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,9 @@ SRCS_PATH = srcs/ SRCS = $(SRCS_PATH)main.c \ $(SRCS_PATH)utils.c \ $(SRCS_PATH)woody.c \ - $(SRCS_PATH)encrypt.c + $(SRCS_PATH)encrypt.c \ + $(SRCS_PATH)rsa.c \ + $(SRCS_PATH)primes.c OBJS = ${SRCS:.c=.o} @@ -23,7 +25,7 @@ all: ${NAME} .c.o: ${CC} ${INCLUDES} ${DEFINES} ${CFLAGS} -c $< -o $@ -$(NAME): ${OBJS} +$(NAME): ${OBJS} includes/woody.h make -C ft_printf ${CC} ${OBJS} ${LIBFT_FLAGS} -o ${NAME} @@ -31,11 +33,11 @@ clean: make -C ft_printf clean ${RM} ${OBJS} -fclean: +fclean: clean make -C ft_printf fclean ${RM} ${NAME} re: fclean make all -.PHONY : all clean fclean re \ No newline at end of file +.PHONY : all clean fclean re diff --git a/includes/rsa.h b/includes/rsa.h new file mode 100644 index 0000000..4131693 --- /dev/null +++ b/includes/rsa.h @@ -0,0 +1,29 @@ +#ifndef _RSA_H +#define _RSA_H 1 + +#include +#include +#include +#include +#include +#include +#include +#include + +#define RSA_BLOCK_SIZE 128 + +typedef struct rsa_s { + uint64_t n; + uint64_t d; +} rsa_t; + +void *protected_malloc(size_t size); + +rsa_t rsa_generate_keys(); + +uint16_t generate_prime(); +uint64_t pow_mod(uint64_t nn, uint64_t e, uint64_t mm); +uint16_t get_random_bytes(int fd); + +#endif + diff --git a/includes/woody.h b/includes/woody.h index 0833ae4..01d898e 100644 --- a/includes/woody.h +++ b/includes/woody.h @@ -1,6 +1,8 @@ #ifndef WOODY_H #define WOODY_H +#include "rsa.h" + #include "../ft_printf/includes/ft_printf.h" #include #include @@ -17,10 +19,11 @@ #define JUMP "\xe9" #define WOODY "....WOODY...." -#define JUMP_VALUE "\xda\xda\xda" +#define JUMP_VALUE "\xda\xda" #define TEXT_OFFSET "\xba\xba\xba\xba\xba\xba\xba\xba" #define SECTION_SIZE "\xca\xca\xca\xca\xca\xca\xca\xca" +#define PRIVATE_KEY "\xcd\xab\xef\xcd\xab\xef\xcd\xab" typedef struct payload { @@ -49,7 +52,7 @@ int get_symbols_count(int sh_size, int sh_entsize); int prepare_injection(t_elf_content *woody); // encrypt.c -void encrypt(char *file, unsigned long int offset, unsigned long int size); +unsigned long encrypt(char *file, unsigned long int offset, unsigned long int size, rsa_t rsa); #endif diff --git a/print.s b/print.s index 0b5475f..1c0c26e 100644 --- a/print.s +++ b/print.s @@ -2,36 +2,108 @@ bits 64 global _start _start: - push rax - push rdi - push rsi - push rdx + push rbp + push rsp + push rbx + push r12 + push r13 + push r14 + push r15 mov rdi, 1 lea rsi, [rel msg] - mov rax, rsi - sub rax, qword [rel text_section] ;text_section address - mov r8, qword [rel section_sisze] ;text_section size + mov rbx, rsi + sub rbx, qword [rel text_section] ;text_section address because of this and that + mov r8, qword [rel section_size] ;text_section size mov r9, 0 ;increment register + mov r10, 0 ;increment register xor r10, r10 - encrypt: - cmp r8, r9 - je end_encrypt - movzx r10, byte[rax + r9] - inc r10b ;rot + 1 - mov byte[rax + r9], r10b - inc r9 - jmp encrypt - end_encrypt: + xor r13, r13 + mov r13d, dword [rel private_key] + xor r12, r12 + mov r12d, dword [rel private_key + 4] + ;shr r12, 32 + push r13 ; push rsa.d + push r12 ; push rsa.n + jmp decrypt_loop + + ; rbx is adress of text(encrypted) section + ; r8 is section size + ; r9 is index + ; rax is cypher that needs to be converted to message + ; dword [rsp + 16] is rsa.d + ; dword [rsp + 8] is rsa.n + ; qword [rsp] is cypher backup + decrypt_once: + mov r11, 0x100000000 + sq_mul_bit_index: + shr r11, 1 + mov r12, r11 + and r12, qword [rsp + 16] + jz sq_mul_bit_index + sq_mul_loop: + shr r11, 1 + cmp r11, 0 + je decrypt_loop2 + mul rax, + ; modulo n ... + mov r13, qword [rsp + 8] + xor rdx, rdx + div r13 + mov rax, rdx + ; modulo n ... + mov r12, r11 + and r12, qword [rsp + 16] + cmp r12, 0 + je sq_mul_loop + mov r13, qword [rsp] + mul r13 + ; modulo n ... + mov r13, qword [rsp + 8] + xor rdx, rdx + div r13 + mov rax, rdx + ; modulo n ... + jmp sq_mul_loop + + decrypt_loop: + cmp r8, r10 + je end_decrypt + xor rax, rax + mov eax, dword [rbx + r9] + push rax + ;push r10 + jmp decrypt_once + decrypt_loop2: + sub rax, 42 ; remove 42 of result (avoid 0 values) + sub rax, r10 ; remove index of result (caesar like cypher so 0/42 values are differents) + ; unpadding and write back here + ;mov [rbx + r9], rax + ; unpadding and write back here + pop rax + add r9, 4 + inc r10 + jmp decrypt_loop + + end_decrypt: mov rdx, 14 mov rax, 1 syscall - pop rdx - pop rsi - pop rdi - pop rax - jmp 0xdadadada - msg db "....WOODY....",10 + pop r12 ; pop rsa.n + pop r12 ; pop rsa.d + + + pop r15 + pop r14 + pop r13 + pop r12 + pop rbx + pop rsp + pop rbp + + jmp 0xdadadada ; this needs to be just before that + msg db "....WOODY....",10 ; that needs to be just after this text_section dq 0xbabababababababa - section_sisze dq 0xcacacacacacacaca + section_size dq 0xcacacacacacacaca + private_key dq 0xabcdefabcdefabcd diff --git a/srcs/encrypt.c b/srcs/encrypt.c index c57c8f1..abbe67f 100644 --- a/srcs/encrypt.c +++ b/srcs/encrypt.c @@ -1,15 +1,38 @@ #include "../includes/woody.h" +#include "../includes/rsa.h" -void encrypt(char *file, unsigned long int offset, unsigned long int size) +unsigned long encrypt(char *file, unsigned long int offset, unsigned long int size, rsa_t rsa) { + size_t padded_len = size * sizeof(char) * 33 / sizeof(uint32_t) / 32 + 1; // every 32 octet one padding octet, plus one for the remainder (uses too much memory for size % 128 == 0 but fuck you) + uint32_t *padded = (uint32_t *)malloc(sizeof(uint32_t) * padded_len); + for (size_t i = 0; i < padded_len; i++) { + padded[padded_len] = 0; + } + (void)rsa; size_t i = 0; - while (i < size) - { - file[offset + i] = file[offset + i] - 1; + while (i < size) { + size_t j = 0; + while (j < 8) { + size_t bit_index = i * 8 * sizeof(char) + j; + //printf("bit_index : %ld\n", bit_index); + padded[bit_index / 31] += (1 & (file[bit_index / 8] >> j)) << (bit_index % 31); + j++; + } + //file[offset + i] = file[offset + i] - 1; ++i; } + for (size_t i = 0; i < padded_len; i++) { + printf("block : %x\n", padded[i]);//, padded[i]); + padded[i] = pow_mod(padded[i] + 42 + i, 11317, rsa.n); + printf("encrypted block : %x\n\n", padded[i]);//, padded[i]); + //printf("decipher block : %lu (%lx)\n", pow_mod(padded[i], rsa.d, rsa.n) - 42 - i, pow_mod(padded[i], rsa.d, rsa.n) - 42 - i); + } + memcpy(&file[offset], padded, padded_len * sizeof(uint32_t)); printf("\nENCRYPTION : \n"); printf(" File encrypted from %ld (%lx) to %ld (%lx)\n", offset, offset, offset + size, offset + size); printf(" Size of encryption = %ld (%lx)\n", size, size); + printf(" Size of padded encryption = %ld (%lx)\n", padded_len * sizeof(uint32_t), padded_len * sizeof(uint32_t)); printf("\n"); -} \ No newline at end of file + return offset + padded_len * sizeof(uint32_t); +} + diff --git a/srcs/primes.c b/srcs/primes.c new file mode 100644 index 0000000..a95ac06 --- /dev/null +++ b/srcs/primes.c @@ -0,0 +1,74 @@ +#include "../includes/rsa.h" + +uint16_t get_random_bytes(int fd) { + uint16_t ret; + if (read(fd, &ret, sizeof(uint16_t)) == -1) { + exit(1); + } + return ret; +} + +// n pow e mod m +uint64_t pow_mod(uint64_t n, uint64_t e, uint64_t m) { + uint64_t y = 1; + + while (e > 1) { + if (e & 1) { + y = (y * n) % m; + } + n = (n * n) % m; + e = e >> 1; + } + return (n * y) % m; +} + +bool is_prime(uint16_t n, size_t k_max, int fd) { + uint16_t a = get_random_bytes(fd); + uint16_t d = n - 1; + uint16_t s = 0; + + while ((d & 1) == 0) { + s++; + d = d >> 1; + } + + for (size_t k = 0; k < k_max; k++) { + a = 0; + while (a < 2 || a > (n - 2)) { + a = get_random_bytes(fd); + } + uint16_t x = pow_mod(a, d, n); + uint16_t y; + for (uint16_t i = 0; i < s; i++) { + y = pow_mod(x, 2, n); + if (y == 1 && x != 1 && x != n - 1) + return false; + x = y; + } + if (y != 1) { + return false; + } + } + return true; +} + +uint16_t generate_prime_fd(int fd) { + uint16_t n = get_random_bytes(fd); + n |= 1 << 15; + n |= 1; + + while (!is_prime(n, 16, fd)) { + n = get_random_bytes(fd); + n |= 1 << 15; + n |= 1; + } + return n; +} + +uint16_t generate_prime() { + int fd = open("/dev/urandom", O_RDONLY); + uint16_t n = generate_prime_fd(fd); + close(fd); + return n; +} + diff --git a/srcs/rsa.c b/srcs/rsa.c new file mode 100644 index 0000000..b1db1e3 --- /dev/null +++ b/srcs/rsa.c @@ -0,0 +1,53 @@ +#include "../includes/rsa.h" + +int64_t euler(int64_t r0, int64_t r1) { + int64_t s0 = 1; + int64_t s1 = 0; + int64_t t0 = 0; + int64_t t1 = 1; + int64_t q0 = 0; + + while (r1 != 0) { + q0 = r0 / r1; + int64_t tmp = r0 % r1; + r0 = r1; + r1 = tmp; + tmp = s0 - q0 * s1; + s0 = s1; + s1 = tmp; + tmp = t0 - q0 * t1; + t0 = t1; + t1 = tmp; + } + return s0; +} + +rsa_t rsa_generate_keys(void) { + int64_t p = (uint64_t)generate_prime(); + int64_t q = (uint64_t)generate_prime(); + int64_t ln = (p - 1) * (q - 1); + int64_t e = 11317; + + while (ln % e == 0 || p == q) { + p = generate_prime(); + q = generate_prime(); + ln = (p - 1) * (q - 1); + } + + if (q > p) { + uint64_t tmp = p; + p = q; + q = tmp; + } + + int64_t n = p * q; + int64_t d = euler(e, ln) + ln; + if (d > n) { + d -= ln; + } + rsa_t rsa; + rsa.d = d; + rsa.n = n; + return rsa; +} + diff --git a/srcs/woody.c b/srcs/woody.c index 580758c..83722d5 100644 --- a/srcs/woody.c +++ b/srcs/woody.c @@ -1,4 +1,5 @@ #include "../includes/woody.h" +#include "../includes/rsa.h" int elf_magic_numbers(char *str) { @@ -92,18 +93,25 @@ t_payload *get_payload() exit(1); } payload->len = read(fd, buffer, 1024); + printf("payload len%ld\n", payload->len); payload->payload = malloc(sizeof(char) * payload->len); ft_memcpy(payload->payload, buffer, payload->len); return payload; } -int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_position, int load_segment_index) +int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_position, int load_segment_index, rsa_t rsa) { + (void)rsa; + //printf("salut %s\n", JUMP_VALUE); + for (size_t i = 0; i < payload->len; i++) { + printf("%c", *(payload->payload + i)); + } char *ptr_jmp_value = ft_strnstr_nullterminated(payload->payload, JUMP_VALUE, payload->len); char *ptr_woody = ft_strnstr_nullterminated(payload->payload, WOODY, payload->len); char *ptr_text_section = ft_strnstr_nullterminated(payload->payload, TEXT_OFFSET, payload->len); + char *ptr_private_key = ft_strnstr_nullterminated(payload->payload, PRIVATE_KEY, payload->len); char *ptr_section_size = ft_strnstr_nullterminated(payload->payload, SECTION_SIZE, payload->len); - if (ptr_jmp_value && ptr_woody && ptr_text_section && ptr_section_size) + if (ptr_jmp_value && ptr_woody && ptr_text_section && ptr_section_size && ptr_private_key) { int32_t woody_index = ptr_woody - payload->payload; int32_t jmp_index = ptr_jmp_value - sizeof(JUMP) - payload->payload; @@ -118,14 +126,24 @@ int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_posi int64_t section_value = woody->Phdr[load_segment_index].p_memsz; //woody->text_section->sh_size; ft_memcpy(&payload->payload[section_index], §ion_value, sizeof(section_value)); + int64_t private_key_index = ptr_private_key - payload->payload; + int64_t private_key_value = (rsa.n << 32) + rsa.d; + ft_memcpy(&payload->payload[private_key_index], &private_key_value, sizeof(uint64_t)); + + ft_memcpy(woody->file + payload_position, payload->payload, payload->len); - printf("Old entry : %ld (%lx)\n", woody->Ehdr->e_entry, woody->Ehdr->e_entry); printf("Code cave start = %ld (%lx)\n", payload_position, payload_position); printf("Payload size = %ld (%lx)\n", payload->len, payload->len); printf("Backward offset = %d (%x)(%x)\n", jump_value, jump_value, -jump_value); return EXIT_SUCCESS; } + printf("c'est la merde\n"); + printf("ptr_woody : %p\n", ptr_woody); + printf("ptr_section_size: %p\n", ptr_section_size); + printf("ptr_text_section : %p\n", ptr_text_section); + printf("ptr_jmp_value : %p\n", ptr_jmp_value); + printf("ptr_private_key: %p\n", ptr_private_key); return EXIT_FAILURE; } @@ -148,12 +166,15 @@ void inject(t_elf_content *woody) { payload_position = create_codecave(woody, &woody->Phdr[i], payload); } - encrypt(woody->file, woody->Phdr[i].p_offset, woody->Phdr[i].p_memsz); - insert_payload(woody, payload, payload_position, i); + rsa_t rsa = rsa_generate_keys(); + printf("key n : %ld (%lx) key d %ld (%lx), key total : %ld (%lx)\n", rsa.n, rsa.n, rsa.d, rsa.d, (rsa.n << 32) + rsa.d, (rsa.n << 32) + rsa.d); + payload_position = encrypt(woody->file, woody->Phdr[i].p_offset, woody->Phdr[i].p_memsz, rsa); + printf("Payload position : %ld (%lx)\n", payload_position, payload_position); + insert_payload(woody, payload, payload_position, i, rsa); woody->Ehdr->e_entry = payload_position; - woody->Phdr[i].p_filesz += payload->len; - woody->Phdr[i].p_memsz += payload->len; + woody->Phdr[i].p_filesz += payload->len + 15; + woody->Phdr[i].p_memsz += payload->len + 15; woody->Phdr[i].p_flags = PF_X | PF_W | PF_R; printf("New entry = %ld (%lx)\n", woody->Ehdr->e_entry, woody->Ehdr->e_entry); } diff --git a/zreset_woody.sh b/zreset_woody.sh new file mode 100755 index 0000000..8837fa8 --- /dev/null +++ b/zreset_woody.sh @@ -0,0 +1 @@ +./gen_payload.sh && rm -f woody && ./woody_woodpacker resources/sample64 | less