source: zzuf/trunk/src/libzzuf/sys.c @ 4649

Last change on this file since 4649 was 4649, checked in by Sam Hocevar, 10 years ago

Refactor the DLL initialisation code to allow several diversions.

  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
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
37#if defined HAVE_WINDOWS_H
38static void insert_funcs(void *);
39
40/* TODO: get rid of this later */
41HINSTANCE (WINAPI *LoadLibraryA_orig)(LPCSTR);
42HINSTANCE WINAPI LoadLibraryA_new(LPCSTR path)
43{
44    void *ret;
45    fprintf(stderr, "This is the diverted LoadLibraryA\n");
46    ret = LoadLibraryA_orig(path);
47    fprintf(stderr, "Now the real LoadLibraryA was called\n");
48    return ret;
49}
50#endif
51
52void _zz_sys_init(void)
53{
54#if defined HAVE_WINDOWS_H
55    MEMORY_BASIC_INFORMATION mbi;
56    MODULEENTRY32 entry;
57    void *list;
58    int k;
59
60    VirtualQuery(_zz_sys_init, &mbi, sizeof(mbi));
61    list = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
62    entry.dwSize = sizeof(entry);
63    for(k = Module32First(list, &entry); k; k = Module32Next(list, &entry))
64    {
65        if(entry.hModule == mbi.AllocationBase)
66            continue; /* Don't replace our own functions */
67
68        insert_funcs(entry.hModule);
69    }
70    CloseHandle(list);
71#else
72    /* Nothing to do on our platform */
73#endif
74}
75
76#if defined HAVE_WINDOWS_H
77static void insert_funcs(void *module)
78{
79    struct { char const *lib, *name; void **old; void *new; }
80    diversions[] =
81    {
82        { "kernel32.dll", "LoadLibraryA", &LoadLibraryA_orig, LoadLibraryA_new },
83    };
84
85    unsigned long dummy;
86    import_t import;
87    thunk_t thunk;
88    int k, j, i;
89
90    import = (import_t)
91        ImageDirectoryEntryToData(module, TRUE,
92                                  IMAGE_DIRECTORY_ENTRY_IMPORT, &dummy);
93    if(!import)
94        return;
95
96    for (k = 0; k < sizeof(diversions) / sizeof(*diversions); k++)
97    {
98        void *lib = GetModuleHandleA(diversions[k].lib);
99        *diversions[k].old = (void *)GetProcAddress(lib, diversions[k].name);
100
101        for(j = 0; import[j].Name; j++)
102        {
103            char *name = (char *)module + import[j].Name;
104            if(lstrcmpiA(name, diversions[k].lib) != 0)
105                continue;
106
107            thunk = (thunk_t)((char *)module + import->FirstThunk);
108            for(i = 0; thunk[i].u1.Function; i++)
109            {
110                void **func = (void **)&thunk[i].u1.Function;
111                if(*func != *diversions[k].old)
112                    continue;
113
114                /* FIXME: The StarCraft 2 hack uses two methods for function
115                 * diversion. See HookSsdt() and HookHotPatch(). */
116                VirtualProtect(func, sizeof(func), PAGE_EXECUTE_READWRITE, &dummy);
117                WriteProcessMemory(GetCurrentProcess(), func, &diversions[k].new,
118                                   sizeof(diversions[k].new), NULL);
119            }
120        }
121    }
122}
123#endif
124
Note: See TracBrowser for help on using the repository browser.