using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; namespace Launcher { public class Program { #region pinvoke structure declarations public struct Startupinfo { public uint Cb; public string LpReserved; public string LpDesktop; public string LpTitle; public uint DwX; public uint DwY; public uint DwXSize; public uint DwYSize; public uint DwXCountChars; public uint DwYCountChars; public uint DwFillAttribute; public uint DwFlags; public short WShowWindow; public short CbReserved2; public IntPtr LpReserved2; public IntPtr HStdInput; public IntPtr HStdOutput; public IntPtr HStdError; } public struct ProcessInformation { public IntPtr HProcess; public IntPtr HThread; public uint DwProcessId; public uint DwThreadId; } #endregion #region pinvoke declarations [DllImport("kernel32.dll")] private static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref Startupinfo lpStartupInfo, out ProcessInformation lpProcessInformation); [DllImport("kernel32.dll")] private static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode); [DllImport("kernel32.dll")] private static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttribute, IntPtr dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId); [DllImport("kernel32.dll")] private static extern uint ResumeThread(IntPtr hThread); [DllImport("kernel32.dll")] private static extern IntPtr GetModuleHandle(string lpModuleName); [DllImport("kernel32.dll")] private static extern int CloseHandle(IntPtr hObject); [DllImport("kernel32.dll")] private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); [DllImport("kernel32.dll")] private static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect); [DllImport("kernel32.dll")] private static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, uint dwFreeType); [DllImport("kernel32.dll")] private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, ref uint lpNumberOfBytesWritten); [DllImport("kernel32.dll")] private static extern int WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, int size, int lpNumberOfBytesWritten); #endregion /// /// the program entry point. /// public static void Main(string[] args) { const string LOADER_DLL = "Loader.dll"; const string MOD_DLL = "Mod.dll"; #region required file existance check if (!File.Exists(LOADER_DLL)) { Error(string.Format("the {0} does not exist.", LOADER_DLL)); return; } if (!File.Exists(MOD_DLL)) { Error(string.Format("the {0} does not exist.", MOD_DLL)); return; } #endregion #region guild wars process creation if (Process.GetProcessesByName("Gw").Length > 0) { Error("guild wars is already running."); return; } var si = new Startupinfo(); ProcessInformation pi; if (!CreateProcess(null, "Gw.exe -oldauth \0", IntPtr.Zero, IntPtr.Zero, false, 4, IntPtr.Zero, null, ref si, out pi)) { Error("could not start guild wars."); return; } #endregion #region loader and mod information storage if (WriteProcessMemory(pi.HProcess, (IntPtr)0x00DEA000, BitConverter.GetBytes(Environment.Version.Major), 4, 0) == 0) { Error("could not setup the loader loading procedure (CLR major version)."); return; } if (WriteProcessMemory(pi.HProcess, (IntPtr)0x00DEA004, BitConverter.GetBytes(Environment.Version.Minor), 4, 0) == 0) { Error("could not setup the loader loading procedure (CLR minor version)."); return; } if (WriteProcessMemory(pi.HProcess, (IntPtr)0x00DEA008, BitConverter.GetBytes(Environment.Version.Build), 4, 0) == 0) { Error("could not setup the loader loading procedure (CLR build version)."); return; } if (WriteProcessMemory(pi.HProcess, (IntPtr)0x00DEA00C, new byte[] { 0 }, 1, 0) == 0) { Error("could not setup the loader loading procedure (mod setup completion flag)."); return; } #endregion #region loader loading var lpLlAddress = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA"); if (lpLlAddress == IntPtr.Zero) { Error("could not find the address of LoadLibraryA in kernel32.dll."); return; } var lpAddress = VirtualAllocEx(pi.HProcess, IntPtr.Zero, LOADER_DLL.Length, (0x1000 | 0x2000), 0x40); if (lpAddress == IntPtr.Zero) { Error("could not setup the loader loading procedure (memory allocation)."); return; } var bytes = Encoding.ASCII.GetBytes(LOADER_DLL); if (WriteProcessMemory(pi.HProcess, lpAddress, bytes, bytes.Length, 0) == 0) { Error("could not setup the loader loading procedure (loader name)."); return; } var hThread = CreateRemoteThread(pi.HProcess, IntPtr.Zero, IntPtr.Zero, lpLlAddress, lpAddress, 0, IntPtr.Zero); if (hThread == IntPtr.Zero) { Error("the thread to load the loader could not be created."); return; } #endregion #region mod setup administration var buffer = new byte[1]; uint dummy = 0; var timeStamp = DateTime.Now; const int TIMEOUT = 5000; do { Thread.Sleep(10); if (!ReadProcessMemory(pi.HProcess, (IntPtr)0x00DEA00C, buffer, 1, ref dummy)) { Error("could not pull information about the mod loading state from the client."); return; } if (DateTime.Now.Subtract(timeStamp).TotalMilliseconds > TIMEOUT) { Error("timeout while waiting for the mod to complete the setup phase."); TerminateProcess(pi.HProcess, 0); return; } } while (buffer[0] == 0); #endregion #region cleanup VirtualFreeEx(hThread, lpAddress, bytes.Length, 0x10000); CloseHandle(hThread); ResumeThread(pi.HThread); #endregion } /// /// displays an error message. /// private static void Error(string description) { MessageBox.Show(string.Format("the entice client has encountered an error.\n\nReason: {0}", description), "entice", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }