写一段对pe文件分析的程序(仅供参考,来自AI)

// 写一段对pe文件分析的程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <elf.h>
#include <errno.h>
#include <assert.h>
#include <stdbool.h>

int main(int argc, char *argv[])
{
    int fd;
    struct stat st;
    void *ptr;
    Elf32_Ehdr *ehdr;
    Elf32_Shdr *shdr;
    Elf32_Sym *sym;
    int i;
    int j;
    char *strtab;
    char *shstrtab;
    char *name;
    int num;
    int n;
    int num2;
    int n2;

    if (argc < 2)
    {
        printf("Usage: %s <file>\n", argv[0]);
        exit(1);
    }
    fd = open(argv[1], O_RDONLY);
    if (fd < 0)
    {
        perror("open");
        exit(1);
    }
    if (fstat(fd, &st) < 0)
    {
        perror("fstat");
        exit(1);
    }
    ptr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (ptr == MAP_FAILED)
    {
        perror("mmap");
        exit(1);
        close(fd);
        return 1;
    }
    close(fd);
    ehdr = (Elf32_Ehdr *)ptr;
    shdr = (Elf32_Shdr *)(ptr + ehdr->e_shoff);
    strtab = (char *)ptr + shdr[ehdr->e_shstrndx].sh_offset;
    shstrtab = (char *)ptr + shdr[ehdr->e_shstrndx].sh_offset;
    for (i = 0; i < ehdr->e_shnum; i++)
    {
        name = strtab + shdr[i].sh_name;
        printf("%s:\n", name);
        if (shdr[i].sh_type == SHT_SYMTAB)
        {
            sym = (Elf32_Sym *)(ptr + shdr[i].sh_offset);
            num = shdr[i].sh_size / sizeof(Elf32_Sym);
            for (j = 0; j < num; j++)
            {
                if (ELF32_ST_TYPE(sym[j].st_info) == STT_FUNC)
                {
                    n = sym[j].st_name;
                    printf("  %s\n", strtab + n);
                    num2 = shdr[i].sh_link;
                    shdr[num2].sh_offset;
                    shdr[num2].sh_size;
                    shdr[num2].sh_addr;
                    shdr[num2].sh_name;
                    shdr[num2].sh_type;
                    shdr[num2].sh_flags;
                    shdr[num2].sh_link;
                    shdr[num2].sh_info;
                    shdr[num2].sh_addralign;
                    shdr[num2].sh_entsize;
                }
                else
                {
                    continue;
                }
            }
        }
    }
}

 

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称常用语 夸夸
夸夸
还有吗!没看够!
表情代码图片

    暂无评论内容