woody-woodpacker/srcs/woody.c

144 lines
4.3 KiB
C
Raw Normal View History

2024-02-14 10:37:05 +00:00
#include "../includes/woody.h"
2024-02-14 11:31:18 +00:00
int elf_magic_numbers(char *str)
{
return (!ft_strncmp(str, ELFMAG, SELFMAG));
}
int get_load_segment(t_elf_content *woody, int start, bool executable)
2024-02-14 10:37:05 +00:00
{
2024-02-19 10:35:40 +00:00
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;
}
2024-03-21 14:44:29 +00:00
t_payload *get_payload()
2024-03-19 16:20:11 +00:00
{
2024-03-21 14:44:29 +00:00
t_payload *payload = malloc(sizeof(t_payload));
if (!payload)
return NULL;
char buffer[1024];
int fd = open("payload", O_RDONLY);
2024-04-09 08:28:35 +00:00
if (fd == -1) {
ft_put_error("Failed to open payload");
2024-04-09 08:28:35 +00:00
free(payload);
close(fd);
return NULL;
2024-04-09 08:28:35 +00:00
}
2024-03-21 14:44:29 +00:00
payload->len = read(fd, buffer, 1024);
if (payload->len == -1)
{
ft_put_error("Failed to read payload");
free(payload);
close(fd);
return NULL;
}
close(fd);
2024-03-21 14:44:29 +00:00
payload->payload = malloc(sizeof(char) * payload->len);
if (!payload->payload)
{
ft_put_error("Allocation error");
free(payload);
return NULL;
}
2024-03-21 14:44:29 +00:00
ft_memcpy(payload->payload, buffer, payload->len);
return payload;
}
2024-03-19 16:20:11 +00:00
int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_position, int load_segment_index)
2024-03-21 14:44:29 +00:00
{
char *ptr_jmp_value = ft_strnstr_nullterminated(payload->payload, JUMP_VALUE, payload->len);
2024-04-17 10:14:08 +00:00
char *ptr_woody = ft_strnstr_nullterminated(payload->payload, WOODY, payload->len);
2024-04-11 21:15:15 +00:00
char *ptr_text_section = ft_strnstr_nullterminated(payload->payload, TEXT_OFFSET, 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)
2024-02-21 12:13:17 +00:00
{
2024-04-17 10:14:08 +00:00
int32_t woody_index = ptr_woody - payload->payload;
int32_t jmp_index = ptr_jmp_value - sizeof(JUMP) - payload->payload;
2024-04-17 07:02:51 +00:00
int32_t jump_value = ((payload_position + jmp_index + 5) - woody->Ehdr->e_entry) * -1; // 5 = JUMP SIZE (OPCODE + 4 bytes operand)
2024-03-21 14:44:29 +00:00
ft_memcpy(&payload->payload[jmp_index + 1], &jump_value, sizeof(jump_value));
2024-03-19 16:20:11 +00:00
2024-04-15 08:16:28 +00:00
int64_t text_index = ptr_text_section - payload->payload;
int64_t text_value = payload_position - woody->Phdr[load_segment_index].p_offset + woody_index;
2024-04-15 08:16:28 +00:00
ft_memcpy(&payload->payload[text_index], &text_value, sizeof(text_value));
2024-04-17 07:02:51 +00:00
2024-04-15 08:16:28 +00:00
int64_t section_index = ptr_section_size - payload->payload;
int64_t section_value = woody->Phdr[load_segment_index].p_memsz;
2024-04-15 08:16:28 +00:00
ft_memcpy(&payload->payload[section_index], &section_value, sizeof(section_value));
ft_memcpy(woody->file + payload_position, payload->payload, payload->len);
2024-03-19 16:20:11 +00:00
return EXIT_SUCCESS;
2024-02-21 12:13:17 +00:00
}
2024-03-19 16:20:11 +00:00
return EXIT_FAILURE;
2024-02-21 12:13:17 +00:00
}
int inject(t_elf_content *woody)
2024-02-19 10:35:40 +00:00
{
2024-03-21 14:44:29 +00:00
t_payload *payload = get_payload();
if (!payload)
return EXIT_FAILURE;
2024-04-15 04:17:31 +00:00
int i = get_load_segment(woody, 0, true);
2024-02-19 10:35:40 +00:00
int j = get_load_segment(woody, i + 1, false);
2024-04-15 04:17:31 +00:00
if (i == -1 || j != i + 1)
{
free(payload->payload);
free(payload);
return ft_put_error("PT_LOAD segment missing");
}
2024-03-19 16:20:11 +00:00
size_t code_cave_size = woody->Phdr[j].p_offset - (woody->Phdr[i].p_offset + woody->Phdr[i].p_filesz);
2024-05-23 11:37:43 +00:00
size_t payload_position = woody->Phdr[i].p_offset + woody->Phdr[i].p_filesz;
if (code_cave_size < (size_t)payload->len)
2024-03-19 16:20:11 +00:00
{
free(payload->payload);
free(payload);
return ft_put_error("Unable to insert payload, not enough space for code cave");
2024-03-19 16:20:11 +00:00
}
2024-05-23 11:37:43 +00:00
2024-03-21 14:44:29 +00:00
woody->Phdr[i].p_filesz += payload->len;
woody->Phdr[i].p_memsz += payload->len;
if (insert_payload(woody, payload, payload_position, i))
{
free(payload->payload);
free(payload);
return ft_put_error("Unable to insert payload, please regenerate it");
}
2024-06-17 17:28:54 +00:00
woody->Ehdr->e_entry = payload_position;
2024-06-16 14:30:50 +00:00
woody->Phdr[i].p_flags = PF_X | PF_W | PF_R;
free(payload->payload);
free(payload);
return EXIT_SUCCESS;
2024-02-19 10:35:40 +00:00
}
int get_elf_sections(t_elf_content *woody)
2024-02-19 10:35:40 +00:00
{
2024-02-21 12:54:33 +00:00
woody->Ehdr = (Elf64_Ehdr *)fetch(woody->file, woody->file_size, 0, sizeof(Elf64_Ehdr));
2024-02-23 13:17:23 +00:00
if (!woody->Ehdr || !elf_magic_numbers(woody->file) || woody->Ehdr->e_ident[EI_CLASS] != ELFCLASS64)
2024-02-14 11:31:18 +00:00
{
printf("Error: \'%s\' is not a valid 64-bit ELF file\n", woody->file_path);
2024-02-14 10:37:05 +00:00
return EXIT_FAILURE;
2024-02-14 11:31:18 +00:00
}
2024-03-19 16:20:11 +00:00
woody->Phdr = (Elf64_Phdr *)fetch(woody->file, woody->file_size, woody->Ehdr->e_phoff, sizeof(Elf64_Phdr));
2024-02-14 11:31:18 +00:00
2024-03-19 16:20:11 +00:00
woody->Shdr = (Elf64_Shdr *)fetch(woody->file, woody->file_size, woody->Ehdr->e_shoff, sizeof(Elf64_Shdr));
2024-04-11 10:20:44 +00:00
if (!woody->Shdr|| !fetch(woody->file, woody->file_size, woody->Ehdr->e_shoff, woody->Ehdr->e_shnum * sizeof(Elf64_Shdr)))
return EXIT_FAILURE;
2024-02-21 12:54:33 +00:00
return EXIT_SUCCESS;
}