加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JNA:缺少一些细节方法

发布时间:2020-12-15 02:15:46 所属栏目:Java 来源:网络整理
导读:我想在 Java中创建一个dll注入器(并且只在Java中),为自己创建教育方案,并在 website especialized in online game中找到了一个基本的例子. autor只说是使用JNA接口. 所以,我正在研究这段代码,并尝试使用NetBeans IDE和JNA成功编译,但似乎我在这里的JNA接口(4
我想在 Java中创建一个dll注入器(并且只在Java中),为自己创建教育方案,并在 website especialized in online game中找到了一个基本的例子.

autor只说是使用JNA接口.

所以,我正在研究这段代码,并尝试使用NetBeans IDE和JNA成功编译,但似乎我在这里的JNA接口(4.2.2)没有在autor留下的代码片段上使用的所有方法和函数.

他们是:

> GetProcAddress
> VirtualAllocEx
> VirtualFreeEx

所以,如果可能的话,我想在这里寻求一些帮助,因为尝试解决了JNA中缺少方法的麻烦.

我已经修复了大部分这些错误,但仍然缺少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,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() + '';
        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,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,loadLibraryAddress,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,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缺少方法? It ain’t so!

您只需要扩展库并添加自己的(并且理想情况下,还将“缺失”方法返回到JNA库,以便其他人可以受益.

Here is an example如何有人映射GetProcAddress.

Someone has mapped VirtualAllocEx here(尽管他们应该正确地扩展Kernel32而不是完全复制它并编辑部分)

我在相同的15秒内找不到VirtualFreeEx的例子我找到了其他的…并不意味着它不在那里但是在写完其他之后你也不会有太多麻烦写它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读