【问题标题】:C# Reading process memory returning wrong valuesC#读取进程内存返回错误值
【发布时间】:2018-11-11 05:16:47
【问题描述】:

我正在尝试使用多个指针/偏移量从进程内存中读取一些值 在我的控制台 .Net 应用程序上,但我得到了错误的最后 3 个值,我不知道我做错了什么我一直在检查代码并尝试不同的方法几个小时,但结果仍然相同。

我正在从 64 位进程中读取这些值

这是我的应用和作弊引擎的预览(作弊引擎包含正确的值)。

这是我阅读这些指针的代码:

        Memory.OpenProcess(Data.Core.ProcessID);
        Data.Core.GameBase = (uint)Memory.BaseAddress("Game.dll");


        uint Num0 = Memory.ReadInt((int)Data.Core.GameBase + 
        (int)Data.Core.Offsets.Animation);
        uint Num1 = Memory.ReadInt((int)Num0 + (int)Data.Core.Offsets.P1);
        uint Num2 = Memory.ReadInt((int)Num1 + (int)Data.Core.Offsets.P2);
        uint Num3 = Memory.ReadInt((int)Num2 + (int)Data.Core.Offsets.P3);
        uint Num4 = Memory.ReadInt((int)Num3 + (int)Data.Core.Offsets.P4);
        uint Num5 = Memory.ReadInt((int)Num4 + (int)Data.Core.Offsets.P5);

ReadInt 函数:

 public uint ReadInt(int iMemoryAddress)
 {
    byte[] bBuffer = new byte[4];
    IntPtr lpNumberOfBytesRead;
    if (Mapi.ReadProcessMemory(this._hReadProcess, (IntPtr) iMemoryAddress, 
    bBuffer, 4U, out lpNumberOfBytesRead) == 0)
    return 0;
    return BitConverter.ToUInt32(bBuffer, 0);
 }

还有:

    public uint ReadInt(int Address)
    {
        OpenProcessMemory();
        int BytesRead = 0;
        byte[] Data = new byte[4];
        ReadProcessMemory((int)PHandle, Address, Data, 4, ref BytesRead);
        CloseProcessMemory();
        return BitConverter.ToUInt32(Data, 0);
    }

偏移枚举:

    public enum Offsets : uint
    {
        Animation = 0x1494198,
        P1 = 0x68,
        P2 = 0x70,
        P3 = 0x28,
        P4 = 0x378,
        P5 = 0x522,
    }

赢API:

[DllImport("kernel32.dll")]
public static extern int ReadProcessMemory(IntPtr hProcess, IntPtr 
lpBaseAddress, [In, Out] byte[] bBuffer, uint size, out IntPtr 
lpNumberOfBytesRead);

我尝试使用 IntPtr / uint / int / Int32 为每个 Pointer+Offset 添加指针和偏移量,但最后仍然是相同的奇怪值。 我想我显然不能做更多的事情..

【问题讨论】:

  • 请注意,在附加了聊天引擎的屏幕截图中,所有/一些在那里工作但在您的程序中不起作用的指针会显示一些指针值(“P->...”的东西) 大于 32 位。因此,尝试使用IntPtr 变量(或long)来处理程序中各处的指针值,而不是使用uint/int

标签: c# .net memory-management console-application


【解决方案1】:

如果目标进程是 x64,那么您还需要针对 x64 进行编译,并且应该对所有指针、偏移量和地址使用 IntPtr,以确保它们的长度正确,可以接受 64 位地址。

对于步行指针链,您应该使用此函数取消引用每个指针,然后为您添加偏移量。

public static IntPtr FindDMAAddy(IntPtr hProc, IntPtr ptr, int[] offsets)
{
    var buffer = new byte[IntPtr.Size];
    foreach (int i in offsets)
    {
        ReadProcessMemory(hProc, ptr, buffer, buffer.Length, out var read);

        ptr = (IntPtr.Size == 4)
        ? IntPtr.Add(new IntPtr(BitConverter.ToInt32(buffer, 0)), i)
        : ptr = IntPtr.Add(new IntPtr(BitConverter.ToInt64(buffer, 0)), i);
    }
    return ptr;
}

var ammoAddr = FindDMAAddy(hProc, (IntPtr)(modBase + 0x10f4f4), new int[] { 0x374, 0x14, 0 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多