Work with PE Headers using x32dbg

Content

  • 1. Create an executable file
    • (1) Create a C Program
    • (2) Compile the C program
  • 2. Work with x32dbg
    • (1) Find the PE header in x32dbg
    • (2) Detailed things in PE header


1. Create an executable file

(1) Create a C Program

Create a new folder called “test” in C:/, and create a text document called “1.cpp” in the folder.
Like this:
新建文件
Right click the file and edit it with Notepad++. Input the codes as follows.
在这里插入图片描述

#include
int main (){
     
	printf("Hello World!\n");
	return 0;
}

Save the file.

(2) Compile the C program

Open the Developer’s command prompt of Visual Studio 2017 in the start menu.
在这里插入图片描述
Switch to the file location C:\test using “cd C:\test”
在这里插入图片描述
Then compile 1.cpp using “cl 1.cpp”
在这里插入图片描述

We can run it using command “1.exe”
在这里插入图片描述
Success.

2. Work with x32dbg

(1) Find the PE header in x32dbg

1.Open x32dbg.
Work with PE Headers using x32dbg_第1张图片
2. Drug 1.exe to the opened x32dbg
Work with PE Headers using x32dbg_第2张图片
3. Press F9 (Run) to go to the entry point of file
Work with PE Headers using x32dbg_第3张图片
4. You will see call, jmp commands.
Work with PE Headers using x32dbg_第4张图片
5. Set breakpoint to the jmp command using F2 key
Work with PE Headers using x32dbg_第5张图片
6. Press F9(Run) to continue till this breakpoint
Work with PE Headers using x32dbg_第6张图片
7. Press F8(Step over) to continue till push, push, push, call Work with PE Headers using x32dbg_第7张图片
8. On call command press F7(Step into)
Work with PE Headers using x32dbg_第8张图片

Congratulations! you found main function of C program
9. Press CTRL+M to go to memory map
Work with PE Headers using x32dbg_第9张图片
10. You see table with memory segments. Find memory segment with name of your exe file and double click on it
Work with PE Headers using x32dbg_第10张图片
11. At Dump 1 window you will get the beginning of this segment. It is PE-header
12. Right click to display ASCII.
Work with PE Headers using x32dbg_第11张图片
The PE executable file format:

OLD EXE (MZ header)

+0  WORD e_magic;         // Magic number MZ
 2  WORD e_cblp;          // Bytes on last page of file
 4  WORD e_cp;            // Pages in file
 6  WORD e_crlc;          // Relocations
 8  WORD e_cparhdr;       // Size of header in paragraphs
 A  WORD e_minalloc;      // Minimum extra paragraphs needed
 C  WORD e_maxalloc;      // Maximum extra paragraphs needed
 E  WORD e_ss;            // Initial (relative) SS value
10  WORD e_sp;            // Initial SP value
12  WORD e_csum;          // Checksum
14  WORD e_ip;            // Initial IP value
16  WORD e_cs;            // Initial (relative) CS value
18  WORD e_lfarlc;        // File address of relocation table
1A  WORD e_ovno;          // Overlay number
1C  WORD e_res[4];        // Reserved words
24  WORD e_oemid;         // OEM identifier (for e_oeminfo)
26  WORD e_oeminfo;       // OEM information; e_oemid specific
28  WORD e_res2[10];      // Reserved words
3C  DWORD e_lfanew;       // File address of new exe header
 :
 :
 \/
NEW EXE
+0 PE
 4 WORD  Machine;
 6 WORD  NumberOfSections;    
 8 DWORD   TimeDateStamp;
 C DWORD   PointerToSymbolTable;    
10 DWORD   NumberOfSymbols;
14 WORD  SizeOfOptionalHeader;    
16 WORD  Characteristics;
 :
 :
  1. Verify that first two bytes are MZ
    Work with PE Headers using x32dbg_第12张图片
  2. Go to 0x3c offset from MZ position and find the offset of PE header (in example its 0xF8)
    Work with PE Headers using x32dbg_第13张图片
  3. Add 0xF8 to the beginning of segment (address of MZ) and verify that PE signature there
    Work with PE Headers using x32dbg_第14张图片

(2) Detailed things in PE header

  1. Look at the format. We can see that the offset of ImageBase is 0x34
NEW EXE
+0 PE
 4 WORD  Machine;
 6 WORD  NumberOfSections;    
 8 DWORD   TimeDateStamp;
 C DWORD   PointerToSymbolTable;    
10 DWORD   NumberOfSymbols;
14 WORD  SizeOfOptionalHeader;    
16 WORD  Characteristics;

18 Optional Header

18 WORD    Magic;    
1a UCHAR   MajorLinkerVersion;    
1b UCHAR   MinorLinkerVersion;
1c DWORD   SizeOfCode;    
20 DWORD   SizeOfInitializedData;
24 DWORD   SizeOfUninitializedData;    
28 DWORD   AddressOfEntryPoint;
2c DWORD   BaseOfCode;    
30 DWORD   BaseOfData;    //    // NT additional fields.
   //    
34 DWORD   ImageBase; 
  1. Add 0x34 offset to PE signature position and get 4-byte number from it. This is ImageBase field
    Work with PE Headers using x32dbg_第15张图片
    We need to reverse it to get the actual number. That is 01 35 00 00. Remeber that ImageBase is 01 35 00 00.

  2. Add 0x28 offset to PE signature position and get 4-byte number from it. It is EntryPoint field
    Work with PE Headers using x32dbg_第16张图片
    We can know the sddress of EntryPoint is 00 00 12 E1
    Add up ImageBase and EntryPoint, that is 01350000 + 000012E1
    Click on the CPU panel, then press Ctrl+G and input “01350000+000012E1”.
    Work with PE Headers using x32dbg_第17张图片
    Press OK
    在这里插入图片描述
    We got the address of EntryPoint.

  3. Go to 0x80 offset and get 4-byte number. This is Import Directory table (IDT) address

  4. Add ImageBase to IDT and go to this memory location. It is actual IDT.

TO BE UPDATED…

你可能感兴趣的:(CyberScurity,网络安全)