ELF - Получение SEGFAULT при изменении точки входа - PullRequest
0 голосов
/ 17 ноября 2018

Я пытаюсь исправить точку входа в файл ELF напрямую через поле e_entry:

Elf64_Ehdr *ehdr = NULL;
Elf64_Phdr *phdr = NULL;
Elf64_Shdr *shdr = NULL;

if (argc < 2)
{
    printf("Usage: %s <executable>\n", argv[0]);
    exit(EXIT_SUCCESS);
}

fd = open(argv[1], O_RDWR);

if (fd < 0)
{
    perror("open");
    exit(EXIT_FAILURE);
}

if (fstat(fd, &st) < 0)
{
    perror("fstat");
    exit(EXIT_FAILURE);
}

/* map whole executable into memory */
mapped_file = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

if (mapped_file < 0)
{
    perror("mmap");
    exit(EXIT_FAILURE);
}

// check for an ELF file
check_elf(mapped_file, argv);

ehdr = (Elf64_Ehdr *) mapped_file;
phdr = (Elf64_Phdr *) &mapped_file[ehdr->e_phoff];
shdr = (Elf64_Shdr *) &mapped_file[ehdr->e_shoff];

mprotect((void *)((uintptr_t)&ehdr->e_entry & ~(uintptr_t)4095), 4096, PROT_READ | PROT_WRITE);

if (ehdr->e_type != ET_EXEC)
{
    fprintf(stderr, "%s is not an ELF executable.\n", argv[1]);
    exit(EXIT_FAILURE);
}

printf("Program entry point: %08x\n", ehdr->e_entry);



int text_found = 0;
uint64_t test_addr;
uint64_t text_end;
size_t test_len = strlen(shellcode);
int text_idx;
for (i = 0; i < ehdr->e_phnum; ++i)
{

    if (text_found)
    {
        phdr[i].p_offset += PAGE_SIZE;
        continue;
    }


    if (phdr[i].p_type == PT_LOAD && phdr[i].p_flags == ( PF_R | PF_X))
    {

        test_addr = phdr[i].p_vaddr + phdr[i].p_filesz;
        text_end = phdr[i].p_vaddr + phdr[i].p_filesz;

        printf("TEXT SEGMENT ends at 0x%x\n", text_end);


        puts("Changing entry point...");
        ehdr->e_entry = (Elf64_Addr *) test_addr;

        memmove(test_addr, shellcode, test_len);


        phdr[i].p_filesz += test_len;
        phdr[i].p_memsz += test_len;    

        text_found++;
    }


}

//patch sections

for (i = 0; i < ehdr->e_shnum; ++i)
{
    if (shdr->sh_offset >= test_addr)
        shdr->sh_offset += PAGE_SIZE;

    else
        if (shdr->sh_size + shdr->sh_addr == test_addr)
            shdr->sh_size += test_len;
}

ehdr->e_shoff += PAGE_SIZE;
close(fd);


}

шелл-код в данном случае - это просто набор NOP с инструкцией int3 в конце.
Я позаботился о том, чтобы настроить сегменты и разделы, которые идут после этого нового кода, но проблема в том, что как только я исправляю точку входа, происходит сбой программы, почему это так?

Ответы [ 2 ]

0 голосов
/ 03 февраля 2019

меняется:

memmove(test_addr, shellcode, test_len);

на:

memmove(mapped_file + phdr[i].p_offset + phdr[i].p_filesz, shellcode, test_len);

Кажется, чтобы решить вашу проблему.test_addr - это виртуальный адрес, принадлежащий файлу, который вы отобразили;Вы не можете использовать это непосредственно как указатель.Биты, которые вы хотите удалить, это адрес карты файлов, p_offset и p_filesz.

0 голосов
/ 17 ноября 2018

Я подозреваю, что вы не включили доступ на запись в заголовок программы.Вы можете сделать это через что-то вроде

const uintptr_t page_size = 4096;
mprotect((void *)((uintptr_t)&ehdr->e_entry & ~(uintptr_t)4095), 4096, PROT_READ | PROT_WRITE);
ehdr->e_entry = test_addr;
...