Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Program from byte array

Tags:

arrays

c#

byte

I have a program stored in byte array.

Is it possible to run it inside C#?

like image 447
DanSpd Avatar asked Jun 04 '10 19:06

DanSpd


People also ask

When to use byte array in c#?

For example, if you're dealing with a small icon file (less than 50KB) available on the local machine and you know the filesize, go with a byte array. Conversely, if you're working with a movie file where it would be difficult, not to mention unnecessary, to keep 2GB of content in memory at once, use a stream.

What is byte array?

ByteArray is an extremely powerful Class that can be used for many things related to data manipulation, including (but not limited to) saving game data online, encrypting data, compressing data, and converting a BitmapData object to a PNG or JPG file.

What is ByteArray in c#?

In C#.Net, we can create an unsigned byte array by using byte, byte is used to store only positive values between the range of 0 to 255 (Unsigned 8 bits integer). It occupies 1-byte memory for each element, if array size is 10, it will take 10 bytes memory.

How do you split bytes in Python?

Solution: To split a byte string into a list of lines—each line being a byte string itself—use the Bytes. split(delimiter) method and use the Bytes newline character b'\n' as a delimiter.


4 Answers

Yes. This answer shows you can directly execute the contents of a byte array. Basically, you use VirtualAlloc to allocate an executable region on the heap with a known address (a IntPtr). You then copy your byte array to that address with Marshal.Copy. You convert the pointer to a delegate with GetDelegateForFunctionPointer, and finally call it as a normal delegate.

like image 78
Matthew Flaschen Avatar answered Sep 30 '22 17:09

Matthew Flaschen


Sure.

  1. Save the byte array to an .exe file.
  2. Use the Process class to execute the file.

Note: this is assuming that your byte array is executable code, and not source code. This also assumes that you have a valid PE header or know how to make one.

like image 38
JSBձոգչ Avatar answered Sep 30 '22 16:09

JSBձոգչ


Assuming the byte array contains a .net assembly (.exe or .dll):

 Assembly assembly = AppDomain.Load(yourByteArray)
 Type typeToExecute = assembly.GetType("ClassName");
 Object instance = Activator.CreateInstance(typeToExecute);

Now, if typeToExecute implements an interface known to your calling program, you can cast it to this interface and invoke methods on it:

 ((MyInterface)instance).methodToInvoke();
like image 11
ckarras Avatar answered Sep 30 '22 17:09

ckarras


If the byte array is a .Net assembly with an EntryPoint (Main method) you could just do this. Most of the time returnValue would be null. And if you wanted to provide command line arguments you could put them in the commandArgs string listed below.

var assembly = Assembly.Load(assemblyBuffer);
var entryPoint = assembly.EntryPoint;
var commandArgs = new string[0];
var returnValue = entryPoint.Invoke(null, new object[] { commandArgs });
like image 8
Matthew Whited Avatar answered Sep 30 '22 17:09

Matthew Whited