Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run an assembly .exe from a buffer in C

Tags:

c

.net

powershell

I'm working in a project related with sandboxing technologies.

Currently I'm writing a C program that gets a small assembly .exe binary (.NET) from a remote web server. This binary is stored in memory (never touch disk) and my intention is to run this binary from memory. If my client was made in .NET I would have no problem to run that assembly from memory (in fact, are many differents ways of getting this) but of course that's not possible with C (not completely sure).

My questions is: having that assembly .exe in the address space of my C program, is there any way to run it from there? Is there any way to allow the CLR to run it? At first I thought to invoke powershell to run it from there (using Reflection.Assembly) but that case involve writing the assembly to disk.

In any case, no doubt that the best and efficient way of getting this is by using a .NET client.

like image 823
Tomas Jeff Avatar asked May 01 '15 01:05

Tomas Jeff


1 Answers

If it's a exe. build from .net then it is possible, check http://www.codeproject.com/Articles/13897/Load-an-EXE-File-and-Run-It-from-Memory

If it is an unmanaged binary then I am not aware of a method to do this. If you cannot save it to the HDD, even in a temporary folder you could try some sort of ramdrive. The execution the is easy with the following code:

        //this is using imports from System.Diagnostics and System.IO

        byte[] exeBytes = GetBytesFromStream(stream); // Get the .exe file from a webstream or something
        string tmpFilename = Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".exe";
        string tmpFilePath = Path.Combine(Path.GetTempPath, tmpFilename);

        File.WriteAllBytes(tmpFilePath, exeBytes);

        Process.Start(tmpFilePath);
like image 90
Sotirios Avatar answered Oct 19 '22 01:10

Sotirios