forked from yavfast/dbg-spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uExceptionHook.pas
222 lines (183 loc) · 5.29 KB
/
uExceptionHook.pas
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
unit uExceptionHook;
interface
uses Classes, SysUtils, Windows;
implementation
const
MAX_STACK_LENGTH = 16;
type
PStackFrame = ^TStackFrame;
TStackFrame = record
CallerFrame: Pointer;
CallerAddr: Pointer;
end;
NT_TIB32 = packed record
ExceptionList: DWORD;
StackBase: DWORD;
StackLimit: DWORD;
SubSystemTib: DWORD;
case Integer of
0 : (
FiberData: DWORD;
ArbitraryUserPointer: DWORD;
Self: DWORD;
);
1 : (
Version: DWORD;
);
end;
threadvar
_Buf: TMemoryBasicInformation;
var
OldDebugHook: Byte = 0;
InDebugMode: LongBool = False;
function IsValidCodeAddr(const Addr: Pointer): LongBool;
const
_PAGE_CODE: Cardinal = (PAGE_EXECUTE Or PAGE_EXECUTE_READ or PAGE_EXECUTE_READWRITE Or PAGE_EXECUTE_WRITECOPY);
Begin
Result := (VirtualQuery(Addr, _Buf, SizeOf(TMemoryBasicInformation)) <> 0) And ((_Buf.Protect And _PAGE_CODE) <> 0);
end;
function IsValidAddr(const Addr: Pointer): LongBool;
Begin
Result := (VirtualQuery(Addr, _Buf, SizeOf(TMemoryBasicInformation)) <> 0);
end;
function GetStackTop: Pointer; assembler;
asm
MOV EAX, FS:[0].NT_TIB32.StackBase
end;
function GetCallStack(const EIP, EBP: Pointer): TList; overload;
var
TopOfStack: Pointer;
BaseOfStack: Pointer;
StackFrame: PStackFrame;
Level: Integer;
begin
Result := TList.Create;
try
Level := 0; // ïðîïóñê ïî ñòåêó
Result.Add(EIP);
StackFrame := EBP;
BaseOfStack := Pointer(Cardinal(StackFrame) - 1);
TopOfStack := GetStackTop;
while (Level < MAX_STACK_LENGTH) and (
(Cardinal(BaseOfStack) < Cardinal(StackFrame)) and
(Cardinal(StackFrame) < Cardinal(TopOfStack)) and
IsValidAddr(StackFrame) and
(StackFrame <> StackFrame^.CallerFrame) and
IsValidCodeAddr(StackFrame^.CallerAddr)
)
do begin
if Level >= 0 then
Result.Add(Pointer(Cardinal(StackFrame^.CallerAddr) - 1));
StackFrame := PStackFrame(StackFrame^.CallerFrame);
Inc(Level);
end;
except
// Skip
end;
end;
function GetCallStack(Context: PContext): TList; overload;
begin
Result := GetCallStack(Pointer(Context^.Eip), Pointer(Context^.Ebp));
end;
procedure _CleanUpStackInfoProc(Info: Pointer);
begin
FreeAndNil(Info);
end;
function _GetStackInfoStringProc(Info: Pointer): String;
var
StackList: TList;
I: Integer;
begin
Result := '';
if Assigned(Info) then
begin
StackList := TList(Info);
for I := 0 to StackList.Count - 1 do
begin
if Result <> '' then
Result := Result + ' ';
Result := Result + Format('%p', [StackList[I]]);
end;
end;
end;
var
_BaseRaiseExceptionProc: TRaiseExceptionProc = nil;
type
TParamArray = array[0..14] of Pointer;
HookException = class(Exception);
const
cNonDelphiException = $0EEDFAE4;
cDelphiException = $0EEDFADE;
cContinuable = 0;
procedure _RaiseExceptionProc(ExceptionCode, ExceptionFlags: LongWord; NumberOfArguments: LongWord; Args: Pointer); stdcall;
var
ContextRecord: PContext;
ExceptionObj: HookException;
begin
if InDebugMode then
begin
// Äåáàãåð ñàì îòðàáîòàåò âñå êîäû ýêñåïøèíîâ
_BaseRaiseExceptionProc(ExceptionCode, ExceptionFlags, NumberOfArguments, Args);
end
else
begin
if (ExceptionCode = cNonDelphiException) then
begin
ContextRecord := TParamArray(Args^)[0];
ExceptionObj := TParamArray(Args^)[1];
ExceptionObj.SetStackInfo(GetCallStack(ContextRecord));
end
else
if (ExceptionCode = cDelphiException) and (ExceptionFlags <> cContinuable) then
begin
ExceptionObj := TParamArray(Args^)[1]; // Except object
ExceptionObj.SetStackInfo(GetCallStack(TParamArray(Args^)[0]{Address}, TParamArray(Args^)[5]{Stack frame}));
end;
if ExceptionFlags <> cContinuable then
begin
if not InDebugMode then
DebugHook := OldDebugHook;
try
_BaseRaiseExceptionProc(ExceptionCode, ExceptionFlags, NumberOfArguments, Args)
finally
if not InDebugMode then
DebugHook := 1;
end;
end;
end;
end;
procedure _InitExceptionHook;
begin
// Áëîêèðîâêà âñåõ ñèñòåìûíõ ñîîáùåíèé ïðî îøèáêó
{$IFNDEF DEBUG}
SetErrorMode(SEM_FAILCRITICALERRORS or SEM_NOGPFAULTERRORBOX or SEM_NOALIGNMENTFAULTEXCEPT or SEM_NOOPENFILEERRORBOX);
{$ENDIF}
// Ïðèëîæåíèå çàïóùåíî ïî äåáàãîì
InDebugMode := (DebugHook <> 0);
OldDebugHook := DebugHook;
if not InDebugMode then
DebugHook := 1; // Äëÿ âûçîâà RaiseExceptionProc
//if not InDebugMode then
begin
_BaseRaiseExceptionProc := RaiseExceptionProc;
RaiseExceptionProc := @_RaiseExceptionProc;
Exception.CleanUpStackInfoProc := @_CleanUpStackInfoProc;
Exception.GetStackInfoStringProc := @_GetStackInfoStringProc;
end;
end;
procedure _ReleaseExceptionHook;
begin
if not InDebugMode then
DebugHook := OldDebugHook;
//if not InDebugMode then
begin
RaiseExceptionProc := @_BaseRaiseExceptionProc;
Exception.CleanUpStackInfoProc := nil;
Exception.GetStackInfoStringProc := nil;
end;
end;
initialization
_InitExceptionHook;
finalization
_ReleaseExceptionHook;
end.