Java JNA:缺少一些特定的方法
我想用Java(并且仅用Java)创建一个dll注入器,以供我自己学习,并在一个示例中找到了一个基本示例 autor只说这是使用JNA接口制作的 所以,我正在研究这段代码,并尝试使用NetBeans IDE和JNA成功编译,但我这里的JNA接口(4.2.2)似乎没有在autor留下的代码上使用所有方法和函数 它们是:Java JNA:缺少一些特定的方法,java,jna,Java,Jna,我想用Java(并且仅用Java)创建一个dll注入器,以供我自己学习,并在一个示例中找到了一个基本示例 autor只说这是使用JNA接口制作的 所以,我正在研究这段代码,并尝试使用NetBeans IDE和JNA成功编译,但我这里的JNA接口(4.2.2)似乎没有在autor留下的代码上使用所有方法和函数 它们是: GetProcAddress VirtualAllocEx VirtualFreeEx 所以,如果可能的话,我想在这里得到一些帮助,因为我尝试解决了JNA中缺少方法的问题 我已经修
package inject;
//////////////////// JNA-4.2.2 /////////////////////
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.Tlhelp32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HMODULE;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.W32APIOptions;
import java.io.File;
//////////////////////////////////////////////////
// Extracted from: https://github.com/warmuuh/AndroidCtx/tree/master/HotContext/src/luz/winapi
import inject.luz.winapi.constants.DwDesiredAccess;
import inject.luz.winapi.tools.Advapi32Tools;
import inject.luz.winapi.tools.Kernel32Tools;
import luz.winapi.api.exception.Kernel32Exception;
//////////////////////////////////////////////////////////////////////////////////////////////
public class Inject {
private static int GetPid(String proc){
int id = 0;
Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
try {
while (kernel32.Process32Next(snapshot, processEntry)) {
if (Native.toString(processEntry.szExeFile).equalsIgnoreCase(proc)) {
id = processEntry.th32ProcessID.intValue();
}
}
}
finally {
kernel32.CloseHandle(snapshot);
}
return id;
}
private static String findProcessByPID(int pid){
String name = "";
Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
try {
while (kernel32.Process32Next(snapshot, processEntry)) {
if (pid == processEntry.th32ProcessID.intValue()) {
name = processEntry.szExeFile.toString();
}
}
}
finally {
kernel32.CloseHandle(snapshot);
}
return name;
}
public static void inject(File dll, Integer pId) throws Kernel32Exception {
if(null == dll || !dll.exists() || !dll.isFile() || !dll.getName().endsWith(".dll"))
return;
String p = findProcessByPID(pId);
if(null == p) return;
Kernel32 kernel = Kernel32.INSTANCE;
HMODULE kernel32Pointer = kernel.GetModuleHandle("Kernel32");
// Cannot find "GetProcAddress"
Pointer loadLibraryAddress = kernel.GetProcAddress(kernel32Pointer, "LoadLibraryA");
HANDLE process = null;
DwDesiredAccess access = new DwDesiredAccess();
access.setPROCESS_ALL_ACCESS();
try {
Advapi32Tools.getInstance().enableDebugPrivilege(Kernel32Tools.getInstance().GetCurrentProcess());
} catch (Exception e) {
}
// Incompatible types "Pointer" and "HANDLE"
process = Kernel32Tools.getInstance().OpenProcess(access, false, pId);
String path = dll.getPath() + '\0';
byte[] bytes = path.getBytes();
int pathLength = bytes.length;
// Cannot find "VirtualAllocEx"
Pointer memoryDllPath = kernel.VirtualAllocEx(process, null, pathLength, Kernel32Tools.MEM_COMMIT, Kernel32Tools.PAGE_READWRITE);
Memory dllPathContent = new Memory(pathLength);
for(int i=0;i<pathLength;i++)
dllPathContent.setByte(i, bytes[i]);
IntByReference writeResult = new IntByReference();
boolean successWritting = kernel.WriteProcessMemory(process, memoryDllPath, dllPathContent, pathLength, writeResult);
if(!successWritting) {
kernel.CloseHandle(process);
return;
}
IntByReference threadId = new IntByReference();
// Pointer cannot be converted to "FOREIGN_THREAD_START_ROUTINE"
Pointer thread = kernel.CreateRemoteThread(process, null, 0, loadLibraryAddress, memoryDllPath, 0, threadId);
boolean res = false;
// Incompatible types "Pointer" and "HANDLE" //Cannot find "WAIT_TIMEOUT"
res = kernel.WaitForSingleObject(thread, Integer.MAX_VALUE) != Kernel32Tools.WAIT_TIMEOUT;
// Cannot find "VirtualFreeEx" method // Cannot find "MEM_RELEASE"
kernel.VirtualFreeEx(process, memoryDllPath, pathLength, Kernel32Tools.MEM_RELEASE);
kernel.CloseHandle(process);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(GetPid("notepad.exe"));
}
}
包注入;
////////////////////JNA-4.2.2/////////////////////
导入com.sun.jna.Memory;
导入com.sun.jna.Native;
导入com.sun.jna.Pointer;
导入com.sun.jna.platform.win32.Kernel32;
导入com.sun.jna.platform.win32.Tlhelp32;
导入com.sun.jna.platform.win32.WinDef;
导入com.sun.jna.platform.win32.WinDef.HMODULE;
导入com.sun.jna.platform.win32.WinNT;
导入com.sun.jna.platform.win32.WinNT.HANDLE;
导入com.sun.jna.ptr.IntByReference;
导入com.sun.jna.win32.w32api选项;
导入java.io.File;
//////////////////////////////////////////////////
//摘自:https://github.com/warmuuh/AndroidCtx/tree/master/HotContext/src/luz/winapi
导入inject.luz.winapi.constants.DwDesiredAccess;
导入inject.luz.winapi.tools.Advapi32Tools;
导入inject.luz.winapi.tools.Kernel32Tools;
导入luz.winapi.api.exception.Kernel32Exception;
//////////////////////////////////////////////////////////////////////////////////////////////
公共类注入{
私有静态int-GetPid(字符串过程){
int id=0;
Kernel32 Kernel32=(Kernel32)Native.loadLibrary(Kernel32.class,w32apipoptions.UNICODE_选项);
Tlhelp32.PROCESSENTRY32.ByReference processEntry=新的Tlhelp32.PROCESSENTRY32.ByReference();
WinNT.HANDLE snapshot=kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS,新的WinDef.DWORD(0));
试一试{
while(kernel32.Process32Next(快照,processEntry)){
if(Native.toString(processEntry.szExeFile.equalsIgnoreCase(proc)){
id=processEntry.th32ProcessID.intValue();
}
}
}
最后{
内核32.CloseHandle(快照);
}
返回id;
}
私有静态字符串findProcessByPID(int-pid){
字符串名称=”;
Kernel32 Kernel32=(Kernel32)Native.loadLibrary(Kernel32.class,w32apipoptions.UNICODE_选项);
Tlhelp32.PROCESSENTRY32.ByReference processEntry=新的Tlhelp32.PROCESSENTRY32.ByReference();
WinNT.HANDLE snapshot=kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS,新的WinDef.DWORD(0));
试一试{
while(kernel32.Process32Next(快照,processEntry)){
如果(pid==processEntry.th32ProcessID.intValue()){
name=processEntry.szExeFile.toString();
}
}
}
最后{
内核32.CloseHandle(快照);
}
返回名称;
}
公共静态无效注入(文件dll,整数pId)引发内核32Exception{
如果(null==dll | |!dll.exists()| |!dll.isFile()| |!dll.getName().endsWith(“.dll”))
返回;
字符串p=findProcessByPID(pId);
if(null==p)返回;
Kernel32 kernel=Kernel32.INSTANCE;
HMODULE Kernel32指针=kernel.GetModuleHandle(“Kernel32”);
//找不到“GetProcAddress”
指针loadLibraryAddress=kernel.GetProcAddress(kernel32Pointer,“LoadLibraryA”);
句柄进程=null;
DwDesiredAccess access=新的DwDesiredAccess();
setPROCESS_ALL_access();
试一试{
Advapi32Tools.getInstance().enableDebugPrivilege(内核32Tools.getInstance().GetCurrentProcess());
}捕获(例外e){
}
//不兼容的类型“指针”和“句柄”
process=Kernel32Tools.getInstance().OpenProcess(access,false,pId);
字符串路径=dll.getPath()+'\0';
byte[]bytes=path.getBytes();
int pathLength=bytes.length;
//找不到“VirtualAllocEx”
指针memoryDllPath=kernel.VirtualAllocEx(进程,null,路径长度,Kernel32Tools.MEM_提交,Kernel32Tools.PAGE_读写);
内存dllPathContent=新内存(路径长度);
对于(inti=0;iJNA缺失的方法
您只需要扩展库并添加您自己的(理想情况下,还需要将“缺少的”方法贡献回JNA库,以便其他人可以从中受益
某人是如何映射GetProcAddress的
(尽管他们应该正确地扩展内核32,而不是完全复制并编辑部分)
我找不到一个VirtualFreeEx的例子,就在我找到其他例子的15秒内……这并不意味着它不存在,但在写了其他例子之后,你应该也不会有太多的麻烦。如果你列出了缺少的方法或其他给你带来问题的方法,这可能是一个合理的问题。具体点。期待e每个人都编译你的代码来找出你的意思,但这并不是很受欢迎。@Erwin Bolwidt,我在上面编辑过。