分享一个PE文件操作库
本帖最后由 李沉舟 于 2018-10-1 17:37 编辑GitHub地址:https://github.com/metoo10987/PEfile
我修改了一下,在PE::File类里加入了一个rva、va、offset地址互转成员函数。
uint32_t getaddr(uint32_t dwAddr, uint32_t from, uint32_t to); //转换地址类型
enum AddrType {vt_rva, vt_offset, vt_va}; //地址类型
const uint32_t ERROR_INVALID_ADDRESS = 1 << 29 | 100; //地址转换出错,设置异常代码,由get_err()、set_err()操作,注意最好不要直接用WINAPI或C库函数
地址转换函数我只写了x32 PE版本的。静态库我也只编译了x32版本的。
静态库下载:
示例源代码。
#include "stdafx.h"
#pragma comment(lib, "PEFile.lib")
#include "PEFile.h"
int _tmain(int argc, _TCHAR* argv[])
{
using namespace PE;
File pe(L"C:\\WINDOWS\\system32\\notepad.exe", true);
char szName = {0};
if (!pe.isLoaded())
{
printf("Can not open the pefile!\n");
return 1;
}
for (int i = 0; i < pe.getNtHeaders32()->FileHeader.NumberOfSections; i++)
{
printf("Section %d >>\n", i + 1);
memcpy(szName, pe.getSectionHeader(i)->Name, 8);
printf("Name:%s\n", szName);
printf("Offset:%08x\n", pe.getSectionHeader(i)->PointerToRawData);
printf("VA:%08x\n", pe.getaddr(pe.getSectionHeader(i)->PointerToRawData, vt_offset, vt_va));
printf("Size:%08x\n", pe.getSectionHeader(i)->SizeOfRawData);
}
system("pause");
return 0;
}
评分=感恩!简单却充满爱!感谢您的作品!{:5_116:} 感谢楼主分享 支持一下没分了
页:
[1]