forked from Arkshine/Orpheu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.cpp
619 lines (486 loc) · 12.7 KB
/
function.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#include <function.h>
#include <configManager.h>
#include <cbase.h>
#include <am-string.h>
#include <global.h>
#include <functionMethodHooks.h>
unsigned char PreJumpBase[] ={ 0x66, 0xc7, 0x5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0xe9, 0x00, 0x00, 0x00, 0x00 };
unsigned int PreJumpSize = sizeof PreJumpBase;
unsigned int PreJumpIDAddressPosition = 3;
unsigned int PreJumpIDValuePosition = 7;
unsigned int PreJumpIDHookAddressPosition = 10;
unsigned char *PreJumps[0xFFFF];
unsigned short int ID;
long Hook(long argument, ...)
{
Function* function = Global::FunctionManagerObj->getFunction(ID);
long* argumentsHolder = function->getHookArgumentsHolder();
va_list arguments;
va_start(arguments, argument);
for (size_t i = 0; i < function->getArgumentsCount(); ++i)
{
argumentsHolder[i] = argument;
argument = va_arg(arguments, long);
}
va_end(arguments);
return function->hook();
}
#if defined WIN32
long HookMethod(long object, long argument, ...)
{
Function* function = Global::FunctionManagerObj->getFunction(ID);
long* argumentsHolder = function->getHookArgumentsHolder();
argumentsHolder[0] = (long)object;
va_list arguments;
va_start(arguments, argument);
for (unsigned int i=1; i < function->getArgumentsCount(); ++i)
{
argumentsHolder[i] = argument;
argument = va_arg(arguments, long);
}
va_end(arguments);
return function->hook();
}
#endif
unsigned char Function::patch[] ={ 0xE9, 0x00, 0x00, 0x00, 0x00 };
int Function::patchSize = sizeof(patch);
unsigned short int* Function::patchFunctionIDAddress = (unsigned short int*) &patch[2];
long* Function::patchFunctionJumpAddress = (long*)&patch[1];
Function::Function(void* address, TypeHandler** argumentsHandlers, unsigned int argumentsCount, TypeHandler* returnHandler, ke::AString library, bool isMethod)
{
this->address = address;
this->isMethod = isMethod;
this->argumentsHandlers = argumentsHandlers;
this->argumentsCount = argumentsCount;
this->returnHandler = returnHandler;
this->library = library;
ifReturnByRefParamsCount = returnHandler ? returnHandler->getIfByRefParamsCount() : 0;
normalArguments = new long[argumentsCount];
espDislocation = sizeof(long) * argumentsCount;
espDislocationAddHook = espDislocation + sizeof(long) + sizeof(char*) + sizeof(AMX*);
espDislocationCallForward = espDislocation + sizeof(long);
#if defined WIN32
passObject = (isMethod) ? &Function::passObjectMethod : &Function::passObjectNonMethod;
if (isMethod)
{
espDislocationCall = 0;
}
else
{
espDislocationCall = espDislocation;
}
#endif
argumentsByRef = new TypeHandler*[argumentsCount];
argumentsByRefPosition = new unsigned int[argumentsCount];
argumentsByRefCount = 0;
parameters = new ForwardParam[argumentsCount];
argumentsToAmx = new cell[argumentsCount];
for (size_t i = 0; i < argumentsCount; ++i)
{
parameters[i] = argumentsHandlers[i]->getParamType();
if (argumentsHandlers[i]->isPassedByRef())
{
argumentsByRef[argumentsByRefCount] = argumentsHandlers[i];
argumentsByRefPosition[argumentsByRefCount] = i;
argumentsByRefCount++;
}
}
originalBytes = (unsigned char*) new unsigned char[patchSize];
patchedBytes = (unsigned char*) new unsigned char[patchSize];
argumentsPassLoopStart = argumentsCount - 1;
#if defined WIN32
argumentsPassLoopEnd = (int)isMethod;
#else
argumentsPassLoopEnd = 0;
#endif
isPatched = false;
this->currentHookID[OrpheuHookPre] = this->currentHookID[OrpheuHookPost] = 0;
hooks[OrpheuHookPre] = new HooksDataMap;
hooks[OrpheuHookPost] = new HooksDataMap;
hooks[OrpheuHookPre]->init();
hooks[OrpheuHookPost]->init();
shouldCallHooks = true;
}
void Function::setID(unsigned short int id)
{
this->id = id;
this->preparePatch();
}
void Function::preparePatch()
{
unsigned char* preJump = PreJumps[id] = (unsigned char*)malloc(PreJumpSize);
Memory::ChangeMemoryProtection(preJump, PreJumpSize, PAGE_EXECUTE_READWRITE);
memcpy((void*)preJump, PreJumpBase, PreJumpSize);
*((long*)(&preJump[PreJumpIDAddressPosition])) = (long)&ID;
*((unsigned short int*)(&preJump[PreJumpIDValuePosition])) = id;
#if defined WIN32
if (isMethod)
*((long*)(&preJump[PreJumpIDHookAddressPosition])) = (char*)HooksMethod[argumentsCount - 1] - (char*)preJump - PreJumpSize;
else
#endif
*((long*)(&preJump[PreJumpIDHookAddressPosition])) = (char*)&Hook - (char*)preJump - PreJumpSize;
patchedBytes[0] = 0xE9;
*((long*)(&patchedBytes[1])) = (char*)preJump - (char*)address - 5;
memcpy((void*)originalBytes, address, patchSize);
}
void Function::doPatch()
{
if (!isPatched)
{
if (Memory::ChangeMemoryProtection(address, patchSize, PAGE_EXECUTE_READWRITE))
{
isPatched = true;
memcpy(address, (void*)patchedBytes, patchSize);
}
else
{
Global::ConfigManagerObj->ModuleConfig.append(ke::AString("Memory patching failed\n"));
}
}
}
void Function::undoPatch()
{
if (isPatched)
{
if (Memory::ChangeMemoryProtection(address, patchSize, PAGE_EXECUTE_READWRITE))
{
isPatched = false;
memcpy(address, (void*)originalBytes, this->patchSize);
}
else
{
Global::ConfigManagerObj->ModuleConfig.append(ke::AString("Memory patching failed\n"));
}
}
}
cell Function::call(AMX* amx, cell* params)
{
for (size_t i = 0; i < argumentsCount; ++i)
{
normalArguments[i] = (long)argumentsHandlers[i]->convertFromAmx(amx, params[i]);
}
#if defined WIN32
int end = (int)isMethod;
#else
int end = 0;
#endif
for (int i = argumentsCount - 1; i >= end; i--)
{
#if defined WIN32
long argument = normalArguments[i];
__asm push argument;
#else
asm volatile ("push %0"::"m"(normalArguments[i]));
#endif
}
long returnValue;
#if defined WIN32
void* address = this->address;
(this->*(passObject))();
_asm
{
call address;
mov returnValue, eax;
};
#else
asm ("call *%0"::"m"(address));
asm ("mov %%eax , %0":"=r"(returnValue));
#endif
#if defined WIN32
long espDislocation = this->espDislocationCall;
_asm add esp, espDislocation
#else
asm ("add %0, %%esp"::"m"(espDislocation));
#endif
cell value = 0;
if (returnHandler)
{
returnHandler->convertToAmx(value, returnValue, Call);
returnHandler->convertToAmxByRef(amx, ¶ms[argumentsCount], returnValue);
}
for (size_t i = 0; i < argumentsByRefCount; ++i)
{
unsigned int argumentPositionInFunction = argumentsByRefPosition[i];
argumentsByRef[i]->convertToAmxByRef(amx, ¶ms[argumentPositionInFunction], normalArguments[argumentPositionInFunction]);
}
return value;
}
unsigned int& Function::getIfReturnByRefParamsCount()
{
return ifReturnByRefParamsCount;
}
unsigned int& Function::getArgumentsCount()
{
return argumentsCount;
}
long Function::callOriginal()
{
for (int i=argumentsPassLoopStart; i >= argumentsPassLoopEnd; i--)
{
#if defined WIN32
long argument = normalArguments[i];
__asm push argument;
#else
asm volatile ("push %0"::"m"(normalArguments[i]));
#endif
}
long returnValue;
#if defined WIN32
void* address = this->address;
(this->*(passObject))();
_asm
{
call address;
mov returnValue, eax;
};
#else
asm ("call *%0"::"m"(address));
asm ("mov %%eax , %0":"=r"(returnValue));
#endif
#if defined WIN32
long espDislocation = this->espDislocationCall;
_asm add esp, espDislocation
#else
asm ("add %0, %%esp"::"m"(espDislocation));
#endif
return returnValue;
}
long Function::hook()
{
hookPhase = OrpheuHookPre;
hookReturnStatus = OrpheuIgnored;
Function* functionBeingHookedBefore = Global::FunctionBeingHooked;
Global::FunctionBeingHooked = this;
hookReturnValue = 0;
bool shouldCallHooks = this->shouldCallHooks;
this->shouldCallHooks = true;
convertArgumentsToAmx();
if (shouldCallHooks)
hookReturnStatus = callHooks(OrpheuHookPre);
if (hookReturnStatus < OrpheuSupercede)
{
this->undoPatch();
long returnValue = callOriginal();
this->doPatch();
hookPhase = OrpheuHookPost;
if (hookReturnStatus != OrpheuOverride)
{
hookReturnValue = returnValue;
if (returnHandler && !getIfReturnByRefParamsCount())
{
returnHandler->convertToAmx(hookReturnValueConverted, hookReturnValue, CallHooked);
}
}
}
if (shouldCallHooks)
hookReturnStatus = callHooks(OrpheuHookPost);
Global::FunctionBeingHooked = functionBeingHookedBefore;
return hookReturnValue;
}
long Function::addHook(AMX* amx, const char* functionName, OrpheuHookPhase phase)
{
doPatch();
long hookID = this->currentHookID[OrpheuHookPre]++;
long forward;
#ifdef WIN32
unsigned int start = argumentsCount;
long addressStart = (long)¶meters[0];
long espDislocation = this->espDislocationAddHook;
_asm
{
mov eax, addressStart
mov ebx, start
push FP_DONE
loopStartAdd :
cmp ebx, 0
je loopEndAdd
dec ebx
mov ecx, [eax + ebx * 4]
push ecx
jmp loopStartAdd
loopEndAdd :
push functionName
push amx
call MF_RegisterSPForwardByName
mov forward, eax
add esp, espDislocation
}
#else
asm volatile(
"mov %1, %%eax;"
"mov %2, %%ebx;"
"pushl $-1;"
"loopStart:"
"cmp $0, %%ebx;"
"je loopEnd;"
"dec %%ebx;"
"mov (%%eax,%%ebx,4),%%ecx;"
"pushl %%ecx;"
"jmp loopStart;"
"loopEnd:"
"pushl %3;"
"pushl %4;"
"call *%5;"
"mov %%eax, %0;"
"add %6, %%esp;"
:"=r"(forward):"m"(parameters),"m"(argumentsCount),"r"(functionName),"r"(amx),"m"(MF_RegisterSPForwardByName),"m"(espDislocationAddHook):"eax","ebx","ecx");
#endif
HooksDataMap::Insert i = hooks[phase]->findForAdd(hookID);
if (!i.found())
{
if (hooks[phase]->add(i))
{
i->key = hookID;
}
}
i->value = forward;
return hookID;
}
void Function::removeAllHooks()
{
for (int i=0; i <= 1; ++i)
{
for (Function::HooksDataMap::iterator iter = hooks[i]->iter(); !iter.empty(); iter.next())
{
MF_UnregisterSPForward(iter->value);
iter.erase();
}
}
undoPatch();
this->currentHookID[0] = this->currentHookID[1] = 0;
}
void Function::removeHook(OrpheuHookPhase phase, long functionHookPhaseID)
{
Function::HooksDataMap::Result r = hooks[phase]->find(functionHookPhaseID);
if (r.found())
{
MF_UnregisterSPForward(r->value);
hooks[phase]->remove(r);
if (!hooks[phase]->elements())
{
this->currentHookID[phase] = 0;
if (!hooks[!phase]->elements())
{
undoPatch();
}
}
}
}
void Function::convertArgumentsToAmx()
{
for (size_t i = 0; i < argumentsCount; ++i)
{
argumentsHandlers[i]->convertToAmx(argumentsToAmx[i], normalArguments[i], Pass);
}
}
OrpheuHookReturn Function::callHooks(OrpheuHookPhase phase)
{
OrpheuHookReturn hookReturnStatus = OrpheuIgnored;
for (HooksDataMap::iterator iter = hooks[phase]->iter(); !iter.empty(); iter.next())
{
long forward = iter->value;
OrpheuHookReturn newHookReturnStatus = callForward(forward);
Global::FunctionBeingHooked = this;
if (newHookReturnStatus > hookReturnStatus)
{
hookReturnStatus = newHookReturnStatus;
if (hookReturnStatus == OrpheuSupercede)
{
break;
}
}
}
return hookReturnStatus;
}
OrpheuHookReturn Function::callForward(long forward)
{
OrpheuHookReturn hookReturnStatus;
#if defined WIN32
unsigned int start = argumentsCount;
long addressStart = (long)&argumentsToAmx[0];
long espDislocation = this->espDislocationCallForward;
_asm
{
mov eax, addressStart
mov ebx, start
loopStart :
cmp ebx, 0
je loopEnd
dec ebx
mov ecx, [eax + ebx * 4]
push ecx
jmp loopStart
loopEnd :
push forward
call MF_ExecuteForward
mov hookReturnStatus, eax
add esp, espDislocation
}
#else
asm volatile (
"mov %1, %%eax;"
"mov %2, %%ebx;"
"0:"
"cmp $0, %%ebx;"
"je 1f;"
"dec %%ebx;"
"mov (%%eax,%%ebx,4),%%ecx;"
"pushl %%ecx;"
"jmp 0b;"
"1:"
"pushl %3;"
"call *%4;"
"mov %%eax, %0;"
"add %5, %%esp;"
:"=r"(hookReturnStatus):"m"(argumentsToAmx),"m"(argumentsCount),"r"(forward),"m"(MF_ExecuteForward),"r"(espDislocationCallForward):"eax","ebx","ecx");
#endif
return hookReturnStatus;
}
cell Function::getReturnToAmx(AMX* amx, cell* params)
{
if (this->getIfReturnByRefParamsCount())
{
returnHandler->convertToAmxByRef(amx, params, hookReturnValue);
}
else
{
return hookReturnValueConverted;
}
return 0;
}
void Function::setReturnFromAmx(AMX* amx, cell* params)
{
hookReturnValue = (long)returnHandler->convertFromAmx(amx, *params);
}
bool Function::setParamFromAmx(AMX* amx, cell* params)
{
unsigned int argumentID = params[0] - 1;
if (argumentID < argumentsCount)
{
normalArguments[argumentID] = (long)argumentsHandlers[argumentID]->convertFromAmx(amx, params[1]);
return true;
}
return false;
}
TypeHandler* Function::getArgumentHandler(unsigned int argumentID)
{
if (argumentID < argumentsCount)
{
return argumentsHandlers[argumentID];
}
return NULL;
}
long Function::getArgumentValue(unsigned int argumentID)
{
if (argumentID < argumentsCount)
{
return normalArguments[argumentID];
}
return 0;
}
Function::~Function()
{
delete[] originalBytes;
delete[] patchedBytes;
delete[] normalArguments;
}