public static uint InjectCode(AppProcess process, uint address, int replacedInstructionSize, byte[] newCode) { if (replacedInstructionSize < 5) { throw new Exception("Replaced instruction size much be greater than 5."); } //Allocate the memory required uint allocatedMem = process.AllocateMemory((uint)(newCode.Length + replacedInstructionSize + JMPSize)); //Write the new code in the allocated memory process.WriteBytes((int)allocatedMem, newCode); //Copy the old code CopyInstructions(process, address, allocatedMem + (uint)newCode.Length, replacedInstructionSize); //Write jump at the end of the allocated memory WriteJump(process, allocatedMem + (uint)newCode.Length + (uint)replacedInstructionSize, address + (uint)replacedInstructionSize); //Write jump address WriteJump(process, address, allocatedMem); //Write nops to be clean WriteNOPs(process, address + JMPSize, replacedInstructionSize - JMPSize); return(allocatedMem); }
public void Activate() { //Allocate the memory required mAllocatedMemory = mProcess.AllocateMemory((uint)(mNewCode.Length + mReplacedInstructionSize + Hack.JMPSize)); if (mAllocatedMemory == 0) { throw new Exception(); } //Write the new code in the allocated memory mProcess.WriteBytes((int)mAllocatedMemory, mNewCode); //Copy the old code Hack.CopyInstructions(mProcess, mAddress, mAllocatedMemory + (uint)mNewCode.Length, mReplacedInstructionSize); //Write jump at the end of the allocated memory Hack.WriteJump(mProcess, mAllocatedMemory + (uint)mNewCode.Length + (uint)mReplacedInstructionSize, mAddress + (uint)mReplacedInstructionSize); //Write jump address Hack.WriteJump(mProcess, mAddress, mAllocatedMemory); //Write nops to be clean Hack.WriteNOPs(mProcess, mAddress + Hack.JMPSize, mReplacedInstructionSize - Hack.JMPSize); }