source: zzuf/trunk/src/sys.c @ 2566

Last change on this file since 2566 was 1701, checked in by Sam Hocevar, 14 years ago
  • Added DLL initialisation code for Win32.
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006,2007 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: sys.c 1701 2007-01-23 15:38:18Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  sys.c: system-dependent initialisation
17 */
18
19#include "config.h"
20
21#if defined HAVE_STDINT_H
22#   include <stdint.h>
23#elif defined HAVE_INTTYPES_H
24#   include <inttypes.h>
25#endif
26
27#if defined HAVE_WINDOWS_H
28#   include <windows.h>
29#   include <imagehlp.h>
30#   include <tlhelp32.h>
31#   define import_t PIMAGE_IMPORT_DESCRIPTOR
32#   define thunk_t PIMAGE_THUNK_DATA
33#endif
34
35#include <stdio.h>
36
37#include "sys.h"
38
39#if defined HAVE_WINDOWS_H
40static void insert_func(void *, void *, void *);
41
42/* TODO: get rid of this later */
43HINSTANCE (*LoadLibraryA_orig)(LPCSTR);
44HINSTANCE __stdcall LoadLibraryA_new(LPCSTR path)
45{
46    void *ret = LoadLibraryA_orig(path);
47    fprintf(stderr, "If you see this message, DLL preloading worked\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, *kernel32;
58    int k;
59
60    kernel32 = GetModuleHandleA("kernel32.dll");
61    LoadLibraryA_orig = (void *)GetProcAddress(kernel32, "LoadLibraryA");
62
63    VirtualQuery(_zz_sys_init, &mbi, sizeof(mbi));
64    list = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
65    entry.dwSize = sizeof(entry);
66    for(k = Module32First(list, &entry); k; k = Module32Next(list, &entry))
67    {
68        if(entry.hModule == mbi.AllocationBase)
69            continue; /* Don't replace our own functions */
70
71        insert_func(entry.hModule, LoadLibraryA_orig, LoadLibraryA_new);
72    }
73    CloseHandle(list);
74#else
75    /* Nothing to do on our platform */
76#endif
77}
78
79#if defined HAVE_WINDOWS_H
80static void insert_func(void *module, void *old, void *new)
81{
82    unsigned long dummy;
83    import_t import;
84    thunk_t thunk;
85    int j, i;
86
87    import = (import_t)
88        ImageDirectoryEntryToData(module, TRUE,
89                                  IMAGE_DIRECTORY_ENTRY_IMPORT, &dummy);
90    if(!import)
91        return;
92
93    for(j = 0; import[j].Name; j++)
94    {
95        char *name = (char *)module + import[j].Name;
96        if(lstrcmpiA(name, "kernel32.dll") != 0)
97            continue;
98
99        thunk = (thunk_t)((char *)module + import->FirstThunk);
100        for(i = 0; thunk[i].u1.Function; i++)
101        {
102            void **func = (void **)&thunk[i].u1.Function;
103            if(*func != old)
104                continue;
105
106            VirtualProtect(func, sizeof(func), PAGE_EXECUTE_READWRITE, &dummy);
107            WriteProcessMemory(GetCurrentProcess(), func, &new,
108                               sizeof(new), NULL);
109            return;
110        }
111    }
112}
113#endif
114
Note: See TracBrowser for help on using the repository browser.