I figured if there were any other C# monkeys out there ;] I would drop my part in this too:
made the same thing as above only in C# (.NET) with a simple class that I use to fondle other process' memory space.
Code: Select all
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace SweepMine
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.Write("Looking for \"winmine\"...");
//find the process and if we dont find it exit.
Process[] pArray = Process.GetProcessesByName("winmine");
if (pArray.Length == 0)
return;
Console.WriteLine("Found!");
// Create memory reader
ProcessMemoryReader pReader = new ProcessMemoryReader();
// Take the first process found
pReader.ReadProcess = pArray[0];
//Open the process for reading/writing
pReader.OpenProcess();
//something to count the number of bytes we read (-1 to mean error if we fail)
int aout=-1;
//write the the damn hack at 0x1002FF5 with the NOP bytes 'new byte [] {0x90,0x90,0x90,0x90,0x90,0x90}'
pReader.WriteProcessMemory(((IntPtr)0x1002FF5),new byte[] {0x90,0x90,0x90,0x90,0x90,0x90},out aout);
//output the number of bytes we wrote
Console.WriteLine("Wrote {0} bytes to memory!",aout);
//close the handle
pReader.CloseHandle();
//wait for user input
Console.Read();
}
public class ProcessMemoryReader
{
public class ProcessMemoryAPI
{
//API calls (this is what lets us call native win32 apis)
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId);
[DllImport("kernel32.dll")]
public static extern Int32 CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,[In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
[DllImport("kernel32.dll")]
public static extern Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,[In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);
//Process access flags
[Flags]
public enum ProcessAccessType
{
PROCESS_TERMINATE = (0x0001),
PROCESS_CREATE_THREAD = (0x0002),
PROCESS_SET_SESSIONID = (0x0004),
PROCESS_VM_OPERATION = (0x0008),
PROCESS_VM_READ = (0x0010),
PROCESS_VM_WRITE = (0x0020),
PROCESS_DUP_HANDLE = (0x0040),
PROCESS_CREATE_PROCESS = (0x0080),
PROCESS_SET_QUOTA = (0x0100),
PROCESS_SET_INFORMATION = (0x0200),
PROCESS_QUERY_INFORMATION = (0x0400)
}
}
//Simple class for managing processes and make it look clean
public ProcessMemoryReader()
{
}
//public place to read processes from
public Process ReadProcess
{
get
{
return m_ReadProcess;
}
set
{
m_ReadProcess = value;
}
}
//private process ALL MINE MINEEE!!!
private Process m_ReadProcess = null;
//default PID
private IntPtr m_hProcess = IntPtr.Zero;
//Opens the process for read/write
public void OpenProcess()
{
//sets what type of access we want; in this care Read/Write
ProcessMemoryAPI.ProcessAccessType access;
access = ProcessMemoryAPI.ProcessAccessType.PROCESS_VM_READ
| ProcessMemoryAPI.ProcessAccessType.PROCESS_VM_WRITE
| ProcessMemoryAPI.ProcessAccessType.PROCESS_VM_OPERATION;
//calls the API and actually opens up the process
m_hProcess = ProcessMemoryAPI.OpenProcess((uint)access, 1, (uint)m_ReadProcess.Id);
}
//closes the process from whatever we were doing.
public void CloseHandle()
{
int iRetValue;
iRetValue = ProcessMemoryAPI.CloseHandle(m_hProcess);
if (iRetValue == 0)
throw new Exception("CloseHandle failed");
}
//reads that perdy memory AND outs the number of bytes read.
public byte[] ReadProcessMemory(IntPtr MemoryAddress, uint bytesToRead, out int bytesRead)
{
byte[] buffer = new byte[bytesToRead];
IntPtr ptrBytesRead;
ProcessMemoryAPI.ReadProcessMemory(m_hProcess,MemoryAddress,buffer ,bytesToRead,out ptrBytesRead);
bytesRead = ptrBytesRead.ToInt32();
return buffer;
}
//even better than the top function, writes things to the memory and returns number of bytes written.
public void WriteProcessMemory(IntPtr MemoryAddress, byte[] bytesToWrite ,out int bytesWritten)
{
IntPtr ptrBytesWritten;
ProcessMemoryAPI.WriteProcessMemory(m_hProcess,MemoryAddress,bytesToWrite,(uint)bytesToWrite.Length,out ptrBytesWritten);
bytesWritten = ptrBytesWritten.ToInt32();
}
}
}
}