woody-woodpacker/srcs/woody.c

174 lines
5.4 KiB
C
Raw Normal View History

2024-02-14 10:37:05 +00:00
#include "../includes/woody.h"
2024-02-19 10:35:40 +00:00
2024-02-14 11:31:18 +00:00
int elf_magic_numbers(char *str)
{
return (!ft_strncmp(str, ELFMAG, SELFMAG));
}
2024-02-14 13:16:28 +00:00
void encrypt_zone(char *file, unsigned long int offset, unsigned long int size)
2024-02-14 11:31:18 +00:00
{
2024-02-14 13:16:28 +00:00
size_t i = 0;
while (i < size)
2024-02-14 11:31:18 +00:00
{
2024-02-14 13:16:28 +00:00
file[offset + i] = 0;
++i;
2024-02-14 11:31:18 +00:00
}
}
2024-02-14 13:16:28 +00:00
int save_elf(char *path, char *file, unsigned long int size)
2024-02-14 11:31:18 +00:00
{
2024-02-14 13:16:28 +00:00
int fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0755);
if (fd == -1) {
ft_printf("Error: Failed to create new file \'%s\'\n", path);
return EXIT_FAILURE;
}
2024-02-14 11:31:18 +00:00
2024-02-14 13:16:28 +00:00
if (write(fd, file, size) == -1) {
close(fd);
ft_printf("Error: Failed to write new file \'%s\'\n", path);
return EXIT_FAILURE;
}
2024-02-14 11:31:18 +00:00
2024-02-14 13:16:28 +00:00
if (close(fd) == -1) {
ft_printf("Error: Failed to close new file \'%s\'\n", path);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
2024-02-14 11:31:18 +00:00
}
2024-02-19 10:35:40 +00:00
int get_load_segment(t_efl_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-02-21 12:13:17 +00:00
int32_t find_jmp(char *code, size_t len)
{
char *jump = JUMP;
char *ptr = ft_strnstr_nullterminated(code, jump, len);
if (ptr)
{
return ptr - code;
}
return 0;
}
2024-02-19 10:35:40 +00:00
void find_cave(t_efl_content *woody)
{
2024-02-19 15:49:40 +00:00
woody->Phdr = (Elf64_Phdr *)secure_jump(woody->file, woody->file_size, woody->Ehdr->e_phoff, sizeof(Elf64_Phdr));
2024-02-19 10:35:40 +00:00
int i = get_load_segment(woody, 0, true);
int j = get_load_segment(woody, i + 1, false);
printf("%d %ld\n", i, woody->Phdr[i].p_align);
printf("%d %ld\n", j, woody->Phdr[j].p_align);
printf("code_cave_start = %lx, virtual adress = %lx\n", woody->Phdr[i].p_offset, woody->Phdr[i].p_vaddr);
printf("code_cave_size = %lx\n", woody->Phdr[j].p_offset - (woody->Phdr[i].p_offset + woody->Phdr[i].p_filesz));
2024-02-21 12:13:17 +00:00
Elf64_Off payload_off = woody->Phdr[i].p_offset + woody->Phdr[i].p_memsz;
size_t len = sizeof(PAYLOAD) - 1;
char payload[] = PAYLOAD;
int32_t jmp = find_jmp(payload, len);
2024-02-19 10:35:40 +00:00
2024-02-21 12:13:17 +00:00
printf("%ld\n", (long int)payload[jmp + 1]);
int32_t test = ((payload_off + len) - woody->Ehdr->e_entry) * -1;
2024-02-19 10:35:40 +00:00
2024-02-21 12:13:17 +00:00
ft_memcpy(&payload[jmp + 1], &test, sizeof(test));
2024-02-19 10:35:40 +00:00
2024-02-21 12:13:17 +00:00
ft_memcpy(woody->file + payload_off, payload, len);
2024-02-14 10:37:05 +00:00
2024-02-19 15:49:40 +00:00
printf("old entry : %lx\n", woody->Ehdr->e_entry);
2024-02-21 12:13:17 +00:00
printf("backward offset = %ld\n", (payload_off + len) - woody->Ehdr->e_entry);
woody->Ehdr->e_entry = payload_off;
2024-02-19 10:35:40 +00:00
woody->Phdr[i].p_filesz += len;
woody->Phdr[i].p_memsz += len;
2024-02-21 12:13:17 +00:00
printf("new entry = %lx\n", woody->Ehdr->e_entry);
2024-02-19 15:49:40 +00:00
printf("p_filesz = %lx\n", woody->Phdr[i].p_filesz);
printf("p_memsz = %lx\n", woody->Phdr[i].p_memsz);
2024-02-21 12:13:17 +00:00
woody->file_size += len;
2024-02-19 10:35:40 +00:00
}
int inject(t_efl_content *woody)
{
2024-02-19 15:49:40 +00:00
woody->Ehdr = (Elf64_Ehdr *)secure_jump(woody->file, woody->file_size, 0, sizeof(Elf64_Ehdr));
2024-02-19 10:35:40 +00:00
if (!woody->Ehdr || !elf_magic_numbers(woody->file) || woody->Ehdr->e_ident[EI_CLASS] != 2)
2024-02-14 11:31:18 +00:00
{
2024-02-19 10:35:40 +00:00
ft_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-02-19 10:35:40 +00:00
printf("entry point = %ld\n", woody->Ehdr->e_entry);
2024-02-19 15:49:40 +00:00
Elf64_Shdr *Shdr = (Elf64_Shdr *)secure_jump(woody->file, woody->file_size, woody->Ehdr->e_shoff, sizeof(Elf64_Shdr));
if (Shdr == NULL || !secure_jump(woody->file, woody->file_size, woody->Ehdr->e_shoff, woody->Ehdr->e_shnum * sizeof(Elf64_Shdr)))
2024-02-14 11:31:18 +00:00
{
return ft_put_error("Corrupted file");
}
2024-02-19 10:35:40 +00:00
if (woody->file_size > woody->Ehdr->e_shoff + woody->Ehdr->e_shnum * sizeof(Elf64_Shdr))
2024-02-14 11:31:18 +00:00
{
2024-02-19 10:35:40 +00:00
printf("extra_data !\n"); // save it in woody->extra_data and append it to the end of the woody file ? Could be dangerous
2024-02-14 11:31:18 +00:00
}
2024-02-14 13:16:28 +00:00
Elf64_Shdr *symbols_table = NULL;
2024-02-19 10:35:40 +00:00
for (int i = 0; i < woody->Ehdr->e_shnum; i++) {
2024-02-14 13:16:28 +00:00
if (Shdr[i].sh_type == SHT_SYMTAB) {
2024-02-19 15:49:40 +00:00
symbols_table = secure_jump(woody->file, woody->file_size, woody->Ehdr->e_shoff + (i * sizeof(Elf64_Shdr)), sizeof(Elf64_Shdr));
2024-02-14 13:16:28 +00:00
}
}
if (symbols_table == NULL)
2024-02-19 10:35:40 +00:00
return ft_put_error("No symbols");
2024-02-19 15:49:40 +00:00
if (!secure_jump(woody->file, woody->file_size, woody->Ehdr->e_shoff + (woody->Ehdr->e_shstrndx * sizeof(Elf64_Shdr)), sizeof(Elf64_Shdr)))
2024-02-19 10:35:40 +00:00
return ft_put_error("Corrupted file");
2024-02-19 15:49:40 +00:00
char *Sshstrtab = (char *)secure_jump(woody->file, woody->file_size, Shdr[woody->Ehdr->e_shstrndx].sh_offset, 0);
2024-02-19 10:35:40 +00:00
if (Sshstrtab == NULL)
2024-02-14 11:31:18 +00:00
return ft_put_error("Corrupted file");
2024-02-19 10:35:40 +00:00
for (int i = 0; i < woody->Ehdr->e_shnum; i++) {
char *section_name = Sshstrtab + Shdr[i].sh_name;
printf("%s : Offset: %lx | Size: %lx | Virtual adress: %lx\n", section_name, Shdr[i].sh_offset, Shdr[i].sh_size, Shdr[i].sh_addr);
}
// useless for now
2024-02-19 15:49:40 +00:00
Elf64_Shdr *strtab_header = (Elf64_Shdr *)secure_jump(woody->file, woody->file_size, woody->Ehdr->e_shoff + (symbols_table->sh_link * woody->Ehdr->e_shentsize), sizeof(Elf64_Shdr));
2024-02-14 13:16:28 +00:00
if (!strtab_header)
return ft_put_error("Corrupted file");
2024-02-19 15:49:40 +00:00
char *strtab = secure_jump(woody->file, woody->file_size, strtab_header->sh_offset, 0);
2024-02-19 10:35:40 +00:00
if (strtab == NULL)
return ft_put_error("Corrupted file");
2024-02-19 15:49:40 +00:00
Elf64_Sym *symbols = (Elf64_Sym *)secure_jump(woody->file, woody->file_size, symbols_table->sh_offset, sizeof(Elf64_Sym));
2024-02-19 10:35:40 +00:00
if (symbols == NULL)
return ft_put_error("Corrupted file");
// end useless woody->Ehdr->e_entry =
find_cave(woody);
2024-02-14 13:16:28 +00:00
2024-02-19 10:35:40 +00:00
char *woody_file = malloc(woody->file_size);
2024-02-14 11:31:18 +00:00
2024-02-19 10:35:40 +00:00
ft_memcpy(woody_file, woody->file, woody->file_size);
2024-02-14 13:16:28 +00:00
2024-02-19 15:49:40 +00:00
// encrypt_zone(woody_file, strtab_header->sh_offset , strtab_header->sh_size);
2024-02-21 12:13:17 +00:00
munmap(woody_file, woody->file_size);
2024-02-19 10:35:40 +00:00
return save_elf("woody", woody_file, woody->file_size);
2024-02-14 10:37:05 +00:00
}