C# Making a program run in memory also  .net and non .net ?)

ray_code
ray_code used Ask the Experts™
on
Hi,

Is it possible to make a program run in memory and also non.net.

I know the process class you can run a program, but that runs an existing program file location.

Also, I know there is assembly class that you can invoke the main method, but that limited to .net.

How I can acheive this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017

Commented:
If the question you are asking is, is there a way to write, compile and execute a program that not depend on the .Net Framework the answer is yes. You can use Visual Studio .Net C++ compiler in native mode. This will produce a native exe that will not reference .Net. Of course that also means that you can't use any of the .Net functions but only the Windows API's. You can also get a non .Net compiler and use it.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
If the question is can you create a program that will run under the .Net Framework and also on systems that do not have the .Net Framework then the answer is NO.

Author

Commented:
ok thanks, however to clarify:

In C sharp, how would you run a  .exe file , non .net, in binary , and run that in memory?
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Fernando SotoRetired
Distinguished Expert 2017

Commented:
Are we talking about Windows System? Or Linux system?

Author

Commented:
Windows.

I want to run any executable from memory as a byte array. So the exe can be  encrypted text file or even as resource data, and run it directly from memory.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Need to understand the question first:

1. The initial program the one that will run the 2nd program from memory, Is this a C# .Net Application?
2. The program that will be embedded into resource of program 1 above, Is it a .Net application?
3. Is program 2 a windows, console or other application.

Author

Commented:
1. Yes, the c# application will read in an external file (the exe), as bytes.

2. The bytes of the read in exe, could be .net application or might be a binary c program.

3.  The program could be a windows or console or anything.

--
So, I have the bytes of the program, I want to run it in memory. So, I can check if it's a .net assembly and I think if I'm currect I can do something along the lines like:

// load the bytes into Assembly
Assembly a = Assembly.Load(bin);

// search for the Entry Point
MethodInfo method = a.EntryPoint;
if (method != null) {
// create an istance of the Startup form Main method
object o = a.CreateInstance(method.Name);
// invoke the application starting point
method.Invoke(o, null);
}

...

But like, what if it isn't a .net application, how would I run that?

Author

Commented:
The reason why I want to read the exe in, is because the exe might be pre-encrypted, store various data or need various manipulation, decryption on the bytes before running them in memory.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
To your statement, "2. The bytes of the read in exe, could be .net application or might be a binary c program.", To be able to use Reflection on an object the object needs to be a .Net assembly. An assembly "is a reusable, versionable, and self-describing building block of a common language, CLR, runtime application", so it can NOT be used on a binary c program.

So if the program to be run in memory is a .Net application then this can be done.

Author

Commented:
Yes, I want a to run a NON .net exe in memory.

So this is not possible?
Fernando SotoRetired
Distinguished Expert 2017

Commented:
To your statement, "Yes, I want a to run a NON .net exe in memory."

No you can NOT do this because you can only use Reflection on .Net assembles therefore you will not be able to do any of the following :

Assembly a = Assembly.Load(bin);
MethodInfo method = a.EntryPoint;
object o = a.CreateInstance(method.Name);
method.Invoke(o, null);

Because it all uses Reflection.

Author

Commented:
So there is no other way?
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Well you could read the program in and then do what ever on the file such as decrypting it, save it out to a disk file, then run the program using System.Diagnostics.Process to start the program, once you have completed using the program delete it from disk. But that is about it.

Author

Commented:
Oh, I knew that method from the start. Not exactly what I wanted. hmm.

Author

Commented:
I think I need to reverse engineer visual basic project found at:

http://www.vbforums.com/showthread.php?t=535124&highlight=runpe
 
and see how that works.
Retired
Distinguished Expert 2017
Commented:
That is using a VB 6, pre-.Net, using allot of Win32 API's that might not work because it looks like it is writing to the memory assigned to the program space where in the .Net world this is controlled by the .Net Framework very tightly.

Author

Commented:
An accurate solution didn't seem possible

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial