private CodeInjectionResult Inject() { if (process.Id == 0) { return(CodeInjectionResult.ProcessNotFound); } byte[] fullInjectedCode = Container.GetBytes(); IntPtr address = ExtensionMethods.AllocateMemory(process, 512); if (address == null) { return(CodeInjectionResult.FailedToVirtualAlloc); } alocAdress = address; if (!ExtensionMethods.WriteBytes(process, address, fullInjectedCode)) { return(CodeInjectionResult.FailedToWriteInstructionToMemory); } //Replace temp return addresses with proper ones foreach (var Detour in Container.Detours) { IntPtr LocationOfReturnJump = IntPtr.Add(alocAdress, Detour.Value.MasterContainerJmpBackLocation.ToInt32()); byte[] backAddy = BitConverter.GetBytes(Detour.Value.InjectionPoint.ToInt32() + Detour.Value.OverridenBytes - (LocationOfReturnJump.ToInt32() + 4)); if (!ExtensionMethods.WriteBytes(process, LocationOfReturnJump, backAddy)) { return(CodeInjectionResult.FailedToWriteInstructionToMemory); } } //Replace temp addresses with actual position of a variable foreach (var variable in Container.Variables) { foreach (var variableUse in variable.Value.VariableUsage) { if (!ExtensionMethods.WriteBytes(process, IntPtr.Add(alocAdress, variableUse.ToInt32()), BitConverter.GetBytes(alocAdress.ToInt32() + variable.Value.VariableLocation.ToInt32()))) { return(CodeInjectionResult.FailedToWriteInstructionToMemory); } } } foreach (var Detour in Container.Detours) { byte[] DetourInstruction = new byte[Detour.Value.OverridenBytes]; DetourInstruction[0] = 0xE9; //Jmp for (int i = 5; i < Detour.Value.OverridenBytes; i++) { DetourInstruction[i] = 0x90; //Nop } IntPtr LocationOfDetourJMP = Detour.Value.InjectionPoint; byte[] backAddy = BitConverter.GetBytes(alocAdress.ToInt32() + Detour.Value.MasterContainerStartLocation.ToInt32() - (Detour.Value.InjectionPoint.ToInt32() + 5)); backAddy.CopyTo(DetourInstruction, 1); if (!ExtensionMethods.WriteBytes(process, LocationOfDetourJMP, DetourInstruction)) { return(CodeInjectionResult.FailedToWriteInstructionToMemory); } } return(CodeInjectionResult.Success); }