获得PE文件输入表和输出表
2013-09-02 15:26
232 查看
#include <windows.h>
#include <iostream>
#include<ImageHlp.h>
#pragma comment(lib,"imagehlp.lib")
using namespace std;
int main()
{
char* strExePath="C:\\Windows\\System32\\kernel32.dll";
//打开文件
HANDLE hFile=::CreateFile(strExePath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(NULL==hFile)
{
cout<<"CreateFile error:"<<GetLastError()<<endl;
return 0;
}
cout<<"CreateFile successfully\n";
//创建内存映像
HANDLE hMap=CreateFileMappingA(hFile,NULL,PAGE_READONLY,0,0,NULL);
if(NULL==hMap)
{
cout<<"CreateFileMappingA error:"<<GetLastError()<<endl;
return 0;
}
cout<<"CreateFilemappingA successfully\n";
//获得文件基地址
LPVOID lpImageBase=MapViewOfFile(hMap,FILE_MAP_READ,0,0,0);
if(NULL==lpImageBase)
{
cout<<"MapViewOfFile error:"<<GetLastError()<<endl;
return 0;
}
cout<<"MapViewOfFile successfully\n";
//获得DOS头部指针
PIMAGE_DOS_HEADER pDos=(PIMAGE_DOS_HEADER)lpImageBase;
if(pDos->e_magic==IMAGE_DOS_SIGNATURE)
cout<<"DOS 头部检测成功\n";
else
{
cout<<"并非exe文件\n";
}
//获得NT文件头
PIMAGE_NT_HEADERS pNtHeader;
pNtHeader = (PIMAGE_NT_HEADERS)((DWORD)pDos + pDos->e_lfanew);
if(pNtHeader->Signature==IMAGE_NT_SIGNATURE)
{
cout<<"可用exe文件\n";
}
//获得文件头部
PIMAGE_FILE_HEADER pFile=(PIMAGE_FILE_HEADER)(&pNtHeader->FileHeader);
//获得可选头部
PIMAGE_OPTIONAL_HEADER pOptionalHeader=(PIMAGE_OPTIONAL_HEADER)(&pNtHeader->OptionalHeader);
//获得区段头
PIMAGE_SECTION_HEADER pSectionHeader;
pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)pOptionalHeader + pFile->SizeOfOptionalHeader);
PIMAGE_SECTION_HEADER pSectionUse=pSectionHeader;
for(int i=0;i<pFile->NumberOfSections;i++)
{
char tmp[256]={0};
wsprintf(tmp,"%s",pSectionUse->Name);
cout<<tmp<<endl;
//pSectionHeader+=sizeof(PIMAGE_SECTION_HEADER);
pSectionUse++;
}
//获得输出表
PIMAGE_EXPORT_DIRECTORY pExportDir=(PIMAGE_EXPORT_DIRECTORY)ImageRvaToVa(pNtHeader,lpImageBase,pOptionalHeader->DataDirectory[0].VirtualAddress,&pSectionHeader);
cout<<"输出函数个数是:"<<pExportDir->NumberOfNames<<endl;
DWORD **ppdwNames = (DWORD **)pExportDir->AddressOfNames;
ppdwNames = (PDWORD*)ImageRvaToVa(pNtHeader,
lpImageBase, (DWORD)ppdwNames, 0);
DWORD ppdwFunc = (DWORD )pExportDir->AddressOfFunctions;
for(int i=0;i<1;i++)
{
char *szFunc=(PSTR)ImageRvaToVa(pNtHeader, lpImageBase, (DWORD)*ppdwNames, 0);
DWORD dwAddress=(DWORD)ImageRvaToVa(pNtHeader, lpImageBase, (DWORD)ppdwFunc, 0);
cout<<szFunc<<"::"<<dwAddress<<endl;
ppdwNames++;
}
DWORD* pFuncAddress=(DWORD*)pExportDir->AddressOfNameOrdinals;
//获得输入表
PIMAGE_IMPORT_DESCRIPTOR pImportDir ;
cout<<"通过寻找OrignalFirstThunk\n";
pImportDir = (PIMAGE_IMPORT_DESCRIPTOR)ImageRvaToVa(pNtHeader,lpImageBase,(DWORD)pOptionalHeader->DataDirectory[1].VirtualAddress,&pSectionHeader);
while((pImportDir->Name)!=NULL)
{
cout<<(char*)ImageRvaToVa(pNtHeader,lpImageBase,pImportDir->Name,&pSectionHeader)<<endl<<endl;;
PIMAGE_THUNK_DATA thunk_data=(PIMAGE_THUNK_DATA)ImageRvaToVa(pNtHeader,lpImageBase,pImportDir->OriginalFirstThunk,&pSectionHeader);
while((thunk_data->u1.AddressOfData)!=NULL)
{
PIMAGE_IMPORT_BY_NAME he=(PIMAGE_IMPORT_BY_NAME)ImageRvaToVa(pNtHeader,lpImageBase,thunk_data->u1.AddressOfData,&pSectionHeader);
cout<<he->Hint<<":"<<he->Name<<endl;
thunk_data++;
}
pImportDir++;
cout<<endl;
}
return 0;
}
#include <iostream>
#include<ImageHlp.h>
#pragma comment(lib,"imagehlp.lib")
using namespace std;
int main()
{
char* strExePath="C:\\Windows\\System32\\kernel32.dll";
//打开文件
HANDLE hFile=::CreateFile(strExePath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(NULL==hFile)
{
cout<<"CreateFile error:"<<GetLastError()<<endl;
return 0;
}
cout<<"CreateFile successfully\n";
//创建内存映像
HANDLE hMap=CreateFileMappingA(hFile,NULL,PAGE_READONLY,0,0,NULL);
if(NULL==hMap)
{
cout<<"CreateFileMappingA error:"<<GetLastError()<<endl;
return 0;
}
cout<<"CreateFilemappingA successfully\n";
//获得文件基地址
LPVOID lpImageBase=MapViewOfFile(hMap,FILE_MAP_READ,0,0,0);
if(NULL==lpImageBase)
{
cout<<"MapViewOfFile error:"<<GetLastError()<<endl;
return 0;
}
cout<<"MapViewOfFile successfully\n";
//获得DOS头部指针
PIMAGE_DOS_HEADER pDos=(PIMAGE_DOS_HEADER)lpImageBase;
if(pDos->e_magic==IMAGE_DOS_SIGNATURE)
cout<<"DOS 头部检测成功\n";
else
{
cout<<"并非exe文件\n";
}
//获得NT文件头
PIMAGE_NT_HEADERS pNtHeader;
pNtHeader = (PIMAGE_NT_HEADERS)((DWORD)pDos + pDos->e_lfanew);
if(pNtHeader->Signature==IMAGE_NT_SIGNATURE)
{
cout<<"可用exe文件\n";
}
//获得文件头部
PIMAGE_FILE_HEADER pFile=(PIMAGE_FILE_HEADER)(&pNtHeader->FileHeader);
//获得可选头部
PIMAGE_OPTIONAL_HEADER pOptionalHeader=(PIMAGE_OPTIONAL_HEADER)(&pNtHeader->OptionalHeader);
//获得区段头
PIMAGE_SECTION_HEADER pSectionHeader;
pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)pOptionalHeader + pFile->SizeOfOptionalHeader);
PIMAGE_SECTION_HEADER pSectionUse=pSectionHeader;
for(int i=0;i<pFile->NumberOfSections;i++)
{
char tmp[256]={0};
wsprintf(tmp,"%s",pSectionUse->Name);
cout<<tmp<<endl;
//pSectionHeader+=sizeof(PIMAGE_SECTION_HEADER);
pSectionUse++;
}
//获得输出表
PIMAGE_EXPORT_DIRECTORY pExportDir=(PIMAGE_EXPORT_DIRECTORY)ImageRvaToVa(pNtHeader,lpImageBase,pOptionalHeader->DataDirectory[0].VirtualAddress,&pSectionHeader);
cout<<"输出函数个数是:"<<pExportDir->NumberOfNames<<endl;
DWORD **ppdwNames = (DWORD **)pExportDir->AddressOfNames;
ppdwNames = (PDWORD*)ImageRvaToVa(pNtHeader,
lpImageBase, (DWORD)ppdwNames, 0);
DWORD ppdwFunc = (DWORD )pExportDir->AddressOfFunctions;
for(int i=0;i<1;i++)
{
char *szFunc=(PSTR)ImageRvaToVa(pNtHeader, lpImageBase, (DWORD)*ppdwNames, 0);
DWORD dwAddress=(DWORD)ImageRvaToVa(pNtHeader, lpImageBase, (DWORD)ppdwFunc, 0);
cout<<szFunc<<"::"<<dwAddress<<endl;
ppdwNames++;
}
DWORD* pFuncAddress=(DWORD*)pExportDir->AddressOfNameOrdinals;
//获得输入表
PIMAGE_IMPORT_DESCRIPTOR pImportDir ;
cout<<"通过寻找OrignalFirstThunk\n";
pImportDir = (PIMAGE_IMPORT_DESCRIPTOR)ImageRvaToVa(pNtHeader,lpImageBase,(DWORD)pOptionalHeader->DataDirectory[1].VirtualAddress,&pSectionHeader);
while((pImportDir->Name)!=NULL)
{
cout<<(char*)ImageRvaToVa(pNtHeader,lpImageBase,pImportDir->Name,&pSectionHeader)<<endl<<endl;;
PIMAGE_THUNK_DATA thunk_data=(PIMAGE_THUNK_DATA)ImageRvaToVa(pNtHeader,lpImageBase,pImportDir->OriginalFirstThunk,&pSectionHeader);
while((thunk_data->u1.AddressOfData)!=NULL)
{
PIMAGE_IMPORT_BY_NAME he=(PIMAGE_IMPORT_BY_NAME)ImageRvaToVa(pNtHeader,lpImageBase,thunk_data->u1.AddressOfData,&pSectionHeader);
cout<<he->Hint<<":"<<he->Name<<endl;
thunk_data++;
}
pImportDir++;
cout<<endl;
}
return 0;
}
相关文章推荐
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- 获得PE文件的导入模块和导入函数
- C语言文件输入、输出 I/O(十二)
- Python菜鸟学习手册10----文件的输入与输出
- 文件输入输出错误,无法读取txt文件
- 文件格式化输入和输出
- Linux C学习笔记——文件的输入/输出操作
- vim的补充及文件输入输出管理
- 这个C++程序 输入一组数据 为何会输出很多个结果啊~ 是有关文件处理的
- C++的文件输入与输出
- Java输入输出之字节流拷贝文件
- java 关于中文字的输出 输入 和文件操作(对象序列化)
- java笔记→IO流中字节,字符的输入与输出(文件)
- Java输入输出之字节流拷贝文件
- freopen - C/C++文件输入输出利器
- 第4章 处理用户输入与显示数据------------(禁止命令输出、lsof命令、关闭文件描述符、创建读取/写入文件描述符)
- C#文件的输入与输出
- 转:通过ReverseMe学习PE文件结构-输出表
- 14 序列化和文件的输入/输出
- 文件操作:输入-输出