core/CrazyDebugger.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun May 25 12:46:03 2008 +0000 (2008-05-25)
changeset 2080 9593426bd276
parent 2041 1ba9eb0f85c7
child 2289 44045ead61cd
permissions -rw-r--r--
Fixed CrazyDebug not working with Episode One games
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include "sm_globals.h"
ds@1952
    33
#include "sourcemm_api.h"
ds@1952
    34
#include "Tlhelp32.h"
ds@1952
    35
#include "LibrarySys.h"
ds@1952
    36
#include "minidump.h"
ds@2040
    37
#include "sm_stringutil.h"
ds@2040
    38
ds@2040
    39
bool HookImportAddrTable(BYTE *base, const char *func, DWORD hookfunc, char *err, size_t maxlength)
ds@2040
    40
{
ds@2040
    41
	IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)base;
ds@2040
    42
	if (dos->e_magic != IMAGE_DOS_SIGNATURE)
ds@2040
    43
	{
ds@2040
    44
		UTIL_Format(err, maxlength, "%s", "Could not detect valid DOS signature");
ds@2041
    45
		return false;
ds@2040
    46
	}
ds@2040
    47
ds@2040
    48
	IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS *)(base + dos->e_lfanew);
ds@2040
    49
	if (nt->Signature != IMAGE_NT_SIGNATURE)
ds@2040
    50
	{
ds@2040
    51
		UTIL_Format(err, maxlength, "%s", "Could not detect valid NT signature");
ds@2040
    52
		return false;
ds@2040
    53
	}
ds@2040
    54
ds@2040
    55
	IMAGE_IMPORT_DESCRIPTOR *desc = 
ds@2040
    56
		(IMAGE_IMPORT_DESCRIPTOR *)
ds@2040
    57
		(base + nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
ds@2040
    58
	if (base == (BYTE *)desc)
ds@2040
    59
	{
ds@2040
    60
		UTIL_Format(err, maxlength, "%s", "Could not find IAT");
ds@2040
    61
		return false;
ds@2040
    62
	}
ds@2040
    63
ds@2040
    64
	bool entryFound = false;
ds@2040
    65
	while (desc->Name)
ds@2040
    66
	{
ds@2040
    67
		if (desc->FirstThunk != 0)
ds@2040
    68
		{
ds@2040
    69
			IMAGE_THUNK_DATA *data = (IMAGE_THUNK_DATA *)(base + desc->OriginalFirstThunk);
ds@2040
    70
			DWORD *iat = (DWORD *)(base + desc->FirstThunk);
ds@2040
    71
			while (data->u1.Function)
ds@2040
    72
			{
ds@2040
    73
				if ((data->u1.Ordinal & IMAGE_ORDINAL_FLAG32) != IMAGE_ORDINAL_FLAG32)
ds@2040
    74
				{
ds@2040
    75
					IMAGE_IMPORT_BY_NAME *import = (IMAGE_IMPORT_BY_NAME *)(base + data->u1.AddressOfData);
ds@2040
    76
					if (strcmp((char *)import->Name, func) == 0)
ds@2040
    77
					{
ds@2040
    78
						DWORD oldprot, oldprot2;
ds@2040
    79
						VirtualProtect(iat, 4, PAGE_READWRITE, &oldprot);
ds@2040
    80
						*iat = hookfunc;
ds@2040
    81
						VirtualProtect(iat, 4, oldprot, &oldprot2);
ds@2040
    82
						entryFound = true;
ds@2040
    83
					}
ds@2040
    84
				}
ds@2040
    85
				data++;
ds@2040
    86
				iat++;
ds@2040
    87
			}
ds@2040
    88
		}
ds@2040
    89
		desc++;
ds@2040
    90
	}
ds@2040
    91
ds@2040
    92
	if (!entryFound)
ds@2040
    93
	{
ds@2040
    94
		UTIL_Format(err, maxlength, "Could not find IAT entry for %s", func);
ds@2040
    95
		return false;
ds@2040
    96
	}
ds@2040
    97
ds@2040
    98
	return true;
ds@2040
    99
}
ds@1952
   100
ds@1952
   101
BOOL
ds@1952
   102
WINAPI
ds@1952
   103
MiniDumpWriteDump2(
ds@1952
   104
				  IN HANDLE hProcess,
ds@1952
   105
				  IN DWORD ProcessId,
ds@1952
   106
				  IN HANDLE hFile,
ds@1952
   107
				  IN MINIDUMP_TYPE DumpType,
ds@1952
   108
				  IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, OPTIONAL
ds@1952
   109
				  IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, OPTIONAL
ds@1952
   110
				  IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam OPTIONAL
ds@1952
   111
				  )
ds@1952
   112
{
ds@1952
   113
	DumpType = (MINIDUMP_TYPE)((int)DumpType|MiniDumpWithFullMemory|MiniDumpWithHandleData);
ds@1952
   114
	return MiniDumpWriteDump(hProcess, ProcessId, hFile, DumpType, ExceptionParam, UserStreamParam, CallbackParam);
ds@1952
   115
}
ds@1952
   116
ds@1952
   117
FARPROC WINAPI GetProcAddress2(HMODULE hModule, LPCSTR lpProcName)
ds@1952
   118
{
ds@1952
   119
	if (strcmp(lpProcName, "MiniDumpWriteDump") == 0)
ds@1952
   120
	{
ds@1952
   121
		return (FARPROC)MiniDumpWriteDump2;
ds@1952
   122
	}
ds@1952
   123
ds@1952
   124
	return GetProcAddress(hModule, lpProcName);
ds@1952
   125
}
ds@1952
   126
ds@2040
   127
HMODULE WINAPI LoadLibraryEx2(LPCTSTR lpFileName, HANDLE hFile, DWORD dwFlags)
ds@2040
   128
{
ds@2040
   129
	HMODULE lib = LoadLibraryEx(lpFileName, hFile, dwFlags);
ds@2040
   130
ds@2040
   131
	/* Steam.dll seems to get loaded using an abs path, thus the use of stristr() */
ds@2040
   132
	if (stristr(lpFileName, "steam.dll"))
ds@2040
   133
	{
ds@2040
   134
		char err[64];
ds@2040
   135
		if (!HookImportAddrTable((BYTE *)lib, "GetProcAddress", (DWORD)GetProcAddress2, err, sizeof(err)))
ds@2040
   136
		{
ds@2040
   137
			Error("[SM] %s in steam.dll\n", err);
ds@2040
   138
			return NULL;
ds@2040
   139
		}
ds@2040
   140
	}
ds@2040
   141
ds@2040
   142
	return lib;
ds@2040
   143
}
ds@2040
   144
ds@1952
   145
class CrazyWindowsDebugger : public SMGlobalClass
ds@1952
   146
{
ds@1952
   147
public:
ds@1952
   148
	void OnSourceModAllInitialized()
ds@1952
   149
	{
ds@1952
   150
		HANDLE hModuleList = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
ds@1952
   151
		MODULEENTRY32 me32;
ds@2040
   152
		BYTE *engine = NULL;
ds@2040
   153
		BYTE *steam = NULL;
ds@2040
   154
		char err[64];
ds@1952
   155
ds@1952
   156
		me32.dwSize = sizeof(MODULEENTRY32);
ds@1952
   157
ds@1952
   158
		if (!Module32First(hModuleList, &me32))
ds@1952
   159
		{
ds@1952
   160
			Error("Could not initialize crazy debugger!");
ds@1952
   161
		}
ds@1952
   162
ds@1952
   163
		do
ds@1952
   164
		{
ds@2040
   165
			if (strcasecmp(me32.szModule, "engine.dll") == 0)
ds@1952
   166
			{
ds@2040
   167
				engine = me32.modBaseAddr;
ds@2040
   168
			}
ds@2040
   169
			else if (strcasecmp(me32.szModule, "steam.dll") == 0)
ds@2040
   170
			{
ds@2040
   171
				steam = me32.modBaseAddr;
ds@1952
   172
			}
ds@1952
   173
		} while  (Module32Next(hModuleList, &me32));
ds@1952
   174
ds@2040
   175
		CloseHandle(hModuleList);
ds@1952
   176
ds@2040
   177
		/* This really should not happen, but ... */
ds@2040
   178
		if (!engine)
ds@1952
   179
		{
ds@2040
   180
			Error("[SM] Could not find engine.dll\n");
ds@1952
   181
		}
ds@1952
   182
ds@2040
   183
		/* Steam.dll isn't loaded yet */
ds@2040
   184
		if (!steam)
ds@2040
   185
		{
ds@2040
   186
			if (!HookImportAddrTable(engine, "LoadLibraryExA", (DWORD)LoadLibraryEx2, err, sizeof(err)))
ds@2040
   187
			{
ds@2040
   188
				Error("[SM] %s in engine.dll)\n", err);
ds@2040
   189
			}
ds@2040
   190
		}
ds@2040
   191
		else
ds@2040
   192
		{
ds@2040
   193
			if (!HookImportAddrTable(steam, "GetProcAddress", (DWORD)GetProcAddress2, err, sizeof(err)))
ds@2040
   194
			{
ds@2040
   195
				Error("[SM] %s in steam.dll)\n", err);
ds@2040
   196
			}
ds@2040
   197
		}
ds@1952
   198
	}
ds@1952
   199
} s_CrazyDebugger;