检查打开的文件是不是ELF格式的文件,如果是就输出节头表的文件偏移
2014-09-12 08:38
393 查看
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "elf.h"
#define FALSE 0
#define TURE 1
#define MAX_SIZE 52
#define Elf_Ehdr Elf64_Ehdr
int OpenElf(char *filename)
{
int fd;
fd = open(filename, O_RDONLY);
if(fd == -1){
printf("Open %s Error!\n", filename);
return FALSE;
}
return fd;
}
//读取Elf头部表函数 :int ReadElf(int fd);
int ReadElf(int fd)
{
char str[MAX_SIZE];
int num;
Elf_Ehdr ehdr64;
memset(str, 0, MAX_SIZE);
if((num = read(fd, str, 52)) != 52){
perror("File NO ELF!\n");
return FALSE;
}
if((str[0] == 0x7f) && (str[1] == 'E') && (str[2] == 'L') && (str[3] == 'F')){
printf("This is ELF file.\n");
printf("文件类别: ");
switch(str[4]){
case 0:
printf("非法目标文件\n");
break;
case 1:
printf("32位目标文件\n");
break;
case 2:
printf("64位目标文件\n");
break;
default:
break;
}
printf("节头表偏移:%p\n",ehdr64.e_shoff);
}else{
perror("File NO ELF!\n");
return FALSE;
}
}
int main(int argc, char *argv[])
{
int boolen;
if(argc == 2){
boolen = OpenElf(argv[1]);
if(boolen == FALSE){
printf("open %s fail!",argv[1]);
return -1;
}
else
{
ReadElf(boolen);
}
}
else
{
printf("参数格式不正确!请正确输入:./main testfile\n");
}
return 0;
}
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "elf.h"
#define FALSE 0
#define TURE 1
#define MAX_SIZE 52
#define Elf_Ehdr Elf64_Ehdr
int OpenElf(char *filename)
{
int fd;
fd = open(filename, O_RDONLY);
if(fd == -1){
printf("Open %s Error!\n", filename);
return FALSE;
}
return fd;
}
//读取Elf头部表函数 :int ReadElf(int fd);
int ReadElf(int fd)
{
char str[MAX_SIZE];
int num;
Elf_Ehdr ehdr64;
memset(str, 0, MAX_SIZE);
if((num = read(fd, str, 52)) != 52){
perror("File NO ELF!\n");
return FALSE;
}
if((str[0] == 0x7f) && (str[1] == 'E') && (str[2] == 'L') && (str[3] == 'F')){
printf("This is ELF file.\n");
printf("文件类别: ");
switch(str[4]){
case 0:
printf("非法目标文件\n");
break;
case 1:
printf("32位目标文件\n");
break;
case 2:
printf("64位目标文件\n");
break;
default:
break;
}
printf("节头表偏移:%p\n",ehdr64.e_shoff);
}else{
perror("File NO ELF!\n");
return FALSE;
}
}
int main(int argc, char *argv[])
{
int boolen;
if(argc == 2){
boolen = OpenElf(argv[1]);
if(boolen == FALSE){
printf("open %s fail!",argv[1]);
return -1;
}
else
{
ReadElf(boolen);
}
}
else
{
printf("参数格式不正确!请正确输入:./main testfile\n");
}
return 0;
}
相关文章推荐
- 检查打开的文件是不是ELF格式的文件,如果是就输出节头表表項的数目,各表項指向的节的地址
- 检查打开的文件是不是ELF格式的文件,如果是就输出节头字符串表的地址,并依次输出各节的名称,字符串
- 检查打开的文件是不是ELF格式的文件,提取“特别”的节中的信息
- 解决:无法将“Add-Migration”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次 1、输入的中划线“-”格式不对,检查是
- 重定向top输出到文件后,用Vi打开乱码,用cat输出格式正常
- php判断上传文件是不是图片,如果是,返回图片格式
- 如果你的Windows XP不能打开.chm格式的文件
- 打开操作,以及如果没有默认格式,弹出打开方式让用户自己选择何种程序打开文件
- 按指定格式输出文件最后更新时间
- Unix/ELF文件格式及病毒分析
- 如何让axd调试elf格式文件
- [转载]ELF文件格式(二)
- [转载]ELF文件格式(一)
- ELF文件格式 程序加载 执行 图示 OBJDUMP
- C# Visual Studio 2005中RichTextBox打开文件为rtf格式
- 打开Daa格式的文件PowerISO最新破解注册版本
- 解决tomcat中rar、excel格式文件在下载时直接以乱码打开
- [导入]解决打开CHM格式文件出现“网页不能浏览”错误的方法
- Unix/ELF文件格式及病毒分析
- [转载]ELF文件格式(三)