How can I write values in the memory of another process?

Asked

Viewed 447 times

3

I have the memory address, the process and the value.

// Processo
var p = Process.GetProcessesByName("ePSXe").FirstOrDefault();

// Endereço
IntPtr addr = new IntPtr(0x00A66E11);

// Valor
var val = 0x63;

Now how do I write this value in the memory of the other process?

  • Remember that you will not be able to write to the memory addresses of another process in c#, especially if there is more than one process running in Runtime. NET (CLR)

1 answer

3


Add these methods:

[DllImport("kernel32.dll")]
static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out int lpNumberOfBytesWritten);

[DllImport("kernel32.dll")]
public static extern Int32 CloseHandle(IntPtr hProcess);

Flags:

[Flags]
public enum ProcessAccessFlags : uint
{
    All = 0x001F0FFF,
    Terminate = 0x00000001,
    CreateThread = 0x00000002,
    VMOperation = 0x00000008,
    VMRead = 0x00000010,
    VMWrite = 0x00000020,
    DupHandle = 0x00000040,
    SetInformation = 0x00000200,
    QueryInformation = 0x00000400,
    Synchronize = 0x00100000
}

With this I can implement a method to simplify:

public static void WriteMem(Process p, IntPtr address, long v)
{
    var hProc = OpenProcess(ProcessAccessFlags.All, false, (int)p.Id);
    var val = new byte[] { (byte)v };

    int wtf = 0;
    WriteProcessMemory(hProc, address, val, (UInt32)val.LongLength, out wtf);

    CloseHandle(hProc);
}

This way I can use:

static void Main(string[] args)
{
    var p = Process.GetProcessesByName("ePSXe").FirstOrDefault();

    WriteMem(p, new IntPtr(0x00A66DB9), 99);
}

References in English:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.