-
Notifications
You must be signed in to change notification settings - Fork 51
/
DbgHookUtils.pas
181 lines (144 loc) · 4.62 KB
/
DbgHookUtils.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
unit DbgHookUtils;
interface
uses System.SysUtils, DbgHookTypes;
type
TJclAddr = NativeInt;
PStackFrame = ^TStackFrame;
TStackFrame = record
CallerFrame: TJclAddr;
CallerAddr: TJclAddr;
end;
procedure _Log(const Msg: AnsiString); overload;
procedure _Log(const Msg: String); overload;
procedure _LogException(E: Exception; const Code: Integer = 0);
function IsValidCodeAddr(const Addr: Pointer): LongBool;
function IsValidAddr(const Addr: Pointer): LongBool;
function _GetObjClassType(const Obj: Pointer; var ObjClassName: ShortString): LongBool;
function GetFramePointer: Pointer; assembler;
function GetStackTop: TJclAddr; assembler;
procedure GetCallStack(var Stack: TDbgHookInfoStack; Level: Integer); stdcall;
procedure GetCallStackOS(var Stack: TDbgHookInfoStack; FramesToSkip: Integer); stdcall;
var
RTL_vmtClassName: Integer = System.vmtClassName;
implementation
uses Windows;
{ --- From JCL --- }
type
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;
{ --- From JCL --- }
procedure _Log(const Msg: AnsiString);
begin
OutputDebugStringA(PAnsiChar(Msg));
end;
procedure _Log(const Msg: String);
begin
_Log(AnsiString(Msg));
end;
procedure _LogException(E: Exception; const Code: Integer = 0);
begin
_Log(Format('DbgHook error (%d): %s', [Code, E.Message]));
end;
threadvar
_Buf: TMemoryBasicInformation;
function IsValidCodeAddr(const Addr: Pointer): LongBool;
const
_PAGE_CODE = DWORD(PAGE_EXECUTE Or PAGE_EXECUTE_READ or PAGE_EXECUTE_READWRITE Or PAGE_EXECUTE_WRITECOPY);
var
Buf: PMemoryBasicInformation;
Begin
Result := False;
if (Addr = nil) or (Addr = Pointer(-1)) then Exit;
Buf := @_Buf;
Result := (VirtualQuery(Addr, Buf^, SizeOf(TMemoryBasicInformation)) <> 0) And ((Buf^.Protect And _PAGE_CODE) <> 0);
end;
function IsValidAddr(const Addr: Pointer): LongBool;
var
Buf: PMemoryBasicInformation;
Begin
Result := False;
if (Addr = nil) or (Addr = Pointer(-1)) then Exit;
Buf := @_Buf;
Result := (VirtualQuery(Addr, Buf^, SizeOf(TMemoryBasicInformation)) <> 0);
end;
function _GetObjClassType(const Obj: Pointer; var ObjClassName: ShortString): LongBool;
var
ClassTypePtr: Pointer;
ClassNamePtr: Pointer;
begin
Result := False;
try
if not IsValidAddr(Obj) then Exit;
ClassTypePtr := PPointer(Obj)^;
if not IsValidCodeAddr(ClassTypePtr) then Exit;
ClassNamePtr := Pointer(Integer(ClassTypePtr) + RTL_vmtClassName);
if not IsValidCodeAddr(ClassNamePtr) then Exit;
ClassNamePtr := PPointer(ClassNamePtr)^;
if not IsValidCodeAddr(ClassNamePtr) then Exit;
ObjClassName := PShortString(ClassNamePtr)^;
Result := True;
except
on E: Exception do
_LogException(E, _EHOOK_GetObjClassType);
end;
end;
function GetFramePointer: Pointer; assembler;
asm
MOV EAX, EBP
end;
function GetStackTop: TJclAddr; assembler;
asm
MOV EAX, FS:[0].NT_TIB32.StackBase
end;
procedure GetCallStack(var Stack: TDbgHookInfoStack; Level: Integer); stdcall;
var
TopOfStack: TJclAddr;
BaseOfStack: TJclAddr;
StackFrame: PStackFrame;
begin
try
ZeroMemory(@Stack[0], Length(Stack) * SizeOf(Pointer));
StackFrame := GetFramePointer;
BaseOfStack := TJclAddr(StackFrame) - 1;
TopOfStack := GetStackTop;
while (Level < Length(Stack)) and (
(Level < 0) or (
(BaseOfStack < TJclAddr(StackFrame)) and
(TJclAddr(StackFrame) < TopOfStack) and
IsValidAddr(StackFrame)
// TODO: Ïî êàêîé-òî ïðè÷èíå ýòà ïðîâåðêà ñèëüíî òóïèò
// and IsValidCodeAddr(Pointer(StackFrame^.CallerAddr))
)
)
do begin
if Level >= 0 then
Stack[Level] := Pointer(StackFrame^.CallerAddr - 1);
StackFrame := PStackFrame(StackFrame^.CallerFrame);
Inc(Level);
end;
except
on E: Exception do
_LogException(E, _EHOOK_GetCallStack);
end;
end;
function RtlCaptureStackBackTrace(FramesToSkip: ULONG; FramesToCapture: ULONG; BackTrace: Pointer; BackTraceHash: PULONG): USHORT; stdcall;
external 'kernel32.dll' name 'RtlCaptureStackBackTrace';
procedure GetCallStackOS(var Stack: TDbgHookInfoStack; FramesToSkip: Integer); stdcall;
begin
//ZeroMemory(@Stack[0], SizeOf(TDbgHookInfoStack));
RtlCaptureStackBackTrace(FramesToSkip, DBG_STACK_LENGTH, @Stack[0], Nil);
end;
end.