61 lines
2.1 KiB
C
61 lines
2.1 KiB
C
|
|
#include "../includes/woody.h"
|
|
|
|
t_payload *get_payload()
|
|
{
|
|
t_payload *payload = malloc(sizeof(t_payload));
|
|
if (!payload)
|
|
return NULL;
|
|
char buffer[1024];
|
|
int fd = open("payload", O_RDONLY);
|
|
if (fd == -1) {
|
|
ft_put_error("Failed to open payload");
|
|
free(payload);
|
|
return NULL;
|
|
}
|
|
payload->len = read(fd, buffer, 1024);
|
|
if (payload->len == -1)
|
|
{
|
|
ft_put_error("Failed to read payload");
|
|
free(payload);
|
|
close(fd);
|
|
return NULL;
|
|
}
|
|
close(fd);
|
|
payload->payload = malloc(sizeof(char) * payload->len);
|
|
if (!payload->payload)
|
|
{
|
|
ft_put_error("Allocation error");
|
|
free(payload);
|
|
return NULL;
|
|
}
|
|
ft_memcpy(payload->payload, buffer, payload->len);
|
|
return payload;
|
|
}
|
|
|
|
int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_position, unsigned int e_entry, unsigned int p_offset, unsigned int p_memsz)
|
|
{
|
|
char *ptr_jmp_value = ft_strnstr_nullterminated(payload->payload, JUMP_VALUE, payload->len);
|
|
char *ptr_woody = ft_strnstr_nullterminated(payload->payload, WOODY, payload->len);
|
|
char *ptr_text_section = ft_strnstr_nullterminated(payload->payload, TEXT_OFFSET, payload->len);
|
|
char *ptr_section_size = ft_strnstr_nullterminated(payload->payload, SECTION_SIZE, payload->len);
|
|
if (ptr_jmp_value && ptr_woody && ptr_text_section && ptr_section_size)
|
|
{
|
|
int32_t woody_index = ptr_woody - payload->payload;
|
|
int32_t jmp_index = ptr_jmp_value - sizeof(JUMP) - payload->payload;
|
|
int32_t jump_value = ((payload_position + jmp_index + 5) - e_entry) * -1; // 5 = JUMP SIZE (OPCODE + 4 bytes operand)
|
|
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 - 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 = p_memsz;
|
|
ft_memcpy(&payload->payload[section_index], §ion_value, sizeof(section_value));
|
|
|
|
ft_memcpy(woody->file + payload_position, payload->payload, payload->len);
|
|
return EXIT_SUCCESS;
|
|
}
|
|
return EXIT_FAILURE;
|
|
} |