XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using System.ComponentModel;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

namespace XFEWorkflow.Plugins.WeCom.Native.MemoryLab;

// Research reader only. Its handle has no write, thread-creation or injection rights.
internal sealed class ProcessMemory : IDisposable
{
    private readonly SafeProcessHandle handle;

    internal ProcessMemory(int processId)
    {
        if (!Environment.Is64BitProcess) throw new PlatformNotSupportedException("Use the x64 memory reader.");
        handle = Native.OpenProcess(0x0410, false, processId); // QUERY_INFORMATION | VM_READ
        if (handle.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    internal (ushort ProcessMachine, ushort NativeMachine) GetMachine()
    {
        if (!Native.IsWow64Process2(handle, out ushort process, out ushort native))
            throw new Win32Exception(Marshal.GetLastWin32Error());
        return (process, native);
    }

    internal IEnumerable<MemoryRegion> Regions(ulong maximumAddress)
    {
        ulong address = 0;
        while (address < maximumAddress)
        {
            if (Native.VirtualQueryEx(handle, (nint)address, out MemoryRegion region,
                    (nuint)Marshal.SizeOf<MemoryRegion>()) == 0) yield break;
            ulong next = (ulong)region.BaseAddress + (ulong)region.RegionSize;
            if (next <= address) yield break;
            yield return region;
            address = next;
        }
    }

    internal int Read(ulong address, byte[] buffer, int count)
    {
        // A racing decommit can produce ERROR_PARTIAL_COPY. Keep only the bytes actually copied.
        Native.ReadProcessMemory(handle, (nint)address, buffer, (nuint)count, out nuint read);
        return (int)Math.Min(read, (nuint)count);
    }

    public void Dispose() => handle.Dispose();

    [StructLayout(LayoutKind.Sequential)]
    internal struct MemoryRegion
    {
        internal nint BaseAddress;
        internal nint AllocationBase;
        internal uint AllocationProtect;
        internal ushort PartitionId;
        internal nuint RegionSize;
        internal uint State;
        internal uint Protect;
        internal uint Type;

        internal readonly bool IsReadablePrivateData => State == 0x1000 && Type == 0x20000 &&
            (Protect & 0x100) == 0 && (Protect & 0xFF) is 0x02 or 0x04 or 0x08;
    }

    private static class Native
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern SafeProcessHandle OpenProcess(uint access,
            [MarshalAs(UnmanagedType.Bool)] bool inheritHandle, int processId);

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool IsWow64Process2(SafeProcessHandle process,
            out ushort processMachine, out ushort nativeMachine);

        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern nuint VirtualQueryEx(SafeProcessHandle process, nint address,
            out MemoryRegion buffer, nuint bufferLength);

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool ReadProcessMemory(SafeProcessHandle process, nint address,
            [Out] byte[] buffer, nuint size, out nuint bytesRead);
    }
}