woody-woodpacker/srcs/woody.c

67 lines
1.9 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));
}
size_t find_stringtable_end(char *Sshstrtab) // not secure i think
{
size_t strtab_index = 0;
while (Sshstrtab[strtab_index] != '\0' || Sshstrtab[strtab_index + 1] != '\0')
{
while (Sshstrtab[strtab_index] != '\0')
strtab_index++;
strtab_index++;
}
return strtab_index;
}
void encrypt(char *start, unsigned long int size)
{
size_t i = 0;
// while (i < size)
// {
// }
(void)i;
(void)start;
(void)size;
}
2024-02-14 10:37:05 +00:00
int woody(t_efl_content *file_content)
{
Elf64_Ehdr *Ehdr = (Elf64_Ehdr *)secure_access(file_content->file, file_content->file_size, 0, sizeof(Elf64_Ehdr));
2024-02-14 11:31:18 +00:00
if (!Ehdr || !elf_magic_numbers(file_content->file) || Ehdr->e_ident[EI_CLASS] != 2)
{
ft_printf("Error: \'%s\' is not a valid 64-bit ELF file\n", file_content->file_path);
2024-02-14 10:37:05 +00:00
return EXIT_FAILURE;
2024-02-14 11:31:18 +00:00
}
Elf64_Shdr *Shdr = (Elf64_Shdr *)secure_access(file_content->file, file_content->file_size, Ehdr->e_shoff, sizeof(Elf64_Shdr));
if (Shdr == NULL || !secure_access(file_content->file, file_content->file_size, Ehdr->e_shoff, Ehdr->e_shnum * sizeof(Elf64_Shdr)))
{
return ft_put_error("Corrupted file");
}
if (file_content->file_size > Ehdr->e_shoff + Ehdr->e_shnum * sizeof(Elf64_Shdr))
{
printf("extra_data !\n"); // save it in file_content->extra_data and append it to the end of the woody file ? Could be dangerous
}
char *Sshstrtab = (char *)secure_access(file_content->file, file_content->file_size, Shdr[Ehdr->e_shstrndx].sh_offset, 0);
if (Sshstrtab == NULL)
return ft_put_error("Corrupted file");
size_t stringtable_end = find_stringtable_end(Sshstrtab);
2024-02-14 10:37:05 +00:00
2024-02-14 11:31:18 +00:00
if (Sshstrtab + stringtable_end > file_content->file + file_content->file_size)
{
return ft_put_error("Encrypt after the end of the file");
}
encrypt(Sshstrtab, stringtable_end);
2024-02-14 10:37:05 +00:00
return EXIT_SUCCESS;
}