C# 从内存加载 .net 可执行文件



我正在通过WebClient将DLL流式传输到客户端,我正在尝试将我的.net可执行文件直接加载到内存中,并通过将其加载为程序集来执行它。此可执行文件是一个 Windows 窗体,用于创建 DirectX 覆盖并将内核驱动程序作为 Windows 服务加载。

Unhandled Exception: System.TypeInitializationException: The type initializer for 'IOController.Main' threw an exception. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array. at IOController.Main..cctor() --- End of inner exception stack trace --- at IOController.Main.Dispose(Boolean disposing) at System.ComponentModel.Component.Finalize()

我确实相信可执行文件已执行,但在Application.Run之前或执行Application.Run()时崩溃;

更新 1: IOController Main 中发布的任何内容都是如果我的 Main 函数实际执行时将调用的内容。它将在检查进程的 While 循环处停止。我的窗口直到循环之后才会初始化,因此也不会触发任何事件处理程序。

正在加载的应用程序:

public static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Start();
}
public static void Start()
{
//MessageBox.Show("Hi");
Application.Run(new Main());
}
}

加载它的应用程序:

byte[] bytes = null;       
try
{
using (var client = new WebClient())
{
client.Headers.Add("User-Agent", User_Agent);
bytes = client.DownloadData(uri);
}
}
catch (Exception ex)
{
Console.WriteLine("Download Failed:");
Console.WriteLine(ex.ToString());
SoftEnd();
}
Console.WriteLine("Bytes:" + bytes.LongLength);
var assembly = Assembly.Load(bytes);
var programType = assembly.GetTypes().FirstOrDefault(c => c.Name == "Program");
var method = programType.GetMethod("Start", BindingFlags.Public | BindingFlags.Static);
method.Invoke(null, new object[] { });

IOController Main:

//Thread DebuggerCheck = new Thread(Debugger);      
//DebuggerCheck.Start();
if (!Directory.Exists(User_Path))
{
Directory.CreateDirectory(User_Path);
File.SetAttributes(User_Path, FileAttributes.System | FileAttributes.Hidden);
}
else
{
File.SetAttributes(User_Path, FileAttributes.System | FileAttributes.Hidden);
}
if (!Directory.Exists(HWID_Path))
{
Directory.CreateDirectory(HWID_Path);
File.SetAttributes(HWID_Path, FileAttributes.System | FileAttributes.Hidden);
}
else
{
File.SetAttributes(HWID_Path, FileAttributes.System | FileAttributes.Hidden);
}
WebClient webClient = new WebClient();
if (!File.Exists(HWID_Path + "KasperAV.sys"))
{
webClient.Headers.Add("User-Agent", Authenticate.User_Agent);
webClient.DownloadFile(Authenticate.Auth_Server + "Request=Download&Username=" + Username + "&Password=" + Password + "&HWID=" + FingerPrint.Value() + "&File=KasperAV.sys", HWID_Path + "KasperAV.sys");
File.SetAttributes(HWID_Path + "KasperAV.sys", FileAttributes.System | FileAttributes.Hidden);
}
else
File.SetAttributes(HWID_Path + "KasperAV.sys", FileAttributes.System | FileAttributes.Hidden); 
if (IOControl.LoadDriver())
//Console.WriteLine("Driver Loaded");
//Filter Windows Name for Illegal Chars
WindowsName = WindowsName.Replace("*", "");
WindowsName = WindowsName.Replace("'", "");
WindowsName = WindowsName.Replace(";", "");
//Filter Machine Name for Illegal Chars
MachineName = MachineName.Replace("*", "");
MachineName = MachineName.Replace("'", "");
MachineName = MachineName.Replace(";", "");
//Check hash from Server
bool Now_Started = false;
if (!Now_Started)
{
Console.WriteLine("We advise you when exiting the cheat, use your 'End' button on your keyboard!");
Console.WriteLine("It will properly cleanup any leftover data that may cause a ban on other games.");
if (IsGameRunning()) //if the game is running
{
Now_Started = false;
}
else
{
Now_Started = true;
}
//Skip this
if (Now_Started)
{
Console.WriteLine("Waiting for H1Z1");
while (true)
{
if (IsGameRunning())
{
break;
}
Thread.Sleep(1000);
}
Console.WriteLine("::Game Launched - Waiting 30 seconds for the memory to load.");
int i = 0;
while (i < 30000)
{
Thread.Sleep(1);
Console.Write("rElapsed: {0}ms \ 30000ms", i++);
i++;
}
}
}
else
{
Console.WriteLine("Failed to Load Bypass, attempt a full reboot!");
Thread.Sleep(5000);
Exit();
}

当我没有传递任何参数时调用GetCommandlineArguments(因为我切换到加载程序集而不是执行它)。

最新更新