Skip to content

Instantly share code, notes, and snippets.

@yalayabeeb
Last active October 21, 2018 14:11
Show Gist options
  • Save yalayabeeb/ec186ae7badc6515b56e to your computer and use it in GitHub Desktop.
Save yalayabeeb/ec186ae7badc6515b56e to your computer and use it in GitHub Desktop.
A class to read/write mainly integers to a process's memory address.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class Memory
{
#region WinAPI
[DllImport("kernel32.dll")]
private static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);
#endregion
private Process MainProc { get; set; }
public Memory(Process proc)
{
MainProc = proc;
}
public MemoryAddress GetAddress(int memAddr)
{
return new MemoryAddress(OpenProcess(0x001F0FFF, false, MainProc.Id), (IntPtr)memAddr);
}
}
using System;
using System.Runtime.InteropServices;
public class MemoryAddress
{
#region WinAPI
[DllImport("kernel32.dll")]
static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int dwSize, int lpNumberOfBytesRead);
[DllImport("kernel32.dll")]
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, int lpNumberOfBytesWritten);
#endregion
public IntPtr Handle { get; set; }
public IntPtr Address { get; set; }
public MemoryAddress(IntPtr handle, IntPtr address)
{
Handle = handle;
Address = address;
}
public T Read<T>()
{
T data = default(T);
int size = Marshal.SizeOf(typeof(T));
IntPtr memAlloc = Marshal.AllocHGlobal(size);
ReadProcessMemory(Handle, Address, memAlloc, size, 0);
data = (T)Marshal.PtrToStructure(memAlloc, typeof(T));
Marshal.FreeHGlobal(memAlloc);
return data;
}
public void Write<T>(T input)
{
GCHandle memAlloc = GCHandle.Alloc(input, GCHandleType.Pinned);
int size = Marshal.SizeOf(input);
WriteProcessMemory(Handle, Address, memAlloc.AddrOfPinnedObject(), size, 0);
memAlloc.Free();
}
}
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
// Get the process that you want to read/write to
Process proc = Process.GetProcessesByName("ac_client")[0];
// Setup the classes accordingly
var memory = new Memory(proc);
MemoryAddress address = memory.GetAddress(0x4E4DBC);
// Read the static address (to get the session's current dynamic address)
int playerBase = address.Read<int>();
// The current static address + 0xF4 will get you memory address to the amount of health the player has
address = memory.GetAddress(playerBase + 0xF4);
int health = address.Read<int>();
// We can display the value (the amount of health they have)
Console.WriteLine("The player's health is currently: {0}", health);
// We can then change the amount of health to 150
address.Write<int>(150);
Console.WriteLine("The player's health was successfully changed to: 150");
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment