This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
forked from jsok/PETools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PEStructs.cs
303 lines (285 loc) · 11.9 KB
/
PEStructs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace PETools
{
public struct IMAGE_DOS_HEADER
{
public UInt16 e_magic; // Magic number
public UInt16 e_cblp; // Bytes on last page of file
public UInt16 e_cp; // Pages in file
public UInt16 e_crlc; // Relocations
public UInt16 e_cparhdr; // Size of header in paragraphs
public UInt16 e_minalloc; // Minimum extra paragraphs needed
public UInt16 e_maxalloc; // Maximum extra paragraphs needed
public UInt16 e_ss; // Initial (relative) SS value
public UInt16 e_sp; // Initial SP value
public UInt16 e_csum; // Checksum
public UInt16 e_ip; // Initial IP value
public UInt16 e_cs; // Initial (relative) CS value
public UInt16 e_lfarlc; // File address of relocation table
public UInt16 e_ovno; // Overlay number
public UInt16 e_res_0; // Reserved words
public UInt16 e_res_1; // Reserved words
public UInt16 e_res_2; // Reserved words
public UInt16 e_res_3; // Reserved words
public UInt16 e_oemid; // OEM identifier (for e_oeminfo)
public UInt16 e_oeminfo; // OEM information; e_oemid specific
public UInt16 e_res2_0; // Reserved words
public UInt16 e_res2_1; // Reserved words
public UInt16 e_res2_2; // Reserved words
public UInt16 e_res2_3; // Reserved words
public UInt16 e_res2_4; // Reserved words
public UInt16 e_res2_5; // Reserved words
public UInt16 e_res2_6; // Reserved words
public UInt16 e_res2_7; // Reserved words
public UInt16 e_res2_8; // Reserved words
public UInt16 e_res2_9; // Reserved words
public UInt32 e_lfanew; // File address of new exe header
}
public struct IMAGE_NT_HEADERS
{
public UInt32 Signature;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct IMAGE_OPTIONAL_HEADER32
{
public UInt16 Magic;
public Byte MajorLinkerVersion;
public Byte MinorLinkerVersion;
public UInt32 SizeOfCode;
public UInt32 SizeOfInitializedData;
public UInt32 SizeOfUninitializedData;
public UInt32 AddressOfEntryPoint;
public UInt32 BaseOfCode;
public UInt32 BaseOfData;
public UInt32 ImageBase;
public UInt32 SectionAlignment;
public UInt32 FileAlignment;
public UInt16 MajorOperatingSystemVersion;
public UInt16 MinorOperatingSystemVersion;
public UInt16 MajorImageVersion;
public UInt16 MinorImageVersion;
public UInt16 MajorSubsystemVersion;
public UInt16 MinorSubsystemVersion;
public UInt32 Win32VersionValue;
public UInt32 SizeOfImage;
public UInt32 SizeOfHeaders;
public UInt32 CheckSum;
public UInt16 Subsystem;
public UInt16 DllCharacteristics;
public UInt32 SizeOfStackReserve;
public UInt32 SizeOfStackCommit;
public UInt32 SizeOfHeapReserve;
public UInt32 SizeOfHeapCommit;
public UInt32 LoaderFlags;
public UInt32 NumberOfRvaAndSizes;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct IMAGE_OPTIONAL_HEADER64
{
public UInt16 Magic;
public Byte MajorLinkerVersion;
public Byte MinorLinkerVersion;
public UInt32 SizeOfCode;
public UInt32 SizeOfInitializedData;
public UInt32 SizeOfUninitializedData;
public UInt32 AddressOfEntryPoint;
public UInt32 BaseOfCode;
public UInt64 ImageBase;
public UInt32 SectionAlignment;
public UInt32 FileAlignment;
public UInt16 MajorOperatingSystemVersion;
public UInt16 MinorOperatingSystemVersion;
public UInt16 MajorImageVersion;
public UInt16 MinorImageVersion;
public UInt16 MajorSubsystemVersion;
public UInt16 MinorSubsystemVersion;
public UInt32 Win32VersionValue;
public UInt32 SizeOfImage;
public UInt32 SizeOfHeaders;
public UInt32 CheckSum;
public UInt16 Subsystem;
public UInt16 DllCharacteristics;
public UInt64 SizeOfStackReserve;
public UInt64 SizeOfStackCommit;
public UInt64 SizeOfHeapReserve;
public UInt64 SizeOfHeapCommit;
public UInt32 LoaderFlags;
public UInt32 NumberOfRvaAndSizes;
}
[Flags]
public enum PECharacteristics : UInt16
{
IMAGE_FILE_RELOCS_STRIPPED = 0x0001,
IMAGE_FILE_EXECUTABLE_IMAGE = 0x0002,
IMAGE_FILE_LINE_NUMS_STRIPPED = 0x0004,
IMAGE_FILE_LOCAL_SYMS_STRIPPED = 0x0008,
IMAGE_FILE_AGGRESIVE_WS_TRIM = 0x0010,
IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x0020,
IMAGE_FILE_BYTES_REVERSED_LO = 0x0080,
IMAGE_FILE_32BIT_MACHINE = 0x0100,
IMAGE_FILE_DEBUG_STRIPPED = 0x0200,
IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP = 0x0400,
IMAGE_FILE_NET_RUN_FROM_SWAP = 0x0800,
IMAGE_FILE_SYSTEM = 0x1000,
IMAGE_FILE_DLL = 0x2000,
IMAGE_FILE_UP_SYSTEM_ONLY = 0x4000,
IMAGE_FILE_BYTES_REVERSED_HI = 0x8000,
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct IMAGE_FILE_HEADER
{
public IMAGE_FILE_MACHINE Machine;
public UInt16 NumberOfSections;
public UInt32 TimeDateStamp;
public UInt32 PointerToSymbolTable;
public UInt32 NumberOfSymbols;
public UInt16 SizeOfOptionalHeader;
public PECharacteristics Characteristics;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct IMAGE_DATA_DIRECTORIES
{
public IMAGE_DATA_DIRECTORY_ENTRY export;
public IMAGE_DATA_DIRECTORY_ENTRY import;
public IMAGE_DATA_DIRECTORY_ENTRY resource;
public IMAGE_DATA_DIRECTORY_ENTRY exception;
public IMAGE_DATA_DIRECTORY_ENTRY security;
public IMAGE_DATA_DIRECTORY_ENTRY baseReloc;
public IMAGE_DATA_DIRECTORY_ENTRY debug;
public IMAGE_DATA_DIRECTORY_ENTRY copyright;
public IMAGE_DATA_DIRECTORY_ENTRY architecture;
public IMAGE_DATA_DIRECTORY_ENTRY globalPtr;
public IMAGE_DATA_DIRECTORY_ENTRY tls;
public IMAGE_DATA_DIRECTORY_ENTRY loadConfig;
public IMAGE_DATA_DIRECTORY_ENTRY boundImport;
public IMAGE_DATA_DIRECTORY_ENTRY iat;
public IMAGE_DATA_DIRECTORY_ENTRY delayImport;
public IMAGE_DATA_DIRECTORY_ENTRY comDescriptor;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct IMAGE_DATA_DIRECTORY_ENTRY
{
public UInt32 VirtualAddress;
public UInt32 Size;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct IMAGE_SECTION_HEADER
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public char[] SectionName;
public UInt32 PhysicalAddressOrVirtualSizeUnion;
public UInt32 VirtualAddress;
public UInt32 SizeOfRawData;
public UInt32 PointerToRawData;
public UInt32 PointerToRelocations;
public UInt32 PointerToLinenumbers;
public UInt16 NumberOfRelocations;
public UInt16 NumberOfLinenumbers;
public UInt32 Characteristics;
}
public enum IMAGE_SECTION_FLAGS : uint
{
IMAGE_SCN_CNT_CODE = 0x00000020, // Section contains code.
IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040, // Section contains initialized data.
IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080, // Section contains uninitialized data.
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct IMAGE_RELOCATION
{
public UInt32 VirtualAddress;
public UInt32 SymbolTableIndex;
public IMAGE_REL Type;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct IMAGE_SYMBOL
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] ShortName;
public UInt32 Value;
public IMAGE_SECTION_NUMBER SectionNumber;
public IMAGE_SYMBOL_TYPE Type;
public byte StorageClass;
public byte NumberOfAuxSymbols;
}
public enum IMAGE_SECTION_NUMBER : short
{
IMAGE_SYM_UNDEFINED = 0,
IMAGE_SYM_ABSOLUTE = -1,
IMAGE_SYM_DEBUG = -2,
}
public enum IMAGE_SYMBOL_TYPE : ushort
{
IMAGE_SYM_TYPE_NULL = 0x0000,
IMAGE_SYM_TYPE_VOID = 0x0001,
IMAGE_SYM_TYPE_CHAR = 0x0002,
IMAGE_SYM_TYPE_SHORT = 0x0003,
IMAGE_SYM_TYPE_INT = 0x0004,
IMAGE_SYM_TYPE_LONG = 0x0005,
IMAGE_SYM_TYPE_FLOAT = 0x0006,
IMAGE_SYM_TYPE_DOUBLE = 0x0007,
IMAGE_SYM_TYPE_STRUCT = 0x0008,
IMAGE_SYM_TYPE_UNION = 0x0009,
IMAGE_SYM_TYPE_ENUM = 0x000A,
IMAGE_SYM_TYPE_MOE = 0x000B,
IMAGE_SYM_TYPE_BYTE = 0x000C,
IMAGE_SYM_TYPE_WORD = 0x000D,
IMAGE_SYM_TYPE_UINT = 0x000E,
IMAGE_SYM_TYPE_DWORD = 0x000F,
// Special Microsoft flag
IMAGE_SYM_TYPE_MSFT_FN = 0x0020,
IMAGE_SYM_TYPE_PCODE = 0x8000,
}
public enum IMAGE_FILE_MACHINE : ushort
{
IMAGE_FILE_MACHINE_UNKNOWN = 0x0,
IMAGE_FILE_MACHINE_I386 = 0x14c,
IMAGE_FILE_MACHINE_AMD64 = 0x8664,
}
public enum IMAGE_REL : ushort
{
// i386 relocs
IMAGE_REL_I386_ABSOLUTE = 0x0000,
IMAGE_REL_I386_DIR16 = 0x0001,
IMAGE_REL_I386_REL16 = 0x0002,
IMAGE_REL_I386_DIR32 = 0x0006,
IMAGE_REL_I386_DIR32NB = 0x0007,
IMAGE_REL_I386_SEG12 = 0x0009,
IMAGE_REL_I386_SECTION = 0x000A,
IMAGE_REL_I386_SECREL = 0x000B,
IMAGE_REL_I386_TOKEN = 0x000C,
IMAGE_REL_I386_SECREL7 = 0x000D,
IMAGE_REL_I386_REL32 = 0x0014,
// x64 relocs
//IMAGE_REL_AMD64_ABSOLUTE = 0x0000,
//IMAGE_REL_AMD64_ADDR64 = 0x0001,
//IMAGE_REL_AMD64_ADDR32 = 0x0002,
//IMAGE_REL_AMD64_ADDR32NB = 0x0003,
//IMAGE_REL_AMD64_REL32 = 0x0004,
//IMAGE_REL_AMD64_REL32_1 = 0x0005,
//IMAGE_REL_AMD64_REL32_2 = 0x0006,
//IMAGE_REL_AMD64_REL32_3 = 0x0007,
//IMAGE_REL_AMD64_REL32_4 = 0x0008,
//IMAGE_REL_AMD64_REL32_5 = 0x0009,
//IMAGE_REL_AMD64_SECTION = 0x000A,
//IMAGE_REL_AMD64_SECREL = 0x000B,
//IMAGE_REL_AMD64_SECREL7 = 0x000C,
//IMAGE_REL_AMD64_TOKEN = 0x000D,
//IMAGE_REL_AMD64_SREL32 = 0x000E,
//IMAGE_REL_AMD64_PAIR = 0x000F,
//IMAGE_REL_AMD64_SSPAN32 = 0x0010,
}
/*
* A struct to contain the data of each section.
* More of a convenience structure, also store section name
* and index for easy lookups.
*/
public struct SectionData
{
public byte[] data;
public string name;
public int index;
}
}