José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 3 | * Copyright 2016 VMware, Inc. |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 4 | * Copyright 2011-2012 Jose Fonseca |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | * |
| 25 | **************************************************************************/ |
| 26 | |
| 27 | |
| 28 | /* |
| 29 | * Code for the DLL that will be injected in the target process. |
| 30 | * |
| 31 | * The injected DLL will manipulate the import tables to hook the |
| 32 | * modules/functions of interest. |
| 33 | * |
| 34 | * See also: |
| 35 | * - http://www.codeproject.com/KB/system/api_spying_hack.aspx |
| 36 | * - http://www.codeproject.com/KB/threads/APIHooking.aspx |
| 37 | * - http://msdn.microsoft.com/en-us/magazine/cc301808.aspx |
| 38 | */ |
| 39 | |
| 40 | |
| 41 | #include <assert.h> |
| 42 | #include <stdio.h> |
| 43 | #include <stdarg.h> |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 44 | #include <string.h> |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 45 | |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 46 | #include <algorithm> |
Jose Fonseca | 159d62d | 2014-06-18 14:54:47 +0100 | [diff] [blame] | 47 | #include <set> |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 48 | #include <map> |
| 49 | #include <functional> |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 50 | #include <iterator> |
Jose Fonseca | 159d62d | 2014-06-18 14:54:47 +0100 | [diff] [blame] | 51 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 52 | #include <windows.h> |
| 53 | #include <tlhelp32.h> |
José Fonseca | 8255d9e | 2014-09-12 09:10:02 +0100 | [diff] [blame] | 54 | #include <delayimp.h> |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 55 | |
| 56 | #include "inject.h" |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 57 | #include "mhook.h" |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 58 | |
Jose Fonseca | eb7e874 | 2017-08-04 16:49:02 +0100 | [diff] [blame] | 59 | #include "os_symbols.hpp" |
| 60 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 61 | |
Jose Fonseca | 59f21c4 | 2015-07-06 16:41:55 +0100 | [diff] [blame] | 62 | static int VERBOSITY = 0; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 63 | #define NOOP 0 |
| 64 | |
| 65 | |
Jose Fonseca | 94bbf78 | 2015-11-16 15:02:00 +0000 | [diff] [blame] | 66 | static CRITICAL_SECTION g_Mutex; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 67 | |
| 68 | |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 69 | |
| 70 | static HMODULE g_hThisModule = NULL; |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 71 | static HMODULE g_hHookModule = NULL; |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 72 | |
| 73 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 74 | static std::map<PVOID, PVOID> |
| 75 | g_pRealFunctions; |
| 76 | |
| 77 | |
Jose Fonseca | 8a15426 | 2016-09-19 22:35:44 +0100 | [diff] [blame] | 78 | typedef HMODULE |
| 79 | (WINAPI *PFNLOADLIBRARYA)(LPCSTR); |
| 80 | |
| 81 | typedef HMODULE |
| 82 | (WINAPI *PFNLOADLIBRARYW)(LPCWSTR); |
| 83 | |
| 84 | typedef HMODULE |
| 85 | (WINAPI *PFNLOADLIBRARYEXA)(LPCSTR, HANDLE, DWORD); |
| 86 | |
| 87 | typedef HMODULE |
| 88 | (WINAPI *PFNLOADLIBRARYEXW)(LPCWSTR, HANDLE, DWORD); |
| 89 | |
| 90 | typedef BOOL |
| 91 | (WINAPI *PFNFREELIBRARY)(HMODULE); |
| 92 | |
| 93 | static PFNLOADLIBRARYA RealLoadLibraryA = LoadLibraryA; |
| 94 | static PFNLOADLIBRARYW RealLoadLibraryW = LoadLibraryW; |
| 95 | static PFNLOADLIBRARYEXA RealLoadLibraryExA = LoadLibraryExA; |
| 96 | static PFNLOADLIBRARYEXW RealLoadLibraryExW = LoadLibraryExW; |
| 97 | static PFNFREELIBRARY RealFreeLibrary = FreeLibrary; |
| 98 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 99 | typedef FARPROC (WINAPI * PFNGETPROCADDRESS)(HMODULE hModule, LPCSTR lpProcName); |
| 100 | |
| 101 | static PFNGETPROCADDRESS RealGetProcAddress = GetProcAddress; |
| 102 | |
Jose Fonseca | 1706ffd | 2016-09-18 20:39:11 +0100 | [diff] [blame] | 103 | typedef BOOL |
| 104 | (WINAPI *PFNCREATEPROCESSA) (LPCSTR, LPSTR, |
| 105 | LPSECURITY_ATTRIBUTES, LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, |
| 106 | LPCSTR, LPSTARTUPINFOA, LPPROCESS_INFORMATION); |
| 107 | |
| 108 | static PFNCREATEPROCESSA RealCreateProcessA = CreateProcessA; |
| 109 | |
| 110 | typedef BOOL |
| 111 | (WINAPI *PFNCREATEPROCESSW) (LPCWSTR, LPWSTR, |
| 112 | LPSECURITY_ATTRIBUTES, LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, |
| 113 | LPCWSTR, LPSTARTUPINFOW, LPPROCESS_INFORMATION); |
| 114 | |
| 115 | static PFNCREATEPROCESSW RealCreateProcessW = CreateProcessW; |
| 116 | |
| 117 | typedef BOOL |
| 118 | (WINAPI *PFNCREATEPROCESSASUSERW) (HANDLE, LPCWSTR, LPWSTR, |
| 119 | LPSECURITY_ATTRIBUTES, LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, |
| 120 | LPCWSTR, LPSTARTUPINFOW, LPPROCESS_INFORMATION); |
| 121 | |
| 122 | static PFNCREATEPROCESSASUSERW RealCreateProcessAsUserW = CreateProcessAsUserW; |
| 123 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 124 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 125 | static void |
| 126 | debugPrintf(const char *format, ...) |
| 127 | { |
José Fonseca | 1a617aa | 2013-02-22 09:41:54 +0000 | [diff] [blame] | 128 | char buf[512]; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 129 | |
| 130 | va_list ap; |
| 131 | va_start(ap, format); |
| 132 | _vsnprintf(buf, sizeof buf, format, ap); |
| 133 | va_end(ap); |
| 134 | |
| 135 | OutputDebugStringA(buf); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
Jose Fonseca | 5c9ad11 | 2015-07-06 16:42:21 +0100 | [diff] [blame] | 139 | EXTERN_C void |
| 140 | _assert(const char *_Message, const char *_File, unsigned _Line) |
| 141 | { |
| 142 | debugPrintf("Assertion failed: %s, file %s, line %u\n", _Message, _File, _Line); |
| 143 | TerminateProcess(GetCurrentProcess(), 1); |
| 144 | } |
| 145 | |
| 146 | |
Robert Tarasov | 34921b2 | 2020-11-14 18:04:40 -0800 | [diff] [blame^] | 147 | #ifndef __MINGW32__ |
| 148 | |
Jose Fonseca | 5c9ad11 | 2015-07-06 16:42:21 +0100 | [diff] [blame] | 149 | EXTERN_C void |
| 150 | _wassert(const wchar_t * _Message, const wchar_t *_File, unsigned _Line) |
| 151 | { |
| 152 | debugPrintf("Assertion failed: %S, file %S, line %u\n", _Message, _File, _Line); |
| 153 | TerminateProcess(GetCurrentProcess(), 1); |
| 154 | } |
| 155 | |
Robert Tarasov | 34921b2 | 2020-11-14 18:04:40 -0800 | [diff] [blame^] | 156 | #endif /* !__MINGW32__ */ |
| 157 | |
Jose Fonseca | 5c9ad11 | 2015-07-06 16:42:21 +0100 | [diff] [blame] | 158 | |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 159 | static void |
| 160 | MyCreateProcessCommon(BOOL bRet, |
| 161 | DWORD dwCreationFlags, |
| 162 | LPPROCESS_INFORMATION lpProcessInformation) |
| 163 | { |
| 164 | if (!bRet) { |
Jose Fonseca | 6ddfd98 | 2015-08-03 11:58:35 +0100 | [diff] [blame] | 165 | debugPrintf("inject: warning: failed to create child process\n"); |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 169 | DWORD dwLastError = GetLastError(); |
| 170 | |
Jose Fonseca | 6ddfd98 | 2015-08-03 11:58:35 +0100 | [diff] [blame] | 171 | if (isDifferentArch(lpProcessInformation->hProcess)) { |
| 172 | debugPrintf("inject: error: child process %lu has different architecture\n", |
| 173 | GetProcessId(lpProcessInformation->hProcess)); |
| 174 | } else { |
| 175 | char szDllPath[MAX_PATH]; |
| 176 | GetModuleFileNameA(g_hThisModule, szDllPath, sizeof szDllPath); |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 177 | |
Jose Fonseca | 6ddfd98 | 2015-08-03 11:58:35 +0100 | [diff] [blame] | 178 | if (!injectDll(lpProcessInformation->hProcess, szDllPath)) { |
| 179 | debugPrintf("inject: warning: failed to inject into child process %lu\n", |
| 180 | GetProcessId(lpProcessInformation->hProcess)); |
| 181 | } |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (!(dwCreationFlags & CREATE_SUSPENDED)) { |
| 185 | ResumeThread(lpProcessInformation->hThread); |
| 186 | } |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 187 | |
| 188 | SetLastError(dwLastError); |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 189 | } |
| 190 | |
José Fonseca | 51a7017 | 2014-06-19 13:23:14 +0100 | [diff] [blame] | 191 | static BOOL WINAPI |
| 192 | MyCreateProcessA(LPCSTR lpApplicationName, |
| 193 | LPSTR lpCommandLine, |
| 194 | LPSECURITY_ATTRIBUTES lpProcessAttributes, |
| 195 | LPSECURITY_ATTRIBUTES lpThreadAttributes, |
| 196 | BOOL bInheritHandles, |
| 197 | DWORD dwCreationFlags, |
| 198 | LPVOID lpEnvironment, |
| 199 | LPCSTR lpCurrentDirectory, |
| 200 | LPSTARTUPINFOA lpStartupInfo, |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 201 | LPPROCESS_INFORMATION lpProcessInformation) |
| 202 | { |
| 203 | if (VERBOSITY >= 2) { |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 204 | debugPrintf("inject: intercepting %s(\"%s\", \"%s\", ...)\n", |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 205 | __FUNCTION__, |
| 206 | lpApplicationName, |
| 207 | lpCommandLine); |
| 208 | } |
| 209 | |
| 210 | BOOL bRet; |
Jose Fonseca | 1706ffd | 2016-09-18 20:39:11 +0100 | [diff] [blame] | 211 | bRet = RealCreateProcessA(lpApplicationName, |
| 212 | lpCommandLine, |
| 213 | lpProcessAttributes, |
| 214 | lpThreadAttributes, |
| 215 | bInheritHandles, |
| 216 | dwCreationFlags | CREATE_SUSPENDED, |
| 217 | lpEnvironment, |
| 218 | lpCurrentDirectory, |
| 219 | lpStartupInfo, |
| 220 | lpProcessInformation); |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 221 | |
| 222 | MyCreateProcessCommon(bRet, dwCreationFlags, lpProcessInformation); |
| 223 | |
| 224 | return bRet; |
| 225 | } |
José Fonseca | 51a7017 | 2014-06-19 13:23:14 +0100 | [diff] [blame] | 226 | |
| 227 | static BOOL WINAPI |
| 228 | MyCreateProcessW(LPCWSTR lpApplicationName, |
| 229 | LPWSTR lpCommandLine, |
| 230 | LPSECURITY_ATTRIBUTES lpProcessAttributes, |
| 231 | LPSECURITY_ATTRIBUTES lpThreadAttributes, |
| 232 | BOOL bInheritHandles, |
| 233 | DWORD dwCreationFlags, |
| 234 | LPVOID lpEnvironment, |
| 235 | LPCWSTR lpCurrentDirectory, |
| 236 | LPSTARTUPINFOW lpStartupInfo, |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 237 | LPPROCESS_INFORMATION lpProcessInformation) |
| 238 | { |
| 239 | if (VERBOSITY >= 2) { |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 240 | debugPrintf("inject: intercepting %s(\"%S\", \"%S\", ...)\n", |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 241 | __FUNCTION__, |
| 242 | lpApplicationName, |
| 243 | lpCommandLine); |
| 244 | } |
| 245 | |
| 246 | BOOL bRet; |
Jose Fonseca | 1706ffd | 2016-09-18 20:39:11 +0100 | [diff] [blame] | 247 | bRet = RealCreateProcessW(lpApplicationName, |
| 248 | lpCommandLine, |
| 249 | lpProcessAttributes, |
| 250 | lpThreadAttributes, |
| 251 | bInheritHandles, |
| 252 | dwCreationFlags | CREATE_SUSPENDED, |
| 253 | lpEnvironment, |
| 254 | lpCurrentDirectory, |
| 255 | lpStartupInfo, |
| 256 | lpProcessInformation); |
José Fonseca | 02c6ece | 2014-08-26 15:32:28 +0100 | [diff] [blame] | 257 | |
| 258 | MyCreateProcessCommon(bRet, dwCreationFlags, lpProcessInformation); |
| 259 | |
| 260 | return bRet; |
| 261 | } |
José Fonseca | 51a7017 | 2014-06-19 13:23:14 +0100 | [diff] [blame] | 262 | |
José Fonseca | 193c5b2 | 2014-08-26 15:32:56 +0100 | [diff] [blame] | 263 | static BOOL WINAPI |
José Fonseca | 193c5b2 | 2014-08-26 15:32:56 +0100 | [diff] [blame] | 264 | MyCreateProcessAsUserW(HANDLE hToken, |
| 265 | LPCWSTR lpApplicationName, |
| 266 | LPWSTR lpCommandLine, |
| 267 | LPSECURITY_ATTRIBUTES lpProcessAttributes, |
| 268 | LPSECURITY_ATTRIBUTES lpThreadAttributes, |
| 269 | BOOL bInheritHandles, |
| 270 | DWORD dwCreationFlags, |
| 271 | LPVOID lpEnvironment, |
| 272 | LPCWSTR lpCurrentDirectory, |
| 273 | LPSTARTUPINFOW lpStartupInfo, |
| 274 | LPPROCESS_INFORMATION lpProcessInformation) |
| 275 | { |
| 276 | if (VERBOSITY >= 2) { |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 277 | debugPrintf("inject: intercepting %s(\"%S\", \"%S\", ...)\n", |
José Fonseca | 193c5b2 | 2014-08-26 15:32:56 +0100 | [diff] [blame] | 278 | __FUNCTION__, |
| 279 | lpApplicationName, |
| 280 | lpCommandLine); |
| 281 | } |
| 282 | |
| 283 | BOOL bRet; |
Jose Fonseca | 1706ffd | 2016-09-18 20:39:11 +0100 | [diff] [blame] | 284 | bRet = RealCreateProcessAsUserW(hToken, |
| 285 | lpApplicationName, |
| 286 | lpCommandLine, |
| 287 | lpProcessAttributes, |
| 288 | lpThreadAttributes, |
| 289 | bInheritHandles, |
Jose Fonseca | 460cf72 | 2020-04-15 15:39:35 +0100 | [diff] [blame] | 290 | dwCreationFlags | CREATE_SUSPENDED, |
Jose Fonseca | 1706ffd | 2016-09-18 20:39:11 +0100 | [diff] [blame] | 291 | lpEnvironment, |
| 292 | lpCurrentDirectory, |
| 293 | lpStartupInfo, |
| 294 | lpProcessInformation); |
José Fonseca | 193c5b2 | 2014-08-26 15:32:56 +0100 | [diff] [blame] | 295 | |
| 296 | MyCreateProcessCommon(bRet, dwCreationFlags, lpProcessInformation); |
| 297 | |
| 298 | return bRet; |
| 299 | } |
| 300 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 301 | |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 302 | template< class T, class I > |
| 303 | inline T * |
| 304 | rvaToVa(HMODULE hModule, I rva) |
| 305 | { |
José Fonseca | 8255d9e | 2014-09-12 09:10:02 +0100 | [diff] [blame] | 306 | assert(rva != 0); |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 307 | return reinterpret_cast<T *>(reinterpret_cast<PBYTE>(hModule) + rva); |
| 308 | } |
| 309 | |
| 310 | |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 311 | static PIMAGE_OPTIONAL_HEADER |
Jose Fonseca | 8a25d7e | 2015-10-09 16:06:25 +0100 | [diff] [blame] | 312 | getOptionalHeader(HMODULE hModule, |
| 313 | const char *szModule) |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 314 | { |
José Fonseca | 8255d9e | 2014-09-12 09:10:02 +0100 | [diff] [blame] | 315 | PIMAGE_DOS_HEADER pDosHeader = reinterpret_cast<PIMAGE_DOS_HEADER>(hModule); |
Jose Fonseca | 8a25d7e | 2015-10-09 16:06:25 +0100 | [diff] [blame] | 316 | if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE) { |
| 317 | debugPrintf("inject: warning: %s: unexpected DOS header magic (0x%04x)\n", |
| 318 | szModule, pDosHeader->e_magic); |
| 319 | return NULL; |
| 320 | } |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 321 | PIMAGE_NT_HEADERS pNtHeaders = rvaToVa<IMAGE_NT_HEADERS>(hModule, pDosHeader->e_lfanew); |
Jose Fonseca | 8a25d7e | 2015-10-09 16:06:25 +0100 | [diff] [blame] | 322 | if (pNtHeaders->Signature != IMAGE_NT_SIGNATURE) { |
| 323 | debugPrintf("inject: warning: %s: unexpected NT header signature (0x%08lx)\n", |
| 324 | szModule, pNtHeaders->Signature); |
| 325 | return NULL; |
| 326 | } |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 327 | PIMAGE_OPTIONAL_HEADER pOptionalHeader = &pNtHeaders->OptionalHeader; |
| 328 | return pOptionalHeader; |
| 329 | } |
| 330 | |
José Fonseca | 8255d9e | 2014-09-12 09:10:02 +0100 | [diff] [blame] | 331 | static PVOID |
| 332 | getImageDirectoryEntry(HMODULE hModule, |
| 333 | const char *szModule, |
| 334 | UINT Entry) |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 335 | { |
| 336 | MEMORY_BASIC_INFORMATION MemoryInfo; |
| 337 | if (VirtualQuery(hModule, &MemoryInfo, sizeof MemoryInfo) != sizeof MemoryInfo) { |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 338 | debugPrintf("inject: warning: %s: VirtualQuery failed\n", szModule); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 339 | return NULL; |
| 340 | } |
| 341 | if (MemoryInfo.Protect & (PAGE_NOACCESS | PAGE_EXECUTE)) { |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 342 | debugPrintf("inject: warning: %s: no read access (Protect = 0x%08lx)\n", szModule, MemoryInfo.Protect); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
| 345 | |
Jose Fonseca | 8a25d7e | 2015-10-09 16:06:25 +0100 | [diff] [blame] | 346 | PIMAGE_OPTIONAL_HEADER pOptionalHeader = getOptionalHeader(hModule, szModule); |
| 347 | if (!pOptionalHeader || |
| 348 | pOptionalHeader->DataDirectory[Entry].Size == 0) { |
José Fonseca | 387352f | 2014-09-03 13:05:57 +0100 | [diff] [blame] | 349 | return NULL; |
| 350 | } |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 351 | |
José Fonseca | 8255d9e | 2014-09-12 09:10:02 +0100 | [diff] [blame] | 352 | UINT_PTR ImportAddress = pOptionalHeader->DataDirectory[Entry].VirtualAddress; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 353 | if (!ImportAddress) { |
| 354 | return NULL; |
| 355 | } |
| 356 | |
José Fonseca | 8255d9e | 2014-09-12 09:10:02 +0100 | [diff] [blame] | 357 | return rvaToVa<VOID>(hModule, ImportAddress); |
| 358 | } |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 359 | |
José Fonseca | 8255d9e | 2014-09-12 09:10:02 +0100 | [diff] [blame] | 360 | |
José Fonseca | 387352f | 2014-09-03 13:05:57 +0100 | [diff] [blame] | 361 | static PIMAGE_EXPORT_DIRECTORY |
| 362 | getExportDescriptor(HMODULE hModule) |
| 363 | { |
José Fonseca | 8255d9e | 2014-09-12 09:10:02 +0100 | [diff] [blame] | 364 | PVOID pEntry = getImageDirectoryEntry(hModule, "(wrapper)", IMAGE_DIRECTORY_ENTRY_EXPORT); |
| 365 | return reinterpret_cast<PIMAGE_EXPORT_DIRECTORY>(pEntry); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | |
Jose Fonseca | 159d62d | 2014-06-18 14:54:47 +0100 | [diff] [blame] | 369 | /* Set of previously hooked modules */ |
| 370 | static std::set<HMODULE> |
| 371 | g_hHookedModules; |
| 372 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 373 | |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 374 | enum Action { |
| 375 | ACTION_HOOK, |
| 376 | ACTION_UNHOOK, |
| 377 | |
| 378 | }; |
| 379 | |
| 380 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 381 | static void |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 382 | patchModule(HMODULE hModule, |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 383 | const char *szModule, |
| 384 | Action action) |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 385 | { |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 386 | /* Never patch this module */ |
| 387 | if (hModule == g_hThisModule) { |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | /* Never patch our hook module */ |
| 392 | if (hModule == g_hHookModule) { |
| 393 | return; |
| 394 | } |
| 395 | |
Jose Fonseca | 159d62d | 2014-06-18 14:54:47 +0100 | [diff] [blame] | 396 | /* Hook modules only once */ |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 397 | if (action == ACTION_HOOK) { |
| 398 | std::pair< std::set<HMODULE>::iterator, bool > ret; |
Jose Fonseca | 94bbf78 | 2015-11-16 15:02:00 +0000 | [diff] [blame] | 399 | EnterCriticalSection(&g_Mutex); |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 400 | ret = g_hHookedModules.insert(hModule); |
Jose Fonseca | 94bbf78 | 2015-11-16 15:02:00 +0000 | [diff] [blame] | 401 | LeaveCriticalSection(&g_Mutex); |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 402 | if (!ret.second) { |
| 403 | return; |
| 404 | } |
Jose Fonseca | 159d62d | 2014-06-18 14:54:47 +0100 | [diff] [blame] | 405 | } |
| 406 | |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 407 | if (VERBOSITY > 0) { |
| 408 | debugPrintf("inject: found module %s\n", szModule); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 409 | } |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 410 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 411 | const char *szBaseName = getBaseName(szModule); |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 412 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 413 | if (stricmp(szBaseName, "opengl32.dll") != 0 && |
| 414 | stricmp(szBaseName, "dxgi.dll") != 0 && |
| 415 | stricmp(szBaseName, "d3d10.dll") != 0 && |
| 416 | stricmp(szBaseName, "d3d10_1.dll") != 0 && |
| 417 | stricmp(szBaseName, "d3d11.dll") != 0 && |
| 418 | stricmp(szBaseName, "d3d9.dll") != 0 && |
| 419 | stricmp(szBaseName, "d3d8.dll") != 0 && |
| 420 | stricmp(szBaseName, "ddraw.dll") != 0 && |
| 421 | stricmp(szBaseName, "dcomp.dll") != 0 && |
| 422 | stricmp(szBaseName, "dxva2.dll") != 0 && |
| 423 | stricmp(szBaseName, "d2d1.dll") != 0 && |
| 424 | stricmp(szBaseName, "dwrite.dll") != 0) { |
| 425 | return; |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 426 | } |
| 427 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 428 | PIMAGE_EXPORT_DIRECTORY pExportDescriptor = getExportDescriptor(g_hHookModule); |
| 429 | assert(pExportDescriptor); |
| 430 | |
| 431 | DWORD *pAddressOfNames = (DWORD *)((BYTE *)g_hHookModule + pExportDescriptor->AddressOfNames); |
| 432 | for (DWORD i = 0; i < pExportDescriptor->NumberOfNames; ++i) { |
| 433 | const char *szFunctionName = (const char *)((BYTE *)g_hHookModule + pAddressOfNames[i]); |
| 434 | |
| 435 | LPVOID lpOrigAddress = (LPVOID)RealGetProcAddress(hModule, szFunctionName); |
| 436 | if (lpOrigAddress) { |
| 437 | LPVOID lpHookAddress = (LPVOID)RealGetProcAddress(g_hHookModule, szFunctionName); |
| 438 | assert(lpHookAddress); |
| 439 | |
| 440 | // With mhook we intercept the inner gl* calls, so no need to trace the |
| 441 | // outer wglUseFont* calls. |
| 442 | if (strncmp(szFunctionName, "wglUseFont", strlen("wglUseFont")) == 0) { |
| 443 | debugPrintf("inject: not hooking %s!%s\n", szBaseName, szFunctionName); |
| 444 | continue; |
José Fonseca | 8255d9e | 2014-09-12 09:10:02 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 447 | if (VERBOSITY > 0) { |
| 448 | debugPrintf("inject: hooking %s!%s\n", szModule, szFunctionName); |
| 449 | } |
José Fonseca | 8255d9e | 2014-09-12 09:10:02 +0100 | [diff] [blame] | 450 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 451 | LPVOID lpRealAddress = lpOrigAddress; |
| 452 | if (!Mhook_SetHook(&lpRealAddress, lpHookAddress)) { |
| 453 | debugPrintf("inject: error: failed to hook %s!%s\n", szModule, szFunctionName); |
| 454 | } |
| 455 | |
| 456 | EnterCriticalSection(&g_Mutex); |
| 457 | g_pRealFunctions[lpOrigAddress] = lpRealAddress; |
| 458 | LeaveCriticalSection(&g_Mutex); |
| 459 | |
| 460 | pSharedMem->bReplaced = TRUE; |
José Fonseca | 8255d9e | 2014-09-12 09:10:02 +0100 | [diff] [blame] | 461 | } |
| 462 | } |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 463 | } |
| 464 | |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 465 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 466 | static void |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 467 | patchAllModules(Action action) |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 468 | { |
| 469 | HANDLE hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId()); |
| 470 | if (hModuleSnap == INVALID_HANDLE_VALUE) { |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | MODULEENTRY32 me32; |
| 475 | me32.dwSize = sizeof me32; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 476 | if (Module32First(hModuleSnap, &me32)) { |
| 477 | do { |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 478 | patchModule(me32.hModule, me32.szExePath, action); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 479 | } while (Module32Next(hModuleSnap, &me32)); |
| 480 | } |
| 481 | |
| 482 | CloseHandle(hModuleSnap); |
| 483 | } |
| 484 | |
| 485 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 486 | static HMODULE WINAPI |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 487 | MyLoadLibraryA(LPCSTR lpLibFileName) |
| 488 | { |
Jose Fonseca | 8a15426 | 2016-09-19 22:35:44 +0100 | [diff] [blame] | 489 | HMODULE hModule = RealLoadLibraryA(lpLibFileName); |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 490 | DWORD dwLastError = GetLastError(); |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 491 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 492 | if (VERBOSITY >= 2) { |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 493 | debugPrintf("inject: intercepting %s(\"%s\") = 0x%p\n", |
| 494 | __FUNCTION__ + 2, lpLibFileName, hModule); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 495 | } |
| 496 | |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 497 | // Hook all new modules (and not just this one, to pick up any dependencies) |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 498 | patchAllModules(ACTION_HOOK); |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 499 | |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 500 | SetLastError(dwLastError); |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 501 | return hModule; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static HMODULE WINAPI |
| 505 | MyLoadLibraryW(LPCWSTR lpLibFileName) |
| 506 | { |
Jose Fonseca | 8a15426 | 2016-09-19 22:35:44 +0100 | [diff] [blame] | 507 | HMODULE hModule = RealLoadLibraryW(lpLibFileName); |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 508 | DWORD dwLastError = GetLastError(); |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 509 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 510 | if (VERBOSITY >= 2) { |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 511 | debugPrintf("inject: intercepting %s(L\"%S\") = 0x%p\n", |
| 512 | __FUNCTION__ + 2, lpLibFileName, hModule); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 513 | } |
| 514 | |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 515 | // Hook all new modules (and not just this one, to pick up any dependencies) |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 516 | patchAllModules(ACTION_HOOK); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 517 | |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 518 | SetLastError(dwLastError); |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 519 | return hModule; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 520 | } |
| 521 | |
José Fonseca | a1ddea3 | 2015-02-19 20:55:28 +0000 | [diff] [blame] | 522 | #ifndef LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
| 523 | #define LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR 0x00000100 |
| 524 | #endif |
| 525 | #ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR |
| 526 | #define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200 |
| 527 | #endif |
| 528 | #ifndef LOAD_LIBRARY_SEARCH_USER_DIRS |
| 529 | #define LOAD_LIBRARY_SEARCH_USER_DIRS 0x00000400 |
| 530 | #endif |
| 531 | #ifndef LOAD_LIBRARY_SEARCH_SYSTEM32 |
| 532 | #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800 |
| 533 | #endif |
| 534 | #ifndef LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
| 535 | #define LOAD_LIBRARY_SEARCH_DEFAULT_DIRS 0x00001000 |
| 536 | #endif |
| 537 | |
| 538 | static inline DWORD |
| 539 | adjustFlags(DWORD dwFlags) |
| 540 | { |
| 541 | /* |
| 542 | * XXX: LoadLibraryEx seems to interpret "application directory" in respect |
| 543 | * to the module that's calling it. So when the application restricts the |
| 544 | * search path to application directory via |
| 545 | * LOAD_LIBRARY_SEARCH_APPLICATION_DIR or LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
| 546 | * flags, kernel32.dll ends up searching on the directory of the inject.dll |
| 547 | * module. |
| 548 | * |
| 549 | * XXX: What about SetDefaultDllDirectories? |
| 550 | * |
| 551 | */ |
| 552 | if (dwFlags & (LOAD_LIBRARY_SEARCH_APPLICATION_DIR | |
| 553 | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) { |
| 554 | dwFlags &= ~(LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | |
| 555 | LOAD_LIBRARY_SEARCH_APPLICATION_DIR | |
| 556 | LOAD_LIBRARY_SEARCH_USER_DIRS | |
| 557 | LOAD_LIBRARY_SEARCH_SYSTEM32 | |
| 558 | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); |
| 559 | } |
| 560 | |
| 561 | return dwFlags; |
| 562 | } |
| 563 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 564 | static HMODULE WINAPI |
| 565 | MyLoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags) |
| 566 | { |
Jose Fonseca | 8a15426 | 2016-09-19 22:35:44 +0100 | [diff] [blame] | 567 | HMODULE hModule = RealLoadLibraryExA(lpLibFileName, hFile, adjustFlags(dwFlags)); |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 568 | DWORD dwLastError = GetLastError(); |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 569 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 570 | if (VERBOSITY >= 2) { |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 571 | debugPrintf("inject: intercepting %s(\"%s\", 0x%p, 0x%lx) = 0x%p\n", |
| 572 | __FUNCTION__ + 2, lpLibFileName, hFile, dwFlags, hModule); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 573 | } |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 574 | |
| 575 | // Hook all new modules (and not just this one, to pick up any dependencies) |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 576 | patchAllModules(ACTION_HOOK); |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 577 | |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 578 | SetLastError(dwLastError); |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 579 | return hModule; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static HMODULE WINAPI |
| 583 | MyLoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags) |
| 584 | { |
Jose Fonseca | 8a15426 | 2016-09-19 22:35:44 +0100 | [diff] [blame] | 585 | HMODULE hModule = RealLoadLibraryExW(lpLibFileName, hFile, adjustFlags(dwFlags)); |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 586 | DWORD dwLastError = GetLastError(); |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 587 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 588 | if (VERBOSITY >= 2) { |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 589 | debugPrintf("inject: intercepting %s(L\"%S\", 0x%p, 0x%lx) = 0x%p\n", |
| 590 | __FUNCTION__ + 2, lpLibFileName, hFile, dwFlags, hModule); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 591 | } |
| 592 | |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 593 | // Hook all new modules (and not just this one, to pick up any dependencies) |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 594 | patchAllModules(ACTION_HOOK); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 595 | |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 596 | SetLastError(dwLastError); |
José Fonseca | ac114ee | 2015-02-19 20:16:33 +0000 | [diff] [blame] | 597 | return hModule; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 598 | } |
| 599 | |
José Fonseca | fdda386 | 2014-09-10 11:35:30 +0100 | [diff] [blame] | 600 | |
| 601 | static void |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 602 | logGetProcAddress(PCSTR szAction, HMODULE hModule, LPCSTR lpProcName, HMODULE hCallerModule) |
| 603 | { |
| 604 | char szCaller[MAX_PATH]; |
| 605 | DWORD dwRet = GetModuleFileNameA(hCallerModule, szCaller, sizeof szCaller); |
| 606 | assert(dwRet); |
| 607 | |
José Fonseca | fdda386 | 2014-09-10 11:35:30 +0100 | [diff] [blame] | 608 | if (HIWORD(lpProcName) == 0) { |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 609 | debugPrintf("inject: %s %s(%u) from %s\n", szAction, "GetProcAddress", LOWORD(lpProcName), szCaller); |
José Fonseca | fdda386 | 2014-09-10 11:35:30 +0100 | [diff] [blame] | 610 | } else { |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 611 | debugPrintf("inject: %s %s(\"%s\") from %s\n", szAction, "GetProcAddress", lpProcName, szCaller); |
José Fonseca | fdda386 | 2014-09-10 11:35:30 +0100 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 615 | static HMODULE |
| 616 | GetModuleFromAddress(PVOID pAddress) |
| 617 | { |
| 618 | HMODULE hModule = nullptr; |
| 619 | BOOL bRet = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 620 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 621 | (LPCTSTR)pAddress, |
| 622 | &hModule); |
| 623 | return bRet ? hModule : nullptr; |
| 624 | } |
| 625 | |
| 626 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 627 | static FARPROC WINAPI |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 628 | MyGetProcAddress(HMODULE hModule, LPCSTR lpProcName) |
| 629 | { |
| 630 | FARPROC lpProcAddress = RealGetProcAddress(hModule, lpProcName); |
| 631 | LPCSTR szAction = "ignoring"; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 632 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 633 | void *pCallerAddr = ReturnAddress(); |
| 634 | HMODULE hCallerModule = GetModuleFromAddress(pCallerAddr); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 635 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 636 | if (lpProcAddress) { |
| 637 | if (hCallerModule == g_hThisModule || |
| 638 | hCallerModule == g_hHookModule) { |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 639 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 640 | assert(HIWORD(lpProcName) != 0); |
| 641 | |
| 642 | if (hCallerModule == g_hHookModule) { |
| 643 | EnterCriticalSection(&g_Mutex); |
| 644 | auto search = g_pRealFunctions.find((PVOID)lpProcAddress); |
| 645 | if (search != g_pRealFunctions.end()) { |
| 646 | szAction = "overriding"; |
| 647 | lpProcAddress = (FARPROC)search->second; |
| 648 | } |
| 649 | LeaveCriticalSection(&g_Mutex); |
José Fonseca | fdda386 | 2014-09-10 11:35:30 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 652 | // Check for recursion |
| 653 | HMODULE hResultModule = GetModuleFromAddress((PVOID)lpProcAddress); |
| 654 | if (hResultModule == g_hThisModule || |
| 655 | hResultModule == g_hHookModule) { |
| 656 | debugPrintf("inject: error: recursion in GetProcAddress(\"%s\")\n", lpProcName); |
| 657 | TerminateProcess(GetCurrentProcess(), 1); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | } |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 661 | |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 662 | if (VERBOSITY >= 3) { |
| 663 | /* XXX this can cause segmentation faults */ |
| 664 | logGetProcAddress(szAction, hModule, lpProcName, hCallerModule); |
| 665 | } |
| 666 | |
| 667 | return lpProcAddress; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | |
José Fonseca | 1673609 | 2015-02-07 23:16:58 +0000 | [diff] [blame] | 671 | static BOOL WINAPI |
| 672 | MyFreeLibrary(HMODULE hModule) |
| 673 | { |
| 674 | if (VERBOSITY >= 2) { |
| 675 | debugPrintf("inject: intercepting %s(0x%p)\n", __FUNCTION__, hModule); |
| 676 | } |
| 677 | |
Jose Fonseca | 8a15426 | 2016-09-19 22:35:44 +0100 | [diff] [blame] | 678 | BOOL bRet = RealFreeLibrary(hModule); |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 679 | DWORD dwLastError = GetLastError(); |
José Fonseca | 1673609 | 2015-02-07 23:16:58 +0000 | [diff] [blame] | 680 | |
Jose Fonseca | e7daf65 | 2016-09-05 11:31:51 +0100 | [diff] [blame] | 681 | std::set<HMODULE> hCurrentModules; |
| 682 | HANDLE hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId()); |
| 683 | if (hModuleSnap != INVALID_HANDLE_VALUE) { |
| 684 | MODULEENTRY32 me32; |
| 685 | me32.dwSize = sizeof me32; |
| 686 | if (Module32First(hModuleSnap, &me32)) { |
| 687 | do { |
| 688 | hCurrentModules.insert(me32.hModule); |
| 689 | } while (Module32Next(hModuleSnap, &me32)); |
| 690 | } |
| 691 | CloseHandle(hModuleSnap); |
| 692 | } |
| 693 | |
| 694 | // Clear the modules that have been freed |
Jose Fonseca | 94bbf78 | 2015-11-16 15:02:00 +0000 | [diff] [blame] | 695 | EnterCriticalSection(&g_Mutex); |
Jose Fonseca | e7daf65 | 2016-09-05 11:31:51 +0100 | [diff] [blame] | 696 | std::set<HMODULE> hIntersectedModules; |
| 697 | std::set_intersection(g_hHookedModules.begin(), g_hHookedModules.end(), |
| 698 | hCurrentModules.begin(), hCurrentModules.end(), |
| 699 | std::inserter(hIntersectedModules, hIntersectedModules.begin())); |
| 700 | g_hHookedModules = std::move(hIntersectedModules); |
Jose Fonseca | 94bbf78 | 2015-11-16 15:02:00 +0000 | [diff] [blame] | 701 | LeaveCriticalSection(&g_Mutex); |
José Fonseca | 1673609 | 2015-02-07 23:16:58 +0000 | [diff] [blame] | 702 | |
Jose Fonseca | 953907a | 2015-06-26 10:50:16 +0100 | [diff] [blame] | 703 | SetLastError(dwLastError); |
José Fonseca | 1673609 | 2015-02-07 23:16:58 +0000 | [diff] [blame] | 704 | return bRet; |
| 705 | } |
| 706 | |
| 707 | |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 708 | static void |
Jose Fonseca | 1706ffd | 2016-09-18 20:39:11 +0100 | [diff] [blame] | 709 | setHooks(void) |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 710 | { |
Jose Fonseca | 1706ffd | 2016-09-18 20:39:11 +0100 | [diff] [blame] | 711 | HMODULE hKernel32 = GetModuleHandleA("kernel32"); |
| 712 | assert(hKernel32); |
| 713 | |
Jose Fonseca | 35fb465 | 2016-09-19 20:26:29 +0100 | [diff] [blame] | 714 | # define SET_HOOK(_name) \ |
| 715 | Real##_name = reinterpret_cast<decltype(Real##_name)>(RealGetProcAddress(hKernel32, #_name)); \ |
| 716 | assert(Real##_name); \ |
| 717 | assert(Real##_name != My##_name); \ |
| 718 | if (!Mhook_SetHook((PVOID*)&Real##_name, (PVOID)My##_name)) { \ |
| 719 | debugPrintf("inject: error: failed to hook " #_name "\n"); \ |
| 720 | } |
Jose Fonseca | 1706ffd | 2016-09-18 20:39:11 +0100 | [diff] [blame] | 721 | |
Jose Fonseca | 8a15426 | 2016-09-19 22:35:44 +0100 | [diff] [blame] | 722 | SET_HOOK(LoadLibraryA) |
| 723 | SET_HOOK(LoadLibraryW) |
| 724 | SET_HOOK(LoadLibraryExA) |
| 725 | SET_HOOK(LoadLibraryExW) |
| 726 | SET_HOOK(FreeLibrary) |
Jose Fonseca | 35fb465 | 2016-09-19 20:26:29 +0100 | [diff] [blame] | 727 | SET_HOOK(GetProcAddress) |
| 728 | SET_HOOK(CreateProcessA) |
| 729 | SET_HOOK(CreateProcessW) |
| 730 | SET_HOOK(CreateProcessAsUserW) |
Jose Fonseca | 1706ffd | 2016-09-18 20:39:11 +0100 | [diff] [blame] | 731 | |
Jose Fonseca | 35fb465 | 2016-09-19 20:26:29 +0100 | [diff] [blame] | 732 | # undef SET_HOOK |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 733 | } |
| 734 | |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 735 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 736 | EXTERN_C BOOL WINAPI |
Jose Fonseca | b57679e | 2015-07-15 15:24:05 +0100 | [diff] [blame] | 737 | DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 738 | { |
| 739 | const char *szNewDllName = NULL; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 740 | |
| 741 | switch (fdwReason) { |
| 742 | case DLL_PROCESS_ATTACH: |
Jose Fonseca | 94bbf78 | 2015-11-16 15:02:00 +0000 | [diff] [blame] | 743 | InitializeCriticalSection(&g_Mutex); |
Jose Fonseca | 482c17d | 2015-11-16 14:34:42 +0000 | [diff] [blame] | 744 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 745 | g_hThisModule = hinstDLL; |
| 746 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 747 | /* |
| 748 | * Calling LoadLibrary inside DllMain is strongly discouraged. But it |
| 749 | * works quite well, provided that the loaded DLL does not require or do |
| 750 | * anything special in its DllMain, which seems to be the general case. |
| 751 | * |
| 752 | * See also: |
| 753 | * - http://stackoverflow.com/questions/4370812/calling-loadlibrary-from-dllmain |
| 754 | * - http://msdn.microsoft.com/en-us/library/ms682583 |
| 755 | */ |
| 756 | |
Jose Fonseca | 1f2a2c5 | 2014-06-18 15:10:11 +0100 | [diff] [blame] | 757 | if (!USE_SHARED_MEM) { |
| 758 | szNewDllName = getenv("INJECT_DLL"); |
| 759 | if (!szNewDllName) { |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 760 | debugPrintf("inject: warning: INJECT_DLL not set\n"); |
Jose Fonseca | 1f2a2c5 | 2014-06-18 15:10:11 +0100 | [diff] [blame] | 761 | return FALSE; |
| 762 | } |
| 763 | } else { |
Jose Fonseca | 498710d | 2015-07-15 16:07:05 +0100 | [diff] [blame] | 764 | SharedMem *pSharedMem = OpenSharedMemory(NULL); |
Jose Fonseca | 0eb4fe7 | 2015-07-06 16:16:13 +0100 | [diff] [blame] | 765 | if (!pSharedMem) { |
| 766 | debugPrintf("inject: error: failed to open shared memory\n"); |
| 767 | return FALSE; |
| 768 | } |
| 769 | |
Jose Fonseca | 59f21c4 | 2015-07-06 16:41:55 +0100 | [diff] [blame] | 770 | VERBOSITY = pSharedMem->cVerbosity; |
| 771 | |
Jose Fonseca | 1f2a2c5 | 2014-06-18 15:10:11 +0100 | [diff] [blame] | 772 | static char szSharedMemCopy[MAX_PATH]; |
Jose Fonseca | 0eb4fe7 | 2015-07-06 16:16:13 +0100 | [diff] [blame] | 773 | strncpy(szSharedMemCopy, pSharedMem->szDllName, _countof(szSharedMemCopy) - 1); |
| 774 | szSharedMemCopy[_countof(szSharedMemCopy) - 1] = '\0'; |
| 775 | |
Jose Fonseca | 1f2a2c5 | 2014-06-18 15:10:11 +0100 | [diff] [blame] | 776 | szNewDllName = szSharedMemCopy; |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 777 | } |
Jose Fonseca | 1f2a2c5 | 2014-06-18 15:10:11 +0100 | [diff] [blame] | 778 | |
José Fonseca | 1a617aa | 2013-02-22 09:41:54 +0000 | [diff] [blame] | 779 | if (VERBOSITY > 0) { |
Jose Fonseca | 42b31cf | 2015-07-13 12:01:45 +0100 | [diff] [blame] | 780 | debugPrintf("inject: DLL_PROCESS_ATTACH\n"); |
| 781 | } |
| 782 | |
| 783 | if (VERBOSITY > 0) { |
| 784 | char szProcess[MAX_PATH]; |
| 785 | GetModuleFileNameA(NULL, szProcess, sizeof szProcess); |
| 786 | debugPrintf("inject: attached to process %s\n", szProcess); |
| 787 | } |
| 788 | |
| 789 | if (VERBOSITY > 0) { |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 790 | debugPrintf("inject: loading %s\n", szNewDllName); |
José Fonseca | 1a617aa | 2013-02-22 09:41:54 +0000 | [diff] [blame] | 791 | } |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 792 | |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 793 | g_hHookModule = LoadLibraryA(szNewDllName); |
| 794 | if (!g_hHookModule) { |
| 795 | debugPrintf("inject: warning: failed to load %s\n", szNewDllName); |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 796 | return FALSE; |
| 797 | } |
| 798 | |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 799 | patchAllModules(ACTION_HOOK); |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 800 | |
Jose Fonseca | 1706ffd | 2016-09-18 20:39:11 +0100 | [diff] [blame] | 801 | setHooks(); |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 802 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 803 | break; |
| 804 | |
| 805 | case DLL_THREAD_ATTACH: |
| 806 | break; |
| 807 | |
| 808 | case DLL_THREAD_DETACH: |
| 809 | break; |
| 810 | |
| 811 | case DLL_PROCESS_DETACH: |
José Fonseca | 1a617aa | 2013-02-22 09:41:54 +0000 | [diff] [blame] | 812 | if (VERBOSITY > 0) { |
José Fonseca | 5e6332c | 2014-09-03 14:34:44 +0100 | [diff] [blame] | 813 | debugPrintf("inject: DLL_PROCESS_DETACH\n"); |
José Fonseca | 1a617aa | 2013-02-22 09:41:54 +0000 | [diff] [blame] | 814 | } |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 815 | |
Jose Fonseca | b57679e | 2015-07-15 15:24:05 +0100 | [diff] [blame] | 816 | assert(!lpvReserved); |
| 817 | |
José Fonseca | 3e60c24 | 2014-09-26 19:44:29 +0100 | [diff] [blame] | 818 | patchAllModules(ACTION_UNHOOK); |
| 819 | |
| 820 | if (g_hHookModule) { |
| 821 | FreeLibrary(g_hHookModule); |
| 822 | } |
Jose Fonseca | ff5a585 | 2016-09-07 16:06:09 +0100 | [diff] [blame] | 823 | |
| 824 | Mhook_Unhook((PVOID*)&RealGetProcAddress); |
| 825 | |
José Fonseca | bd4937e | 2012-12-04 13:23:03 +0000 | [diff] [blame] | 826 | break; |
| 827 | } |
| 828 | return TRUE; |
| 829 | } |
Jose Fonseca | b57679e | 2015-07-15 15:24:05 +0100 | [diff] [blame] | 830 | |
| 831 | |
| 832 | /* |
| 833 | * Prevent the C/C++ runtime from destroying things when the program |
| 834 | * terminates. |
| 835 | * |
| 836 | * There is no effective way to control the order DLLs receive |
| 837 | * DLL_PROCESS_DETACH -- patched DLLs might get detacched after we are --, and |
| 838 | * unpatching our hooks doesn't always work. So instead just do nothing (and |
| 839 | * prevent C/C++ runtime from doing anything too), so our hooks can still work |
| 840 | * after we are dettached. |
| 841 | */ |
| 842 | |
| 843 | #ifdef _MSC_VER |
| 844 | # define DLLMAIN_CRT_STARTUP _DllMainCRTStartup |
| 845 | #else |
| 846 | # define DLLMAIN_CRT_STARTUP DllMainCRTStartup |
| 847 | # pragma GCC optimize ("no-stack-protector") |
| 848 | #endif |
| 849 | |
| 850 | EXTERN_C BOOL WINAPI |
| 851 | DLLMAIN_CRT_STARTUP(HANDLE hDllHandle, DWORD dwReason, LPVOID lpvReserved); |
| 852 | |
| 853 | EXTERN_C BOOL WINAPI |
| 854 | DllMainStartup(HANDLE hDllHandle, DWORD dwReason, LPVOID lpvReserved) |
| 855 | { |
| 856 | if (dwReason == DLL_PROCESS_DETACH && lpvReserved) { |
| 857 | if (VERBOSITY > 0) { |
| 858 | debugPrintf("inject: DLL_PROCESS_DETACH\n"); |
| 859 | } |
| 860 | return TRUE; |
| 861 | } |
| 862 | |
| 863 | return DLLMAIN_CRT_STARTUP(hDllHandle, dwReason, lpvReserved); |
| 864 | } |