-
Notifications
You must be signed in to change notification settings - Fork 1
/
delayimp.cpp
166 lines (153 loc) · 5.34 KB
/
delayimp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
///////////////////////////////////////////////////////////////////////////////
///
/// Delay-load helper for ntobjx.
/// This code allows us to delay resolving of functions from ntdll.dll to
/// runtime by using a little trick.
///
/// Dual-licensed under MS-PL and MIT license (see below).
///
///////////////////////////////////////////////////////////////////////////////
///
/// Copyright (c) 2016 Oliver Schneider (assarbad.net)
///
/// Permission is hereby granted, free of charge, to any person obtaining a
/// copy of this software and associated documentation files (the "Software"),
/// to deal in the Software without restriction, including without limitation
/// the rights to use, copy, modify, merge, publish, distribute, sublicense,
/// and/or sell copies of the Software, and to permit persons to whom the
/// Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
/// DEALINGS IN THE SOFTWARE.
///
///////////////////////////////////////////////////////////////////////////////
#define DELAYIMP_INSECURE_WRITABLE_HOOKS
#if defined(_CONSOLE) || defined(_CONSOLE_APP)
# include <Windows.h>
# include <tchar.h>
# define DelayLoadError _tprintf
#else
# include "common.h" /* Must alias DelayLoadError to a function that behaves like _tprintf */
#endif
#ifndef FACILITY_VISUALCPP
# define FACILITY_VISUALCPP ((LONG)0x6d)
#endif // !FACILITY_VISUALCPP
#include <delayimp.h>
#pragma comment(lib, "delayimp")
namespace
{
LPCSTR lpszNtDllName = "ntdll.dll";
typedef struct
{
LPCSTR lpszDestName;
LPCSTR lpszSourceName;
} redir_entry_t;
redir_entry_t mod_redir[] = {
{lpszNtDllName, "ntdll.dld"},
};
redir_entry_t mod_sys32[] = {
{"version.dll", nullptr},
};
} // namespace
static LPCSTR GetRedirectedName_(LPCSTR lpszDllName)
{
for (size_t idx = 0; idx < _countof(mod_redir); idx++)
{
if (0 == lstrcmpiA(lpszDllName, mod_redir[idx].lpszSourceName))
{
return mod_redir[idx].lpszDestName;
}
}
return lpszDllName;
}
static BOOL IsSystemDllName_(LPCSTR lpszDllName)
{
for (size_t idx = 0; idx < _countof(mod_sys32); idx++)
{
if (0 == lstrcmpiA(lpszDllName, mod_sys32[idx].lpszSourceName))
{
return TRUE;
}
}
return FALSE;
}
static LONG WINAPI DelayLoadFilter(PEXCEPTION_POINTERS pExcPointers)
{
LONG lDisposition = EXCEPTION_EXECUTE_HANDLER;
PDelayLoadInfo pdli = (PDelayLoadInfo)(pExcPointers->ExceptionRecord->ExceptionInformation[0]);
switch (pExcPointers->ExceptionRecord->ExceptionCode)
{
case VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND):
(void)DelayLoadError(_T("The DLL %hs could not be loaded."), pdli->szDll);
break;
case VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND):
{
LPCSTR lpszDllName = GetRedirectedName_(pdli->szDll);
if (pdli->dlp.fImportByName)
{
(void)DelayLoadError(_T("Function %hs::%hs not found."), lpszDllName, pdli->dlp.szProcName);
}
else
{
(void)DelayLoadError(_T("Function %hs::#%u not found."), lpszDllName, pdli->dlp.dwOrdinal);
}
}
break;
default:
lDisposition = EXCEPTION_CONTINUE_SEARCH;
break;
}
return lDisposition;
}
EXTERN_C void force_resolve_all(void)
{
__try
{
/* Force all delay-loaded symbols to be resolved at once */
for (size_t idx = 0; idx < _countof(mod_redir); idx++)
{
(void)__HrLoadAllImportsForDll(mod_redir[idx].lpszSourceName);
}
}
__except (DelayLoadFilter(GetExceptionInformation()))
{
::ExitProcess(1);
}
}
static FARPROC WINAPI GetModuleHandleDliHook(unsigned dliNotify, PDelayLoadInfo pdli)
{
LPCSTR lpszDllName = NULL;
switch (dliNotify)
{
case dliNotePreLoadLibrary: // called just before LoadLibrary, can override w/ new HMODULE return val
if (NULL != (lpszDllName = GetRedirectedName_(pdli->szDll)))
{
if (HMODULE hModule = ::GetModuleHandleA(lpszDllName))
{
/*lint -save -e611 */
return reinterpret_cast<FARPROC>(hModule);
/*lint -restore */
}
}
// TODO: load DLL from System32 ... LOAD_LIBRARY_SEARCH_SYSTEM32 isn't available on downlevel OS versions,
// though
if (IsSystemDllName_(pdli->szDll))
{
}
break; /* proceed with default processing */
case dliNotePreGetProcAddress: // called just before GetProcAddress, can override w/ new FARPROC return value
break;
default:
break;
}
return NULL;
}
PfnDliHook __pfnDliNotifyHook2 = GetModuleHandleDliHook;