Repository navigation
Expand file tree
/
Copy pathcommon.cpp
More file actions
95 lines (85 loc) · 2.32 KB
/
Copy pathcommon.cpp
File metadata and controls
95 lines (85 loc) · 2.32 KB
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
#define UNICODE
#include <stdio.h>
#include <string>
#include "common.h"
extern wchar_t dll_log[MAX_PATH];
__declspec(dllexport) void log_(const wchar_t *format, ...)
{
FILE *file = _wfopen(dll_log, L"a+, ccs=UTF-8");
if (file) {
va_list params;
va_start(params, format);
vfwprintf(file, format, params);
va_end(params);
fclose(file);
}
}
__declspec(dllexport) void logu_(const char *format, ...)
{
FILE *file = _wfopen(dll_log, L"a+");
if (file) {
va_list params;
va_start(params, format);
vfprintf(file, format, params);
va_end(params);
fclose(file);
}
}
__declspec(dllexport) void start_log_(const wchar_t *format, ...)
{
FILE *file = _wfopen(dll_log, L"wt, ccs=UTF-8");
if (file) {
va_list params;
va_start(params, format);
vfwprintf(file, format, params);
va_end(params);
fclose(file);
}
}
DWORD get_target_addr(DWORD call_location)
{
if (call_location) {
BYTE* bptr = (BYTE*)call_location;
DWORD protection = 0;
DWORD newProtection = PAGE_EXECUTE_READWRITE;
if (VirtualProtect(bptr, 8, newProtection, &protection)) {
// get original target
DWORD* ptr = (DWORD*)(call_location + 1);
return (DWORD)(ptr[0] + call_location + 5);
}
}
return 0;
}
void hook_call_point(
DWORD addr, void* func, int codeShift, int numNops, bool addRetn)
{
DWORD target = (DWORD)func + codeShift;
if (addr && target)
{
BYTE* bptr = (BYTE*)addr;
DWORD protection = 0;
DWORD newProtection = PAGE_EXECUTE_READWRITE;
if (VirtualProtect(bptr, 16, newProtection, &protection)) {
bptr[0] = 0xe8;
DWORD* ptr = (DWORD*)(addr + 1);
ptr[0] = target - (DWORD)(addr + 5);
// padding with NOPs
for (int i=0; i<numNops; i++) bptr[5+i] = 0x90;
if (addRetn)
bptr[5+numNops]=0xc3;
log_(L"Function (%08x) HOOKED at address (%08x)\n", target, addr);
}
}
}
BYTE* find_code_frag(BYTE *base, DWORD max_offset, BYTE *frag, size_t frag_len)
{
BYTE *p = base;
BYTE *max_p = base + max_offset;
while (p < max_p && memcmp(p, frag, frag_len)!=0) {
p += 1;
}
if (p < max_p) {
return p;
}
return NULL;
}