diff --git a/includes/woody.h b/includes/woody.h index a9005cc..c86a04a 100644 --- a/includes/woody.h +++ b/includes/woody.h @@ -17,7 +17,7 @@ typedef struct efl_content long unsigned int file_size; char *file_path; char *file; - + char *extra_data; } t_efl_content; diff --git a/srcs/woody.c b/srcs/woody.c index 57f8a3f..657fb30 100644 --- a/srcs/woody.c +++ b/srcs/woody.c @@ -1,12 +1,67 @@ #include "../includes/woody.h" +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; +} + 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)); - if (!Ehdr) + 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); return EXIT_FAILURE; + } + + 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); - // if (Ehdr->e_ident[EI_CLASS]) + 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); + return EXIT_SUCCESS; } \ No newline at end of file