Repository navigation
Expand file tree
/
Copy pathcommon.cpp
More file actions
202 lines (184 loc) · 4.99 KB
/
Copy pathcommon.cpp
File metadata and controls
202 lines (184 loc) · 4.99 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
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
#define UNICODE
#include <stdio.h>
#include <string>
#include "common.h"
#include "utf8.h"
#include "kmp.h"
extern wchar_t dll_log[MAX_PATH];
extern wchar_t app_log[MAX_PATH];
static FILE *file = NULL;
static FILE *file2 = NULL;
static CRITICAL_SECTION _log_cs;
#define LOG_BUF_LEN 0x200
void string_strip_quotes(wstring& s)
{
static const wchar_t* chars = L" \t\n\r\"'";
int e = s.find_last_not_of(chars);
s.erase(e + 1);
int b = s.find_first_not_of(chars);
s.erase(0,b);
}
__declspec(dllexport) void log_(const wchar_t *format, ...)
{
size_t count = LOG_BUF_LEN;
wchar_t buffer[LOG_BUF_LEN];
EnterCriticalSection(&_log_cs);
if (file) {
va_list params;
va_start(params, format);
vswprintf_s(buffer, count, format, params);
va_end(params);
char *encoded = Utf8::unicodeToUtf8(buffer);
fwrite(encoded, strlen(encoded), 1, file);
Utf8:free(encoded);
fflush(file);
}
LeaveCriticalSection(&_log_cs);
}
__declspec(dllexport) void logu_(const char *format, ...)
{
EnterCriticalSection(&_log_cs);
if (file) {
va_list params;
va_start(params, format);
vfprintf(file, format, params);
va_end(params);
fflush(file);
}
LeaveCriticalSection(&_log_cs);
}
__declspec(dllexport) void start_log_(const wchar_t *format, ...)
{
InitializeCriticalSection(&_log_cs);
EnterCriticalSection(&_log_cs);
file = _wfopen(dll_log, L"wt");
if (file) {
va_list params;
va_start(params, format);
vfwprintf(file, format, params);
va_end(params);
fflush(file);
}
LeaveCriticalSection(&_log_cs);
}
__declspec(dllexport) void open_log_(const wchar_t *format, ...)
{
InitializeCriticalSection(&_log_cs);
EnterCriticalSection(&_log_cs);
file = _wfopen(dll_log, L"a+");
if (file) {
va_list params;
va_start(params, format);
vfwprintf(file, format, params);
va_end(params);
fflush(file);
}
LeaveCriticalSection(&_log_cs);
}
__declspec(dllexport) void close_log_()
{
EnterCriticalSection(&_log_cs);
if (file) {
fflush(file);
fclose(file);
file = NULL;
}
LeaveCriticalSection(&_log_cs);
DeleteCriticalSection(&_log_cs);
}
__declspec(dllexport) void truncate_applog_()
{
FILE *f = _wfopen(app_log, L"wt");
if (f) {
fclose(f);
}
}
__declspec(dllexport) void applog_(const wchar_t *format, ...)
{
FILE *f = _wfopen(app_log, L"at");
if (f) {
va_list params;
va_start(params, format);
vfwprintf(f, format, params);
va_end(params);
fflush(f);
fclose(f);
}
}
BYTE* get_target_addr(BYTE* call_location)
{
if (call_location) {
BYTE* bptr = 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 call_location + ptr[0] + 5;
}
}
return NULL;
}
void hook_call_point(BYTE *addr, void* func, int codeShift, int numNops, bool addRetn)
{
BYTE *target = (BYTE*)func + codeShift;
if (addr && target)
{
BYTE* bptr = 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 - (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* check_hint(BYTE *base, LONGLONG max_offset, BYTE *frag, size_t frag_len, void *hint)
{
BYTE *p = base;
BYTE *max_p = base + max_offset;
BYTE *hp = (BYTE*)hint;
if (p <= hp && hp + frag_len <= max_p) {
if (memcmp(hp, frag, frag_len)==0) {
return hp;
}
}
return NULL;
}
BYTE* find_code_frag(BYTE *base, LONGLONG max_offset, BYTE *frag, size_t frag_len)
{
BYTE *p = base;
BYTE *max_p = base + max_offset;
//logu_("searching range: %p : %p for %lu bytes\n", p, max_p, frag_len);
return (BYTE*)kmp_search((char*)frag, frag_len, (char*)p, (char*)max_p);
/*
while (p < max_p && memcmp(p, frag, frag_len)!=0) {
p += 1;
}
if (p < max_p) {
return p;
}
return NULL;
*/
}
void patch_at_location(BYTE *addr, void *patch, size_t patch_len)
{
if (addr) {
BYTE* bptr = addr;
DWORD protection = 0;
DWORD newProtection = PAGE_EXECUTE_READWRITE;
if (VirtualProtect(bptr, patch_len, newProtection, &protection)) {
memcpy(addr, patch, patch_len);
log_(L"Patch (size=%d) installed at (%p)\n", patch_len, addr);
}
else {
log_(L"Problem with VirtualProtect at: %p\n", addr);
}
}
}