-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLMethodHook.pas
209 lines (186 loc) · 5.09 KB
/
GLMethodHook.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
unit GLMethodHook;
interface
const
DirecotrCount = 214;
type
PMethodDirector = ^TMethodDirector;
TMethodDirector = packed record
PopEcx: Byte;
Jmp: Byte;
JmpOffset: Integer;
Call: Byte;
CallOffset: Integer;
case Integer of
0: (Next: PMethodDirector);
1: (Method: TMethod);
end;
PDirectorBlock = ^TDirectorBlock;
TDirectorBlock = packed record
Next: PDirectorBlock;
Directors: array[0..DirecotrCount] of TMethodDirector;
end;
function AllocMethodDirector(const Method: TMethod;
Proc: Pointer): Pointer; overload;
procedure FreeMethodDirector(Director: Pointer);
function AllocMethodDirector(Instance: TObject; MethAddr,
Proc: Pointer): Pointer; overload;
function SetMethodDirector(var Director:TMethodDirector;
const Method: TMethod; Proc: Pointer): Pointer; overload;
function SetMethodDirector(var Director: TMethodDirector; Instance: TObject;
MethAddr, Proc: Pointer): Pointer; overload;
procedure OneParamStdCall(Param: Integer); stdcall;
procedure TwoParamStdCall(P1, P2: Integer); stdcall;
procedure ThreeParamStdCall(P1, P2, P3: Integer); stdcall;
procedure FourParamStdCall(P1, P2, P3, P4: Integer); stdcall;
procedure FiveParamStdCall(P1, P2, P3, P4, P5: Integer); stdcall;
procedure SixParamStdCall(P1, P2, P3, P4, P5, P6: Integer); stdcall;
implementation
type
DWORD = Cardinal;
const
MEM_COMMIT = $1000;
PAGE_EXECUTE_READWRITE = $40;
function VirtualAlloc(addr: Pointer; size, alloc_type,
protect: DWORD): Pointer; stdcall; external 'kernel32' name 'VirtualAlloc';
function VirtualProtect(addr: Pointer; size, new_protect: DWORD;
var old_protect: DWORD): LongBool; stdcall;
external 'kernel32' name 'VirtualProtect';
var
DirectorFreeList: PMethodDirector;
DirectorBlockList: PDirectorBlock;
procedure OneParamStdCall(Param: Integer); stdcall;
asm
MOV EDX, Param
MOV EAX,[ECX].TMethod.Data
CALL [ECX].TMethod.Code
end;
procedure TwoParamStdCall(P1, P2: Integer); stdcall;
asm
PUSH EBX
MOV EBX, ECX
MOV EDX, P1
MOV ECX, P2
MOV EAX,[EBX].TMethod.Data
CALL [EBX].TMethod.Code
POP EBX
end;
procedure ThreeParamStdCall(P1, P2, P3: Integer); stdcall;
asm
PUSH EBX
MOV EBX, ECX
MOV EDX, P1
MOV ECX, P2
PUSH P3
MOV EAX,[EBX].TMethod.Data
CALL [EBX].TMethod.Code
POP EBX
end;
procedure FourParamStdCall(P1, P2, P3, P4: Integer); stdcall;
asm
PUSH EBX
MOV EBX, ECX
MOV EDX, P1
MOV ECX, P2
PUSH P3
PUSH P4
MOV EAX,[EBX].TMethod.Data
CALL [EBX].TMethod.Code
POP EBX
end;
procedure FiveParamStdCall(P1, P2, P3, P4, P5: Integer); stdcall;
asm
PUSH EBX
MOV EBX, ECX
MOV EDX, P1
MOV ECX, P2
PUSH P3
PUSH P4
PUSH P5
MOV EAX,[EBX].TMethod.Data
CALL [EBX].TMethod.Code
POP EBX
end;
procedure SixParamStdCall(P1, P2, P3, P4, P5, P6: Integer); stdcall;
asm
PUSH EBX
MOV EBX, ECX
MOV EDX, P1
MOV ECX, P2
PUSH P3
PUSH P4
PUSH P5
PUSH P6
MOV EAX,[EBX].TMethod.Data
CALL [EBX].TMethod.Code
POP EBX
end;
function AllocMethodDirector(Instance: TObject;
MethAddr, Proc: Pointer): Pointer;
const
PageSize = 4096;
var
Director: PMethodDirector;
Block: PDirectorBlock;
begin
if DirectorFreeList = nil then
begin
Block := VirtualAlloc(nil, PageSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
Block^.Next := DirectorBlockList;
Director := @Block^.Directors;
repeat
Director^.PopEcx := $59;
Director^.Jmp := $e9;
Director^.Call := $e8;
Director^.CallOffset := LongInt(@Director^.PopEcx) - LongInt(@Director^.Method);
Director^.Next := DirectorFreeList;
DirectorFreeList := Director;
Inc(Longint(Director), SizeOf(TMethodDirector));
until Longint(Director) - Longint(Block) >= SizeOf(TDirectorBlock);
DirectorBlockList := Block;
end;
Director := DirectorFreeList;
DirectorFreeList := Director^.Next;
Director^.JmpOffset := LongInt(Proc) - LongInt(@Director^.Call);
Director^.Method.Code := MethAddr;
Director^.Method.Data := Instance;
Result := @Director^.Call;
end;
function AllocMethodDirector(const Method: TMethod; Proc: Pointer): Pointer;
begin
Result := AllocMethodDirector(TObject(Method.Data), Method.Code, Proc);
end;
procedure FreeMethodDirector(Director: Pointer);
var
dummy: TMethodDirector;
begin
if Director <> nil then
begin
Dec(PAnsiChar(Director), LongInt(@dummy.Call) - LongInt(@dummy));
PMethodDirector(Director)^.Next := DirectorFreeList;
DirectorFreeList := Director;
end;
end;
function SetMethodDirector(var Director: TMethodDirector; const Method: TMethod;
Proc: Pointer): Pointer; overload;
begin
Result := SetMethodDirector(Director, TObject(Method.Data), Method.Code, Proc);
end;
function SetMethodDirector(var Director: TMethodDirector; Instance: TObject;
MethAddr, Proc: Pointer): Pointer; overload;
var
old_prot: DWORD;
begin
VirtualProtect(@Director, SizeOf(Director), PAGE_EXECUTE_READWRITE, old_prot);
with Director do
begin
PopEcx := $59;
Jmp := $e9;
JmpOffset := Longint(Proc) - LongInt(@Call);
Call := $e8;
CallOffset := LongInt(@PopEcx) - LongInt(@Method);
Method.Code := MethAddr;
Method.Data := Instance;
Result := @Call;
end;
end;
end.