Click here to Skip to main content
15,885,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I would like to know if it's possible to run an assembly stored in a buffer from an unmanaged application.

For example, I'm using a C++ program (unmanaged) to get an assembly (.exe) from a web server and store it in a buffer (it does not touch disk). I get to load the CLR (by using CLRCreateInstance) but I don't know any way to run it from memory. The method pClrRuntimeHost->ExecuteInDefaultAppDomain only allows you to take it from a file. Would I be forced to stored if on disk first?. I haven't seen any example with this.

Thank you so much.
Posted
Comments
[no name] 12-May-15 7:56am    
Really a good question. I have not done something like this on such a low level for quite some time. Basically you are right: A program can be loaded into memory and executed. The only question is, how the OS will play along with this.

You mentioned C++. Of course you can easily load the code for a function into a memory buffer and call it over a function pointer to that buffer. That would work and eliminate all overhead or interference from the OS.
Member 11653141 12-May-15 8:01am    
CDP1802 thank you for your reply. Do you have any code about that?. I'm working in a sandboxing project. I need to run some malicious .exe made in .NET from memory. I don't find any method similar to Assembly.Load in the COM api to get this. Thank you so much.
[no name] 12-May-15 9:07am    
Sandboxing? Then it will not be so simple. You must not only load the application and run it in any process, but also your application must host this process and insulate it from the rest of the system.
Member 11653141 12-May-15 13:19pm    
Yes, not easy problem. Bu now I'm just focusing on execute the assembly from memory. But that's seem complicated too. Thank you
Sergey Alexandrovich Kryukov 12-May-15 8:43am    
I don't think you can do it. The memory layout of an application loaded in memory by the system loaded has nothing to do with the layout of code in PE file. The operations of loading needs kernel-mode instructions and cannot be done without "interference from the OS".
—SA

1 solution

Though this seems possible but requires in depth knowledge of CLR, PE formats. Found a good reference which have sample code to do it though i dont know whether its working or not but could give you a good start may be


Load an EXE File and Run It from Memory[^]

Load an EXE file and run it from memory using C#[^]
 
Share this answer
 
Comments
Member 11653141 12-May-15 10:44am    
I read that tutorial, but the problem is the same. In my case I need to run a buffer containing manage code (an exe assembly) from unamange code (c++). In that example, all are done from manage code. Thank for the help anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900