Here is an exampe in C#.
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace ExampleCreateRemoteThread
{
class Program
{
[DllImport("kernel32.dll")]
private static extern int CreateRemoteThread(int hProcess, int lpThreadAttributes, int dwStackSize, int lpStartAddress, int lpParameter, int dwCreationFlags, int lpThreadId);
[DllImport("kernel32.dll")]
private static extern int OpenProcess(int dwDesiredAccess, int bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll")]
private static extern int CloseHandle(int hObject);
[DllImport("kernel32.dll")]
private static extern int ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int nSize, int lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
private static extern int WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int nSize, int lpNumberOfBytesWritten);
const int PROCESS_ALL_ACCESS = 0x1F0FFF;
const int CodeCave = 0x00530FF0;
/// <summary>
/// MOV ECX,57FBF0
/// </summary>
static byte[] Instruction1 = { 0xB9,0xF0,0xFB,0x57,0x00};
const int BreakCall = 0x004C2394;
#region ReadX
static int ReadInt(int handle, int addr)
{
byte[] buf = new byte[4];
ReadProcessMemory(handle, addr, buf, 4, 0);
return BitConverter.ToInt32(buf, 0);
}
static int ReadShort(int handle, int addr)
{
byte[] buf = new byte[2];
ReadProcessMemory(handle, addr, buf, 2, 0);
return BitConverter.ToInt16(buf, 0);
}
static byte ReadByte(int handle, int addr)
{
byte[] buf = new byte[1];
ReadProcessMemory(handle, addr, buf, 1, 0);
return buf[0];
}
static byte[] ReadBytes(int handle, int addr, int size)
{
byte[] buf = new byte[size];
ReadProcessMemory(handle, addr, buf, size, 0);
return buf;
}
#endregion
#region WriteX
static void WriteInt(int handle, int addr,int val)
{
WriteProcessMemory(handle, addr, BitConverter.GetBytes(val), 4, 0);
}
static void WriteShort(int handle, int addr, short val)
{
WriteProcessMemory(handle, addr, BitConverter.GetBytes(val), 2, 0);
}
static void WriteByte(int handle, int addr, byte val)
{
WriteProcessMemory(handle, addr, BitConverter.GetBytes(val), 1, 0);
}
static void WriteBytes(int handle, int addr, byte[] b)
{
WriteProcessMemory(handle, addr, b, b.Length, 0);
}
static void WriteCall(int handle, int addr, int call)
{
WriteByte(handle, addr, 0xE8);
WriteInt(handle, addr+1, call - addr - 5);
}
#endregion
static void Break(Process p)
{
Break(p.Id);
}
static void Break(int id)
{
int h = OpenProcess(PROCESS_ALL_ACCESS, 0, id); //OpenProcess
if (h == 0)
throw new Exception("Could not open process");
if (ReadByte(h, CodeCave) == 0) //If code is not there, write it.
{
WriteBytes(h, CodeCave, Instruction1); //Mov ecx,0x0057FBF0
WriteCall(h, CodeCave + 5, BreakCall); //Call 0x004C2394
WriteByte(h, CodeCave + 10, 0xC3); //Ret
}
CreateRemoteThread(h, 0, 0, CodeCave, 0, 0, 0); //Call function
CloseHandle(h); //CloseHandle
}
static void Main(string[] args)
{
Process[] procs = Process.GetProcessesByName("conquer");
if (procs.Length > 0)
{
Break(procs[0]);
}
}
}
}