Creation of woody

This commit is contained in:
pbonilla 2024-02-14 14:16:28 +01:00
parent 94c1680fab
commit 8050a1f142
2 changed files with 46 additions and 28 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.o
*.a
woody_woodpacker
woody_woodpacker
woody

View File

@ -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);
}