I am creating a program to see if I can run an array of bytes in C #.
The program should capture an array of bytes "MyBinaryData" and Load + Run it as a new program. There will be a text box in which you can enter bytes to see the result (this is an experiment;)). I tried this:
byte[] binaryData = System.IO.File.ReadAllBytes("MyBytes.txt");
Assembly LoadByte = Assembly.Load(binaryData);
MethodInfo M = LoadByte.EntryPoint;
if (M != null)
{ object o = LoadByte.CreateInstance(M.Name);
M.Invoke(o, new Object[] { null });
}
else {
..... fail code here....
}
The problem is that it gives this error: "System.Reflection.TargetInvocationException: ...... SetCompatibleTextRenderingDefault must be called before the first IWin32Window is created in the application."
My second test:
Assembly assembly = Assembly.Load(binaryData);
Type bytesExe = assembly.GetType();
Object inst = Activator.CreateInstance(bytesExe);
But this should know which class in the byte array it needs to run.
Then I tried:
var bytes = Assembly.Load(binaryData);
var entryPoint = bytes.EntryPoint;
var commandArgs = new string[0];
var returnValue = entryPoint.Invoke(null, new object[] { commandArgs });
:
"System.Reflection.TargetInvocationException: . --- > System.InvalidOperationException: SetCompatibleTextRenderingDefault , IWin32Window."
program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Crypter
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
}
, ?
.