This repository was archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhook.cpp
More file actions
74 lines (65 loc) · 3.4 KB
/
hook.cpp
File metadata and controls
74 lines (65 loc) · 3.4 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
#include <Windows.h>
#include "mhook/mhook-lib/mhook.h"
#include "jni.h"
#include "proxy.h"
#include "dump.h"
using namespace std;
typedef jclass JNICALL (*sig_JVM_DefineClass)(JNIEnv *env, const char *name, jobject loader,
const jbyte *buf, jsize len, jobject pd);
typedef jclass JNICALL(*sig_JVM_DefineClassWithSource)(JNIEnv *env, const char *name, jobject loader, const jbyte *buf,
jsize len, jobject pd, const char *source);
typedef jclass JNICALL (*sig_JVM_DefineClassWithSourceCond)(JNIEnv *env, const char *name,
jobject loader, const jbyte *buf, jsize len, jobject pd,
const char *source, jboolean verify);
sig_JVM_DefineClass orig_JVM_DefineClass = NULL;
sig_JVM_DefineClassWithSource orig_JVM_DefineClassWithSource = NULL;
sig_JVM_DefineClassWithSourceCond orig_JVM_DefineClassWithSourceCond = NULL;
jclass JNICALL detour_JVM_DefineClass(JNIEnv *env, const char *name, jobject loader,
const jbyte *buf, jsize len, jobject pd) {
DoDump((char *) buf, len);
return orig_JVM_DefineClass(env, name, loader, buf, len, pd);
}
jclass JNICALL detour_JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf,
jsize len, jobject pd, const char *source) {
DoDump((char *) buf, len);
return orig_JVM_DefineClassWithSource(env, name, loader, buf, len, pd, source);
}
jclass JNICALL detour_JVM_DefineClassWithSourceCond(JNIEnv *env, const char *name, jobject loader, const jbyte *buf,
jsize len, jobject pd, const char *source, jboolean verify) {
DoDump((char *) buf, len);
return orig_JVM_DefineClassWithSourceCond(env, name, loader, buf, len, pd, source, verify);
}
bool doHook() {
HMODULE hJvm = LoadLibrary("jvm.dll");
if (!hJvm) {
return FALSE;
}
orig_JVM_DefineClass = (sig_JVM_DefineClass) GetProcAddress(hJvm, "JVM_DefineClass");
if (!orig_JVM_DefineClass) {
return FALSE;
}
orig_JVM_DefineClassWithSource = (sig_JVM_DefineClassWithSource) GetProcAddress(hJvm, "JVM_DefineClassWithSource");
if (!orig_JVM_DefineClassWithSource) {
return FALSE;
}
orig_JVM_DefineClassWithSourceCond = (sig_JVM_DefineClassWithSourceCond) GetProcAddress(hJvm,
"JVM_DefineClassWithSourceCond");
if (!orig_JVM_DefineClassWithSourceCond) {
return FALSE;
}
HOOK_INFO hooks[] = {{(PVOID *) &orig_JVM_DefineClass, (PVOID) detour_JVM_DefineClass},
{(PVOID *) &orig_JVM_DefineClassWithSource, (PVOID) detour_JVM_DefineClassWithSource},
{(PVOID *) &orig_JVM_DefineClassWithSourceCond, (PVOID) detour_JVM_DefineClassWithSourceCond}};
return Mhook_SetHookEx(hooks, 3) == 3;
}
bool WINAPI DllMain(HMODULE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
if (fdwReason == DLL_PROCESS_ATTACH) {
SourceInit();
if (doHook()) {
MessageBox(NULL, "Hooks initialized.", "Success", MB_OK);
} else {
MessageBox(NULL, "Something went wrong.", "Error", MB_OK);
}
}
return TRUE;
}