From 78ff534aee21a00e3dc6a6a79911fc27b1dc86a2 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 16 Jun 2024 16:27:37 +0200 Subject: [PATCH] feat(print.s): unpadding WIP --- print.s | 26 ++++++++++++++++++++++++-- srcs/encrypt.c | 28 +++++++++++++++++++++------- srcs/woody.c | 16 +++++++++++----- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/print.s b/print.s index 1c0c26e..5fcfbd4 100644 --- a/print.s +++ b/print.s @@ -15,6 +15,11 @@ _start: 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 + shr r8, 2 + inc r8 + inc r8 + inc r8 + inc r8 mov r9, 0 ;increment register mov r10, 0 ;increment register xor r10, r10 @@ -78,7 +83,25 @@ _start: 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 + mov dword [rbx + r9], 0 + mov rcx, r10 + mov r15, r10 + shr r15, 5 + shl r15, 2 + inc rcx + shl rcx, 59 + shr rcx, 59 + shl rax, cl + mov r14, r9 + sub r14, r15 + add [rbx + r14], eax + mov rcx, rax + shr rcx, 32 + cmp r9, 0 + je first_block_skip + add [rbx + r14 - 4], ecx + + first_block_skip: ; unpadding and write back here pop rax add r9, 4 @@ -93,7 +116,6 @@ _start: pop r12 ; pop rsa.n pop r12 ; pop rsa.d - pop r15 pop r14 pop r13 diff --git a/srcs/encrypt.c b/srcs/encrypt.c index abbe67f..39b3c9b 100644 --- a/srcs/encrypt.c +++ b/srcs/encrypt.c @@ -10,21 +10,35 @@ unsigned long encrypt(char *file, unsigned long int offset, unsigned long int si } (void)rsa; size_t i = 0; - while (i < size) { + while (i < (size + 4)) { + /*if (i < 8) { + printf("%x\n", file[offset+i]); + }*/ size_t j = 0; + size_t tool = i % 4; + int tool2 = 0; + if (tool == 0) { + tool2 = 3; + } else if (tool == 1) { + tool2 = 1; + } else if (tool == 2) { + tool2 = -1; + } else { + tool2 = -3; + } + 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); + size_t bit_index = i * 8 + j; + //printf("gonna encrypt index %lu\n", offset + bit_index / 8); + padded[bit_index / 31] += (1 & (file[offset + bit_index / 8 + tool2] >> (7 - j))) << (30 - 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]); + printf("block : %x\n", padded[i]); padded[i] = pow_mod(padded[i] + 42 + i, 11317, rsa.n); - printf("encrypted block : %x\n\n", padded[i]);//, padded[i]); + printf("encrypted block : %x\n\n", 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)); diff --git a/srcs/woody.c b/srcs/woody.c index 83722d5..7f6f6e2 100644 --- a/srcs/woody.c +++ b/srcs/woody.c @@ -103,9 +103,9 @@ int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_posi { (void)rsa; //printf("salut %s\n", JUMP_VALUE); - for (size_t i = 0; i < payload->len; i++) { + /*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); @@ -113,17 +113,23 @@ int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_posi 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 && ptr_private_key) { + printf("payload position %ld (%lx)\n", payload_position, payload_position); + 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); int32_t woody_index = ptr_woody - payload->payload; int32_t jmp_index = ptr_jmp_value - sizeof(JUMP) - payload->payload; - int32_t jump_value = ((payload_position + jmp_index + 5) - woody->Ehdr->e_entry) * -1; // 5 = JUMP SIZE (OPCODE + 4 bytes operand) - ft_memcpy(&payload->payload[jmp_index + 1], &jump_value, sizeof(jump_value)); + int32_t jump_value = ((payload_position + jmp_index + 5 - 1) - woody->Ehdr->e_entry) * -1; // 5 = JUMP SIZE (OPCODE + 4 bytes operand) + ft_memcpy(&payload->payload[jmp_index + 1 - 1], &jump_value, sizeof(jump_value)); int64_t text_index = ptr_text_section - payload->payload; int64_t text_value = payload_position - woody->Phdr[load_segment_index].p_offset + woody_index; ft_memcpy(&payload->payload[text_index], &text_value, sizeof(text_value)); int64_t section_index = ptr_section_size - payload->payload; - int64_t section_value = woody->Phdr[load_segment_index].p_memsz; //woody->text_section->sh_size; + int64_t section_value = woody->Phdr[load_segment_index].p_memsz * 33/32 + 1; //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;