0

I've developped a small decrypt and execute application and I'm stuck at the execution part. I succesfully execute .NET assemblies using the method below:

 Assembly asm = Assembly.Load(decryptedBytes);   
 if (asm.EntryPoint == null)
     throw new ApplicationException("No entry point found!");

 MethodInfo ePoint = asm.EntryPoint;
 object ins = asm.CreateInstance(ePoint.Name);
 ePoint.Invoke(ins, null);

But when I try allocating an executable region using this post the application crashes

The only useful information I get is this:

Fault Module Name:  StackHash_0a9e

Here is my code:

  const uint PAGE_EXECUTE_READWRITE = 0x40;
  const uint MEM_COMMIT = 0x1000;

  [DllImport("kernel32.dll", SetLastError = true)]
  static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);

  private delegate int IntReturner();

IntPtr buf = VirtualAlloc(IntPtr.Zero, (uint)decryptedBytes.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
Marshal.Copy(decryptedBytes, 0, buf, decryptedBytes.Length);

IntReturner ptr = (IntReturner)Marshal.GetDelegateForFunctionPointer(buf, typeof(IntReturner));
Console.WriteLine(ptr());
Community
  • 1
  • 1
Samson
  • 2,801
  • 7
  • 37
  • 55
  • what does VirtualAlloc return? It might be NULL – alexm Jun 04 '12 at 18:05
  • 1
    Are you sure you need Assembly.Load and VirtualAlloc? The question you've linked talks about native code execution... – Alexei Levenkov Jun 04 '12 at 18:07
  • I need to execute all .exe application. I'm not into this area at all and I'm not sure how to do this. Any suggestions? – Samson Jun 04 '12 at 18:09
  • This may help: http://www.nokola.com/TryCSharp/HowToBuild.aspx In this project the assembly is compiled on a server, its raw bytes are transferred to a Silverlight client, where it's loaded into an AssemblyPart. – kol Jun 04 '12 at 18:10

0 Answers0