gpt4 book ai didi

c - C语言中如何获取每个PE段的起始地址?

转载 作者:行者123 更新时间:2023-11-30 20:32:21 25 4
gpt4 key购买 nike

是否可以编写一段 C 代码,在将其编译为 .exe 后检索其 PE 节地址?

最佳答案

您的意思是要访问 exe 的每个部分吗?如果是,请找到以下方法:

#include<windows.h>
#include<stdio.h>

int main()
{
LPCSTR fileName="inputFile.exe";
HANDLE hFile;
HANDLE hFileMapping;
LPVOID lpFileBase;
PIMAGE_DOS_HEADER dosHeader;
PIMAGE_NT_HEADERS peHeader;
PIMAGE_SECTION_HEADER sectionHeader;

hFile = CreateFileA(fileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);

if(hFile==INVALID_HANDLE_VALUE)
{
printf("\n CreateFile failed \n");
return 1;
}

hFileMapping = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL);

if(hFileMapping==0)
{
printf("\n CreateFileMapping failed \n");
CloseHandle(hFile);
return 1;
}

lpFileBase = MapViewOfFile(hFileMapping,FILE_MAP_READ,0,0,0);

if(lpFileBase==0)
{
printf("\n MapViewOfFile failed \n");
CloseHandle(hFileMapping);
CloseHandle(hFile);
return 1;
}

dosHeader = (PIMAGE_DOS_HEADER) lpFileBase;
if(dosHeader->e_magic==IMAGE_DOS_SIGNATURE)
{
printf("\n DOS Signature (MZ) Matched \n");

peHeader = (PIMAGE_NT_HEADERS) ((u_char*)dosHeader+dosHeader->e_lfanew);
if(peHeader->Signature==IMAGE_NT_SIGNATURE)
{
printf("\n PE Signature (PE) Matched \n");
//once found valid exe or dll

//go to first section
sectionHeader = IMAGE_FIRST_SECTION(peHeader);
UINT nSectionCount = peHeader->FileHeader.NumberOfSections;

//No of sections
printf("\n No of sections : %d \n",nSectionCount);

//sectionHeader contains address of first section
//traverse each section by below way
for( UINT i=0; i<nSectionCount; ++i, ++sectionHeader )
{
//section information
}
}
else
{
return 1;
}
}
else
{
return 1;
}
return 0;
}

关于c - C语言中如何获取每个PE段的起始地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47892726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com