写得很不错的文章,国人用英语写的,还有代码。
1 A" [0 `" q8 KHooking Direct3d1 A* P; U: Q; ?( W# H: N6 N2 W% O
By Jijun Wang* T% t' w( n' E, L# z
Introduction
# p% i6 T. V, [In the field of hooking technologies, hooking COM based applications is a big challenging because you can’t know their member functions’ real addresses previously. There are many papers on the Internet that talk about how to hook (somebody call it hijack) an application. However, seldom of them mention how to hook COM. Since I have seen some people asked questions about this topic and nobody answered their questions clearly, I decide to write this paper to share my experience with the guys who are interested in hooking COM. : \5 L/ v! x O: a6 m' l
' R9 A+ I# h* U+ {$ Q
In this paper, I use direct3d, which is widely used in current games, as an example of COM. At first I will briefly introduce the hooking technologies and explain why I select Detours to hook direct3d. Then more details and examples of Detours will be presented and I will discus virtual functions and find the way to hook direct3d. ' S5 o% ~3 s. J5 g
Hooking Technologies
) J) h/ L1 S4 F* ?The basic idea of hooking is injecting your code into a piece of code. When the target code executes your code will be invoked. To do this, you need to at first attach your code into the target process. And then inject your code into the target process’ code. The ways to attach your code into another thread or process include 9 V3 c) u8 L5 C2 K( n q; |3 V
1) Register your DLL to the registry table. ; Y) Q8 h3 F, ^- @' x5 w( y: c" Z
This method registers your DLL to the key: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs and the dll will be loaded when user32.dll initializes itself. It is a safe method. But it only works for the applications that use user32.dll. And it’s not convenient to activate/deactivate your DLL.
+ d' J0 _7 ]0 O% `) V0 R2) System-wide Windows Hooks& U9 V" H7 p6 R* W- c' W
This method use SetWindowsHookEx(),CallNextHookEx() and UnhookWindowsHookEx() winapis to monitor the system’s message traffic and attach your code into the processes that fit the hook filter. Since it is system-wide message monitor, the system’s performance will be significantly affected. . }/ ]( R1 {: q. k: v. J& _
3) Create Remote Thread
8 S. D6 q! ?5 Q5 B/ G, IThis method use CreateRemoteThread()to inject your DLL into another thread (the to-be-hooked thread). It’s a very effective method. But there is one thing we need to consider. You must make sure you have enough privileges to access the to-be-hooked thread. . a0 |7 i7 P% a" y
& a! J+ d3 S8 s9 W- WTo inject your code into another process’ code, we could use:* G" E4 ^3 r: b" N" Z& C
1) Proxy DLL
s: C3 W3 F3 z8 l0 ~- [" F- CThis method writes a new DLL to replace the to-be-hooked DLL. The new DLL must have the same name, exported functions and variables as the old one. For example, the GLtrace is a replacement of the opengl32.dll. It is used to trace OpenGL. Although you could use function forward to reduce the time spent in rewriting functions, it’s a tedious work in some cases. 0 E8 I2 q4 W) A" m* L
2) Altering of the IAT (Import Address Table)
* K% x& X. E7 r5 E6 ~/ TThis is a wildly used hooking method. Windows use IAT to find the functions’ addresses. Through changing the data of IAT, you could use your own function to replace the to-be-hooked function. The method is robust and easy to implement. However, it only works for statically linked API calls. And sometimes you may miss API calls since you must patch every module in the target application.1 s5 q9 B% q \' h! \5 t6 R# ^$ x
3) Code Overwriting
& i, s% L% r/ SThe basic idea is to overwrite the API’s binary code in memory. We could save and replace the first several codes of a function call to a JUMP code to jump to your codes and then go back to the original position and restore the saved codes. It’s very hard to implement. However, once you have mastered it, it’s really a good hooking method.* A/ D9 A1 Z* u! ?
" n S) @6 N; D- R2 a) WFor more details about hooking technologies you can read the wonderful paper “API hooking revealed” written by Ivo Ivanov and madshi’s discussion about hooking methods. There are some libraries or tools that wrap the hooking methods. "Detours" is a library developed by a Microsoft’s research term. It uses code overwriting technology. “madCodeHook” is a library built by madshi. It’s also based on code overwriting. “hookapi” is another tool can help you develop your hooking applications. Since Detours is free for research purpose and it’s stable and effective, I will use it to hook direct3d.4 T+ j$ r3 G3 N
Detours
% }, ^) w7 }0 V" T' O“Detours” is originally built for change a standalone system to distributed system. It can inject your code into another win32 function. Some utilities that attach your codes to another process are also provided in this library. The typical case you may need Detours is that you need to modify an application’s behavior without knowing its source code. For more details please visit Detours’ web site: http://www.research.microsoft.com/sn/detours/
+ I0 Y1 ?- h' l* E9 i( o2 ~: X! b/ m, x- ` [( `
As an example of how to use Detours, we will try to catch the function “SetTimer()”. Then change the elapse time to a big number. So for the applications use SetTimer(), you can steal time by hooking this function. The first step is to create TRAMPOLINE. It changes SetTimer()’s binary code and lets it jump to the Real_SetTimer() function.
% s9 C" X r' V1 l! {1 T
+ p0 Y9 E2 |' b" y5 E+ K% KDETOUR_TRAMPOLINE(! |6 `4 Z4 z" I+ j0 j
UINT WINAPI Real_SetTimer(HWND hWnd, // handle of window for timer messages
1 s5 i+ o+ w5 w/ D UINT nIDEvent, // timer identifier
. W7 N1 @- q! k UINT uElapse, // time-out value
; t. u- g' D c TIMERPROC lpTimerFunc), // address of timer procedure
% B" j" Z# I6 U8 y0 `8 u0 ZSetTimer);1 y$ m; }$ J$ y% O
6 D6 U* w5 W8 r. j" g
Then we write our own SetTimer function.
* t k8 m$ h( r0 M+ o, eUINT WINAPI Mine_SetTimer(
* o5 K+ S( ~. i4 a* l$ N9 v HWND hWnd, // handle of window for timer messages% K. k4 o- s% j( d; a8 W4 S6 r
UINT nIDEvent, // timer identifier
+ d1 a' |1 z8 Q* O& h! t1 p UINT uElapse, // time-out value$ O+ k$ e# x+ X
TIMERPROC lpTimerFunc // address of timer procedure
4 j/ w9 b' l+ ]' Q4 x* | )7 l. i: V b! o
{9 \; z0 v9 ~6 q6 i' \
uElapse=10* uElapse;
- d$ s, h% d1 o! M return Real_SetTimer(hWnd,nIDEvent,uElapse,lpTimerFunc);, X- U4 X3 B) _8 k1 _& o) g. L
}
) J/ X+ N+ h4 h M) Z1 K7 ^6 J2 G, Z* C5 H6 S5 u
Then write the functions to intercept the Real_SetTimer() function to your own Mine_SetTimer() function and the function that removes the interception.
# i: Z) s& `2 L ^2 \% s0 x; }; Y3 r3 a
BOOL TrampolineWith(VOID)' O5 G1 }; E) g. w. F T" `* }% J- l
{8 l( H# ~6 W5 M/ V6 c
DetourFunctionWithTrampoline((PBYTE)Real_SetTimer,
, S, j5 \! ^4 d+ w% T' @ (PBYTE)Mine_SetTimer);1 D/ P6 u: t7 j- W. H- P9 b
return TURE;6 `# a- w- Y- j+ s
}9 H4 L, U. ]- E, u
. W% H" I1 r, P# s1 lBOOL TrampolineRemove(VOID)
" c' m. `' f3 Z' W0 b3 Y, c+ G{8 ~6 ^! X4 _9 P/ X, b+ P5 g# m+ ?
DetourRemove((PBYTE)Real_SetTimer,
+ F7 Z6 S8 c; ]3 Y( _$ k; i1 K (PBYTE)Mine_SetTimer);
# O F7 ^& N2 a6 w3 ~7 `% X return TRUE;" [# D* e+ L7 r7 t2 u
}
) f$ L$ k- i1 p: n4 G1 n
n+ |0 F# K) ~" e0 G/ f$ D. Q4 E% [At last, we write the DLLmain function.
( I& s- ^- @: O6 b( O. _6 Y: R" p: Y/ ~& |
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD dwReason, PVOID lpReserved)4 j4 r6 G9 d4 m! R3 K) K# D
{
: w' `1 V. L! j1 c/ G switch (dwReason) {
3 n* }; I2 F: V; m9 U" q3 z9 G case DLL_PROCESS_ATTACH:
5 q& s }; g- [2 W5 N6 g( n return TrampolineWith();3 o/ }- q% `1 g6 C) F
case DLL_PROCESS_DETACH:
5 D+ {; s, R% t return TrampolineRemove();( {( G% f2 U( K- J' }
case DLL_THREAD_ATTACH:
+ y5 ]0 p: t3 L( V7 F1 {& n return TrampolineWith();
u5 M5 D9 s0 S9 [ case DLL_THREAD_DETACH:
2 X8 T" p4 ~9 ?: w% K. u return TrampolineRemove();
V [+ M- U% z" l3 Y- `9 l }9 a M7 `1 C% x- H l3 \
return TRUE;+ `8 a# l. @4 [
}
) {, p7 n" s% \4 V* S1 O' `1 g: [% P! B& k4 K0 f
By including the “detours.h” file and linking the “detours.lib”, you can build this DLL (let’s say it is myTest.dll). Then you can write your own application to attach the DLL into another application, or use the “withdll.exe”, which is included in Detours 1.5’s samples, to attach it. If you place the DLL file, withdll.exe and winmine.exe in the same directory, then play the Mine game use command “withdll.exe –d:myTest.dll winmine.exe”, you will get a very high score.+ I. g0 h3 p( o( u( V) y
/ E) h; }# F* O+ J
This is the simplest case of using Detours. If you want to hook a member function of a class, you need to write your own class and your member function. You can find an example of hooking member function in Detours 1.5’s samples. * f9 [7 X1 u3 f' E
. K7 w, P; }/ K, D) O) J
To hook direct3d, you may create a class, write your own member functions, for example CreateDevice(), Present(), and then detour direct3d’s member function to your own functions. Unfortunately this doesn’t work. The reason is that Directx is COM based, it uses virtual functions. When you detour the member functions, you will use the virtual function’s address not the real implement function’s address. So the key of hooking direct3d is finding out the real function address. To do this we need to know how virtual function works.
4 Z+ c( g* X9 I' TUnderstanding Virtual Functions and Hooking Direct3d
; t' H4 f, O A3 lVirtual function is used for reusable and polymorphic purposes. It is runtime bound to the real implement function. For more details about how virtual functions work, please read Shivesh V.’s paper “Virtual Functions and their implementation in C”. * _% d& U0 \1 G3 \2 B) K! R
) Y- d# O- m. Y0 Q( [+ e$ E9 VBasically, after you call the Direct3DCreate8() function (let’s assume we use directx8), you will get a pointer to the created IDirect3D8 object. In the pointer is the address of the vtable, a structure that contains all the member functions’ addresses. The address in the vtable is the real implement function’s address. So if we want to build a benchmark application to test the frame update rate, we could do it in the following steps:/ Z/ _$ m( P' J/ G4 o1 L% I
1) Catch Direct3DCreate8() to get the IDirect3D8 object9 t% J: ?9 t8 s, H0 H
2) Use the pointer of IDirect3D8 object, catch the CreateDevice() function to get the IDirect3DDevice8 object.
& [- p9 N6 X$ K" o1 Z4 B4 U1 \3) Use the pointer of IDirect3DDevice8 object, catch Present() function.+ ~% W$ b4 v* R
4) In the Present() function calculates the frame rate.5 r! c+ {' O5 G& ^5 E8 e* ^: h# w
7 Q( c6 D7 P8 n) `# u* x5 n
However, to catch the member function, there is a very important parameter we need to know. It’s the address data in the vtable that contain the to-be-hooked function’s address. There is no general way to know the offset in the vtable. For direct3d, you could use the d3d8.h file to figure out the offset. Or you can debug a direct3d application and get the offset by read the disassembly code. The following is a piece code of the directx 8.1 SDK’s Text3D sample code. The comments of the disassembly code are added by the author., i/ E1 A* N$ `) I
% m/ a$ N6 A* w5 J+ t873: // Create the device
& V5 G2 @+ f. }874: hr = m_pD3D->CreateDevice( m_dwAdapter, pDeviceInfo->DeviceType,* E- A" [) d; G% s1 W% H
875: m_hWndFocus, pModeInfo->dwBehavior, &m_d3dpp,; l, y2 M" G+ y5 ?
876: &m_pd3dDevice );) m- n4 u! ]" p7 B1 M4 Z6 s8 e" C
00403114 mov eax,dword ptr [this]4 ^( u! R: s- C% m" I4 D, t
00403117 add eax,2A4A8h, A% x4 s4 I0 R( s# \. f& i2 g
0040311C push eax // push the sixth parameter &m_pd3dDevice4 O( K! d/ x) Z' h# ]& x% x0 A
0040311D mov ecx,dword ptr [this]1 r4 f+ i% b! i) C3 U
00403120 add ecx,2A464h
& j4 ]- B9 c; E. c00403126 push ecx //push the fifth parameter &m_d3dp
" p2 J+ f9 A: i) d8 S00403127 mov edx,dword ptr [pModeInfo]
7 q1 u9 N6 P3 W: [* c* a! h0040312A mov eax,dword ptr [edx+0Ch]) U0 r: p8 b$ r2 K0 v
0040312D push eax //push the fourth parameter pModeInfo->dwBehavior
! v2 I! i! ^0 S9 ~; C0040312E mov ecx,dword ptr [this]
! F b. N% I9 A$ B# `" W00403131 mov edx,dword ptr [ecx+2A49Ch]7 B- f) h& { C9 B0 G# w, ~+ t7 e
00403137 push edx //push the third parameter m_hWndFocus9 ]# n* H1 O9 l
00403138 mov eax,dword ptr [pDeviceInfo]
" g, B: f( a8 p4 m1 y0040313B mov ecx,dword ptr [eax]' h# f+ q( |. ]
0040313D push ecx //push the second parameter pDeviceInfo->DeviceType
- A- Z' G6 c9 c6 I+ t( L0040313E mov edx,dword ptr [this]* E, e7 q. A: y. R. x/ Q
00403141 mov eax,dword ptr [edx+2A448h], q! o) E: U c. ]
00403147 push eax //push the first parameter m_dwAdapter* [% k, D% N; t$ e$ }
00403148 mov ecx,dword ptr [this]
* y8 A. U3 w* L8 ?0040314B mov edx,dword ptr [ecx+2A4A4h] //calculate the return address
4 ~# I5 E1 N& B/ L# e$ f1 t, m: V" m00403151 mov eax,dword ptr [this]8 x# }2 _% ^# F4 b- G3 m) C
00403154 mov ecx,dword ptr [eax+2A4A4h]
, K8 I( {7 C! G+ e. [9 d: H0040315A mov eax,dword ptr [ecx] //calculate the vtable address. It’s the value : o. ~$ U1 I/ [
//stored in ecx register.
! y; _. _4 u! ?: d0040315C push edx //push the return address4 L9 v7 C4 d4 P. N. C: Y
0040315D call dword ptr [eax+3Ch] //call the CreateDevice() function 7 }+ C; f L$ A# T
00403160 mov dword ptr ,eax
g4 T3 ^$ r" p( {% b7 m9 o0 c, ^0 i9 I7 }5 L
From the assembler code, it’s obvious that the offset is 0x3ch. You can do the same thing for the Present() function. And the offset happen is also 0x3ch. p/ l( h. m$ @- d
3 n: q! |) S8 ?3 T: iThe sample code can be found at http://usl.sis.pitt.edu/wjj/UTClient/direct3d8.zip. To compile it you need Directx 8.1 SDK. Please note that the method of display frame count is not every effective. If you remove the displaying code, you will find Detours has almost no effect to the frame rate.
" R# e7 e' \/ BReference:9 x2 N& g$ m# ]7 B4 b4 V
[1] “API hooking revealed”, Ivo Ivanov
b' Y: y7 g9 ]' Q. j[2] “madCodeHook”, madshi
+ D( n0 O! K2 f[3] "Detours", Galen Hunt and Doug Brubacher/ F$ X0 j- d# N( C
[4] “Virtual Functions and their implementation in C”, Shivesh V.9 s' d, q" R7 s1 k
[5] “Pointers to C++ Member Functions”, Michael D. Crawford |