#include "../includes/woody.h" int get_load_segment32(t_elf_content *woody, int start, bool executable) { t_elf32 *elf = woody->elf32; for (int i = start; i < elf->Ehdr->e_phnum; i++) { if (elf->Phdr[i].p_type == PT_LOAD) { if (executable) { if (elf->Phdr[i].p_flags & PF_X) return i; } else return i; } } return -1; } int inject32(t_elf_content *woody) { t_elf32 *elf = woody->elf32; t_payload *payload = get_payload(); if (!payload) return EXIT_FAILURE; int i = get_load_segment32(woody, 0, true); int j = get_load_segment32(woody, i + 1, false); if (i == -1 || j != i + 1) { free(payload->payload); free(payload); return ft_put_error("PT_LOAD segment missing"); } size_t code_cave_size = elf->Phdr[j].p_offset - (elf->Phdr[i].p_offset + elf->Phdr[i].p_filesz); size_t payload_position = elf->Phdr[i].p_offset + elf->Phdr[i].p_filesz; if (code_cave_size < (size_t)payload->len) { free(payload->payload); free(payload); return ft_put_error("Unable to insert payload, not enough space for code cave"); } elf->Phdr[i].p_filesz += payload->len; elf->Phdr[i].p_memsz += payload->len; elf->Ehdr->e_entry = payload_position; if (insert_payload(woody, payload, payload_position, elf->Ehdr->e_entry, elf->Phdr[i].p_offset, elf->Phdr[i].p_memsz)) { free(payload->payload); free(payload); return ft_put_error("Unable to insert payload, please regenerate it"); } elf->Phdr[i].p_flags = PF_X | PF_W | PF_R; free(payload->payload); free(payload); return EXIT_SUCCESS; } int get_elf_sections32(t_elf_content *woody) { woody->elf32->Ehdr = (Elf32_Ehdr *)fetch(woody->file, woody->file_size, 0, sizeof(Elf32_Ehdr)); if (!woody->elf32->Ehdr) return EXIT_FAILURE; woody->elf32->Phdr = (Elf32_Phdr *)fetch(woody->file, woody->file_size, woody->elf32->Ehdr->e_phoff, sizeof(Elf32_Phdr)); if (!woody->elf32->Phdr) return EXIT_FAILURE; woody->elf32->Shdr = (Elf32_Shdr *)fetch(woody->file, woody->file_size, woody->elf32->Ehdr->e_shoff, sizeof(Elf32_Shdr)); if (!woody->elf32->Shdr || !fetch(woody->file, woody->file_size, woody->elf32->Ehdr->e_shoff, woody->elf32->Ehdr->e_shnum * sizeof(Elf32_Shdr))) return EXIT_FAILURE; return EXIT_SUCCESS; }