|
|
|
@ -9,18 +9,18 @@ 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);
|
|
|
|
|
printf("Error: Failed to create new file \'%s\'\n", path);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (write(fd, file, size) == -1) {
|
|
|
|
|
close(fd);
|
|
|
|
|
ft_printf("Error: Failed to write new file \'%s\'\n", path);
|
|
|
|
|
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);
|
|
|
|
|
printf("Error: Failed to close new file \'%s\'\n", path);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
@ -97,7 +97,7 @@ t_payload *get_payload()
|
|
|
|
|
return payload;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_position)
|
|
|
|
|
int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_position, int load_segment_index)
|
|
|
|
|
{
|
|
|
|
|
char *ptr_jmp_value = ft_strnstr_nullterminated(payload->payload, JUMP_VALUE, payload->len);
|
|
|
|
|
char *ptr_woody = ft_strnstr_nullterminated(payload->payload, WOODY, payload->len);
|
|
|
|
@ -111,11 +111,11 @@ int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_posi
|
|
|
|
|
ft_memcpy(&payload->payload[jmp_index + 1], &jump_value, sizeof(jump_value));
|
|
|
|
|
|
|
|
|
|
int64_t text_index = ptr_text_section - payload->payload;
|
|
|
|
|
int64_t text_value = payload_position - woody->Ehdr->e_entry + woody_index;
|
|
|
|
|
int64_t text_value = payload_position - woody->Phdr[load_segment_index].p_offset + woody_index;
|
|
|
|
|
ft_memcpy(&payload->payload[text_index], &text_value, sizeof(text_value));
|
|
|
|
|
|
|
|
|
|
int64_t section_index = ptr_section_size - payload->payload;
|
|
|
|
|
int64_t section_value = woody->text_section->sh_size;
|
|
|
|
|
int64_t section_value = woody->Phdr[load_segment_index].p_memsz; //woody->text_section->sh_size;
|
|
|
|
|
ft_memcpy(&payload->payload[section_index], §ion_value, sizeof(section_value));
|
|
|
|
|
|
|
|
|
|
ft_memcpy(woody->file + payload_position, payload->payload, payload->len);
|
|
|
|
@ -148,30 +148,22 @@ void inject(t_elf_content *woody)
|
|
|
|
|
{
|
|
|
|
|
payload_position = create_codecave(woody, &woody->Phdr[i], payload);
|
|
|
|
|
}
|
|
|
|
|
insert_payload(woody, payload, payload_position);
|
|
|
|
|
encrypt(woody->file, woody->Phdr[i].p_offset, woody->Phdr[i].p_memsz);
|
|
|
|
|
insert_payload(woody, payload, payload_position, i);
|
|
|
|
|
|
|
|
|
|
woody->Ehdr->e_entry = payload_position;
|
|
|
|
|
woody->Phdr[i].p_filesz += payload->len;
|
|
|
|
|
woody->Phdr[i].p_memsz += payload->len;
|
|
|
|
|
woody->Phdr[i].p_flags = PF_X | PF_W | PF_R;
|
|
|
|
|
// woody->text_section->sh_size += payload->len;
|
|
|
|
|
printf("New entry = %ld (%lx)\n", woody->Ehdr->e_entry, woody->Ehdr->e_entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int is_special_section_indice(uint16_t section_index) {
|
|
|
|
|
return (section_index == SHN_LOPROC || section_index == SHN_BEFORE ||
|
|
|
|
|
section_index == SHN_AFTER || section_index == SHN_HIPROC ||
|
|
|
|
|
section_index == SHN_LOOS || section_index == SHN_HIOS ||
|
|
|
|
|
section_index == SHN_ABS || section_index == SHN_COMMON ||
|
|
|
|
|
section_index == SHN_XINDEX || section_index == SHN_HIRESERVE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int get_elf_sections(t_elf_content *woody)
|
|
|
|
|
{
|
|
|
|
|
woody->Ehdr = (Elf64_Ehdr *)fetch(woody->file, woody->file_size, 0, sizeof(Elf64_Ehdr));
|
|
|
|
|
if (!woody->Ehdr || !elf_magic_numbers(woody->file) || woody->Ehdr->e_ident[EI_CLASS] != ELFCLASS64)
|
|
|
|
|
{
|
|
|
|
|
ft_printf("Error: \'%s\' is not a valid 64-bit ELF file\n", woody->file_path);
|
|
|
|
|
printf("Error: \'%s\' is not a valid 64-bit ELF file\n", woody->file_path);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
woody->Phdr = (Elf64_Phdr *)fetch(woody->file, woody->file_size, woody->Ehdr->e_phoff, sizeof(Elf64_Phdr));
|
|
|
|
@ -179,29 +171,6 @@ int get_elf_sections(t_elf_content *woody)
|
|
|
|
|
woody->Shdr = (Elf64_Shdr *)fetch(woody->file, woody->file_size, woody->Ehdr->e_shoff, sizeof(Elf64_Shdr));
|
|
|
|
|
if (!woody->Shdr|| !fetch(woody->file, woody->file_size, woody->Ehdr->e_shoff, woody->Ehdr->e_shnum * sizeof(Elf64_Shdr)))
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
|
|
if (!fetch(woody->file, woody->file_size, woody->Ehdr->e_shoff + (woody->Ehdr->e_shstrndx * sizeof(Elf64_Shdr)), sizeof(Elf64_Shdr)))
|
|
|
|
|
{
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
char *Sshstrtab = (char *)fetch(woody->file, woody->file_size, woody->Shdr[woody->Ehdr->e_shstrndx].sh_offset, 0);
|
|
|
|
|
if (Sshstrtab == NULL)
|
|
|
|
|
{
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
for (int j = 0; j < woody->Ehdr->e_shnum;j++)
|
|
|
|
|
{
|
|
|
|
|
if (woody->Shdr[j].sh_name > woody->Shdr[woody->Ehdr->e_shstrndx].sh_size) return EXIT_FAILURE;
|
|
|
|
|
if (woody->Shdr[j].sh_type == SHT_PROGBITS && woody->Shdr[j].sh_flags & SHF_EXECINSTR &&
|
|
|
|
|
woody->Shdr[j].sh_flags & SHF_ALLOC &&
|
|
|
|
|
Sshstrtab + woody->Shdr[j].sh_name < (char *)woody->file + woody->file_size &&
|
|
|
|
|
!ft_strncmp(".text\0", Sshstrtab + woody->Shdr[j].sh_name, 6))
|
|
|
|
|
{
|
|
|
|
|
woody->text_section = &woody->Shdr[j];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -211,7 +180,6 @@ int prepare_injection(t_elf_content *woody)
|
|
|
|
|
if (elf_statut)
|
|
|
|
|
return elf_statut;
|
|
|
|
|
inject(woody);
|
|
|
|
|
encrypt(woody->file, woody->text_section->sh_offset, woody->text_section->sh_size);
|
|
|
|
|
char *woody_file;
|
|
|
|
|
if (!(woody_file = malloc(woody->file_size)))
|
|
|
|
|
return ft_put_error("Allocation error");
|
|
|
|
|