80 lines
2.2 KiB
C
80 lines
2.2 KiB
C
|
#include "../includes/woody.h"
|
||
|
|
||
|
int get_load_segment64(t_elf_content *woody, int start, bool executable)
|
||
|
{
|
||
|
t_elf64 *elf = woody->elf64;
|
||
|
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 inject64(t_elf_content *woody)
|
||
|
{
|
||
|
t_elf64 *elf = woody->elf64;
|
||
|
t_payload *payload = get_payload();
|
||
|
if (!payload)
|
||
|
return EXIT_FAILURE;
|
||
|
|
||
|
int i = get_load_segment64(woody, 0, true);
|
||
|
int j = get_load_segment64(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;
|
||
|
|
||
|
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->Ehdr->e_entry = payload_position;
|
||
|
elf->Phdr[i].p_flags = PF_X | PF_W | PF_R;
|
||
|
free(payload->payload);
|
||
|
free(payload);
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
int get_elf_sections64(t_elf_content *woody)
|
||
|
{
|
||
|
woody->elf64->Ehdr = (Elf64_Ehdr *)fetch(woody->file, woody->file_size, 0, sizeof(Elf64_Ehdr));
|
||
|
if (!woody->elf64->Ehdr)
|
||
|
return EXIT_FAILURE;
|
||
|
|
||
|
woody->elf64->Phdr = (Elf64_Phdr *)fetch(woody->file, woody->file_size, woody->elf64->Ehdr->e_phoff, sizeof(Elf64_Phdr));
|
||
|
if (!woody->elf64->Phdr)
|
||
|
return EXIT_FAILURE;
|
||
|
|
||
|
woody->elf64->Shdr = (Elf64_Shdr *)fetch(woody->file, woody->file_size, woody->elf64->Ehdr->e_shoff, sizeof(Elf64_Shdr));
|
||
|
if (!woody->elf64->Shdr || !fetch(woody->file, woody->file_size, woody->elf64->Ehdr->e_shoff, woody->elf64->Ehdr->e_shnum * sizeof(Elf64_Shdr)))
|
||
|
return EXIT_FAILURE;
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|