SharpNeedle icon indicating copy to clipboard operation
SharpNeedle copied to clipboard

Inject C# dll from memory instead of from a file

Open chico-depressive opened this issue 8 years ago • 7 comments

hi, it is possible to run managed dll from memory rather than from file? just like stephwn fewer reflective injector?

chico-depressive avatar Apr 11 '17 06:04 chico-depressive

I'm not familiar with stephwn reflective injector, can you link me?

SharpNeedle invokes the managed dll using the ICLRRuntimeHost::ExecuteInDefaultAppDomain method, and the dll argument is LPCWSTR pwzAssemblyPath which has to be a path.

ChadSki avatar Apr 11 '17 22:04 ChadSki

@ChadSki I was looking for something like this: https://github.com/stephenfewer/ReflectiveDLLInjection

but for managed dlls, do you think it is possible?

chico-depressive avatar Apr 22 '17 21:04 chico-depressive

You could always make the injected native dll write the c# dll to file. That seems to work fine for me.

Although my solution seems more like its solving #2

CoderReece avatar Apr 18 '19 17:04 CoderReece

I think it is possible by injecting a fake managed DLL and then loading the managed code in memory via Assembly.Load(assemblyBytes).

abevol avatar Dec 27 '22 19:12 abevol

What about use mono runtime instead, it was exports to load assembly from memory (mono_image_open_from_data_with_name) and invoke methods manually.

marcussacana avatar Dec 27 '22 19:12 marcussacana

What about use mono runtime instead, it was exports to load assembly from memory (mono_image_open_from_data_with_name) and invoke methods manually.

You need to inject the mono runtime first. In fact, this is no different from injecting regular managed code. Regular .net programs can also load managed code in memory.

abevol avatar Dec 27 '22 20:12 abevol

Obvious, this project works as well by injecting a dll in the target process and starting the CLR to invoke the ExecuteInDefaultAppDomain. I don't know if the CLR allows you load a assembly from memory using C++ but I'm aware that the mono can do it, since I already did in another project.

marcussacana avatar Dec 27 '22 20:12 marcussacana