【问题标题】:DLLImport of the origin function in the DLLDLL中源函数的DLLImport
【发布时间】:2016-02-25 10:22:19
【问题描述】:

我想在kernel32.dll 中调用一个函数(不管是哪个函数)。它工作得很好。

现在我有以下问题:

另一个程序注入了在 kernel32.dll 中挂钩函数并更改其返回值的代码。我怎样才能调用 origin 函数呢?

在我可以先打开 我的 程序之前,它运行良好。但在 Windows 10 中,它调用了注入函数。

希望我能解释清楚。

这是一个例子:

我使用了ReadProcessMemory()这个函数,效果很好。有一个程序可以更改此功能。如果启动这个程序,我只会收到来自ReadProcessMemory() 的 0xFF。

例如,在 Windows 7 中,我只需要在另一个程序更改功能之前启动我的程序。

但在 Windows 10 中,它不再工作了。当另一个程序启动时,我只收到 0xFF。

所以我导入函数:

[DllImport(@"C:\WINDOWS\system32\kernel32.dll", EntryPoint = "ReadProcessMemory")]
private static extern bool ReadProcessMemory(IntPtr hProcess, UIntPtr lpBaseAddress, [Out] byte[] lpBuffer, UIntPtr nSize, IntPtr lpNumberOfBytesRead);

我的阅读功能:

public static bool ReadMemory(Int64 Address, ref byte[] buffer)
{
    return ReadProcessMemory(ProcessHandle, (UIntPtr)Address, buffer, (UIntPtr)buffer.Length, IntPtr.Zero);
}

【问题讨论】:

  • 不,一点都不清楚
  • 这应该是一个问题吗...你需要给我更多的细节。什么程序在注入函数,返回值是什么,变成了什么?你是如何从 kernel32.dll 调用函数的?您正在使用的代码示例怎么样?
  • 还不是很清楚。其他程序如何修改ReadProcessMemory 在您的程序中的行为方式?是否有一些骇人听闻的注入?我们怎么知道您不只是错误地调用ReadProcessMemory 并且误诊了您的问题。除非您准备提供具体细节,否则没有人可以帮助您。请出示minimal reproducible example

标签: windows winapi


【解决方案1】:

如果只更改kernel32.dll,您可以调用ntdll.dll!NtReadVirtualMemoryReadProcessMemory 本身调用此函数)。

如果 ntdll.dll 似乎也被第 3 方进程更改,您可以将 ntdll.dll 复制到另一个临时文件 (ntdll_copy.dll),然后使用它:

[DllImport("ntdll_copy.dll", EntryPoint = "NtReadVirtualMemory")]
private static extern bool NtReadVirtualMemory(IntPtr hProcess, UIntPtr lpBaseAddress, [Out] byte[] lpBuffer, UIntPtr nSize, IntPtr lpNumberOfBytesRead);

【讨论】:

  • 非常感谢您。是否存在文档?因为我把它叫做 ReadProcessMemory 但我只收到 0Xff。但我确信我做错了什么。我将在答案中显示我的代码
  • 我的代码:[DllImport("ntdll.dll", EntryPoint = "NtReadVirtualMemory")] private static extern bool NtReadVirtualMemory(IntPtr hProcess, UIntPtr lpBaseAddress, [Out] byte[] lpBuffer, UIntPtr nSize, IntPtr lpNumberOfBytesRead); public static bool ReadMemory(Int64 Address, ref byte[] buffer, int size) { return NtReadVirtualMemory(ProcessHandle, (UIntPtr)Address, buffer, (UIntPtr)size, IntPtr.Zero); }
  • public uint ReadUInt(long Address) { byte[] buffer = new byte[4]; if (ReadMemory(Address, ref buffer, 4)) return BitConverter.ToUInt32(buffer, 0);返回 uint.MaxValue; }
  • Class_HP_NT PTest = new Class_HP_NT(); PTest.SetProcessIDHandle("进程名"); uint mAddress = 0x52D4F560 uint mValue = PTest.ReadUInt(mAAddress); MessageBox.Show(mValue.ToString("X"));
  • 好的,现在可以了。当我复制这个 dll 时,我收到了找不到模块的消息。当我将它复制到一个临时文件中时,它会说:“以错误的格式加载它”
猜你喜欢
  • 2012-03-23
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多