附加到已经运行的 JVM
有没有办法附加到已经运行的 JVM?
Is there a way to attach to an already running JVM?
例如,在 JNI 中,您可以使用 JNI_CreateJavaVM
创建 VM 并运行 jar 并检查其所有类..
For example, in JNI you can use JNI_CreateJavaVM
to create a VM and run a jar and inspect all its classes..
但是,如果 jar 已经在运行,我无法找到附加到其 JVM 并与其类进行通信或获取其 env
指针的方法..
However, if the jar is already running, I cannot find a way to attach to its JVM and communicate with its classes or get its env
pointer..
另一个问题是,如果 jar 加载了我的本机库 (.dll) 并且我想在 .dll 中创建一个 JVM,我不能......我也不能在没有 jar 调用我的函数的情况下附加 jar 的当前 JVM..
Another problem is that if the jar loads my native library (.dll) and I want to create a JVM inside the .dll, I cannot.. Nor can I attach the jar's current JVM either without the jar calling my function..
Java 端的示例:
class Foo
{
static {loadLibrary("Foo")}
}
在 C++ 方面:
void Foo()
{
//CreateJVM
//Attach to the current process..
//Call function from the jar that loaded me.
}
如果 jar 不先调用 Foo
,则无法做到这一点.
This cannot be done without the jar calling Foo
first.
有什么想法吗?有没有办法获取当前的 JVM 或附加到它或外部的 jvm 实例?
Any ideas? Is there no way to get the current JVM or to attach to it or an external jvm instance?
推荐答案
可以.
1) 在托管 JVM 的进程中注入 DLL(例如,java.exe
、javaw.exe
或 >iexplore.exe
).一种常见的注入技术是使用 SetWindowsHookEx
1) Inject a DLL in the process hosting the JVM (eg, java.exe
, or javaw.exe
, or iexplore.exe
). A common injection technique is to use SetWindowsHookEx
2)在DLL中,使用GetModuleHandle
3) 获取JNI_GetCreatedJavaVMs
函数的地址,使用GetProcAddress
3) Get the address of the JNI_GetCreatedJavaVMs
function, using GetProcAddress
4) 调用函数,如果成功,则使用 JavaVM
AttachCurrentThread 函数指针将线程附加到找到的第一个 JVM> 结构.
4) Call the function and, if successfull, attach your thread to the first JVM found, using the AttachCurrentThread
function pointer from the JavaVM
struture.
5) 完成.
有用的链接:调用 API
相关文章