using System; using System.Runtime.InteropServices; using System.Text; using GuildWarsInterface.Native; namespace Mod.Hooks { /// /// intercepts the pw and caches it until it is used to authenticate the user on the custom server /// public static class PwCache { private static HookType _hookDelegate; private static readonly IntPtr _hookAddress = (IntPtr) 0x0043B58D; public static string CachedPw; public static void InstallHook() { _hookDelegate = Hook; IntPtr codeCave = Marshal.AllocHGlobal(128); var writeStream = new WriteStream(codeCave); writeStream.Write(new[] {ASM.PUSH_AD, ASM.PUSH_EDX}); writeStream.Write(ASM.Call((int) writeStream.CurrentAddress, (int) Marshal.GetFunctionPointerForDelegate(_hookDelegate))); writeStream.Write(new[] {ASM.POP_AD}); writeStream.Write(ASM.LEA_ECX_DWORD_ESI80); writeStream.Write(ASM.Jmp((int) writeStream.CurrentAddress, (int) (_hookAddress + 6))); uint dwOldProtection; Kernel32.VirtualProtect(_hookAddress, 5, 0x40, out dwOldProtection); Marshal.Copy(ASM.Jmp((int) _hookAddress, (int) codeCave), 0, _hookAddress, 5); Kernel32.VirtualProtect(_hookAddress, 5, dwOldProtection, out dwOldProtection); } private static void Hook(IntPtr pwLocation) { var result = new StringBuilder(); int currentPosition = 0; while (true) { short nextChar = Marshal.ReadInt16(pwLocation + currentPosition); if (nextChar == 0) break; result.Append((char) nextChar); currentPosition += 2; } CachedPw = result.ToString(); } [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void HookType(IntPtr pwLocation); private class WriteStream { public WriteStream(IntPtr address) { CurrentAddress = address; } public IntPtr CurrentAddress { get; private set; } public void Write(byte[] code) { Marshal.Copy(code, 0, CurrentAddress, code.Length); CurrentAddress += code.Length; } } } }