Writeprocessmemory does not write data

Asked

Viewed 47 times

-2

Hello! I am trying to write data in INT type memory, and it is not working, Readprocessmemory worked normally! But the Writeprocessmemory does not.

I don’t know if I’m doing anything wrong more if you can help I appreciate!

Code I have so far.

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace CSGO_InfiniteAmmo
{
    class Program
    {

        [DllImport("Kernel32.dll")]
        private static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

        [DllImport("Kernel32.dll")]
        private static extern Boolean ReadProcessMemory(IntPtr hProcess, int lpBaseAddress, out int lpBuffer, int nSize, int lpNumberOfBytesRead);
        [DllImport("kernel32.dll")]
        static extern bool WriteProcessMemory(
            IntPtr hProcess,
            IntPtr lpBaseAddress,
            byte[] lpBuffer,
            int dwSize,
            out IntPtr lpNumberOfBytesWritten);
        private const int PROCESS_VM_READ = 0x0010;
        static private int pId = 0;
        static private int lpBaseAddress = 0x0058059C;
        static private int getAdressValue = 0;
        static IntPtr hProcess = IntPtr.Zero;

        static void Main()
        {
            pId = Process.GetProcessesByName("project")[0].Id;
            if (pId == 0)
                return;

            hProcess = OpenProcess(PROCESS_VM_READ, false, pId);

            if (ReadProcessMemory(hProcess, 0x6A4ED8E4, out getAdressValue, 4, 0))
                Console.WriteLine("Output: " + getAdressValue.ToString());

            Write(hProcess, 0x6A4ED8E4, 25);
        }

        static bool Write(IntPtr handle, int address, int valor)
        {
            byte[] dataBuffer = { (byte)5 };
            IntPtr bytesWritten = IntPtr.Zero;

            WriteProcessMemory(handle, (IntPtr)address, dataBuffer, dataBuffer.Length, out bytesWritten);

            Console.WriteLine(bytesWritten);
            if (bytesWritten == IntPtr.Zero)
            {
                Console.WriteLine("Não foi escrito nenhum valor!");
                return false;
            }
            if (bytesWritten.ToInt32() < dataBuffer.Length)
            {
                Console.WriteLine(" Escrevemos {0} de {1} bytes!", bytesWritten.ToInt32(), dataBuffer.Length.ToString());
                return false;
            }
            return true;
        }


    }
}
  • In the line in which it opens the case hProcess = OpenProcess(PROCESS_VM_READ, false, pId); you pass to the parameter DWORD dwDesiredAccess the value PROCESS_VM_READ if you consult the documentation will see that the value is needed to read the memory in a process that uses ReadProcessMemory() but not WriteProcessMemory(). To use WriteProcessMemory() pass as parameter dwDesiredAccess the value PROCESS_ALL_ACCESS or else PROCESS_VM_READ | PROCESS_VM_WRITE

1 answer

-1

Here follows an example of how to use the function:

public bool Wpm<T>(IntPtr lpBaseAddress, T value) where T : struct
    {
        var buffer = new T[Marshal.SizeOf<T>()];
        buffer[0] = value;
        return WriteProcessMemory(_gameProcess.Process.Handle, lpBaseAddress, buffer, Marshal.SizeOf<T>(), out var bytesread);
    }

code reference

Try modifying your code to look similar to the example, especially the way you are creating the buffer, and by accessing the value of the buffer size, try using Marshal.SizeOf<byte>().

Browser other questions tagged

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