2024-02-14 10:37:05 +00:00
|
|
|
#include "../includes/woody.h"
|
|
|
|
|
2024-04-09 08:30:12 +00:00
|
|
|
int get_elf_file(t_elf_content *woody)
|
2024-02-14 08:58:04 +00:00
|
|
|
{
|
2024-02-14 10:37:05 +00:00
|
|
|
int fd;
|
|
|
|
off_t off;
|
|
|
|
|
2024-02-19 10:35:40 +00:00
|
|
|
fd = open(woody->file_path, O_RDONLY);
|
2024-02-14 10:37:05 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2024-06-18 16:11:20 +00:00
|
|
|
ft_printf("Error: Failed to open \'%s\'\n", woody->file_path);
|
2024-02-14 10:37:05 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
off = lseek(fd, 0, SEEK_END);
|
|
|
|
if (off == -1)
|
|
|
|
{
|
|
|
|
close(fd);
|
2024-06-18 16:11:20 +00:00
|
|
|
ft_printf("Error: Failed to read file offset \'%s\'\n", woody->file_path);
|
2024-02-14 10:37:05 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2024-02-19 10:35:40 +00:00
|
|
|
woody->file_size = off;
|
2024-03-21 14:44:29 +00:00
|
|
|
woody->file = mmap(NULL, woody->file_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
|
2024-04-11 10:20:44 +00:00
|
|
|
if (woody->file == MAP_FAILED)
|
2024-02-14 10:37:05 +00:00
|
|
|
{
|
|
|
|
close(fd);
|
2024-06-18 16:11:20 +00:00
|
|
|
ft_printf("Error: Failed to map file \'%s\'\n", woody->file_path);
|
2024-02-14 10:37:05 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2024-02-14 08:58:04 +00:00
|
|
|
|
2024-06-18 16:11:20 +00:00
|
|
|
int save_file(char *path, char *file, unsigned long int size)
|
|
|
|
{
|
|
|
|
int fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0755);
|
|
|
|
if (fd == -1) {
|
|
|
|
printf("Error: Failed to create new file \'%s\'\n", path);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(fd, file, size) == -1) {
|
|
|
|
close(fd);
|
|
|
|
printf("Error: Failed to write new file \'%s\'\n", path);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int save_woody(t_elf_content *woody)
|
|
|
|
{
|
|
|
|
char *woody_file = malloc(woody->file_size);
|
|
|
|
if (!woody_file)
|
|
|
|
return ft_put_error("Allocation error");
|
|
|
|
ft_memcpy(woody_file, woody->file, woody->file_size);
|
|
|
|
|
|
|
|
if (munmap(woody->file, woody->file_size))
|
|
|
|
return ft_put_error("Umapping error");
|
|
|
|
|
|
|
|
int save_error = save_file("woody", woody_file, woody->file_size);
|
|
|
|
if (save_error)
|
|
|
|
return save_error;
|
|
|
|
free(woody_file);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2024-02-14 10:37:05 +00:00
|
|
|
int main(int ac, char **av)
|
|
|
|
{
|
2024-04-09 08:30:12 +00:00
|
|
|
t_elf_content woody;
|
2024-02-14 10:37:05 +00:00
|
|
|
if (ac != 2)
|
|
|
|
{
|
|
|
|
return ft_put_error("Woody_woodpacker take 1 argument\n");
|
|
|
|
}
|
2024-02-19 10:35:40 +00:00
|
|
|
woody.file_path = av[1];
|
2024-06-18 16:11:20 +00:00
|
|
|
int elf_error = get_elf_file(&woody);
|
|
|
|
if (elf_error)
|
|
|
|
return elf_error;
|
|
|
|
elf_error = get_elf_sections(&woody);
|
|
|
|
if (elf_error)
|
|
|
|
return elf_error;
|
|
|
|
|
|
|
|
int inject_error = inject(&woody);
|
|
|
|
if (!inject_error)
|
|
|
|
{
|
|
|
|
return save_woody(&woody);
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
2024-04-15 04:17:31 +00:00
|
|
|
}
|
|
|
|
|