您的位置:首页 > 其它

检查打开的文件是不是ELF格式的文件,提取“特别”的节中的信息

2014-09-12 08:47 330 查看
//检查打开的文件是不是ELF格式的文件

//检验ELF头

//获得节头表的地址

//遍历节头表,依次查看一个节的属性,从节头字符串表中找到其名称,如果名称和“特别”的section名匹配,则找到此节的地址

//提取“特别”的节中的信息

//显示上面提取的信息

#include<stdio.h>

#include<elf.h>

#include<stdlib.h>

#include<string.h>

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

{

FILE *fp;

Elf64_Ehdr *ehdr;

Elf64_Shdr *shdr;

char buffer[1024]={0};

int i=0,j=0;

long length;

fp=fopen(argv[1],"rb");

if(fp==NULL)

{

printf("can not open %s\n",argv[1]);

return -1;

}

ehdr = (Elf64_Ehdr *)malloc(sizeof(Elf64_Ehdr));

shdr = (Elf64_Shdr *)malloc(sizeof(Elf64_Shdr));

fread(ehdr,sizeof(Elf64_Ehdr),1,fp);

if(ehdr->e_ident[EI_MAG0] == ELFMAG0 ||

ehdr->e_ident[EI_MAG1] == ELFMAG1 ||

ehdr->e_ident[EI_MAG2] == ELFMAG2 ||

ehdr->e_ident[EI_MAG3] == ELFMAG3)

{

printf("此文件是ELF文件!\n");

printf("节头表地址是 %p\n",ehdr+ehdr->e_shoff);

//获取节头字符串表地址

fseek(fp,ehdr->e_shoff,0);

fread(shdr,sizeof(Elf64_Shdr),ehdr->e_shnum,fp);

length = shdr[ehdr->e_shstrndx].sh_offset;

printf("节头字符串表的地址 %p\n",length);

//获取字符串表各节名称

fseek(fp,length,0);

fread(buffer,1,sizeof(buffer),fp);

//匹配"infosection"

for(i=0;i<=(int)ehdr->e_shnum;i++)

{

char *name=&buffer[shdr[i].sh_name];

if(!strcmp(name,".infosection"))

{

printf("匹配到的section name is %s\n", name);

char temp[shdr[i].sh_size];

fseek(fp, shdr[i].sh_offset, 0);

fread(temp, 1, shdr[i].sh_size, fp);

printf("匹配到的节的 message is : %s", temp);

}

}

}

else

{

printf("此文件不是ELF文件\n");

}

fclose(fp);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐