Tutorial 3: File Header

本文介绍了PE文件的基本结构,包括DOS MZ头、PE签名、FileHeader和OptionalHeader等内容。重点讲解了IMAGE_FILE_HEADER结构中Machine、NumberOfSections及Characteristics字段的作用,并探讨了这些字段在PE文件分析中的应用。

In this tutorial, we will study the file header portion of the PE header.

Let's summarize what we have learned so far:

  • DOS MZ header is called IMAGE_DOS_HEADER. Only two of its members are important to us: e_magic which contains the string "MZ" and e_lfanew which contains the file offset of the PE header.
  • We use the value in e_magic to check if the file has a valid DOS header by comparing it to the value IMAGE_DOS_SIGNATURE. If both values match, we can assume that the file has a valid DOS header.
  • In order to go to the PE header, we must move the file pointer to the offset specified by the value in e_lfanew.
  • The first dword of the PE header should contain the string "PE" followed by two zeroes. We compare the value in this dword to the value IMAGE_NT_SIGNATURE. If they match, then we can assume that the PE header is valid.

We will learn more about the PE header in this tutorial. The official name of the PE header is IMAGE_NT_HEADERS. To refresh your memory, I show it below.

IMAGE_NT_HEADERS STRUCT
    Signature dd ?
    FileHeader IMAGE_FILE_HEADER <>
    OptionalHeader IMAGE_OPTIONAL_HEADER32 <>
IMAGE_NT_HEADERS ENDS

Signature is the PE signature, "PE" followed by two zeroes. You already know and use this member.
FileHeader is a structure that contains the information about the physical layout/properies of the PE file in general.
OptionalHeader is also a structure that contains the information about the logical layout inside the PE file.

The most interesting information is in OptionalHeader. However, some fields in FileHeader are also important. We will learn about FileHeader in this tutorial so we can move to study OptionalHeader in the next tutorials.

IMAGE_FILE_HEADER STRUCT
    Machine WORD ?
    NumberOfSections WORD ?
    TimeDateStamp dd ?
    PointerToSymbolTable dd ?
    NumberOfSymbols dd ?
    SizeOfOptionalHeader WORD ?
    Characteristics WORD ?
IMAGE_FILE_HEADER ENDS

Field nameMeanings
MachineThe CPU platform the file is intended for. For Intel platform, the value is IMAGE_FILE_MACHINE_I386 (14Ch). I tried to use 14Dh and 14Eh as stated in the pe.txt by LUEVELSMEYER but Windows refused to run it. This field is rarely of interest to us except as a quick way of preventing a program to be executed.
NumberOfSections The number of sections in the file. We will need to modify the value in this member if we add or delete a section from the file.
TimeDateStampThe date and time the file is created. Not useful to us.
PointerToSymbolTableused for debugging.
NumberOfSymbolsused for debugging.
SizeOfOptionalHeaderThe size of the OptionalHeader member that immediately follows this structure. Must be set to a valid value.
CharacteristicsContains flags for the file, such as whether this file is an exe or a dll.

In summary, only three members are somewhat useful to us: Machine, NumberOfSections and Characteristics. You would normally not change the values of Machine and Characteristics but you must use the value in NumberOfSections when you're walking the section table.
I'm jumping the gun here but in order to illustrate the use of NumberOfSections, I need to digress briefly to the section table.

The section table is an array of structures. Each structure contains the information of a section. Thus if there are 3 sections, there will be 3 members in this array. You need the value in NumberOfSections so you know how many members there are in the array. You would think that checking for the structure with all zeroes in its members would help. Windows does use this approach. You can verify this fact by setting the value in NumberOfSections to a value higher than the real value and Windows still runs the file without problem. From my observation, I think Windows reads the value in NumberOfSections and examines each structure in the section table. If it finds a structure that contains all zeroes, it terminates the search. Else it would process until the number of structures specified in NumberOfSections is met. Why can't we ignore the value in NumberOfSections? Several reasons. The PE specification doesn't specify that the section table array must end with an all-zero structure. Thus there may be a situation where the last array member is contiguous to the first section, without empty space at all. Another reason has to do with bound imports. The new-style binding puts the information immediately following the section table's last structure array member. Thus you still need NumberOfSections.


[Iczelion's Win32 Assembly Homepage]

# Cross-platform C++ BaseClass and TypeSystem, UnitSystem, Geometry (FreeCAD Base module) Extracted by Qingfeng Xia, 2019-2021 It is extracted from FreeCAD project, <https://github.com/FreeCAD/FreeCAD>, it has the same license as FreeCAD: LGPL v2.1+ This piece of code extraction personal work is NOT sponsored by Qingfeng Xia&#39;s employment. v0.19 master branch, Nov 13, 2021. ## Features Most features from FreeCAD Base module, except for XML related IO. Conversion from PyCxx to pybind11 will be experimented here. - `BaseClass` and TypeSystem for C++: extracted from FreeCAD&#39;s Base module if only TypeSystem is needed, this can be configured by cmake `set(FC_TYPESYSTEM_ONLY ON)` - Collection of some header only libraries to catch Python productivity: json, argparse - Design pattern: Factory, Observer - Unit and Quantity System - Fundametnal classes for gometry: Vector3D, Boundbox, Axis, Matrix, Quaternion, VievProjection, 2D Shapes, OpenInventor Builder3D Todo: - `std::shared_ptr<T>` replace all void* - python wrapping helper, using pybind11 ## BaseClass ### Java and C# base class C++ does not have a base/root class for all objects, but lot of other high level languages have. see For a few C++ framework like QT, VTK, GTK, there is base class to provide shared functions see: [QT: `QObject`](https://doc.qt.io/qt-5/qobject.html) [VTK: `vtkObjectBase` and `vtkObject`](https://vtk.org/doc/nightly/html/classvtkObjectBase.html) ### Typical functions of base class 1. type system, implemented by c++ macro 2. reference counting, C++11 shared_pointer<> has this function 3. event/observer/subscription pattern, depends on the design of the framework 4. serialization,`std::to_string` ### help on script wrapping ### Tutorial for C++ TypeSystem 1. header file In each class&#39;s header file (inc. header only class), `TYPESYSTEM_HEADER();` must be the first line of that class Just like `Q_CLASS` for Qt meta system. ``` class CClass : public Base::BaseClass { TYPESYSTEM_HEADER(); public: int d = 0; }; ``` 2. source file In very beginnning of the source file for that class, not within class scope. Or any other cpp file for header only class. `TYPESYSTEM_SOURCE(CClass, Base::BaseClass);` header only is not supported for static member data declaration. 3. main source file To use this type system: in `main()` or module initialization function (called in `main()`) ```c++ int main() { using namespace Base; Type::init(); // init the type system // then init each class (actually register this class type), including the BaseClass BaseClass::init(); // this root class must be initialized // user classes init, can be wrap in a module init() CClass::init(); Type::destruct(); return 0; } ``` see the [TypeTest source](TypeTest.cpp) ## Collection of Design pattern - Design Patterns in C++ with Real Examples <https://github.com/ehsangazar/design-patterns-cpp> - <https://github.com/JakubVojvoda/design-patterns-cpp> ### included design patterns - AbstractFactory - Observor ## Collection of some header only libraries - toml11: header only lib: https://github.com/ToruNiina/toml11 https://github.com/skystrife/cpptoml - python style C++17 argparse: https://github.com/p-ranav/argparse - json: https://nlohmann.github.io/json/ see more header-only open source libraries at :https://awesomeopensource.com/projects/header-only ## More doc in doc subfolder <doc/FreeCADBaseChangeLog.md> <doc/PythonBinding.md> <doc/TypeSystem.md>翻译
06-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值