-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathcpp_template
80 lines (72 loc) · 1.88 KB
/
cpp_template
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
#include <windows.h>
#include <stdio.h>
#include "hook_macro.h"
HINSTANCE mHinst = 0, mHinstDLL = 0;
{{ 'extern "C" ' if architecture == 'x64' else '' }}UINT_PTR mProcs[{{ ordinal_and_names|length }}] = {0};
LPCSTR mImportNames[] = {
{% for ordinal, symbol, name in ordinal_and_names %}
{% if name == '[NONAME]' %}
(LPCSTR){{ ordinal }},
{% else %}
"{{ name }}",
{% endif %}
{% endfor %}
};
#ifndef _DEBUG
inline void log_info(const char* info) {
}
#else
FILE* debug;
inline void log_info(const char* info) {
fprintf(debug, "%s\n", info);
fflush(debug);
}
#endif
#include "{{ hook }}"
inline void _hook_setup() {
{% for ordinal, symbol, name in ordinal_and_names %}
#ifdef {{ name | upper }}
{{ name }}_real = ({{ name }}_ptr)mProcs[{{ loop.index0 }}];
mProcs[{{ loop.index0 }}] = (UINT_PTR)&{{ name }}_fake;
#endif
{% endfor %}
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
mHinst = hinstDLL;
if (fdwReason == DLL_PROCESS_ATTACH) {
mHinstDLL = LoadLibrary("real_{{ dll }}");
if (!mHinstDLL) {
return FALSE;
}
for (int i = 0; i < {{ ordinal_and_names|length }}; ++i) {
mProcs[i] = (UINT_PTR)GetProcAddress(mHinstDLL, mImportNames[i]);
}
_hook_setup();
#ifdef _DEBUG
debug = fopen("./debug.log", "a");
#endif
} else if (fdwReason == DLL_PROCESS_DETACH) {
#ifdef _DEBUG
fclose(debug);
#endif
FreeLibrary(mHinstDLL);
}
return TRUE;
}
{% for ordinal, symbol, name in ordinal_and_names %}
{% if name == '[NONAME]' %}
{% set wrapper_name = 'ExportByOrdinal' + ordinal|string %}
{% else %}
{% set wrapper_name = name + '_wrapper' %}
{% endif %}
{% if architecture == 'x64' %}
extern "C" void {{ wrapper_name }}();
{% else %}
extern "C" __declspec(naked) void __stdcall {{ wrapper_name }}(){
#ifdef _DEBUG
log_info("calling {{ name }}");
#endif
__asm{jmp mProcs[{{ loop.index0 }} * 4]}
}
{% endif %}
{% endfor %}