-
Notifications
You must be signed in to change notification settings - Fork 46
/
CDetour.cpp
321 lines (250 loc) · 7.53 KB
/
CDetour.cpp
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
/*
PROJECT: mod_sa
LICENSE: See LICENSE in the top level directory
COPYRIGHT: Copyright we_sux, BlastHack
mod_sa is available from https://github.com/BlastHackNet/mod_s0beit_sa/
mod_sa is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
mod_sa is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with mod_sa. If not, see <http://www.gnu.org/licenses/>.
*/
#include "main.h"
void *CDetour::memcpy_s ( void *pvAddress, const void *pvBuffer, size_t stLen )
{
MEMORY_BASIC_INFORMATION mbi;
VirtualQuery( pvAddress, &mbi, sizeof(mbi) );
VirtualProtect( mbi.BaseAddress, mbi.RegionSize, PAGE_EXECUTE_READWRITE, &mbi.Protect );
void *pvRetn = memcpy( pvAddress, pvBuffer, stLen );
VirtualProtect( mbi.BaseAddress, mbi.RegionSize, mbi.Protect, &mbi.Protect );
FlushInstructionCache( GetCurrentProcess(), pvAddress, stLen );
return pvRetn;
}
void *CDetour::Create ( BYTE *orig, const BYTE *det, int iPatchType, int len )
{
BYTE *jmp = NULL;
int iMinLen = 0;
if ( !(iMinLen = GetDetourLen(iPatchType)) )
return 0;
if ( len != 0 && len < iMinLen )
return 0;
// Try and find the end of the instruction automatically
if ( len == 0 )
{
len = GetDetourLenAuto( orig, iMinLen );
if ( len < iMinLen )
return 0;
}
if ( !Detour(jmp, orig, det, iPatchType, len) )
return 0;
return jmp - len;
}
void *CDetour::Create ( char *dllName, char *apiName, const BYTE *det, int iPatchType, int len )
{
BYTE *jmp = NULL;
BYTE *orig = NULL;
int iMinLen = 0;
if ( !(iMinLen = GetDetourLen(iPatchType)) )
return 0;
if ( len != 0 && len < iMinLen )
return 0;
// Get the API address
m_hModule = GetModuleHandle( dllName );
m_dwAddress = ( DWORD ) GetProcAddress( m_hModule, apiName );
if ( !m_dwAddress || !det )
return 0;
orig = (BYTE *)m_dwAddress;
// Try and find the end of the instruction automatically
if ( len == 0 )
{
len = GetDetourLenAuto( orig, iMinLen );
if ( len < iMinLen )
return 0;
}
if ( !Detour(jmp, orig, det, iPatchType, len) )
return 0;
return jmp - len;
}
bool CDetour::Detour ( BYTE * &jmp, BYTE * &orig, const BYTE * &det, int iPatchType, int len )
{
DWORD dwBack = 0;
int i = 0;
BYTE *pPatchBuf = NULL;
// Allocate space for the jump
jmp = (BYTE *)malloc( len + 5 );
// Force page protection flags to read|write
MEMORY_BASIC_INFORMATION mbi;
VirtualQuery( (void *)orig, &mbi, sizeof(mbi) );
VirtualProtect( mbi.BaseAddress, mbi.RegionSize, PAGE_READWRITE, &mbi.Protect );
// Copy the overwritten opcodes at the original to the malloced space
memcpy( jmp, orig, len );
// Increment to the end of the opcodes at the malloced space
jmp += len;
// Place a jump back to the original at this point
jmp[0] = 0xE9;
*( DWORD * ) ( jmp + 1 ) = ( DWORD ) ( orig + len - jmp ) - 5;
// Generate a random opcode
int iTmpRnd = ( rand() * 0xFF ) + rand();
BYTE bTmpRnd = ( BYTE ) iTmpRnd;
// Place a jump at the original to the detour function
pPatchBuf = new BYTE[len];
// Pad out the bytes with NOPs so we don't have ends of intructions
memset( pPatchBuf, 0x90, len );
// Write the opcodes to the buffer according to patch type
switch ( iPatchType )
{
case DETOUR_TYPE_JMP:
pPatchBuf[0] = '\xE9';
*(DWORD *) &pPatchBuf[1] = ( DWORD ) ( det - orig ) - 5;
break;
case DETOUR_TYPE_PUSH_RET:
pPatchBuf[0] = '\x68';
*(DWORD *) &pPatchBuf[1] = ( DWORD ) det;
pPatchBuf[5] = '\xC3';
break;
case DETOUR_TYPE_PUSH_FUNC:
pPatchBuf[0] = '\x68';
*(DWORD *) &pPatchBuf[1] = ( DWORD ) det;
break;
case DETOUR_TYPE_CALL_FUNC:
pPatchBuf[0] = '\xE8';
*(DWORD *) &pPatchBuf[1] = ( DWORD ) ( det - orig ) - 5;
break;
default:
return false;
}
// Write the detour
for ( i = 0; i < len; i++ )
orig[i] = pPatchBuf[i];
// Put the old page protection flags back
VirtualProtect( mbi.BaseAddress, mbi.RegionSize, mbi.Protect, &mbi.Protect );
FlushInstructionCache( GetCurrentProcess(), orig, len );
return true;
}
bool CDetour::Remove ( BYTE *orig, BYTE *jmp, int iPatchType, int len )
{
int iMinLen = 0;
DWORD dwBack = 0;
if ( !(iMinLen = GetDetourLen(iPatchType)) )
return false;
if ( len != 0 && len < iMinLen )
return false;
// Try and find the end of the instruction automatically
if ( len == 0 )
{
len = GetDetourLenAuto( jmp, iMinLen );
if ( len == 0 )
len = GetDetourLen( iPatchType );
if ( len == 0 || iMinLen == 0 )
return false;
if ( len < iMinLen )
return false;
}
// Write the bytes @ the jmp back to the orig
MEMORY_BASIC_INFORMATION mbi;
VirtualQuery( (void *)orig, &mbi, sizeof(mbi) );
VirtualProtect( mbi.BaseAddress, mbi.RegionSize, PAGE_EXECUTE_READWRITE, &mbi.Protect );
memcpy( orig, jmp, len );
VirtualProtect( mbi.BaseAddress, mbi.RegionSize, mbi.Protect, &mbi.Protect );
FlushInstructionCache( GetCurrentProcess(), (void *)orig, len );
return true;
}
bool CDetour::RestoreFunction ( BYTE *func, int len )
{
MEMORY_BASIC_INFORMATION mbi;
bool bRet = false;
VirtualQuery( (void *)func, &mbi, sizeof(mbi) );
VirtualProtect( mbi.BaseAddress, mbi.RegionSize, PAGE_EXECUTE_READWRITE, &mbi.Protect );
memcpy( (void *)func, (void *)bBackup, len );
if ( *(BYTE *)func == (BYTE) bBackup[0] )
{
bRet = true;
}
else
{
bRet = false;
}
VirtualProtect( mbi.BaseAddress, mbi.RegionSize, mbi.Protect, &mbi.Protect );
FlushInstructionCache( GetCurrentProcess(), (void *)func, len );
return bRet;
}
bool CDetour::BackupFunction ( BYTE *func, int len )
{
MEMORY_BASIC_INFORMATION mbi;
bool bRet = false;
VirtualQuery( (void *)func, &mbi, sizeof(mbi) );
VirtualProtect( mbi.BaseAddress, mbi.RegionSize, PAGE_EXECUTE_READWRITE, &mbi.Protect );
memcpy( (void *)bBackup, (void *)func, len );
if ( (BYTE) bBackup[0] == * (BYTE *)func )
{
bRet = true;
}
else
{
bRet = false;
}
VirtualProtect( mbi.BaseAddress, mbi.RegionSize, mbi.Protect, &mbi.Protect );
FlushInstructionCache( GetCurrentProcess(), (void *)func, len );
return bRet;
}
bool CDetour::Remove ( char *dllName, char *apiName, BYTE *jmp, int iPatchType, int len )
{
DWORD dwBack = 0;
BYTE *orig = NULL;
int iMinLen = 0;
// Get the API address
m_hModule = GetModuleHandle( dllName );
m_dwAddress = ( DWORD ) GetProcAddress( m_hModule, apiName );
if ( !m_dwAddress || !jmp )
return false;
orig = (BYTE *)m_dwAddress;
if ( !(iMinLen = GetDetourLen(iPatchType)) )
return false;
if ( len != 0 && len < iMinLen )
return false;
// Try and find the end of the instruction automatically
if ( len == 0 )
{
len = GetDetourLenAuto( jmp, iMinLen );
if ( len < iMinLen )
return 0;
}
// Write the bytes @ the jmp back to the orig
VirtualProtect( orig, len, PAGE_READWRITE, &dwBack );
memcpy( orig, jmp, len );
VirtualProtect( orig, len, dwBack, &dwBack );
return true;
}
int CDetour::GetDetourLen ( int iPatchType )
{
switch ( iPatchType )
{
case DETOUR_TYPE_JMP:
case DETOUR_TYPE_PUSH_FUNC:
case DETOUR_TYPE_CALL_FUNC:
return 5;
case DETOUR_TYPE_PUSH_RET:
return 6;
default:
return 0;
}
}
int CDetour::GetDetourLenAuto ( BYTE * &orig, int iMinLen )
{
int tmpLen = 0;
BYTE *pCurOp = orig;
while ( tmpLen < iMinLen )
{
int i = oplen( pCurOp );
if ( i == 0 || i == -1 )
return false;
tmpLen += i;
pCurOp += i;
}
return tmpLen;
}