From 8050a1f142dc22e20384e66663ffa37c5099064b Mon Sep 17 00:00:00 2001 From: pbonilla Date: Wed, 14 Feb 2024 14:16:28 +0100 Subject: [PATCH] Creation of woody --- .gitignore | 3 ++- srcs/woody.c | 71 ++++++++++++++++++++++++++++++++-------------------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 985031d..1fde0fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.o *.a -woody_woodpacker \ No newline at end of file +woody_woodpacker +woody \ No newline at end of file diff --git a/srcs/woody.c b/srcs/woody.c index 657fb30..7435bc1 100644 --- a/srcs/woody.c +++ b/srcs/woody.c @@ -5,30 +5,35 @@ 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) +void encrypt_zone(char *file, unsigned long int offset, unsigned long int size) { size_t i = 0; + while (i < size) + { + file[offset + i] = 0; + ++i; + } +} - // while (i < size) - // { +int save_elf(char *path, char *file, unsigned long int size) +{ + 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; + } - // } - (void)i; - (void)start; - (void)size; + if (write(fd, file, size) == -1) { + close(fd); + ft_printf("Error: Failed to write new file \'%s\'\n", path); + return EXIT_FAILURE; + } + + if (close(fd) == -1) { + ft_printf("Error: Failed to close new file \'%s\'\n", path); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; } int woody(t_efl_content *file_content) @@ -51,17 +56,29 @@ int woody(t_efl_content *file_content) 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) + Elf64_Shdr *symbols_table = NULL; + for (int i = 0; i < Ehdr->e_shnum; i++) { + if (Shdr[i].sh_type == SHT_SYMTAB) { + symbols_table = secure_access(file_content->file, file_content->file_size, Ehdr->e_shoff + (i * sizeof(Elf64_Shdr)), sizeof(Elf64_Shdr)); + } + } + if (symbols_table == NULL) return ft_put_error("Corrupted file"); - size_t stringtable_end = find_stringtable_end(Sshstrtab); - - if (Sshstrtab + stringtable_end > file_content->file + file_content->file_size) + Elf64_Shdr *strtab_header = (Elf64_Shdr *)secure_access(file_content->file, file_content->file_size, Ehdr->e_shoff + (symbols_table->sh_link * Ehdr->e_shentsize), sizeof(Elf64_Shdr)); + if (!strtab_header) + return ft_put_error("Corrupted file"); + + + if (strtab_header->sh_offset + strtab_header->sh_size > file_content->file_size) { return ft_put_error("Encrypt after the end of the file"); } - encrypt(Sshstrtab, stringtable_end); - return EXIT_SUCCESS; + char *woody = malloc(file_content->file_size); + ft_memcpy(woody, file_content->file, file_content->file_size); + + encrypt_zone(woody, strtab_header->sh_offset , strtab_header->sh_size); + + return save_elf("woody", woody, file_content->file_size); } \ No newline at end of file