检查打开的文件是不是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;
}
//检验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;
}
相关文章推荐
- 检查打开的文件是不是ELF格式的文件,如果是就输出节头表的文件偏移
- 检查打开的文件是不是ELF格式的文件,如果是就输出节头字符串表的地址,并依次输出各节的名称,字符串
- 检查打开的文件是不是ELF格式的文件,如果是就输出节头表表項的数目,各表項指向的节的地址
- 解析静态库(Lib)文件,提取出所有函数信息,组织成自定义格式文件
- 快速窥探elf格式文件文件的内部信息
- ELF格式解析库之提取信息
- C#遍历文件(夹)、读取tar.gz格式压缩文件,从文件名提取信息批量入数据库
- readelf命令_Linux readelf 命令用法详解:用于显示elf格式文件的信息
- Linux下的ELF文件格式简介
- 当程序用ado的jet4.0方式连接的时候,对于设有access数据库密码的mdb的访问居然报错“无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开”,而用odbc方式不报错,小阴沟里翻船,郁闷中然后查文档解决之
- "无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开"解决办法
- 怎样从MIDI文件中提取出音符的绝对音高信息
- 在处理向该请求提供服务所需的配置文件时出错。请检查下面的特定错误详细信息并适当地修改配置文件。
- 如何让axd调试elf格式文件
- [转载]ELF文件格式(二)
- 分析PE格式文件中的图标信息
- 打开Daa格式的文件PowerISO最新破解注册版本
- [导入]解决打开CHM格式文件出现“网页不能浏览”错误的方法
- 关于 无法打开工作组信息文件中的“MSysAccounts” 数据表
- elf文件格式