【问题标题】:C# / WinAPI - Monitoring a Memory RegionC# / WinAPI - 监控内存区域
【发布时间】:2013-05-12 02:24:42
【问题描述】:

我想知道是否有一种方法可以监视特定内存区域的更改。我正在使用以下方法来打开一个进程并获取其已用内存区域的集合:

internal static MemoryProcess OpenProcess(Process process)
{
    IntPtr processHandle = OpenProcess(ProcessAccess.Desided, true, (UInt32)process.Id);

    if (processHandle == IntPtr.Zero)
        return null;

    IntPtr processAddress = new IntPtr();
    List<MemoryRegion> memoryRegions = new List<MemoryRegion>();

    while (true)
    {
        MemoryBasicInformation info = new MemoryBasicInformation();

        if (VirtualQueryEx(processHandle, processAddress, out info, MemoryBasicInformation.Size) == 0)
            break;

        if (info.Allocation.HasFlag(MemoryAllocation.Commit) && info.Type.HasFlag(MemoryType.Private) && ((info.Protection & MemoryProtection.Readable) != 0) && ((info.Protection & MemoryProtection.Protected) == 0))
            memoryRegions.Add(new MemoryRegion(info.BaseAddress, info.RegionSize));

        processAddress = new IntPtr(info.BaseAddress.ToInt64() + info.RegionSize.ToInt64());
    }

    if (memoryRegions.Count == 0)
    {
        CloseHandle(processHandle);
        return null;
    }

    return (new MemoryProcess(processHandle, memoryRegions));
}

我看到很多应用程序都在这样做。作弊引擎就是其中之一。假设我打开了一个 Google Chrome Tab 进程,并在它的内存中搜索并找到了一个特定值(字符串“stackoverflow”):

现在,我使用该选项卡浏览另一个完全不同的网站,我看到这些地址值发生了变化:

最后一步:我关闭了 Chrome 选项卡,终止了相应的进程:

所以...当然必须对内存区域进行某种监控。而且,其实还有:

使用 Process.Exited 事件不足以实现内存地址值的实时更新我认为......那么,我该怎么做呢?

【问题讨论】:

  • 我很确定您不能为此直接使用 .NET。您也许可以通过互操作使用 Win32 API,但 AFAIK 没有此类事情的 .NET API。

标签: c# .net winapi memory-management native-code


【解决方案1】:

如果你可以拦截目标应用程序中调用的VirtualAlloc,并将MEM_WRITE_WATCH与原始标志结合起来,系统将跟踪这个分配的内存区域,然后你可以调用GetWriteWatch函数来检索页面的地址自分配区域或已重置写入跟踪状态以来已写入的数据。不过,这看起来有很多工作要做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 2018-08-04
    • 2015-07-17
    • 2015-03-23
    相关资源
    最近更新 更多