加载PE标头
基本上,我想做的是找到 PE 文件的最后一部分。我非常仔细地阅读了PE规范,但我无法发现我的代码失败的地方。
PIMAGE_DOS_HEADER pidh = (PIMAGE_DOS_HEADER)buffer;
PIMAGE_NT_HEADERS pinh = (PIMAGE_NT_HEADERS)(pidh + pidh->e_lfanew);
PIMAGE_FILE_HEADER pifh = (PIMAGE_FILE_HEADER)&pinh->FileHeader;
PIMAGE_OPTIONAL_HEADER pioh = (PIMAGE_OPTIONAL_HEADER)&pinh->OptionalHeader;
PIMAGE_SECTION_HEADER pish = (PIMAGE_SECTION_HEADER)(pinh + sizeof(IMAGE_NT_HEADERS) + (pifh->NumberOfSections - 1) * sizeof(IMAGE_SECTION_HEADER));
buffer
是一个包含加载的可执行文件的字节数组,pish
是指向最后一节的指针。由于某种原因,部分数量似乎超过 20 000。
有什么想法吗? 提前致谢
Basically, what I am trying to do is to find last section of PE file. I have read PE specification very attentively, yet I can't discover where my code fails.
PIMAGE_DOS_HEADER pidh = (PIMAGE_DOS_HEADER)buffer;
PIMAGE_NT_HEADERS pinh = (PIMAGE_NT_HEADERS)(pidh + pidh->e_lfanew);
PIMAGE_FILE_HEADER pifh = (PIMAGE_FILE_HEADER)&pinh->FileHeader;
PIMAGE_OPTIONAL_HEADER pioh = (PIMAGE_OPTIONAL_HEADER)&pinh->OptionalHeader;
PIMAGE_SECTION_HEADER pish = (PIMAGE_SECTION_HEADER)(pinh + sizeof(IMAGE_NT_HEADERS) + (pifh->NumberOfSections - 1) * sizeof(IMAGE_SECTION_HEADER));
buffer
is a byte array containing loaded executable, and pish
is a pointer to the last section. For some reason, it appears that number of sections is over 20 000.
Any ideas ?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我立即发现一个问题:e_lfanew 是
IMAGE_NT_HEADERS
结构的偏移量(以字节为单位)。您要将这个字节数添加到IMAGE_DOS_HEADER
指针中,因此您将向前移动sizeof(IMAGE_DOS_HEADER)*pidh->e_lfanew
字节。修复版本:
调试此类问题的最佳方法是使用调试器进入代码并亲自查看内存中的 PE 数据。例如,您可以打开 Visual Studio 十六进制编辑器并查看所有字节数据以及您实际读取的值。
以下是有关在 VS 2010 中查看程序内存的一些信息:
http://msdn.microsoft.com/en-us/library/s3aw423e.aspx
There is one problem I see off hand: e_lfanew is the offset to the
IMAGE_NT_HEADERS
structure in bytes. You are adding this number of bytes to aIMAGE_DOS_HEADER
pointer, so you are moving forward bysizeof(IMAGE_DOS_HEADER)*pidh->e_lfanew
bytes.Fixed version:
The best way to debug problems like this is to drop into the code with your debugger and view the PE data yourself in memory. You can open up the Visual Studio hex editor for example and see all of the byte data, and which values you are actually reading out.
Here's some information on viewing program memory in VS 2010:
http://msdn.microsoft.com/en-us/library/s3aw423e.aspx
各段地址和数据也可以通过以下方式获取:
Various section address and data can be obtained by below way also :
你只是以错误的方式做事。
我给你写了一些代码,希望对你有帮助。它可以显示PE文件最后一段的数据。
简而言之,你不知道数据在哪里,直到你根据文件头分析数据。
You just do it the wrong way.
I wrote some code for you, hope it helps.It can show the data of the last section of a PE file.
In short, you do not know where the data is, until you analyze the data according to the file header.
节指针:
或
最后一个节指针是:
sections pointer:
or
the last section pointer is: