| 1 | /* |
|---|
| 2 | * zzuf - general purpose fuzzer |
|---|
| 3 | * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * This program is free software. It comes without any warranty, to |
|---|
| 7 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 8 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 9 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | /* |
|---|
| 14 | * sys.c: system-dependent initialisation |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #include "config.h" |
|---|
| 18 | |
|---|
| 19 | #if defined HAVE_STDINT_H |
|---|
| 20 | # include <stdint.h> |
|---|
| 21 | #elif defined HAVE_INTTYPES_H |
|---|
| 22 | # include <inttypes.h> |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | #if defined HAVE_WINDOWS_H |
|---|
| 26 | # include <windows.h> |
|---|
| 27 | # include <imagehlp.h> |
|---|
| 28 | # include <tlhelp32.h> |
|---|
| 29 | # define import_t PIMAGE_IMPORT_DESCRIPTOR |
|---|
| 30 | # define thunk_t PIMAGE_THUNK_DATA |
|---|
| 31 | #endif |
|---|
| 32 | |
|---|
| 33 | #include <stdio.h> |
|---|
| 34 | |
|---|
| 35 | #include "sys.h" |
|---|
| 36 | #include "lib-load.h" |
|---|
| 37 | |
|---|
| 38 | #if defined HAVE_WINDOWS_H |
|---|
| 39 | static void insert_funcs(void *); |
|---|
| 40 | |
|---|
| 41 | /* TODO: get rid of this later */ |
|---|
| 42 | HINSTANCE (WINAPI *LoadLibraryA_orig)(LPCSTR); |
|---|
| 43 | HINSTANCE WINAPI LoadLibraryA_new(LPCSTR path) |
|---|
| 44 | { |
|---|
| 45 | return LoadLibraryA_orig(path); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | BOOL (WINAPI *AllocConsole_orig)(void); |
|---|
| 49 | BOOL WINAPI AllocConsole_new(void) |
|---|
| 50 | { |
|---|
| 51 | return AllocConsole_orig(); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | BOOL (WINAPI *AttachConsole_orig)(DWORD); |
|---|
| 55 | BOOL WINAPI AttachConsole_new(DWORD d) |
|---|
| 56 | { |
|---|
| 57 | return AttachConsole_orig(d); |
|---|
| 58 | } |
|---|
| 59 | #endif |
|---|
| 60 | |
|---|
| 61 | void _zz_sys_init(void) |
|---|
| 62 | { |
|---|
| 63 | #if defined HAVE_WINDOWS_H |
|---|
| 64 | MEMORY_BASIC_INFORMATION mbi; |
|---|
| 65 | MODULEENTRY32 entry; |
|---|
| 66 | void *list; |
|---|
| 67 | int k; |
|---|
| 68 | |
|---|
| 69 | VirtualQuery(_zz_sys_init, &mbi, sizeof(mbi)); |
|---|
| 70 | list = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId()); |
|---|
| 71 | entry.dwSize = sizeof(entry); |
|---|
| 72 | for(k = Module32First(list, &entry); k; k = Module32Next(list, &entry)) |
|---|
| 73 | { |
|---|
| 74 | if(entry.hModule == mbi.AllocationBase) |
|---|
| 75 | continue; /* Don't replace our own functions */ |
|---|
| 76 | |
|---|
| 77 | insert_funcs(entry.hModule); |
|---|
| 78 | } |
|---|
| 79 | CloseHandle(list); |
|---|
| 80 | #else |
|---|
| 81 | /* Nothing to do on our platform */ |
|---|
| 82 | #endif |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | #if defined HAVE_WINDOWS_H |
|---|
| 86 | static void insert_funcs(void *module) |
|---|
| 87 | { |
|---|
| 88 | static zzuf_table_t *list[] = |
|---|
| 89 | { |
|---|
| 90 | table_win32, |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | zzuf_table_t *diversion; |
|---|
| 94 | void *lib; |
|---|
| 95 | unsigned long dummy; |
|---|
| 96 | import_t import; |
|---|
| 97 | thunk_t thunk; |
|---|
| 98 | int k, j, i; |
|---|
| 99 | |
|---|
| 100 | import = (import_t) |
|---|
| 101 | ImageDirectoryEntryToData(module, TRUE, |
|---|
| 102 | IMAGE_DIRECTORY_ENTRY_IMPORT, &dummy); |
|---|
| 103 | if(!import) |
|---|
| 104 | return; |
|---|
| 105 | |
|---|
| 106 | for (k = 0, diversion = NULL; k < sizeof(list) / sizeof(*list); ) |
|---|
| 107 | { |
|---|
| 108 | if (!diversion) |
|---|
| 109 | diversion = list[k]; |
|---|
| 110 | |
|---|
| 111 | if (!diversion->lib) |
|---|
| 112 | { |
|---|
| 113 | k++; |
|---|
| 114 | diversion = NULL; |
|---|
| 115 | continue; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | lib = GetModuleHandleA(diversion->lib); |
|---|
| 119 | *diversion->old = (void *)GetProcAddress(lib, diversion->name); |
|---|
| 120 | |
|---|
| 121 | for(j = 0; import[j].Name; j++) |
|---|
| 122 | { |
|---|
| 123 | char *name = (char *)module + import[j].Name; |
|---|
| 124 | if(lstrcmpiA(name, diversion->lib) != 0) |
|---|
| 125 | continue; |
|---|
| 126 | |
|---|
| 127 | thunk = (thunk_t)((char *)module + import->FirstThunk); |
|---|
| 128 | for(i = 0; thunk[i].u1.Function; i++) |
|---|
| 129 | { |
|---|
| 130 | void **func = (void **)&thunk[i].u1.Function; |
|---|
| 131 | if(*func != *diversion->old) |
|---|
| 132 | continue; |
|---|
| 133 | |
|---|
| 134 | /* FIXME: The StarCraft 2 hack uses two methods for function |
|---|
| 135 | * diversion. See HookSsdt() and HookHotPatch(). */ |
|---|
| 136 | VirtualProtect(func, sizeof(func), PAGE_EXECUTE_READWRITE, &dummy); |
|---|
| 137 | WriteProcessMemory(GetCurrentProcess(), func, &diversion->new, |
|---|
| 138 | sizeof(diversion->new), NULL); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | diversion++; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | #endif |
|---|