代码之家  ›  专栏  ›  技术社区  ›  DreTaX

Easyhook LoadLibrary在崩溃时失败

  •  -1
  • DreTaX  · 技术社区  · 6 年前

    我正在尝试使用easyhook来检测本机LoadLibrary调用。

    它确实可以检测到库的加载,但是这个过程会导致冻结。 这是因为 装载库挂钩 以下方法无法加载dll或库,因为 它返回0 intptr (可能找不到图书馆。)

    我甚至尝试将事件设置为“void”类型,但是过程仅仅崩溃,这可能是因为easyhook希望我返回一个值来覆盖函数。

    有没有一种方法可以让我返回确切需要加载的库,或者只获取正在加载的库的名称,而不必手动加载库?

    (也有这样的名字正在加载过程中:有点奇怪…)

    private static LocalHook hook;
    
    [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
    public static extern IntPtr GetModuleHandle(string lpModuleName);
    
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern IntPtr LoadLibrary(string lpFileName);
    
    [DllImport("kernel32.dll", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
    public static extern IntPtr GetProcAddress(IntPtr handle, string varormethodname);
    
    [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
    public delegate IntPtr LoadLibraryDelegate(string lpFileName);
    
    public TestHook()
    {
        IntPtr kernel32 = GetModuleHandle("kernel32.dll");
        Logger.Log("Kernel: " + kernel32);
        IntPtr address = GetProcAddress(kernel32, "LoadLibraryA");
        Logger.Log("Address: " + address);
    
        hook = LocalHook.Create(address,
        new LoadLibraryDelegate(LoadLibrary_Hook),
        null);
        hook.ThreadACL.SetExclusiveACL(new Int32[] {0});
    
        //RemoteHooking.WakeUpProcess();
    }
    
    
    public IntPtr LoadLibrary_Hook(string lpFileName)
    {
        Logger.Log("File load: " + lpFileName);
        return LoadLibrary(lpFileName);
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   DreTaX    6 年前

    public IntPtr LoadLibrary_Hook(string lpFileName)
    {
        Logger.Log("File load: " + lpFileName);
        LoadLibraryDelegate origMethod = (LoadLibraryDelegate)Marshal.GetDelegateForFunctionPointer(LoadLibraryAddress, typeof(LoadLibraryDelegate));
        return origMethod(lpFileName);
    }