簡體   English   中英

打印節標題的精靈名稱

[英]printing elf names of section headers

我有一個 C 程序,我想在其中打印輸入文件的部分標題的名稱。 我在研究 ELF 符號的基礎上做了一切,並幫助了互聯網上的現有程序,但它仍然不起作用。 它只打印來自 for 循環的索引,其中也應該是部分名稱。 有人看到我錯過的東西嗎?

更新:如果將來有人需要它,我更新了代碼並刪除了導致堆棧溢出的錯誤。

代碼:


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <elf.h>

int main(int argc, char *argv[]) {


    int fd;
    int val;

    Elf32_Ehdr elfHdr;
    Elf32_Shdr sectHdr;
    FILE* ElfFile = NULL;
    char* SectNames = NULL;

    if(argc != 2) {
        perror("Error while opening file");
        return 0;
    }   



    ElfFile = fopen(argv[1], "r");
    if(ElfFile == NULL) {
        printf("fopen");
        return -1;
    }

    //preberemo elf header
    fread(&elfHdr, 1, sizeof(Elf32_Ehdr), ElfFile);

    printf("\tVersion: 0x%.2X\n", elfHdr.e_version);

    printf("\tEntry point address: 0x%.8X\n", elfHdr.e_entry);

    printf("\tProgram header offset: 0x%.8X\n", elfHdr.e_phoff);

    printf("\tSection header offset: 0x%.8X\n", elfHdr.e_shoff);

    printf("\tFlags: 0x%.8X\n", elfHdr.e_flags);

    printf("\tSize of this header: 0x%X\n", elfHdr.e_ehsize);

    printf("\tSize of program headers: 0x%X\n", elfHdr.e_phentsize);

    printf("\tNumber of program headers: %d\n", elfHdr.e_phnum);

    printf("\tSize of section headers: 0x%X\n", elfHdr.e_shentsize);

    printf("\tNumber of section headers: %d\n", elfHdr.e_shnum);

    printf("\tSection header string table index: 0x%X\n", elfHdr.e_shstrndx);

    //premik do section tabele
    fseek(ElfFile, elfHdr.e_shoff + elfHdr.e_shstrndx * elfHdr.e_shentsize, SEEK_SET);
    fread(&sectHdr, 1, sizeof(sectHdr), ElfFile);
    SectNames = malloc(sectHdr.sh_size);
    fseek(ElfFile, sectHdr.sh_offset, SEEK_SET);
    fread(SectNames, 1, sectHdr.sh_size, ElfFile);

    for (int idx = 0; idx < elfHdr.e_shnum; idx++){
        char* name = "";

        fseek(ElfFile, elfHdr.e_shoff + idx * sizeof(sectHdr), SEEK_SET);
        fread(&sectHdr, 1, sizeof(sectHdr), ElfFile);

        // print section name
        if (sectHdr.sh_name);
        name = SectNames + sectHdr.sh_name;
            
        printf("%i %s\n", idx, name);
    }



    close(fd);

    return 0;
}

有人看到我錯過的東西嗎?

你是在 32 位模式下編譯你的程序嗎?

更新:

有一個“明顯”的錯誤,我在第一次閱讀時錯過了,並且通過使用-fsanitize=address構建而暴露出來:

Elf32_Ehdr elfHdr;
...
fread(&elfHdr, 1, sizeof(Elf64_Ehdr), ElfFile);

此錯誤會導致堆棧緩沖區溢出。 為了防止此類錯誤,使用sizeof(variable)而不是sizeof(Type)總是更安全,例如

fread(&elfHdr, 1, sizeof(elfHdr), ElfFile);

這個對我有用:

gcc -w -m32 t.c && ./a.out ./a.out
    Version: 0x01
    Entry point address: 0x000010C0
    Program header offset: 0x00000034
    Section header offset: 0x000038B0
    Flags: 0x00000000
    Size of this header: 0x34
    Size of program headers: 0x20
    Number of program headers: 11
    Size of section headers: 0x28
    Number of section headers: 30
    Section header string table index: 0x1D
0
1 .interp
2 .note.gnu.build-id
3 .note.ABI-tag
4 .gnu.hash
5 .dynsym
6 .dynstr
7 .gnu.version
8 .gnu.version_r
9 .rel.dyn
10 .rel.plt
11 .init
12 .plt
13 .plt.got
14 .text
15 .fini
16 .rodata
17 .eh_frame_hdr
18 .eh_frame
19 .init_array
20 .fini_array
21 .dynamic
22 .got
23 .got.plt
24 .data
25 .bss
26 .comment
27 .symtab
28 .strtab
29 .shstrtab

如果您嘗試在 64 位 ELF 文件上運行它,那么您需要使用Elf64_...等效項更改Elf32_EhdrElf32_Shdr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM