Code:
// From mscoree.h
static byte[] CLSID_CLRRuntimeHost = new byte[] { 0x6E, 0xA0, 0xF1, 0x90, 0x12, 0x77, 0x62, 0x47, 0x86, 0xB5, 0x7A, 0x5E, 0xBA, 0x6B, 0xDB, 0x02 };
static byte[] IID_ICLRRuntimeHost = new byte[] { 0x6C, 0xA0, 0xF1, 0x90, 0x12, 0x77, 0x62, 0x47, 0x86, 0xB5, 0x7A, 0x5E, 0xBA, 0x6B, 0xDB, 0x02 };
public static System.Diagnostics.ProcessModule GetMsCoree
{
get
{
foreach (System.Diagnostics.ProcessModule pm in System.Diagnostics.Process.GetCurrentProcess().Modules)
if (pm.ModuleName.ToLower() == "mscoree.dll")
return pm;
return null;
}
}
public static void ExecuteInDefaultAppDomain(Int32 PID, String AssemblyPath, String TypeName, String MethodName, String Args)
{
Magic.BlackMagic BM = new Magic.BlackMagic();
if (!BM.OpenProcessAndThread(PID))
{
Console.WriteLine("Unable to open PID {0} !", PID);
return;
}
if (GetMsCoree == null)
{
Console.WriteLine("Unable to found mscoree.dll !");
return;
}
UIntPtr CorBindToRuntimeExPtr = Magic.Imports.GetProcAddress(Magic.Imports.GetModuleHandle("mscoree.dll"), "CorBindToRuntimeEx");
if (CorBindToRuntimeExPtr == UIntPtr.Zero)
{
Console.WriteLine("Unable to found CorBindToRuntimeExPtr");
return;
}
BM.InjectDllCreateThread(GetMsCoree.FileName);
uint CLSID_CLRRuntimeHostPtr = BM.AllocateMemory(CLSID_CLRRuntimeHost.Length * 4);
uint IID_ICLRRuntimeHostPtr = BM.AllocateMemory(IID_ICLRRuntimeHost.Length);
uint ClrHostPtr = BM.AllocateMemory(0x4);
uint dwRetPtr = BM.AllocateMemory(0x4);
uint codeCave_Code = BM.AllocateMemory(0x256);
uint AssemblyPathPtr = BM.AllocateMemory(AssemblyPath.Length + 1);
uint TypeNamePtr = BM.AllocateMemory(TypeName.Length + 1);
uint MethodNamePtr = BM.AllocateMemory(MethodName.Length + 1);
uint ArgsPtr = BM.AllocateMemory(Args.Length + 1);
uint BuildFlavorPtr = BM.AllocateMemory(0x10);
BM.WriteUnicodeString(BuildFlavorPtr, "wks");
BM.WriteUnicodeString(AssemblyPathPtr, AssemblyPath);
BM.WriteUnicodeString(TypeNamePtr, TypeName);
BM.WriteUnicodeString(MethodNamePtr, MethodName);
BM.WriteUnicodeString(ArgsPtr, Args);
BM.WriteBytes(CLSID_CLRRuntimeHostPtr, CLSID_CLRRuntimeHost);
BM.WriteBytes(IID_ICLRRuntimeHostPtr, IID_ICLRRuntimeHost);
Fasm.ManagedFasm fasm = new Fasm.ManagedFasm(BM.ProcessHandle);
fasm.AddLine("push " + ClrHostPtr);
fasm.AddLine("push " + IID_ICLRRuntimeHostPtr);
fasm.AddLine("push " + CLSID_CLRRuntimeHostPtr);
fasm.AddLine("push 0");
fasm.AddLine("push " + BuildFlavorPtr);
fasm.AddLine("push 0");
fasm.AddLine("call " + CorBindToRuntimeExPtr);
fasm.AddLine("mov eax, [" + ClrHostPtr + "]");
fasm.AddLine("mov ecx, [eax]");
fasm.AddLine("mov edx, [ecx+0xC]");
fasm.AddLine("push eax");
fasm.AddLine("call edx");
fasm.AddLine("push " + dwRetPtr);
fasm.AddLine("push " + ArgsPtr);
fasm.AddLine("push " + MethodNamePtr);
fasm.AddLine("push " + TypeNamePtr);
fasm.AddLine("push " + AssemblyPathPtr);
fasm.AddLine("mov eax, [" + ClrHostPtr + "]");
fasm.AddLine("mov ecx, [eax]");
fasm.AddLine("push eax");
fasm.AddLine("mov eax, [ecx+0x2C]");
fasm.AddLine("call eax");
fasm.AddLine("retn");
fasm.InjectAndExecute(codeCave_Code);
BM.FreeMemory(CLSID_CLRRuntimeHostPtr);
BM.FreeMemory(IID_ICLRRuntimeHostPtr);
BM.FreeMemory(ClrHostPtr);
BM.FreeMemory(dwRetPtr);
BM.FreeMemory(codeCave_Code);
BM.FreeMemory(AssemblyPathPtr);
BM.FreeMemory(TypeNamePtr);
BM.FreeMemory(MethodNamePtr);
BM.FreeMemory(ArgsPtr);
BM.FreeMemory(BuildFlavorPtr);
}
I'm sure this code can be improved, feel free to add your suggestions