ICZelion PE TUT3

zhaozj2021-02-11  186

Tutorial 3: File 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 Peheader in this Tutorial. The official name of the pe header is image_nt_headers. To Refresh Your Memory, I show it beelow.

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 WORDER Word? Characteristics Word? Image_File_Header Ends

Field nameMeaningsMachineThe 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.

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 PeSpesn't Specify That The section Table Array Must End with an all-zero structure. THU s 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 Thill Need Numberofsections. [Icnelion's win32 assembly homepage]

转载请注明原文地址:https://www.9cbs.com/read-4646.html

New Post(0)