提交 ee71732b 创建 作者: 赵鹏翀's avatar 赵鹏翀

Initial commit

上级
/Debug
/Release
\ No newline at end of file
用于创建pe2bin项目
\ No newline at end of file
#include <windows.h>
#include <stdio.h>
#define BUFFERSIZE 4096
unsigned char DataBuffer[BUFFERSIZE]; // 读各个节的缓冲区
int main(int argc, char* argv[])
{
if (3 != argc)
{
printf("usage: pe2bin.exe exeFilePath binFilePath\n");
return 1;
}
//
// 打开 PE 文件
//
FILE* pExeFile = fopen(argv[1], "rb"); // 此处必须以二进制的方式打开,否则出错。
if(NULL == pExeFile)
{
printf("Open exe file \"%s\" failed.\n", argv[1]);
return 1;
}
//
// 读取 DOS_HEADER
//
IMAGE_DOS_HEADER dosHeader;
fread(&dosHeader, sizeof(dosHeader), 1, pExeFile);
//
// 读取 NT_HEADER
//
IMAGE_NT_HEADERS ntHeader;
fseek(pExeFile, dosHeader.e_lfanew, SEEK_SET);
fread(&ntHeader, sizeof(ntHeader), 1, pExeFile);
if (IMAGE_NT_SIGNATURE != ntHeader.Signature)
{
printf("File \"%s\" is not a PE file.\n", argv[1]);
fclose(pExeFile);
return 1;
}
unsigned short numSections = ntHeader.FileHeader.NumberOfSections;
if (0 != ntHeader.FileHeader.NumberOfSymbols
&& 0 != ntHeader.FileHeader.PointerToSymbolTable)
{
//
// 运行到这里表明没有运行 strip 命令
//
printf("Exe file \"%s\" still has debug section.\n", argv[1]);
fclose(pExeFile);
return 1;
}
//
// 读取 SECTION_TABLE
//
PIMAGE_SECTION_HEADER pSectionTable = (PIMAGE_SECTION_HEADER)malloc(sizeof(IMAGE_SECTION_HEADER) * numSections);
fseek(pExeFile, sizeof(ntHeader) + dosHeader.e_lfanew, SEEK_SET);
fread(pSectionTable, sizeof(IMAGE_SECTION_HEADER), numSections, pExeFile);
// 求出虚拟地址的总大小(也就是所有节的总大小)
int virSize = 0;
for (int i = 0; i<numSections; i++)
virSize += pSectionTable[i].Misc.VirtualSize;
//
// 创建 bin 文件
//
FILE *pBinFile = fopen(argv[2], "wb");
if (NULL == pBinFile)
{
printf("Creating bin file \"%s\" failed.\n" ,argv[2]);
free(pSectionTable);
fclose(pExeFile);
return 1;
}
// 将 bin 文件的大小设置为 virSize,同时用 0 填充
unsigned char c = 0;
for (int i = 0; i < virSize; i++)
fwrite(&c, sizeof(unsigned char), 1, pBinFile);
for (int i = 0; i<numSections; i++)
{
int RawOffset = pSectionTable[i].PointerToRawData; // 节在 PE 文件中的偏移值
int virAddress = pSectionTable[i].VirtualAddress; // 节映射到内存中的虚拟地址
int sizeOfSection= pSectionTable[i].SizeOfRawData; // 对齐后节的大小
if (0 == sizeOfSection || 0 == strcmp((char*)pSectionTable[i].Name, ".idata"))
continue;
printf(" Section Name: %s\n", pSectionTable[i].Name);
printf(" VirtualAddress: %08x\n", pSectionTable[i].VirtualAddress);
printf(" SizeOfRawData: %08x\n", pSectionTable[i].SizeOfRawData);
printf("PointerToRawData: %08x\n", pSectionTable[i].PointerToRawData);
printf("*********************************************************\n");
fseek(pExeFile, RawOffset, SEEK_SET);
fseek(pBinFile, virAddress, SEEK_SET);
// 从 argv[1] 读数据,同时写入 argv[2]
for(; sizeOfSection > 0; )
{
int nSize = sizeOfSection >= BUFFERSIZE ? BUFFERSIZE : sizeOfSection;
fread(DataBuffer, sizeof(unsigned char), nSize, pExeFile);
fwrite(DataBuffer, sizeof(unsigned char), nSize, pBinFile);
sizeOfSection -= BUFFERSIZE;
}
}
free(pSectionTable);
fclose(pExeFile);
fclose(pBinFile);
return 0;
}
<?xml version="1.0" encoding="gb2312"?>
<OSLProject Version="1.00" Name="pe2bin" SubjectID="1580a23c-aa04-4cf3-8cca-488dc577df8d" IncrementalUpload="0">
<Configurations>
<Configuration Name="Debug" PreDebugCommand="">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="_DEBUG" GenerateDebugInformation="-1" AdditionalOptions="-std=c99"/>
<Tool Name="NASMAssemblerTool"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool" OutputFile="$(OutDir)\pe2bin.exe"/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
<Configuration Name="Release">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="NDEBUG" AdditionalOptions="-std=c99"/>
<Tool Name="NASMAssemblerTool"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool" OutputFile="$(OutDir)\pe2bin.exe"/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
</Configurations>
<Files>
<Filter Name="ͷļ" Filter="h;hpp;hxx">
</Filter>
<Filter Name="Դļ" Filter="cpp;c;cc;cxx">
<File RelativePath=".\main.c">
</File>
</Filter>
</Files>
</OSLProject>
添加文件
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论