ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #include "sm_globals.h" ds@1952: #include "sourcemm_api.h" ds@1952: #include "Tlhelp32.h" ds@1952: #include "LibrarySys.h" ds@1952: #include "minidump.h" ds@2040: #include "sm_stringutil.h" ds@2040: ds@2040: bool HookImportAddrTable(BYTE *base, const char *func, DWORD hookfunc, char *err, size_t maxlength) ds@2040: { ds@2040: IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)base; ds@2040: if (dos->e_magic != IMAGE_DOS_SIGNATURE) ds@2040: { ds@2040: UTIL_Format(err, maxlength, "%s", "Could not detect valid DOS signature"); ds@2041: return false; ds@2040: } ds@2040: ds@2040: IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS *)(base + dos->e_lfanew); ds@2040: if (nt->Signature != IMAGE_NT_SIGNATURE) ds@2040: { ds@2040: UTIL_Format(err, maxlength, "%s", "Could not detect valid NT signature"); ds@2040: return false; ds@2040: } ds@2040: ds@2040: IMAGE_IMPORT_DESCRIPTOR *desc = ds@2040: (IMAGE_IMPORT_DESCRIPTOR *) ds@2040: (base + nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress); ds@2040: if (base == (BYTE *)desc) ds@2040: { ds@2040: UTIL_Format(err, maxlength, "%s", "Could not find IAT"); ds@2040: return false; ds@2040: } ds@2040: ds@2040: bool entryFound = false; ds@2040: while (desc->Name) ds@2040: { ds@2040: if (desc->FirstThunk != 0) ds@2040: { ds@2040: IMAGE_THUNK_DATA *data = (IMAGE_THUNK_DATA *)(base + desc->OriginalFirstThunk); ds@2040: DWORD *iat = (DWORD *)(base + desc->FirstThunk); ds@2040: while (data->u1.Function) ds@2040: { ds@2040: if ((data->u1.Ordinal & IMAGE_ORDINAL_FLAG32) != IMAGE_ORDINAL_FLAG32) ds@2040: { ds@2040: IMAGE_IMPORT_BY_NAME *import = (IMAGE_IMPORT_BY_NAME *)(base + data->u1.AddressOfData); ds@2040: if (strcmp((char *)import->Name, func) == 0) ds@2040: { ds@2040: DWORD oldprot, oldprot2; ds@2040: VirtualProtect(iat, 4, PAGE_READWRITE, &oldprot); ds@2040: *iat = hookfunc; ds@2040: VirtualProtect(iat, 4, oldprot, &oldprot2); ds@2040: entryFound = true; ds@2040: } ds@2040: } ds@2040: data++; ds@2040: iat++; ds@2040: } ds@2040: } ds@2040: desc++; ds@2040: } ds@2040: ds@2040: if (!entryFound) ds@2040: { ds@2040: UTIL_Format(err, maxlength, "Could not find IAT entry for %s", func); ds@2040: return false; ds@2040: } ds@2040: ds@2040: return true; ds@2040: } ds@1952: ds@1952: BOOL ds@1952: WINAPI ds@1952: MiniDumpWriteDump2( ds@1952: IN HANDLE hProcess, ds@1952: IN DWORD ProcessId, ds@1952: IN HANDLE hFile, ds@1952: IN MINIDUMP_TYPE DumpType, ds@1952: IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, OPTIONAL ds@1952: IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, OPTIONAL ds@1952: IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam OPTIONAL ds@1952: ) ds@1952: { ds@1952: DumpType = (MINIDUMP_TYPE)((int)DumpType|MiniDumpWithFullMemory|MiniDumpWithHandleData); ds@1952: return MiniDumpWriteDump(hProcess, ProcessId, hFile, DumpType, ExceptionParam, UserStreamParam, CallbackParam); ds@1952: } ds@1952: ds@1952: FARPROC WINAPI GetProcAddress2(HMODULE hModule, LPCSTR lpProcName) ds@1952: { ds@1952: if (strcmp(lpProcName, "MiniDumpWriteDump") == 0) ds@1952: { ds@1952: return (FARPROC)MiniDumpWriteDump2; ds@1952: } ds@1952: ds@1952: return GetProcAddress(hModule, lpProcName); ds@1952: } ds@1952: ds@2040: HMODULE WINAPI LoadLibraryEx2(LPCTSTR lpFileName, HANDLE hFile, DWORD dwFlags) ds@2040: { ds@2040: HMODULE lib = LoadLibraryEx(lpFileName, hFile, dwFlags); ds@2040: ds@2040: /* Steam.dll seems to get loaded using an abs path, thus the use of stristr() */ ds@2040: if (stristr(lpFileName, "steam.dll")) ds@2040: { ds@2040: char err[64]; ds@2040: if (!HookImportAddrTable((BYTE *)lib, "GetProcAddress", (DWORD)GetProcAddress2, err, sizeof(err))) ds@2040: { ds@2040: Error("[SM] %s in steam.dll\n", err); ds@2040: return NULL; ds@2040: } ds@2040: } ds@2040: ds@2040: return lib; ds@2040: } ds@2040: ds@1952: class CrazyWindowsDebugger : public SMGlobalClass ds@1952: { ds@1952: public: ds@1952: void OnSourceModAllInitialized() ds@1952: { ds@1952: HANDLE hModuleList = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId()); ds@1952: MODULEENTRY32 me32; ds@2040: BYTE *engine = NULL; ds@2040: BYTE *steam = NULL; ds@2040: char err[64]; ds@1952: ds@1952: me32.dwSize = sizeof(MODULEENTRY32); ds@1952: ds@1952: if (!Module32First(hModuleList, &me32)) ds@1952: { ds@1952: Error("Could not initialize crazy debugger!"); ds@1952: } ds@1952: ds@1952: do ds@1952: { ds@2040: if (strcasecmp(me32.szModule, "engine.dll") == 0) ds@1952: { ds@2040: engine = me32.modBaseAddr; ds@2040: } ds@2040: else if (strcasecmp(me32.szModule, "steam.dll") == 0) ds@2040: { ds@2040: steam = me32.modBaseAddr; ds@1952: } ds@1952: } while (Module32Next(hModuleList, &me32)); ds@1952: ds@2040: CloseHandle(hModuleList); ds@1952: ds@2040: /* This really should not happen, but ... */ ds@2040: if (!engine) ds@1952: { ds@2040: Error("[SM] Could not find engine.dll\n"); ds@1952: } ds@1952: ds@2040: /* Steam.dll isn't loaded yet */ ds@2040: if (!steam) ds@2040: { ds@2040: if (!HookImportAddrTable(engine, "LoadLibraryExA", (DWORD)LoadLibraryEx2, err, sizeof(err))) ds@2040: { ds@2040: Error("[SM] %s in engine.dll)\n", err); ds@2040: } ds@2040: } ds@2040: else ds@2040: { ds@2040: if (!HookImportAddrTable(steam, "GetProcAddress", (DWORD)GetProcAddress2, err, sizeof(err))) ds@2040: { ds@2040: Error("[SM] %s in steam.dll)\n", err); ds@2040: } ds@2040: } ds@1952: } ds@1952: } s_CrazyDebugger;