-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDebug.cpp
More file actions
5065 lines (4516 loc) · 162 KB
/
Copy pathDebug.cpp
File metadata and controls
5065 lines (4516 loc) · 162 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
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
/*******************************************************************************
*
* Copyright (c) all 2010 黄奇 All rights reserved
* FileName : debug.cpp
* D a t e : 2010.3.4
* 功 能 : 调试功能实现源文件
* 说 明 :
*
*******************************************************************************/
#include "Debug.h"
char chSoftBreak = '\xcc' ;
#define BUFFER_MAX 128
#define MEMPAGE_LEN 4
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Debug::Debug():m_pExePathName(NULL),m_hProcess(INVALID_HANDLE_VALUE),
m_hThread(INVALID_HANDLE_VALUE),m_dwProcessID(0),m_dwThreadID(0),
m_lpStartAddress(0),m_lpBaseOfImage(0),m_nBreakCount(0),
m_uSoftBreakResume(0),m_UseDrRegister(0),m_uDRBreakResume(0),
m_nIsTCommand(0),m_nPageAddress(0),m_nIsTmpDrBreak(FALSE),
m_bIsIntoAPI(FALSE),m_pRecordFileName(NULL),m_bIsRecord(FALSE),
m_dwProtect(0)
{
}
Debug::~Debug()
{
if (NULL != m_pExePathName)
{
delete [] m_pExePathName ;
m_pExePathName = NULL ;
}
if (NULL != m_pRecordFileName)
{
delete [] m_pRecordFileName ;
m_pRecordFileName = NULL ;
}
}
// 扩展命令
Command Debug::c_ExternCommand[] = {".restart", Restart,
".show", DisplayLoadDllName,
".kill", Kill} ;
/*******************************************************************************
*
* 函 数 名 : SetExePathName
* 功能描述 : 设置被调试可执行程序的名字
* 参数列表 : IN _pExePatchName -- 被调试程序的名称
* 说 明 :
* 返回结果 : 如果返回FALSE的话出错,成功返回TRUE
*
*******************************************************************************/
BOOL Debug::SetExePathName(IN char *_pExePatchName)
{
if (NULL == _pExePatchName)
{
return FALSE ;
}
if (NULL != m_pExePathName)
{
delete [] m_pExePathName ;
m_pExePathName = NULL ;
}
int nLen = strlen(_pExePatchName) ;
if (nLen >= BUFFER_MAX)
{
printf("大哥,别乱来?\r\n") ;
return FALSE ;
}
if (nLen < 5)
{
printf("文件名太短了吧!\r\n") ;
return FALSE ;
}
if (NULL != m_pExePathName)
{
delete [] m_pExePathName ;
m_pExePathName = NULL ;
}
++nLen ;
m_pExePathName = new char[nLen] ;
if (NULL == m_pExePathName)
{
return FALSE ;
}
memset(m_pExePathName, 0, sizeof(char) * nLen) ;
strcpy(m_pExePathName, _pExePatchName) ;
return TRUE ;
}
/*******************************************************************************
*
* 函 数 名 : StartDebug
* 功能描述 : 开启调试功能入口函数
* 参数列表 : IN _pExePatchName -- 被调试程序的名称
* 说 明 :
* 返回结果 : 如果返回0的话是文件名出错,创建进程出错则返回-1,
*
*******************************************************************************/
int Debug::StartDebug(IN char *_pExePatchName)
{
if (NULL == m_pExePathName && NULL == _pExePatchName)
{
return 0 ;
}
if (_pExePatchName != NULL)
{
if (FALSE == SetExePathName(_pExePatchName))
{
return 0 ;
}
}
GetStartupInfo(&m_StartupInfo) ;
m_PEInformation.Analyse(m_pExePathName) ;
BOOL bRet = CreateProcess(m_pExePathName, NULL, NULL, NULL, FALSE,
DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS, NULL, NULL,
&m_StartupInfo, &m_pi) ;
m_dwThreadID = m_pi.dwThreadId ;
m_dwProcessID = m_pi.dwProcessId ;
m_hProcess = m_pi.hProcess ;
// 因为线程不一定是我们要的,所以先不用给先
DWORD dwContinueStatus = DBG_CONTINUE; // exception continuation
for(;;)
{
// Wait for a debugging event to occur. The second parameter indicates
// that the function does not return until a debugging event occurs.
dwContinueStatus = DBG_CONTINUE ;
WaitForDebugEvent(&m_DebugEvent, INFINITE);
// Process the debugging event code.
switch (m_DebugEvent.dwDebugEventCode)
{
case EXCEPTION_DEBUG_EVENT:
// Process the exception code. When handling
// exceptions, remember to set the continuation
// status parameter (dwContinueStatus). This value
// is used by the ContinueDebugEvent function.
{
switch (m_DebugEvent.u.Exception.ExceptionRecord.ExceptionCode)
{
case EXCEPTION_ACCESS_VIOLATION:
{
// First chance: Pass this on to the system.
// Last chance: Display an appropriate error.
dwContinueStatus = ParseAccessVioltion() ;
}
break ;
case EXCEPTION_BREAKPOINT:
{
// First chance: Display the current
// instruction and register values.
if (1 == isMyBreakPoint())
{
dwContinueStatus = ParseBreakPoint() ;
// 处理用户的请求
if (FALSE == m_bIsRecord)
{
ParseRequest() ;
}
}
}
break;
case EXCEPTION_DATATYPE_MISALIGNMENT:
{
// First chance: Pass this on to the system.
// Last chance: Display an appropriate error.
}
break;
case EXCEPTION_SINGLE_STEP:
{
// First chance: Update the display of the
// current instruction and register values.
// 这里假设用户的代码中不会修改单步标志
// 先看看是不是软断点需要恢复
ParseSingleSetp() ;
}
break ;
case DBG_CONTROL_C:
{
// First chance: Pass this on to the system.
// Last chance: Display an appropriate error.
// Handle other exceptions.
//ExitProcess(0) ;
}
break;
} // end of DebugEv.u.Exception.ExceptionRecord.ExceptionCode
break ;
} // end of EXCEPTION_DEBUG_EVENT
case CREATE_THREAD_DEBUG_EVENT:
{
// As needed, examine or change the thread's registers
// with the GetThreadContext and SetThreadContext functions;
// and suspend and resume thread execution with the
// SuspendThread and ResumeThread functions.
m_hThread = m_DebugEvent.u.CreateThread.hThread ;
#ifdef _MICROQ
MessageBox(NULL, TEXT("线程创建"), TEXT("Tips"), MB_OK) ;
#endif
}
break ;
case CREATE_PROCESS_DEBUG_EVENT:
{
// As needed, examine or change the registers of the
// process's initial thread with the GetThreadContext and
// SetThreadContext functions; read from and write to the
// process's virtual memory with the ReadProcessMemory and
// WriteProcessMemory functions; and suspend and resume
// thread execution with the SuspendThread and ResumeThread
// functions.
// 取到程序的线程句柄等等
m_hThread = m_DebugEvent.u.CreateProcessInfo.hThread ;
m_hProcess = m_DebugEvent.u.CreateProcessInfo.hProcess ;
m_lpStartAddress = (DWORD)m_DebugEvent.u.CreateProcessInfo.lpStartAddress ;
m_lpBaseOfImage = m_DebugEvent.u.CreateProcessInfo.lpBaseOfImage ;
// 在程序入口处下个断点
char ch ;
BOOL bRet ;
DWORD dwNumberOfBytesRead ;
// 因为要让程序停在入口,所以这里在入口下个CC断点,
// 先将原来的值保存
bRet = ReadProcessMemory(m_hProcess,(LPCVOID)m_lpStartAddress,
(PVOID)&ch, sizeof(char), &dwNumberOfBytesRead) ;
if (FALSE == bRet)
{
OutputDebugString("读目标进程出错了\r\n") ;
break ;
}
// 写下CC 将断点信息放到临时断点结构体里
//m_BreakPointList.Insert(m_lpStartAddress, ch) ;
m_TmpBreakPoint.byOldValue = ch ;
m_TmpBreakPoint.nAddr = m_lpStartAddress ;
m_TmpBreakPoint.StdState() ;
bRet = WriteProcessMemory(m_hProcess,(LPVOID)m_lpStartAddress,
(PVOID)&chSoftBreak,sizeof(char),&dwNumberOfBytesRead) ;
if (FALSE == bRet)
{
OutputDebugString("写目标进程出错了\r\n") ;
break ;
}
// 显示PE信息
m_PEInformation.DispPEInformation() ;
//m_MemList.RemoveAll() ;
// 枚举系统有效内存分页
//m_MemList.EnumDestProcessEffectivePage(m_hProcess) ;
// 输出被调试进程有效的内存页
//m_MemList.Display() ;
printf("\r\n") ;
m_DllExportFun.SetProcessValue(m_hProcess) ;
}
break ;
case EXIT_THREAD_DEBUG_EVENT:
{
// Display the thread's exit code.
OutputDebugString("目标线程创建!\r\n") ;
//ExitProcess(0) ;
}
break ;
case EXIT_PROCESS_DEBUG_EVENT:
{
// Display the process's exit code.
if (TRUE == m_bIsRecord)
{
WriteRecordToFile() ;
}
OutputDebugString( "目标进程退出!\r\n") ;
//ExitProcess(0) ;
}
break;
case LOAD_DLL_DEBUG_EVENT:
{
// Read the debugging information included in the newly
// loaded DLL.
// 加载dll
if ((int)m_DebugEvent.u.LoadDll.lpBaseOfDll & 0xff0000)
{
ParseLoadDllExportTable(m_DebugEvent) ;
}
}
break;
case UNLOAD_DLL_DEBUG_EVENT:
{
// Display a message that the DLL has been unloaded.
}
break;
case OUTPUT_DEBUG_STRING_EVENT:
{
// Display the output debugging string.
}
break;
}
// Resume executing the thread that reported the debugging event.
ContinueDebugEvent(m_DebugEvent.dwProcessId,
m_DebugEvent.dwThreadId, dwContinueStatus);
}
return 0 ;
}
/*******************************************************************************
*
* 函 数 名 : GetCurrentEip
* 功能描述 : 取得当前被调试线程的EIP
* 参数列表 : IN dwThreadId: 线程id
* 说 明 :
* 返回结果 : 成功返回eip值,失败返回0
*
*******************************************************************************/
int Debug::GetCurrentEip(IN DWORD dwThreadId)
{
if (0 == dwThreadId)
{
return 0 ;
}
CONTEXT context ;
context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS ;
if(FALSE == GetCurrentThreadContext(&context))
{
return 0 ;
}
return context.Eip ;
}
/*******************************************************************************
*
* 函 数 名 : PrintContext
* 功能描述 : 输出寄存器环境
* 参数列表 :
* 说 明 :
* 返回结果 :
*
*******************************************************************************/
void Debug::PrintContext(void)
{
CONTEXT context ;
context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS ;
if(FALSE == GetCurrentThreadContext(&context))
{
return ;
}
printf("EAX=%p ", context.Eax) ;
printf("EBX=%p ", context.Ebx) ;
printf("ECX=%p ", context.Ecx) ;
printf("EDX=%p\r\n", context.Edx) ;
printf("ESI=%p ", context.Esi) ;
printf("EDI=%p ",context.Edi) ;
printf("ESP=%p ", context.Esp) ;
printf("EBP=%p\r\n", context.Ebp) ;
printf("EIP=%p ", context.Eip) ;
printf("iopl=%2d ", context.EFlags & 0x3000) ;
printf(" %3s", (context.EFlags & 0x800)?"ov":"nv") ;
printf("%3s", (context.EFlags & 0x400)?"dn":"up") ;
printf("%3s", (context.EFlags & 0x200)?"ei":"di") ;
printf("%3s", (context.EFlags & 0x80 )?"ng":"pl") ;
printf("%3s", (context.EFlags & 0x40 )?"zr":"nz") ;
printf("%3s", (context.EFlags & 0x10 )?"ac":"nc") ;
printf("%3s", (context.EFlags & 0x4 )?"pe":"po") ;
printf("%3s\r\n", (context.EFlags & 0x1 )?"cy":"nc") ;
//cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000
printf("cs=%04X ss=%04X ds=%04X es=%04X fs=%04X gs=%04X\r\n",
context.SegCs, context.SegSs, context.SegDs, context.SegEs,
context.SegFs, context.SegGs) ;
}
/*******************************************************************************
*
* 函 数 名 : PrintInstruction
* 功能描述 : 输出指令
* 参数列表 : IN Eip --- Eip值
IN bIsContimue --- 是否接着上次的输出
IN nItem --- 预输出指令条数
* 说 明 :
* 返回结果 : 成功返回指令,失败返回0
*
*******************************************************************************/
int Debug::PrintInstruction(IN int Eip, IN BOOL bIsContimue, IN int nItem)
{
if (INVALID_HANDLE_VALUE == m_hProcess )
{
OutputDebugString("进程句柄无效\r\n") ;
return 0;
}
DWORD dwTmpEip = 0 ;
DWORD dwProtect = 0 ;
dwTmpEip = GetCurrentEip(m_DebugEvent.dwThreadId) ;
if (0 != Eip)
{
if( FALSE == VirtualProtectEx(m_hProcess, (LPVOID)Eip, BUFFER_MAX,
PAGE_EXECUTE_READWRITE, &dwProtect))
{
return 0 ;
}
}
else
{
if( FALSE == VirtualProtectEx(m_hProcess, (LPVOID)dwTmpEip, BUFFER_MAX,
PAGE_EXECUTE_READWRITE, &dwProtect))
{
return 0 ;
}
}
static DWORD dwEip = 0 ;
if (FALSE == bIsContimue)
{
dwEip = Eip ;
}
// 先恢复原来的指令
RevertAllBreakPoint() ;
char szBuf[BUFFER_MAX] = {0} ;
DWORD dwNumberOfBytesRead ;
if(FALSE == ReadProcessMemory(m_hProcess, (PVOID)dwEip, szBuf,
BUFFER_MAX, &dwNumberOfBytesRead))
{
OutputDebugString("读目标进程失败\r\n") ;
return 0;
}
t_disasm da = {0};
int nIndex = 0 ;
int nInstrLen = 0 ;
for (int i = 0 ; i < nItem; ++i)
{
nInstrLen = Disasm(&szBuf[nIndex], 20, dwEip, &da,DISASM_CODE);
printf("%p %-16s %s", da.ip, da.dump, da.result) ;
char szDllName[MAXBYTE] = {0} ;
char szFunName[MAXBYTE] = {0} ;
if(m_DllExportFun.AddressToFunName(da, szDllName, szFunName))
{
printf(" <%s::", szDllName) ;
printf("%s>", szFunName) ;
}
else if (1 == nItem)
{
if (ParseFunNameOfRegisterValue(da, szDllName, szFunName))
{
printf(" <%s::", szDllName) ;
printf("%s>", szFunName) ;
}
}
printf("\r\n") ;
dwEip += nInstrLen ;
nIndex += nInstrLen ;
}
// 再重新将断点设回去
SetAllBreakPoint() ;
if (0 != Eip)
{
if (FALSE == VirtualProtectEx(m_hProcess, (LPVOID)dwEip, BUFFER_MAX,
dwProtect, &dwProtect))
{
return 0 ;
}
}
else
{
if (FALSE == VirtualProtectEx(m_hProcess, (LPVOID)dwTmpEip, BUFFER_MAX,
dwProtect, &dwProtect))
{
return 0 ;
}
}
return nIndex ;
}
/*******************************************************************************
*
* 函 数 名 : GetInstructionLen
* 功能描述 : 取得指定线程下一条指令的长度
* 参数列表 : IN dwThreadId: 线程id
OUT pLen: 指令长度
* 说 明 :
* 返回结果 : 如果成功返回Eip,否则返回0
*
*******************************************************************************/
int Debug::GetInstructionLen(IN DWORD dwThreadId, OUT int *pLen)
{
if (NULL == m_hProcess || INVALID_HANDLE_VALUE == m_hProcess)
{
return 0 ;
}
*pLen = 0 ;
char szBuf[BUFFER_MAX] = {0} ;
CONTEXT context ;
context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS ;
if (FALSE == GetCurrentThreadContext(&context) )
{
return 0 ;
}
DWORD dwProtect = 0 ;
if(FALSE == VirtualProtectEx(m_hProcess, (LPVOID)context.Eip, BUFFER_MAX,
PAGE_EXECUTE_READWRITE, &dwProtect))
{
OutputDebugString("修改内存属性出错!\r\n") ;
return 0 ;
}
DWORD dwNumberOfBytesRead ;
if (FALSE == ReadProcessMemory(m_hProcess, (PVOID)context.Eip, szBuf,
BUFFER_MAX, &dwNumberOfBytesRead))
{
CloseHandle(m_hThread) ;
m_hThread = INVALID_HANDLE_VALUE ;
}
// 这里是调用OD的反汇编引擎
t_disasm td ;
*pLen = Disasm(szBuf, 20, 0, &td, DISASM_CODE) ;
VirtualProtectEx(m_hProcess, (LPVOID)context.Eip, BUFFER_MAX,
dwProtect, &dwProtect) ;
return context.Eip;
}
/*******************************************************************************
*
* 函 数 名 : ParseAccessVioltion
* 功能描述 : 处理访问异常
* 参数列表 : 无
* 说 明 :
* 返回结果 :
*
*******************************************************************************/
DWORD Debug::ParseAccessVioltion(void)
{
DWORD nType = m_DebugEvent.u.Exception.ExceptionRecord.ExceptionInformation[0] ;
LPVOID pAddr = (LPVOID)m_DebugEvent.u.Exception.ExceptionRecord.ExceptionInformation[1] ;
// 加1和自己定义的 1是读,2是写
++nType ;
int nPage = 0 ;
// 取得内存页id
if ( !m_MemList.HaveIn(pAddr, nPage))
{
return DBG_EXCEPTION_NOT_HANDLED ;
}
// 判断这个页有没有断点信息,没有的话返回
aList *p = &m_PageMapBreak[nPage] ;
if (NULL == p)
{
return DBG_EXCEPTION_NOT_HANDLED ;
}
// 如果没有断点信息
int nBreakCount = p->GetSize() ;
if (0 == nBreakCount)
{
return DBG_EXCEPTION_NOT_HANDLED ;
}
PMemBreakNode pBreakNode = NULL ;
// 取得保存内存页属性的节点
PMemNode pMemNode = m_MemList.FindById(nPage) ;
m_nPageAddress = pAddr ;
DWORD dwProtect = 0 ;
if (NULL == pMemNode)
{
return 0 ;
}
if(FALSE == VirtualProtectEx(m_hProcess, pAddr, MEMPAGE_LEN,
pMemNode->Protect, &dwProtect))
{
return 0 ;
}
m_dwProtect = dwProtect ;
// 再判断一下有没有命中
for (int i = 0; i < nBreakCount; ++i)
{
// 得到那个分页中第i个结点
DistPosition pNode = m_PageMapBreak[nPage][i] ;
// 通过pNode->data得到断点号,再通过FinByID得到断点信息的指针
pBreakNode = m_MemBreak.FindById(pNode->data) ;
if (HaveInBreakArea((int)pAddr, pBreakNode)
&& pBreakNode->Protect == nType)
{
// 如果是处于指令记录的话,就不用给用户输入请示
if (TRUE == m_bIsRecord)
{
InstructionRecord() ;
SetTFRegister() ;
return DBG_CONTINUE ;
}
else if (FALSE == m_bIsRecord)
{
PrintContext() ;
PrintInstruction(GetCurrentEip(m_DebugEvent.dwThreadId), FALSE, 1) ;
SetTFRegister() ;
printf("内存中断!\r\n") ;
ParseRequest() ;
return DBG_CONTINUE ;
}
}
}
// 置单步
SetTFRegister() ;
return DBG_CONTINUE ;
}
/*******************************************************************************
*
* 函 数 名 : isMyBreakPoint
* 功能描述 : 是否是用户设的断点或者是系统第一次调用的软件中断
* 参数列表 :
* 说 明 :
* 返回结果 : 如果是自己能处理的断点,则返回1,否则返回0
*
*******************************************************************************/
int Debug::isMyBreakPoint(void)
{
// 先判断是不是系统自动调的 BreakPoint
if (m_nBreakCount < 1)
{
++m_nBreakCount ;
return 0 ;
}
if (m_nBreakCount == 1)
{
++m_nBreakCount ;
}
char ch = 0 ;
unsigned int nAddr = (int)m_DebugEvent.u.Exception.ExceptionRecord.ExceptionAddress ;
DWORD dwNumberOfBytesWritten = 0 ;
// 先在Temp断点中找
if (m_TmpBreakPoint.bIsActive && m_TmpBreakPoint.nAddr == nAddr)
{
return 1 ;
}
// 在断点链表中找
int nRet = m_BreakPointList.GetValue(nAddr, &ch) ;
if (nRet != 0 )
{
return 1 ;
}
return 0 ;
}
/*******************************************************************************
*
* 函 数 名 : ParseBreakPoint
* 功能描述 : 处理断点异常
* 参数列表 : 无
* 说 明 :
* 返回结果 : 如果能处理,返回DBG_CONTINUE,否则返回DBG_EXCEPTION_NOT_HANDLED
*
*******************************************************************************/
int Debug::ParseBreakPoint(void)
{
// 先判断是不是系统自动调的 BreakPoint
if (m_nBreakCount < 1)
{
++m_nBreakCount ;
return DBG_CONTINUE ;
}
char ch = 0 ;
unsigned int nAddr = (int)m_DebugEvent.u.Exception.ExceptionRecord.ExceptionAddress ;
DWORD dwNumberOfBytesWritten = 0 ;
m_dwThreadID = m_DebugEvent.dwThreadId ;
DWORD dwProtect = 0 ;
if (NULL == VirtualProtectEx(m_hProcess, (LPVOID)nAddr, MEMPAGE_LEN,
PAGE_EXECUTE_READWRITE, &dwProtect))
{
OutputDebugString("修改内存属性出错!\r\n") ;
return 0 ;
}
// 先在Temp断点中找
if (m_TmpBreakPoint.bIsActive && m_TmpBreakPoint.nAddr == nAddr)
{
ch = m_TmpBreakPoint.byOldValue ;
if (WriteProcessMemory(m_hProcess, (LPVOID)nAddr, &ch, sizeof(char),
&dwNumberOfBytesWritten))
m_TmpBreakPoint.ClsState() ;
CONTEXT context ;
context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS ;
if (FALSE == GetCurrentThreadContext(&context))
{
return DBG_EXCEPTION_NOT_HANDLED ;
}
context.Eip -= 1 ;
if (FALSE == SetCurrentThreadContext(&context))
{
return DBG_EXCEPTION_NOT_HANDLED ;
}
if(FALSE == m_bIsRecord)
{
PrintContext() ;
PrintInstruction(context.Eip, FALSE, 1) ;
printf("int3中断!\r\n") ;
}
else
{
t_disasm da = {0};
char szBuf[BUFFER_MAX] = {0};
DWORD dwNoting = 0 ;
ReadProcessMemory(m_hProcess, (LPVOID)context.Eip,szBuf, BUFFER_MAX, &dwNoting);
int nLen = Disasm(szBuf, 20, context.Eip, &da, DISASM_CODE);
AberrTreeNode * p = m_InstructionRecord.Insert(context.Eip,
szBuf, nLen) ;
int nJmpAddr = 0 ;
nJmpAddr = m_DllExportFun.AddressToFunName(da, szBuf, szBuf) ;
if (0 != nJmpAddr)
{
p->jmpconst = (LPVOID)nJmpAddr ;
}
VirtualProtectEx(m_hProcess, (LPVOID)nAddr, BUFFER_MAX,
dwProtect, &dwProtect) ;
InstructionRecord() ;
return DBG_CONTINUE ;
}
m_TmpBreakPoint.bIsActive = FALSE ;
VirtualProtectEx(m_hProcess, (LPVOID)nAddr, BUFFER_MAX,
dwProtect, &dwProtect);
return DBG_CONTINUE ;
}
// 在断点链表中找
if (0 != m_BreakPointList.GetValue(nAddr, &ch))
{
if (WriteProcessMemory(m_hProcess, (LPVOID)nAddr, &ch,
sizeof(char), &dwNumberOfBytesWritten))
{
// eip减1
CONTEXT context ;
context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS ;
if (FALSE == GetCurrentThreadContext(&context))
{
return DBG_EXCEPTION_NOT_HANDLED ;
}
context.Eip -= 1 ;
if (FALSE == SetCurrentThreadContext(&context))
{
return DBG_EXCEPTION_NOT_HANDLED ;
}
// 下次要恢复的软断点标志
m_uSoftBreakResume = nAddr ;
// 将这个断点设为失效
m_BreakPointList.Find(nAddr)->ClsState() ;
VirtualProtectEx(m_hProcess, (LPVOID)nAddr, BUFFER_MAX,
dwProtect, &dwProtect) ;
if(FALSE == m_bIsRecord)
{
SetTFRegister() ;
PrintContext() ;
PrintInstruction(context.Eip, FALSE, 1) ;
printf("int3中断!\r\n") ;
SetTFRegister();
}
else
{
InstructionRecord() ;
}
return DBG_CONTINUE ;
}
}
VirtualProtectEx(m_hProcess, (LPVOID)nAddr, BUFFER_MAX,
dwProtect, &dwProtect) ;
if (TRUE == m_bIsRecord)
{
InstructionRecord() ;
}
return DBG_EXCEPTION_NOT_HANDLED ;
}
/*******************************************************************************
*
* 函 数 名 : ParseRequest
* 功能描述 : 处理用户输入的请求
* 参数列表 : 无
* 说 明 :
* 返回结果 : 如果能够处理的话返回1,不能处理返回0
*
*******************************************************************************/
int Debug::ParseRequest(void)
{
BOOL bFlag = TRUE ;
BOOL isUCommand = FALSE ;
static FILE *fp = NULL ; // 文件指针,处理脚本用
static BOOL bIsScript = FALSE ; // 当前是否从脚本输入
while (bFlag)
{
printf("-") ;
char szBuffer[BUFFER_MAX] = {0} ;
// 控制输入,从文件输入还是等待用户从键盘输入
if (FALSE == bIsScript)
{
// 防止溢出,安全的输入
SafeInput(szBuffer, BUFFER_MAX) ;
}
else
{
if (feof(fp) || 0 == GetLine(fp, szBuffer, BUFFER_MAX))
{
bIsScript = FALSE ;
fclose(fp) ;
fp = NULL ;
SafeInput(szBuffer, BUFFER_MAX) ;
}
else
{
printf("%s\r\n", szBuffer) ;
}
}
// 去除空格
SkipSpace(szBuffer, BUFFER_MAX) ;
// 处理用户命令
// 这里没有用消息表,我觉得这样处理会更加灵活
// 但是函数会显得很肥胖
switch(szBuffer[0])
{
case 'b':
case 'B':
// 处理B命令的命令,比如bp, ba, bd
isUCommand = FALSE ;
ParseBCommand(szBuffer) ;
break ;
case 'e':
case 'E':
// 这个功能还没有做,修改内存的值
{
isUCommand = FALSE ;
LPVOID pAddr = 0 ;
int nParamCount = GetParamCount(szBuffer) ;
if (1 == nParamCount)
{
pAddr = (LPVOID)GetCurrentEip(m_DebugEvent.dwThreadId) ;
ParseEditMemory(pAddr) ;
}
if (2 == nParamCount)
{
sscanf(szBuffer, "%s%x", stderr, &pAddr) ;
ParseEditMemory(pAddr) ;
}
}
break ;
case 't':
case 'T':
// 单步步进
isUCommand = FALSE ;
m_nIsTCommand = TRUE ;
SetTFRegister() ;
bFlag = FALSE ;
break ;
case 'p':
case 'P':
// 单步步过
isUCommand = FALSE ;
SetSingleStep() ;
bFlag = FALSE ;
break ;
case 'r':
case 'R':
isUCommand = FALSE ;
if (2 == GetParamCount(szBuffer))
{
EditRegisterValue(szBuffer) ;
}
PrintContext() ;
break ;
case 'u':
case 'U':
RevertAllBreakPoint() ;
// 这里要处理一下u后面的参数
ParseUCommand(szBuffer, isUCommand) ;
if (FALSE == isUCommand)
{
isUCommand = TRUE ;
}
SetAllBreakPoint() ;
break ;
case '?':
case 'h':
case 'H':
PrintCommandHelp(0) ;
break ;
// 这里要处理一下g后面的参数
case 'g':
case 'G':
ParseGCommand(szBuffer) ;
bFlag = FALSE ;
break ;
case 'l':
case 'L':
isUCommand = FALSE ;
m_PEInformation.DispPEInformation() ;
break ;
case 'V':
case 'v':
DispCopyright() ;
break ;