Injecting .NET Assembly to an Unmanaged Process
Last updated
Was this helpful?
Last updated
Was this helpful?
This is a quick lab to see what API sequence makes it possible to inject C# .NET assemblies / PE files (.exe and .dll) into an unmanaged process and invoke their methods.
At a high level, it works as follows:
CLRCreateInstance
is used to retrieve an interface
ICLRMetaHost->GetRuntime
is used to retrieve interface for a specified CLR version
ICLRRuntimeInfo->GetInterface
is used to load the CLR into the current process and retrieve an interface
ICLRRuntimeHost->Start
is used to initialize the CLR into the current process
ICLRRuntimeHost->EecuteInDefaultAppDomain
is used to load the C# .NET assembly and call a particular method with an optionally provided argument
unmanaged.cpp
(in my lab compiled to LoadCLR.exe
) - a C++ program that loads a C# assembly
CLRHello1.exe
and invokes its method spotlessMethod
managed.cs
(in my lab compiled to CLRHello1.exe
) - a C# program that is loaded by the unmanaged process (LoadCLR.exe
). It has a method spotlessMethod
that is invoked via ExecuteInDefaultAppDomain.
O
Once invoked, the spotlessMethod
prints out Hi from CLR
to the console window.
Below shows how LoadCLR.exe
loaded our C# assembly CLRHello.exe
(seen in LoadCLR.exe
loaded modules tab) and invoked the spotlessMethod
, that printed Hi from CLR
to the console: