220 lines
8.3 KiB
C
220 lines
8.3 KiB
C
#include "../includes/woody.h"
|
|
#include "../includes/rsa.h"
|
|
|
|
int elf_magic_numbers(char *str)
|
|
{
|
|
return (!ft_strncmp(str, ELFMAG, SELFMAG));
|
|
}
|
|
|
|
int save_elf(char *path, char *file, unsigned long int size)
|
|
{
|
|
int fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0755);
|
|
if (fd == -1) {
|
|
printf("Error: Failed to create new file \'%s\'\n", path);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
if (write(fd, file, size) == -1) {
|
|
close(fd);
|
|
printf("Error: Failed to write new file \'%s\'\n", path);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
if (close(fd) == -1) {
|
|
printf("Error: Failed to close new file \'%s\'\n", path);
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int get_load_segment(t_elf_content *woody, int start, bool executable)
|
|
{
|
|
for (int i = start; i < woody->Ehdr->e_phnum; i++)
|
|
{
|
|
if (woody->Phdr[i].p_type == PT_LOAD)
|
|
{
|
|
if (executable)
|
|
{
|
|
if (woody->Phdr[i].p_flags & PF_X)
|
|
return i;
|
|
}
|
|
else
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void offset_sections(t_elf_content *woody, unsigned int from, unsigned int offset_ammount)
|
|
{
|
|
for (int i = 0; i < woody->Ehdr->e_phnum; i++)
|
|
{
|
|
if (woody->Phdr[i].p_offset > from)
|
|
woody->Phdr[i].p_offset += offset_ammount;
|
|
}
|
|
for (int i = 0; i < woody->Ehdr->e_shnum; i++)
|
|
{
|
|
if (woody->Shdr[i].sh_offset > from)
|
|
woody->Shdr[i].sh_offset += offset_ammount;
|
|
}
|
|
}
|
|
|
|
size_t create_codecave(t_elf_content *woody, Elf64_Phdr *load_segment, t_payload *payload)
|
|
{
|
|
const unsigned int page_size = 4096; // getpagesize(); not authorized
|
|
unsigned int padding_size = ((payload->len / page_size) + 1) * page_size;
|
|
unsigned int codecave_start = load_segment->p_offset + load_segment->p_filesz;
|
|
offset_sections(woody, codecave_start, padding_size);
|
|
char *new_woody = malloc(woody->file_size + padding_size);
|
|
if (!new_woody)
|
|
return 0;
|
|
ft_memcpy(new_woody, woody->file, codecave_start);
|
|
ft_bzero(new_woody + codecave_start, padding_size);
|
|
ft_memcpy(new_woody + codecave_start + padding_size, woody->file + codecave_start, woody->file_size - codecave_start);
|
|
munmap(woody->file, woody->file_size);
|
|
woody->file = new_woody;
|
|
woody->file_size += padding_size;
|
|
woody->Ehdr = (Elf64_Ehdr *)new_woody;
|
|
woody->Phdr = (Elf64_Phdr *)fetch(woody->file, woody->file_size, woody->Ehdr->e_phoff, sizeof(Elf64_Phdr));
|
|
woody->Shdr = (Elf64_Shdr *)fetch(woody->file, woody->file_size, woody->Ehdr->e_shoff, sizeof(Elf64_Shdr));
|
|
return codecave_start;
|
|
}
|
|
|
|
t_payload *get_payload()
|
|
{
|
|
t_payload *payload = malloc(sizeof(t_payload));
|
|
if (!payload)
|
|
return NULL;
|
|
char buffer[1024];
|
|
int fd = open("payload", O_RDONLY);
|
|
if (fd == -1) {
|
|
perror("error opening payload");
|
|
free(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, 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 && 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 - 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 * 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;
|
|
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;
|
|
}
|
|
|
|
void inject(t_elf_content *woody)
|
|
{
|
|
t_payload *payload = get_payload();
|
|
int i = get_load_segment(woody, 0, true);
|
|
int j = get_load_segment(woody, i + 1, false);
|
|
|
|
size_t code_cave_size = woody->Phdr[j].p_offset - (woody->Phdr[i].p_offset + woody->Phdr[i].p_filesz);
|
|
size_t payload_position;
|
|
printf("load position = : %ld (%lx)\n", woody->Phdr[i].p_offset, woody->Phdr[i].p_offset);
|
|
printf("load size = : %ld (%lx)\n", woody->Phdr[i].p_filesz, woody->Phdr[i].p_filesz);
|
|
if (code_cave_size > payload->len) // inverse here to test the other technique
|
|
{
|
|
payload_position = woody->Phdr[i].p_offset + woody->Phdr[i].p_memsz;
|
|
printf("Code_cave_size = %ld (%lx)\n", code_cave_size, code_cave_size);
|
|
}
|
|
else
|
|
{
|
|
payload_position = create_codecave(woody, &woody->Phdr[i], payload);
|
|
}
|
|
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 + 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);
|
|
}
|
|
|
|
int get_elf_sections(t_elf_content *woody)
|
|
{
|
|
woody->Ehdr = (Elf64_Ehdr *)fetch(woody->file, woody->file_size, 0, sizeof(Elf64_Ehdr));
|
|
if (!woody->Ehdr || !elf_magic_numbers(woody->file) || woody->Ehdr->e_ident[EI_CLASS] != ELFCLASS64)
|
|
{
|
|
printf("Error: \'%s\' is not a valid 64-bit ELF file\n", woody->file_path);
|
|
return EXIT_FAILURE;
|
|
}
|
|
woody->Phdr = (Elf64_Phdr *)fetch(woody->file, woody->file_size, woody->Ehdr->e_phoff, sizeof(Elf64_Phdr));
|
|
|
|
woody->Shdr = (Elf64_Shdr *)fetch(woody->file, woody->file_size, woody->Ehdr->e_shoff, sizeof(Elf64_Shdr));
|
|
if (!woody->Shdr|| !fetch(woody->file, woody->file_size, woody->Ehdr->e_shoff, woody->Ehdr->e_shnum * sizeof(Elf64_Shdr)))
|
|
return EXIT_FAILURE;
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int prepare_injection(t_elf_content *woody)
|
|
{
|
|
int elf_statut = get_elf_sections(woody);
|
|
if (elf_statut)
|
|
return elf_statut;
|
|
inject(woody);
|
|
char *woody_file;
|
|
if (!(woody_file = malloc(woody->file_size)))
|
|
return ft_put_error("Allocation error");
|
|
ft_memcpy(woody_file, woody->file, woody->file_size);
|
|
munmap(woody_file, woody->file_size);
|
|
save_elf("woody", woody_file, woody->file_size);
|
|
free(woody_file);
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|