RDFU  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Pe.c File Reference

Helper functions for Portable Executable files. More...

Include dependency graph for Pe.c:

Go to the source code of this file.

Macros

#define FIELD_OFFSET(type, field)   ((LONG)(LONG_PTR)&(((type *)0)->field))
 
#define IMAGE_FIRST_SECTION32(ntheader)
 
#define IMAGE_FIRST_SECTION64(ntheader)
 

Functions

BOOLEAN IsItPE64 (C_PTR Base)
 
BOOLEAN GetPEHeaders (C_PTR Base, EFI_IMAGE_DOS_HEADER **pMZ, EFI_IMAGE_NT_HEADERS32 **pPE, EFI_IMAGE_SECTION_HEADER **pSH)
 
ULONG GetPECodeChecksum (C_PTR Base)
 

Detailed Description

Helper functions for Portable Executable files.

Dump support for TE images.

Author
Piotr Bania
Date
Feb 2013 Helper functions for Portable Executable files (resolving the PE headers, calculating code sections checksum)
Author
Piotr Bania
Date
March 2013 Dump support for TE images

Definition in file Pe.c.

Macro Definition Documentation

#define FIELD_OFFSET (   type,
  field 
)    ((LONG)(LONG_PTR)&(((type *)0)->field))

Definition at line 16 of file Pe.c.

#define IMAGE_FIRST_SECTION32 (   ntheader)
Value:
((C_PTR)ntheader + \
FIELD_OFFSET( EFI_IMAGE_NT_HEADERS32, OptionalHeader ) + \
((EFI_IMAGE_NT_HEADERS32*)(ntheader))->FileHeader.SizeOfOptionalHeader \
))

Definition at line 19 of file Pe.c.

Referenced by GetPEHeaders().

#define IMAGE_FIRST_SECTION64 (   ntheader)
Value:
((C_PTR)ntheader + \
FIELD_OFFSET( EFI_IMAGE_NT_HEADERS64, OptionalHeader ) + \
((EFI_IMAGE_NT_HEADERS64*)(ntheader))->FileHeader.SizeOfOptionalHeader \
))

Definition at line 26 of file Pe.c.

Referenced by GetPEHeaders().

Function Documentation

ULONG GetPECodeChecksum ( C_PTR  Base)

Function computes the checksum from code section of the selected PE file. Code sections that are writable are not included in the checksum.

Parameters
BaseBase of PE file in memory
Return values
ChecksumOn success
NULLOn error

Definition at line 144 of file Pe.c.

References EFI_IMAGE_SECTION_HEADER::Characteristics, EFI_IMAGE_SCN_MEM_EXECUTE, EFI_IMAGE_SCN_MEM_WRITE, EFI_IMAGE_NT_HEADERS32::FileHeader, GetPEHeaders(), EFI_IMAGE_FILE_HEADER::NumberOfSections, EFI_IMAGE_SECTION_HEADER::SizeOfRawData, and EFI_IMAGE_SECTION_HEADER::VirtualAddress.

Referenced by AddImageToList(), and GetAllImagesWithDescription().

145 {
146  UINTN i;
150 
151  ULONG Crc32, Crc32Copy;
152 
153 
154  if (!GetPEHeaders(Base, &pMZ, &pPE, &pSH))
155  return 0;
156 
157 
158  // number of sections field is correct for both peh32/peh64
159  Crc32 = 0;
160  for (i = 0; i < pPE->FileHeader.NumberOfSections; i++, pSH++)
161  {
162  // skip writable sections
164  continue;
165 
166  // skip sections without code
168  continue;
169 
170  // calculate the checksum
171  // adding the various calculated checksum together is probably not safe in terms of crypto
172  // but the crc32 even without it is still available to forging
173  Crc32Copy = Crc32;
174  gBS->CalculateCrc32((VOID*)(Base + pSH->VirtualAddress), pSH->SizeOfRawData, &Crc32);
175  Crc32 += Crc32Copy;
176 
177  }
178 
179  return Crc32;
180 }

Here is the call graph for this function:

Here is the caller graph for this function:

BOOLEAN GetPEHeaders ( C_PTR  Base,
EFI_IMAGE_DOS_HEADER **  pMZ,
EFI_IMAGE_NT_HEADERS32 **  pPE,
EFI_IMAGE_SECTION_HEADER **  pSH 
)

Function initializes the IMAGE_DOS_HEADER, IMAGE_NT_HEADERS and IMAGE_SECTION_HEADER.

Parameters
BaseBase of PE file in memory
pMZPointer to variable which receives the EFI_IMAGE_DOS_HEADER pointer
pPEPointer to variable which receives the EFI_IMAGE_NT_HEADERS pointer
pSHPointer to variable which receives the EFI_IMAGE_SECTION_HEADER pointer
Return values
TRUEOn success
FALSEOn error

Definition at line 76 of file Pe.c.

References EFI_IMAGE_DOS_HEADER::e_lfanew, EFI_IMAGE_DOS_HEADER::e_magic, EFI_IMAGE_DOS_SIGNATURE, EFI_IMAGE_NT_SIGNATURE, EFI_IMAGE_NT_HEADERS32::FileHeader, IMAGE_FILE_MACHINE_X64, IMAGE_FIRST_SECTION32, IMAGE_FIRST_SECTION64, LogFullAscii(), EFI_IMAGE_FILE_HEADER::Machine, and EFI_IMAGE_NT_HEADERS32::Signature.

Referenced by BruteFind_gEventSignalQueueAddr(), and GetPECodeChecksum().

81 {
83  EFI_IMAGE_NT_HEADERS32 *plPE32;
84  EFI_IMAGE_NT_HEADERS32 *plPE64;
86 
87  *pMZ = 0;
88  *pPE = 0;
89  *pSH = 0;
90 
91  plMZ = (EFI_IMAGE_DOS_HEADER*)Base;
92 
93  if (plMZ->e_magic != EFI_IMAGE_DOS_SIGNATURE)
94  {
95 #if DEBUG_ON == 1
96  LogFullAscii("GetPEHeaders: invalid MZ signature at %016lx\r\n", (MAX_PTR)Base);
97 #endif
98  return FALSE;
99  }
100 
101  plPE32 = (EFI_IMAGE_NT_HEADERS32*)(Base + plMZ->e_lfanew);
102  plPE64 = (EFI_IMAGE_NT_HEADERS32*)plPE32;
103 
104  if (plPE32->Signature != EFI_IMAGE_NT_SIGNATURE)
105  {
106 #if DEBUG_ON == 1
107  LogFullAscii("GetPEHeaders: invalid PE signature at %016lx\r\n", (MAX_PTR)Base);
108 #endif
109 
110  return FALSE;
111  }
112 
113 
114  // is it a x64 pe?
116  plSH = IMAGE_FIRST_SECTION32(plPE32);
117  else
118  plSH = IMAGE_FIRST_SECTION64(plPE64);
119 
120 
121  *pMZ = plMZ;
122  *pPE = plPE32;
123  *pSH = plSH;
124 
125  return TRUE;
126 }

Here is the call graph for this function:

Here is the caller graph for this function:

BOOLEAN IsItPE64 ( C_PTR  Base)

Function checks whether selected Portable Executable file in memory is 32bit or 64bit.

Parameters
BaseBase of PE file in memory
Return values
TRUEPE is 64bit
FALSEPE is 32bit

Definition at line 46 of file Pe.c.

References EFI_IMAGE_DOS_HEADER::e_lfanew, EFI_IMAGE_NT_HEADERS32::FileHeader, IMAGE_FILE_MACHINE_X64, and EFI_IMAGE_FILE_HEADER::Machine.

47 {
50 
51  pMZ = (EFI_IMAGE_DOS_HEADER*)Base;
52  pPE = (EFI_IMAGE_NT_HEADERS32*)(Base + pMZ->e_lfanew);
53 
55  return FALSE;
56  return TRUE;
57 }