Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a CIL executable not EXE then execute it

Tags:

c#

.net

cil

I have a c# project that generates an EXE file. Now, I'm in a "secure" corporate environment, where I can compile my project, but I cannot execute the EXE file.

As a Java programmer, I'm wondering if there is not a way to compile the c# project into something that would not be an EXE file, but a CIL file and then execute the CIL file by something that corresponds to java.exe in the dotnet world.

EDIT in response to comments:

  1. I can run exe files that have been installed by a package manager
  2. Yes, I know the corporate policy is stupid.
like image 511
David Vonka Avatar asked Aug 15 '26 13:08

David Vonka


1 Answers

Well, this should be pretty easy.

.NET executables are simply DLLs like any other - the main difference being the executable format itself, and the fact that EXE files have an entry point, while DLLs don't.

It also means that you can load the EXE into memory exactly the same way as you would with a DLL:

Assembly.LoadFrom("SomeExe.exe");

You're already half way there - now we just need to find and execute the entry point. And unsurprisingly, this is also pretty trivial:

var assembly = Assembly.LoadFrom("SomeExe.exe");
assembly.EntryPoint.Invoke(null, null);

For most applications, this should work perfectly fine; for some, you'll have to make sure the thread you're using to invoke the entry point has STAThread or MTAThread respectively (Thread.TrySetThreadApartment if you're starting a new thread).

It might need tweaking for some applications, but it shouldn't be too hard to fix.

So you can just make some bootstrap application ("interpreter") that only really contains these two lines of code. If you can't get even that approved, and you really need something as an "official package", try some .NET application that allows you to execute arbitrary code - for example, LINQPad, or PowerShell.

EDIT:

This does have limitations, of course, and it does introduce some extra setup work:

  • The bootstrapper has to target the same or higher version of .NET Framework. .NET Portable might be particularly tricky, but I assume you have that well under control. It also has to have the same bitness (if specified explicitly).
  • You need to run the debugging through the bootstrapper. That actually isn't all too hard - just go to project properties, debug and select "Start external program".
  • The bootstrapper has to run under full trust conditions - it's necessary for reflection to work. On most systems, this simply means you have to have the exe as a local file (e.g. not from a network share). Tools like LINQPad will run under full trust by default.
  • The application must not depend on Assembly.GetEntryAssembly. This isn't used all that often, so it shouldn't be a problem. Quite a few similar issues should also be fine since you build the application you're trying to run yourself.
like image 91
Luaan Avatar answered Aug 18 '26 02:08

Luaan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!