-
Notifications
You must be signed in to change notification settings - Fork 225
/
AheadLibDlg.cpp
1089 lines (906 loc) · 22.7 KB
/
AheadLibDlg.cpp
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// AheadLibDlg.cpp: 实现文件
//
#include "stdafx.h"
#include "AheadLib.h"
#include "AheadLibDlg.h"
#include "afxdialogex.h"
#include "AheadSource.h"
#define AHEADLIB_VERSION _T("AheadLib x86/x64 Ver:1.2")
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CAheadLibDlg 对话框
CAheadLibDlg::CAheadLibDlg(CWnd* pParent /*=nullptr*/)
: CDialog(IDD_AHEADLIB_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAheadLibDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_SHOW, m_show);
DDX_Control(pDX, IDC_STATIC_NAMESTRING, m_NameString);
DDX_Control(pDX, IDC_STATIC_ARCH, m_Arch);
DDX_Control(pDX, IDC_STATIC_TIMESTAMP, m_Timestamp);
DDX_Control(pDX, IDC_EDIT_INPUTFILE, m_InputFile);
DDX_Control(pDX, IDC_EDIT_OUTPUTFILE, m_OutputFile);
}
BEGIN_MESSAGE_MAP(CAheadLibDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_EXIT, &CAheadLibDlg::OnBnClickedButtonExit)
ON_BN_CLICKED(IDC_BUTTON_MAKEFILE, &CAheadLibDlg::OnBnClickedButtonMakefile)
ON_WM_DROPFILES()
ON_BN_CLICKED(IDC_BUTTON_CHOSEFILE, &CAheadLibDlg::OnBnClickedButtonChosefile)
ON_BN_CLICKED(IDC_BUTTON_SAVEFILE, &CAheadLibDlg::OnBnClickedButtonSavefile)
END_MESSAGE_MAP()
/*
* 禁止目录重定向
*/
BOOL safeWow64DisableDirectory(PVOID &arg)
{
typedef BOOL WINAPI fntype_Wow64DisableWow64FsRedirection(PVOID *OldValue);
auto pfnWow64DisableWow64FsRedirection = (fntype_Wow64DisableWow64FsRedirection*)\
GetProcAddress(GetModuleHandleA("kernel32.dll"), "Wow64DisableWow64FsRedirection");
if (pfnWow64DisableWow64FsRedirection) {
(*pfnWow64DisableWow64FsRedirection)(&arg);
return TRUE;
}
else {
return FALSE;
}
}
/*
* 恢复目录重定向
*/
BOOL safeWow64ReverDirectory(PVOID &arg)
{
typedef BOOL WINAPI fntype_Wow64RevertWow64FsRedirection(PVOID *OldValue);
auto pfnWow64RevertWow64FsRedirection = (fntype_Wow64RevertWow64FsRedirection*) \
GetProcAddress(GetModuleHandleA("kernel32.dll"), "Wow64RevertWow64FsRedirection");
if (pfnWow64RevertWow64FsRedirection) {
(*pfnWow64RevertWow64FsRedirection)(&arg);
return TRUE;
}
else {
return FALSE;
}
}
/*
* 安全取得系统真实信息
*/
VOID SafeGetNativeSystemInfo(__out LPSYSTEM_INFO lpSystemInfo)
{
if (NULL == lpSystemInfo) return;
typedef VOID(WINAPI *LPFN_GetNativeSystemInfo)(LPSYSTEM_INFO lpSystemInfo);
LPFN_GetNativeSystemInfo fnGetNativeSystemInfo = \
(LPFN_GetNativeSystemInfo)GetProcAddress(GetModuleHandleA("kernel32"), "GetNativeSystemInfo");
if (NULL != fnGetNativeSystemInfo)
{
fnGetNativeSystemInfo(lpSystemInfo);
}
else
{
GetSystemInfo(lpSystemInfo);
}
}
/**
* 获取系统位数
*/
BOOL IsArch64()
{
SYSTEM_INFO si;
SafeGetNativeSystemInfo(&si);
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
{
return TRUE;
}
return FALSE;
}
// CAheadLibDlg 消息处理程序
BOOL CAheadLibDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
PVOID redir;
SetWindowText(AHEADLIB_VERSION);
if (IsArch64())
{
safeWow64DisableDirectory(redir);
}
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CAheadLibDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CAheadLibDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CAheadLibDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CAheadLibDlg::OnBnClickedButtonMakefile()
{
//
//开始生成文件
//
CString str;
CString source;
CString source_asm;
//文件头
source += g_fileHeader;
//编译器linker头
for (auto exFunc : m_exportFunc)
{
if (exFunc.isTranFunc) //中转函数
{
str.Format(_T("#pragma comment(linker, \"/EXPORT:%s=%s,@%d\")\r\n"),
exFunc.Name.GetString(), exFunc.TranName.GetString(), exFunc.Ordinal);
}
else if (exFunc.isOrd) //序号导出
{
if (m_isx64)
{
str.Format(_T("#pragma comment(linker, \"/EXPORT:Noname%d=AheadLib_Unnamed%d,@%d,NONAME\")\r\n"),
exFunc.Ordinal, exFunc.Ordinal, exFunc.Ordinal);
}
else
{
str.Format(_T("#pragma comment(linker, \"/EXPORT:Noname%d=_AheadLib_Unnamed%d,@%d,NONAME\")\r\n"),
exFunc.Ordinal, exFunc.Ordinal, exFunc.Ordinal);
}
}
else //名称导出
{
if (m_isx64)
{
str.Format(_T("#pragma comment(linker, \"/EXPORT:%s=AheadLib_%s,@%d\")\r\n"),
exFunc.Name.GetString(), exFunc.Name.GetString(), exFunc.Ordinal);
}
else
{
str.Format(_T("#pragma comment(linker, \"/EXPORT:%s=_AheadLib_%s,@%d\")\r\n"),
exFunc.Name.GetString(), exFunc.Name.GetString(), exFunc.Ordinal);
}
}
source += str;
}
source += _T("\r\n");
//全局变量定义
for (auto exFunc : m_exportFunc)
{
//
//第一次先生成 data变量
//
if (exFunc.isTranFunc)
{
continue;
}
if (exFunc.isData)
{
if (exFunc.isOrd)
{
str.Format(_T("EXTERN_C PVOID AheadLib_Unnamed%d[%d] = { 0 };\r\n"),
exFunc.Ordinal, exFunc.isDataCount);
}
else
{
str.Format(_T("EXTERN_C PVOID AheadLib_%s[%d] = { 0 };\r\n"),
exFunc.Name.GetString(), exFunc.isDataCount);
}
source += str;
}
}
source += _T("\r\n");
if (m_isx64) {
source += _T("extern \"C\" \n{\r\n");
}
for (auto exFunc : m_exportFunc)
{
//
//生成函数指针全局变量
//
if (exFunc.isTranFunc)
{
continue;
}
if (exFunc.isOrd)
{
str.Format(_T("PVOID pfnAheadLib_Unnamed%d;\r\n"),
exFunc.Ordinal);
}
else
{
str.Format(_T("PVOID pfnAheadLib_%s;\r\n"),
exFunc.Name.GetString());
}
source += str;
}
if (m_isx64) {
source += _T("}\r\n");
}
source += _T("\r\n");
//
//其他代码
//
CString g_init;
source += g_Free;
str = g_Load;
str.Replace(_T("AHEADLIB_XXXXXX.dll"), m_fileName.GetString());
source += str;
source += g_GetAddress;
//生成Init函数代码
g_init = _T("BOOL WINAPI Init()\r\n{\r\n");
for (auto exFunc : m_exportFunc)
{
if (exFunc.isTranFunc)
{
continue;
}
if (exFunc.isOrd)
{
str.Format(_T("\tpfnAheadLib_Unnamed%d = GetAddress(MAKEINTRESOURCEA(%d));\r\n"),
exFunc.Ordinal, exFunc.Ordinal);
}
else
{
str.Format(_T("\tpfnAheadLib_%s = GetAddress(\"%s\");\r\n"),
exFunc.Name.GetString(), exFunc.Name.GetString());
}
g_init += str;
if (exFunc.isData)
{
if (exFunc.isOrd)
{
str.Format(_T("\tmemcpy(AheadLib_Unnamed%d,pfnAheadLib_Unnamed%d,sizeof(PVOID) * %d);\r\n"),
exFunc.Ordinal, exFunc.Ordinal, exFunc.isDataCount);
}
else
{
str.Format(_T("\tmemcpy(AheadLib_%s,pfnAheadLib_%s,sizeof(PVOID) * %d);\r\n"),
exFunc.Name.GetString(), exFunc.Name.GetString(), exFunc.isDataCount);
}
g_init += str;
}
}
g_init += _T("\treturn TRUE;\r\n");
g_init += _T("}\t\n");
source += g_init;
source += g_ThreadProc;
source += g_Dllmain;
//
//生成汇编跳转代码
//
if (m_isx64)
{
source_asm += g_asmFileHeader;
source_asm += _T(".DATA\r\n");
for (auto exFunc : m_exportFunc)
{
if (exFunc.isTranFunc)
{
continue;
}
if (exFunc.isData)
{
continue;
}
if (exFunc.isOrd)
{
str.Format(_T("EXTERN pfnAheadLib_Unnamed%d:dq;\r\n"),
exFunc.Ordinal);
}
else
{
str.Format(_T("EXTERN pfnAheadLib_%s:dq;\r\n"),
exFunc.Name.GetString());
}
source_asm += str;
}
source_asm += _T("\r\n.CODE\r\n");
for (auto exFunc : m_exportFunc)
{
if (exFunc.isTranFunc)
{
continue;
}
if (exFunc.isData)
{
continue;
}
if (exFunc.isOrd)
{
str.Format(_T(
"AheadLib_Unnamed%d PROC\r\n"
"\tjmp pfnAheadLib_Unnamed%d\r\n"
"AheadLib_Unnamed%d ENDP\r\n\r\n"),
exFunc.Ordinal, exFunc.Ordinal, exFunc.Ordinal);
}
else
{
str.Format(_T(
"AheadLib_%s PROC\r\n"
"\tjmp pfnAheadLib_%s\r\n"
"AheadLib_%s ENDP\r\n\r\n"),
exFunc.Name.GetString(), exFunc.Name.GetString(), exFunc.Name.GetString());
}
source_asm += str;
}
source_asm += _T("\r\nEND\r\n");
}
else
{
for (auto exFunc : m_exportFunc)
{
if (exFunc.isTranFunc)
{
continue;
}
if (exFunc.isData)
{
continue;
}
if (exFunc.isOrd)
{
str.Format(_T("EXTERN_C __declspec(naked) void __cdecl AheadLib_Unnamed%d(void)\r\n"
"{\r\n"
"\t__asm jmp pfnAheadLib_Unnamed%d;\r\n"
"}\r\n"),
exFunc.Ordinal, exFunc.Ordinal);
}
else
{
str.Format(_T("EXTERN_C __declspec(naked) void __cdecl AheadLib_%s(void)\r\n"
"{\r\n"
"\t__asm jmp pfnAheadLib_%s;\r\n"
"}\r\n"),
exFunc.Name.GetString(), exFunc.Name.GetString());
}
source += str;
source += _T("\r\n");
}
}
CString outputPath;
CFile fileOut;
CStringA ansiSource;
m_OutputFile.GetWindowText(outputPath);
if (fileOut.Open(outputPath, CFile::modeCreate | CFile::modeWrite))
{
ansiSource = CW2CW(source.GetString());
fileOut.Write(ansiSource.GetString(), ansiSource.GetLength());
fileOut.Close();
AfxMessageBox(_T("Generate code success!"), MB_ICONINFORMATION);
}
if (m_isx64)
{
CFile fileOutAsm;
CString outputPathAsm;
CStringA ansiSourceAsm;
_tcscpy(outputPathAsm.GetBuffer(outputPath.GetLength() + 16), outputPath.GetString());
PathRenameExtension(outputPathAsm.GetBuffer(), _T("_jump.asm"));
outputPathAsm.ReleaseBuffer();
if (fileOutAsm.Open(outputPathAsm, CFile::modeCreate | CFile::modeWrite))
{
ansiSourceAsm = CW2CW(source_asm.GetString());
fileOutAsm.Write(ansiSourceAsm.GetString(), ansiSourceAsm.GetLength());
fileOutAsm.Close();
}
}
}
void CAheadLibDlg::OnScanFile()
{
CString str;
m_fileBuffer = LoadLibraryEx(m_filePath, NULL, LOAD_LIBRARY_AS_IMAGE_RESOURCE);
m_fileName = m_filePath;
PathStripPath(m_fileName.GetBuffer());
m_fileName.ReleaseBuffer();
if (m_fileBuffer == NULL)
{
str.Format(_T("Mapping file error! code=%d"), GetLastError());
AfxMessageBox(str, MB_ICONERROR);
return;
}
PIMAGE_DOS_HEADER dosHead;
PIMAGE_NT_HEADERS ntHead;
PIMAGE_NT_HEADERS64 ntHead64;
PIMAGE_SECTION_HEADER secHead;
m_isx64 = FALSE;
BOOL correct = FALSE;
CString nameString;
CString fileArch;
CString timestamp;
LPCSTR nameStringPtr;
CString expEdit;
for (int i = 0; i <= 2; i++)
{
dosHead = (PIMAGE_DOS_HEADER)((ULONG_PTR)m_fileBuffer - i);
if (dosHead->e_magic == IMAGE_DOS_SIGNATURE)
{
correct = TRUE;
break;
}
}
if (!correct)
{
AfxMessageBox(_T("Invalid PE File!"), MB_ICONERROR);
goto _exit;
}
if (dosHead->e_magic != IMAGE_DOS_SIGNATURE)
{
AfxMessageBox(_T("Invalid DOS Header!"), MB_ICONERROR);
goto _exit;
}
ntHead = ImageNtHeader(dosHead);
if (ntHead->Signature != IMAGE_NT_SIGNATURE)
{
AfxMessageBox(_T("Invalid NT Header!"), MB_ICONERROR);
goto _exit;
}
if (ntHead->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64 ||
ntHead->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64)
{
m_isx64 = TRUE;
ntHead64 = (PIMAGE_NT_HEADERS64)ntHead;
}
if (!(ntHead->FileHeader.Characteristics & IMAGE_FILE_DLL))
{
AfxMessageBox(_T("The target is not a dynamic link library!"), MB_ICONERROR);
goto _exit;
}
if (m_isx64)
{
if (ntHead64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress == 0 ||
ntHead64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size == 0)
{
AfxMessageBox(_T("Export table does not exist!"), MB_ICONERROR);
goto _exit;
}
}
else
{
if (ntHead->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress == 0 ||
ntHead->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size == 0)
{
AfxMessageBox(_T("Export table does not exist!"), MB_ICONERROR);
goto _exit;
}
}
if (m_isx64)
{
secHead = IMAGE_FIRST_SECTION(ntHead64);
}
else
{
secHead = IMAGE_FIRST_SECTION(ntHead);
}
//
// 获取文件节区表信息
//
m_sections.clear();
if (m_isx64)
{
for (WORD i = 0; i < ntHead64->FileHeader.NumberOfSections; i++)
{
m_sections.push_back(*secHead);
secHead++;
}
}
else
{
for (WORD i = 0; i < ntHead->FileHeader.NumberOfSections; i++)
{
m_sections.push_back(*secHead);
secHead++;
}
}
//
//获取导出表信息
//
PIMAGE_EXPORT_DIRECTORY exports;
if (m_isx64)
{
exports = (PIMAGE_EXPORT_DIRECTORY)\
((ULONG)dosHead + ntHead64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
}
else
{
exports = (PIMAGE_EXPORT_DIRECTORY)\
((ULONG)dosHead + ntHead->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
}
nameStringPtr = (LPCSTR)((ULONG_PTR)dosHead + exports->Name);
if (IsBadReadPtr(nameStringPtr, sizeof(PUCHAR)) == 0)
{
nameString = (WCHAR*)CA2W(nameStringPtr);
}
else
{
nameString = _T("ERROR!");
}
m_exportFunc.clear();
DWORD *pFunc = (DWORD*)(exports->AddressOfFunctions + (ULONG_PTR)dosHead);
DWORD *nameRVA = (DWORD*)(exports->AddressOfNames + (ULONG_PTR)dosHead);
int name = 0;
EXPORT_FUNCTION *exFunc = new EXPORT_FUNCTION;
for (DWORD Index = 0; Index < exports->NumberOfFunctions ; Index++)
{
//
//默认以序号导出
//
exFunc->isOrd = TRUE;
exFunc->Ordinal = exports->Base + Index;
exFunc->FunctionRVA = pFunc[Index];
exFunc->NameOrdinal = 0;
exFunc->NameRVA = 0;
exFunc->Name = _T("N/A");
ZeroMemory(&exFunc->secInfo, sizeof(IMAGE_SECTION_HEADER));
exFunc->isUnkown = FALSE;
exFunc->isFunc = FALSE;
exFunc->isTranFunc = FALSE;
exFunc->isData = FALSE;
exFunc->isDataCount = 0;
//
//过滤无效的RVA
//
if (exFunc->FunctionRVA == 0)
{
continue;
}
WORD *ordName = (WORD*)(exports->AddressOfNameOrdinals + (ULONG_PTR)dosHead);
for (DWORD i = 0; i < exports->NumberOfNames; i++)
{
//
//查找是否是以名称导出
//
if (LOWORD(Index) == *ordName)
{
exFunc->isOrd = FALSE;
exFunc->NameOrdinal = *ordName;
exFunc->NameRVA = nameRVA[i];
exFunc->Name = (WCHAR*)CA2W((LPCSTR)((ULONG_PTR)dosHead + exFunc->NameRVA));
name++;
break;
}
ordName++;
}
//
//查找所在区段,定位导出表函数是否是 函数 或 数据 或 中转导出表
//
exFunc->isUnkown = TRUE;
strcpy((char*)exFunc->secInfo.Name, "ERROR!");
for (auto sec : m_sections)
{
if (exFunc->FunctionRVA >= sec.VirtualAddress &&
exFunc->FunctionRVA <= (sec.VirtualAddress + sec.Misc.VirtualSize))
{
memcpy(&exFunc->secInfo, &sec, sizeof(IMAGE_SECTION_HEADER));
// if ((sec.Characteristics & IMAGE_SCN_MEM_EXECUTE) &&
// !(sec.Characteristics & IMAGE_SCN_MEM_WRITE))
// {
if (sec.Characteristics & IMAGE_SCN_MEM_EXECUTE)
{
//
//可运行不可写 代码区段
//
exFunc->isFunc = TRUE;
exFunc->isUnkown = FALSE;
break;
}
if ((sec.Characteristics & IMAGE_SCN_MEM_READ) &&
!(sec.Characteristics & IMAGE_SCN_MEM_WRITE))
{
//
//可读不可写 .rdata 区段,一般都是中转导出表
//
char *nameTran = (char*)((ULONG_PTR)dosHead + exFunc->FunctionRVA);
if (IsBadReadPtr(nameTran,sizeof(void*)) == 0)
{
if (strstr(nameTran, ".") != NULL)
{
exFunc->isTranFunc = TRUE;
exFunc->isUnkown = FALSE;
exFunc->TranName = (WCHAR*)CA2W((LPCSTR)nameTran);
}
else
{
//
//无法识别的函数,不知道怎么处理,只有退出
//
str.Format(_T(
"Unknown .rdata section data! continue?\r\n"
"ord:%d\r\n"
"func_rva:%08X\r\n"
"name:%s"),
exFunc->Ordinal, exFunc->FunctionRVA, exFunc->Name.GetString());
AfxMessageBox(str, MB_ICONERROR);
ExitProcess(-1);
}
}
else
{
str.Format(_T(
"Try to read .rdata section data exception! continue?\r\n"
"ord:%d\r\n"
"func_rva:%08X\r\n"
"name:%s"),
exFunc->Ordinal, exFunc->FunctionRVA, exFunc->Name.GetString());
AfxMessageBox(str, MB_ICONERROR );
ExitProcess(-1);
}
break;
}
if ((sec.Characteristics & IMAGE_SCN_MEM_READ) &&
(sec.Characteristics & IMAGE_SCN_MEM_WRITE) &&
!(sec.Characteristics & IMAGE_SCN_MEM_EXECUTE))
{
//
//可读可写不可运行,数据区段
//
exFunc->isData = TRUE;
exFunc->isUnkown = FALSE;
//
//探测数据区段的大小
//
if (m_isx64)
{
uint64_t *probePtr = (uint64_t*)((ULONG_PTR)dosHead + exFunc->FunctionRVA);
if (IsBadReadPtr(probePtr,sizeof(void*)) == 0)
{
while (TRUE)
{
if (*probePtr != NULL)
{
exFunc->isDataCount++;
probePtr++;
}
else
{
break;
}
}
}
else
{
str.Format(_T(
"Try to read .data section data exception!\r\n"
"ord:%d\r\n"
"func_rva:%08X\r\n"
"name:%s"),
exFunc->Ordinal, exFunc->FunctionRVA, exFunc->Name.GetString());
AfxMessageBox(str, MB_ICONERROR);
ExitProcess(-1);
}
}
else
{
uint32_t *probePtr = (uint32_t*)((ULONG_PTR)dosHead + exFunc->FunctionRVA);
if (IsBadReadPtr(probePtr, sizeof(void*)) == 0)
{
while (TRUE)
{
if (*probePtr != NULL)
{
exFunc->isDataCount++;
probePtr++;
}
else
{
break;
}
}
}
else
{
str.Format(_T(
"Try to read .data section data exception!\r\n"
"ord:%d\r\n"
"func_rva:%08X\r\n"
"name:%s"),
exFunc->Ordinal, exFunc->FunctionRVA, exFunc->Name.GetString());
AfxMessageBox(str, MB_ICONERROR);
ExitProcess(-1);
}
}
//
//如果这个导出数据全为空的话,默认给他导出一个指针大小
//
if (exFunc->isDataCount == 0)
{
exFunc->isDataCount++;
}
break;
}
AfxMessageBox(_T("Unrecognized export function!"));
ExitProcess(-1);
break;
}
}
m_exportFunc.push_back(*exFunc);
}
delete exFunc;
//
//显示文件信息
//
switch (ntHead->FileHeader.Machine)
{
case IMAGE_FILE_MACHINE_I386:
fileArch = _T("IMAGE_FILE_MACHINE_I386");
break;
case IMAGE_FILE_MACHINE_AMD64:
fileArch = _T("IMAGE_FILE_MACHINE_AMD64");
break;
case IMAGE_FILE_MACHINE_IA64:
fileArch = _T("IMAGE_FILE_MACHINE_IA64");
break;
default:
fileArch.Format(_T("Machine->%d"), ntHead->FileHeader.Machine);
break;
}
struct tm* t = localtime((const time_t*)&ntHead->FileHeader.TimeDateStamp);
if (t != NULL)
{
timestamp = _tasctime(t);
}
m_NameString.SetWindowText(nameString);
m_Arch.SetWindowText(fileArch);
m_Timestamp.SetWindowText(timestamp);
m_InputFile.SetWindowText(m_filePath);
str = m_filePath;
PathRenameExtension((LPWSTR)str.GetString(), _T(".cpp"));
m_OutputFile.SetWindowText(str);
for (auto element : m_exportFunc)
{
if (element.isFunc)
{
str.Format(_T("%04X %08X %s | %hs\r\n"),
element.Ordinal, element.FunctionRVA, element.Name.GetString(), element.secInfo.Name);
}
else if (element.isTranFunc)
{