My favorites
|
Sign in
easyhook-continuing-detours
EasyHook starts where Microsoft Detours ends. This project supports hooking of >native< entry points from within a fully managed environment like C# using Windows 2000 SP4 and later, including Windows XP x64, Windows Vista x64 and Windows Server 2008 x64.
Project Home
Downloads
Wiki
Issues
Source
Checkout
|
Browse
|
Changes
|
r20
Source path:
svn
/
trunk
/
EasyHook_Specific
/
ASM
/
HookSpecific_x64.asm
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
;Copyright (c) 2008 by Christoph Husse, SecurityRevolutions e.K.
;
;Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
;associated documentation files (the "Software"), to deal in the Software without restriction,
;including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
;and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
;subject to the following conditions:
;
;The above copyright notice and this permission notice shall be included in all copies or substantial
;portions of the Software.
;
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
;LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
;SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;
;Visit http://www.codeplex.com/easyhook for more information.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.CODE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Trampoline_ASM_x64
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; This method is highly optimized and executes within 78 nanoseconds
; including the intro, outro and return...
; "IsExecuted" has to be within the next code subpage to prevent the
; Self-Modifing-Code-Condition to apply which would reduce performance
; about 200 ns.
; Only for comparsion: The first proof of concept was unoptimized and
; did execute within 10000 nanoseconds... This optimized version just
; uses RIP relative addressing instead of register relative addressing,
; prevents the SMC condition and uses RIP relative jumps...
public Trampoline_ASM_x64
Trampoline_ASM_x64 PROC
NETIntro:
;void* NETEntry; // fixed 0 (0)
db 0
db 0
db 0
db 0
db 0
db 0
db 0
db 0
OldProc:
;BYTE* OldProc; // fixed 4 (8)
db 0
db 0
db 0
db 0
db 0
db 0
db 0
db 0
NewProc:
;BYTE* NewProc; // fixed 8 (16)
db 0
db 0
db 0
db 0
db 0
db 0
db 0
db 0
NETOutro:
;void* NETOutro; // fixed 12 (24)
db 0
db 0
db 0
db 0
db 0
db 0
db 0
db 0
IsExecutedPtr:
;size_t* IsExecutedPtr; // fixed 16 (32)
db 0
db 0
db 0
db 0
db 0
db 0
db 0
db 0
; ATTENTION: 64-Bit requires stack alignment (RSP) of 16 bytes!!
mov rax, rsp
push rcx ; save not sanitized registers...
push rdx
push r8
push r9
mov r8, rax ; InitialRSP for NETIntro()...
sub rsp, 4 * 16 ; space for SSE registers
movups [rsp + 3 * 16], xmm0
movups [rsp + 2 * 16], xmm1
movups [rsp + 1 * 16], xmm2
movups [rsp + 0 * 16], xmm3
sub rsp, 32; shadow space for method calls
lea rax, [IsExecutedPtr]
mov rax, [rax]
db 0F0h ; interlocked increment execution counter
inc qword ptr [rax]
; is a user handler available?
cmp qword ptr[NewProc], 0
db 3Eh ; branch usually taken
jne CALL_NET_ENTRY
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; call original method
lea rax, [IsExecutedPtr]
mov rax, [rax]
db 0F0h ; interlocked decrement execution counter
dec qword ptr [rax]
lea rax, [OldProc]
jmp TRAMPOLINE_EXIT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; call hook handler or original method...
CALL_NET_ENTRY:
; call NET intro
lea rcx, [IsExecutedPtr + 8] ; Hook handle (only a position hint)
mov rdx, qword ptr [rsp + 32 + 4 * 16 + 4 * 8] ; push return address
call qword ptr [NETIntro] ; Hook->NETIntro(Hook, RetAddr, InitialRSP);
; should call original method?
test rax, rax
db 3Eh ; branch usually taken
jne CALL_HOOK_HANDLER
; call original method
lea rax, [IsExecutedPtr]
mov rax, [rax]
db 0F0h ; interlocked decrement execution counter
dec qword ptr [rax]
lea rax, [OldProc]
jmp TRAMPOLINE_EXIT
CALL_HOOK_HANDLER:
; adjust return address
lea rax, [CALL_NET_OUTRO]
mov qword ptr [rsp + 32 + 4 * 16 + 4 * 8], rax
; call hook handler
lea rax, [NewProc]
jmp TRAMPOLINE_EXIT
CALL_NET_OUTRO: ; this is where the handler returns...
; call NET outro
push 0 ; space for return address
push rax
sub rsp, 32 + 16; shadow space for method calls and SSE registers
movups [rsp + 32], xmm0
lea rcx, [IsExecutedPtr + 8] ; Param 1: Hook handle hint
lea rdx, [rsp + 56] ; Param 2: Address of return address
call qword ptr [NETOutro] ; Hook->NETOutro(Hook);
lea rax, [IsExecutedPtr]
mov rax, [rax]
db 0F0h ; interlocked decrement execution counter
dec qword ptr [rax]
add rsp, 32 + 16
movups xmm0, [rsp - 16]
pop rax ; restore return value of user handler...
; finally return to saved return address - the caller of this trampoline...
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; generic outro for both cases...
TRAMPOLINE_EXIT:
add rsp, 32 + 16 * 4
movups xmm3, [rsp - 4 * 16]
movups xmm2, [rsp - 3 * 16]
movups xmm1, [rsp - 2 * 16]
movups xmm0, [rsp - 1 * 16]
pop r9
pop r8
pop rdx
pop rcx
jmp qword ptr[rax] ; ATTENTION: In case of hook handler we will return to CALL_NET_OUTRO, otherwise to the caller...
; outro signature, to automatically determine code size
db 78h
db 56h
db 34h
db 12h
Trampoline_ASM_x64 ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; HookInjectionCode_ASM_x64
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
public Injection_ASM_x64
Injection_ASM_x64 PROC
; no registers to save, because this is the thread main function
mov r14, rcx ; save parameter to non-volatile register
sub rsp, 40 ; space for register parameter stack, should be 32 bytes... no idea why it only works with 40
; call LoadLibraryW(Inject->EasyHookPath);
mov rcx, qword ptr [r14 + 8]
call qword ptr [r14 + 40] ; LoadLibraryW
mov r13, rax
test rax, rax
je HookInject_FAILURE_A
; call GetProcAddress(hModule, Inject->EntryPoint)
mov rdx, qword ptr [r14 + 24]
mov rcx, rax
call qword ptr [r14 + 56] ; GetProcAddress
test rax, rax
je HookInject_FAILURE_B
; call EasyHookEntry(Inject);
mov rcx, r14
call rax
mov r15, rax ; save error code to non-volatile register
; call FreeLibrary(hEasyHookLib)
mov rcx, r13
call qword ptr [r14 + 48] ; FreeLibrary
test rax, rax
je HookInject_FAILURE_C
jmp HookInject_EXIT
HookInject_FAILURE_A:
call qword ptr [r14 + 88] ; GetLastError
or rax, 40000000h
jmp HookInject_FAILURE_E
HookInject_FAILURE_B:
call qword ptr [r14 + 88] ; GetLastError
or rax, 10000000h
jmp HookInject_FAILURE_E
HookInject_FAILURE_C:
call qword ptr [r14 + 88] ; GetLastError
or rax, 30000000h
jmp HookInject_FAILURE_E
HookInject_FAILURE_E:
mov r15, rax ; save error value
HookInject_EXIT:
; call VirtualProtect(Outro, 8, PAGE_EXECUTE_READWRITE, &OldProtect)
lea rbx, qword ptr [rsp + 8] ; writes into register parameter stack
mov r9, rbx
mov r8, 40h
mov rdx, 8
mov rcx, rbx
call qword ptr [r14 + 72] ; VirtualProtect
test rax, rax
jne HookInject_EXECUTABLE
; failed to make stack executable
call qword ptr [r14 + 88] ; GetLastError
or rax, 01000000h
mov rcx, rax
call qword ptr [r14 + 80] ; ExitThread
HookInject_EXECUTABLE:
; save outro to executable stack
mov rbx, [r14 + 64] ; VirtualFree()
mov rbp, [r14 + 80] ; ExitThread()
mov rax, 000D5FFCF8B49D3FFh
; call rbx
; mov rcx, r15
; call rbp
mov qword ptr [rsp + 8], rax
; save params for VirtualFree(Inject->RemoteEntryPoint, 0, MEM_RELEASE);
mov r8, 8000h
mov rdx, 0h
mov rcx, qword ptr [r14 + 32]
lea rax, qword ptr [rsp + 8]
sub rsp, 40
jmp rax
; outro signature, to automatically determine code size
db 78h
db 56h
db 34h
db 12h
Injection_ASM_x64 ENDP
END
Show details
Hide details
Change log
r9
by LooneyL...@gmx.biz on Jul 10, 2008
Diff
[No log message]
Go to:
/trunk/EasyHook_Specific
/trunk/EasyHook_Specific/ASM
...pecific/ASM/HookSpecific_x64.asm
...pecific/ASM/HookSpecific_x86.asm
...asyHook_Specific/ASM/ILD_x64.asm
...asyHook_Specific/ASM/ILD_x86.asm
/trunk/EasyHook_Specific/AUX_ULIB.H
...syHook_Specific/AUX_ULIB_x64.LIB
...syHook_Specific/AUX_ULIB_x86.LIB
...k/EasyHook_Specific/Debugger.cpp
/trunk/EasyHook_Specific/Debugger.h
...pecific/EasyHook_Specific.vcproj
...vcproj.LYNN-Q7NRAEQ9SB.Lynn.user
...pecific.vcproj.Lynn-PC.Lynn.user
...nk/EasyHook_Specific/FixedList.h
/trunk/EasyHook_Specific/IPC.cpp
/trunk/EasyHook_Specific/LocalHook
.../LocalHook/HookAccessControl.cpp
...ic/LocalHook/HookAccessControl.h
...ic/LocalHook/HookRuntimeInfo.cpp
...ific/LocalHook/HookRuntimeInfo.h
...alHook/HookRuntimeInfo_NOTLS.cpp
...k_Specific/LocalHook/LHUtils.cpp
...Specific/LocalHook/LocalHook.cpp
...k_Specific/LocalHook/LocalHook.h
...asyHook_Specific/LocalHook/TLS.h
...ecific/LocalHook/UpdateFrame.cpp
/trunk/EasyHook_Specific/RemoteHook
...fic/RemoteHook/HelperProgram.cpp
...fic/RemoteHook/HelperService.cpp
...Specific/RemoteHook/RHNative.cpp
...Specific/RemoteHook/RemoteHook.h
...fic/RemoteHook/RemoteHooking.cpp
/trunk/EasyHook_Specific/Stdafx.h
...nk/EasyHook_Specific/Utilities.h
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 8290 bytes, 324 lines
View raw file
Hosted by