forked from EvilKnight1986/DebugerVC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPEAnalyse.cpp
More file actions
540 lines (466 loc) · 14.7 KB
/
Copy pathPEAnalyse.cpp
File metadata and controls
540 lines (466 loc) · 14.7 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// PEAnalyse.cpp: implementation of the PEAnalyse class.
//
//////////////////////////////////////////////////////////////////////
#include "PEAnalyse.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/*******************************************************************************
*
* 函 数 名 : PEAnalyse
* 功能描述 : 构造函数--将成员函数初始化
* 参数列表 :
* 说 明 :
* 返回结果 :
*
*******************************************************************************/
PEAnalyse::PEAnalyse():m_isPE(0),m_hFile(INVALID_HANDLE_VALUE),m_pSectionHeader(NULL),
m_lpFile(NULL),m_hMap(NULL)
{
}
/*******************************************************************************
*
* 函 数 名 : ~PEAnalyse
* 功能描述 : 析构函数--资源的释放
* 参数列表 :
* 说 明 :
* 返回结果 :
*
*******************************************************************************/
PEAnalyse::~PEAnalyse()
{
CloseFileHandle() ;
if (NULL != m_pSectionHeader)
{
delete [] m_pSectionHeader ;
m_pSectionHeader = NULL ;
}
}
/*******************************************************************************
*
* 函 数 名 : CloseFileHandle
* 功能描述 : 关闭打开的文件句柄,包括文件内存映像句柄
* 参数列表 :
* 说 明 :
* 返回结果 :
*
*******************************************************************************/
void PEAnalyse::CloseFileHandle()
{
if (NULL != m_hMap)
{
UnmapViewOfFile(m_hMap) ;
m_hMap = NULL ;
}
if (NULL != m_hMap)
{
CloseHandle(m_hMap) ;
m_hMap = NULL ;
}
if (INVALID_HANDLE_VALUE != m_hFile)
{
CloseHandle(m_hFile) ;
m_hFile = INVALID_HANDLE_VALUE ;
}
}
/*******************************************************************************
*
* 函 数 名 : Analyse
* 功能描述 : PE文件分析
* 参数列表 : 文件全路径
* 说 明 : 同时可以通过函数的返回值判断是否为正确的PE文件
* 返回结果 : 如果函数成功,返回1, 如果函数出错,则返回0,
*
*******************************************************************************/
int PEAnalyse::Analyse(char *pPath)
{
m_isPE = 0 ;
if (NULL == pPath)
{
OutputDebugString("文件指定的路径不对") ;
return 0 ;
}
m_hFile = CreateFile(pPath, GENERIC_ALL,
FILE_SHARE_WRITE | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
FALSE,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL) ;
if (INVALID_HANDLE_VALUE == m_hFile)
{
OutputDebugString("文件打开出错") ;
return 0 ;
}
m_hMap = CreateFileMapping(m_hFile, NULL, PAGE_READWRITE, 0, 0, NULL) ;
if (NULL == m_hMap)
{
CloseFileHandle() ;
OutputDebugString("CreateFileMapping出错") ;
return 0 ;
}
m_lpFile = MapViewOfFile(m_hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0) ;
if (NULL == m_lpFile)
{
CloseFileHandle() ;
OutputDebugString("CreateFileMapping出错") ;
return 0 ;
}
//DWORD dwNumber ; 用文件时的遗留代码
/****************************************************************************
*
* 因调试器需要,将原本的由文件读取改为直接从文件内存映射里读
*
*
****************************************************************************/
// if (FALSE == ReadFile(m_hFile, (LPVOID)&m_DosHeader, sizeof(IMAGE_DOS_HEADER),
// &dwNumber, NULL))
// {
// CloseFileHandle() ;
// OutputDebugString("读文件出错") ;
// return 0 ;
// }
// 对于内存这类函数,没有判断有效性,一般不会出错
// 直接读内存
memcpy((LPVOID)&m_DosHeader, m_lpFile, sizeof(IMAGE_DOS_HEADER)) ;
// 判断DOS头是不是MZ
if (IMAGE_DOS_SIGNATURE != m_DosHeader.e_magic)
{
CloseFileHandle() ;
OutputDebugString("不是一个有效PE文件,DOS头不对!\r\n") ;
return 0 ;
}
// 判断e_lfanew高位,一般的文件高位一般为0,否则会引起程序的非法访问
if (m_DosHeader.e_lfanew & 0xffff0000)
{
CloseFileHandle() ;
OutputDebugString("不是一个有效PE文件,指向不正确的nt头") ;
return 0 ;
}
/* 通过文件指针访问
// 通过DOS头的e_lfanew值去读NT头
SetFilePointer(m_hFile, m_DosHeader.e_lfanew, 0, FILE_BEGIN) ;
if (FALSE == ReadFile(m_hFile, (PVOID)&m_NtHeaders,
sizeof(IMAGE_NT_HEADERS), &dwNumber, NULL))
{
CloseFileHandle() ;
#ifdef _DEBUG
OutputDebugString("读NT头出错") ;
#endif
return 0 ;
}
*/
// 读取NT头
memcpy( (PVOID)&m_NtHeaders,
(PVOID) ((int)m_lpFile + m_DosHeader.e_lfanew),
sizeof(IMAGE_NT_HEADERS)) ;
// 判断PE标志
if (IMAGE_NT_SIGNATURE != m_NtHeaders.Signature)
{
CloseFileHandle() ;
OutputDebugString("不是一个有效PE文件,NT头不对") ;
return 0 ;
}
if (NULL != m_pSectionHeader)
{
delete [] m_pSectionHeader ;
m_pSectionHeader = NULL ;
}
// 取得节的数量
int nSectionOfNumber = m_NtHeaders.FileHeader.NumberOfSections;
if (NULL != m_pSectionHeader)
{
delete [] m_pSectionHeader ;
m_pSectionHeader = NULL ;
}
if (nSectionOfNumber > 0)
{
m_pSectionHeader = new IMAGE_SECTION_HEADER[nSectionOfNumber] ;
if (NULL == m_pSectionHeader)
{
OutputDebugString("申请节信息结构体出错") ;
CloseFileHandle() ;
return 0 ;
}
// 取得节的偏移量
// 选项头的大小以sizeOfOptionHeader为主,否则会有bug
// 一般的编译器不会有bug,但是人家手动写的之类的就不能正常处理了
int nFileOffset = m_DosHeader.e_lfanew
+ m_NtHeaders.FileHeader.SizeOfOptionalHeader
+ sizeof(IMAGE_FILE_HEADER) + sizeof(DWORD);
/*
if (NULL == SetFilePointer(m_hFile, nFileOffset, NULL, FILE_BEGIN))
{
OutputDebugString("移动文件指针出错") ;
CloseFileHandle() ;
return 0 ;
}
if (FALSE == ReadFile(m_hFile, (LPVOID)m_pSectionHeader,
nSectionOfNumber * sizeof(IMAGE_SECTION_HEADER),
&dwNumber, NULL) )
{
OutputDebugString("读取节信息出错了") ;
CloseFileHandle() ;
return 0 ;
}
*/
memcpy((PVOID)m_pSectionHeader, (PVOID)((int)m_lpFile + nFileOffset),
nSectionOfNumber * sizeof(IMAGE_SECTION_HEADER)) ;
}
m_isPE = 1 ;
CloseFileHandle() ;
return 1 ;
}
/*******************************************************************************
*
* 函 数 名 : IsPE
* 功能描述 : 返回文件的结果
* 参数列表 :
* 说 明 :
* 返回结果 : 如果是PE文件返回1,否则返回0
*
*******************************************************************************/
int PEAnalyse::IsPE()
{
return m_isPE ;
}
/*******************************************************************************
*
* 函 数 名 : GetEntryPoint
* 功能描述 : 取得OEP地址
* 参数列表 :
* 说 明 :
* 返回结果 : 如果打开的是PE文件,则返回OEP地址,否则返回0
*
*******************************************************************************/
int PEAnalyse::GetEntryPoint()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.AddressOfEntryPoint ;
}
return 0 ;
}
/*******************************************************************************
*
* 函 数 名 : GetImageBase
* 功能描述 : 取得ImageBase
* 参数列表 :
* 说 明 :
* 返回结果 : 如果打开的是PE文件,则返回ImageBase,否则返回0
*
*******************************************************************************/
int PEAnalyse::GetImageBase()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.ImageBase ;
}
return 0 ;
}
/*******************************************************************************
*
* 函 数 名 : GetSizeOfImage
* 功能描述 : 取得镜像大小
* 参数列表 :
* 说 明 :
* 返回结果 : 如果打开的是PE文件,则返回SizeOfImage,否则返回0
*
*******************************************************************************/
int PEAnalyse::GetSizeOfImage()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.SizeOfImage ;
}
return 0 ;
}
/*******************************************************************************
*
* 函 数 名 : GetBaseOfCode
* 功能描述 :
* 参数列表 :
* 说 明 :
* 返回结果 : 如果打开的是PE文件,则返回BaseOfCod,否则返回0
*
*******************************************************************************/
int PEAnalyse::GetBaseOfCode()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.BaseOfCode ;
}
return 0 ;
}
int PEAnalyse::GetBaseOfData()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.BaseOfData ;
}
return 0 ;
}
int PEAnalyse::GetSectionAlignment()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.SectionAlignment ;
}
return 0 ;
}
int PEAnalyse::GetFileAlignment()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.FileAlignment ;
}
return 0 ;
}
int PEAnalyse::GetMagic()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.Magic ;
}
return 0 ;
}
int PEAnalyse::GetSubSystem()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.Subsystem ;
}
return 0 ;
}
int PEAnalyse::GetNumberOfSection()
{
if (1 == m_isPE)
{
return m_NtHeaders.FileHeader.NumberOfSections ;
}
return 0 ;
}
int PEAnalyse::GetTimeDataStamp()
{
if (1 == m_isPE)
{
return m_NtHeaders.FileHeader.TimeDateStamp ;
}
return 0 ;
}
int PEAnalyse::GetSizeOfHeaders()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.SizeOfHeaders ;
}
return 0 ;
}
int PEAnalyse::GetCharacteristics()
{
if (1 == m_isPE)
{
return m_NtHeaders.FileHeader.Characteristics ;
}
return 0 ;
}
int PEAnalyse::GetCheckSum()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.CheckSum ;
}
return 0 ;
}
int PEAnalyse::GetSizeOfOptionalHeader()
{
if (1 == m_isPE)
{
return m_NtHeaders.FileHeader.SizeOfOptionalHeader ;
}
return 0 ;
}
int PEAnalyse::GetNumberOfRvaAndSizes()
{
if (1 == m_isPE)
{
return m_NtHeaders.OptionalHeader.NumberOfRvaAndSizes ;
}
return 0 ;
}
/*******************************************************************************
*
* 函 数 名 : GetSectionHeaderPtr
* 功能描述 : 返回节信息的Buffer
* 参数列表 :
* 说 明 : 配合NumberOfSection一起使用
* 返回结果 : 如果打开的是pe文件,则返回保存节信息的地址,否则返回0
*
*******************************************************************************/
IMAGE_SECTION_HEADER * PEAnalyse::GetSectionHeaderPtr()
{
if (1 == m_isPE)
{
return m_pSectionHeader ;
}
return 0 ;
}
/*******************************************************************************
*
* 函 数 名 : GetMapToFilePointer
* 功能描述 : 返回文件在内存中的起始地址
* 参数列表 :
* 说 明 :
* 返回结果 : 如果打开的是pe文件,则返回文件在内存中的起始地址
*
*******************************************************************************/
PVOID PEAnalyse::GetMapToFilePointer()
{
if (1 == m_isPE)
{
return m_lpFile ;
}
return 0 ;
}
/*******************************************************************************
*
* 函 数 名 : DispPEInformation
* 功能描述 : 输出PE基本信息
* 参数列表 :
* 说 明 :
* 返回结果 : 0
*
*******************************************************************************/
void PEAnalyse::DispPEInformation()
{
if (0 == m_isPE)
{
return ;
}
printf("PE Base Information\r\n") ;
printf("EntryPoint: %p\t\t SubSystem: %p\r\n",
GetEntryPoint(), GetSubSystem()) ;
printf("ImageBase: %p\t\t NumberOfSection: %p\r\n",
GetImageBase(), GetNumberOfSection()) ;
printf("SizeOfImage: %p\t\t SizeOfOptionalHeader: %p\r\n",
GetSizeOfImage(), GetSizeOfOptionalHeader()) ;
printf("BaseOfCode: %p\t\t SizeOfHeaders: %p\r\n",
GetBaseOfCode(), GetSizeOfHeaders()) ;
printf("BaseOfData: %p\t\t Characteristics: %p\r\n",
GetBaseOfData(), GetCharacteristics()) ;
// 输出节信息
printf("\r\nSection Information:\r\n") ;
printf("SectionName VirtualSize RVA SizeOfRawData PointerToRawData Characteristics\r\n") ;
for (int i = 0; i < GetNumberOfSection(); ++i)
{
printf("%-8s ", m_pSectionHeader[i].Name) ;
printf("%p ", m_pSectionHeader[i].Misc.VirtualSize) ;
printf("%p ", m_pSectionHeader[i].VirtualAddress) ;
printf("%p ", m_pSectionHeader[i].SizeOfRawData) ;
printf("%p ", m_pSectionHeader[i].PointerToRawData) ;
printf("%p\r\n", m_pSectionHeader[i].Characteristics) ;
}
}