-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.c
228 lines (168 loc) · 6.02 KB
/
resolve.c
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "resolve.h"
static ULONG_PTR pebAddress;
static LdrLoadDll_ pLdrLoadDll;
HMODULE hNtdll = NULL;
HMODULE WINAPI ImageBaseAddress(void)
{
return (HMODULE)(ULONG_PTR)((_PPEB)pebAddress)->lpImageBaseAddress;
}
static DWORD64 djb2(PBYTE str) /* cse.yorku.ca/~oz/hash.html */
{
DWORD64 hash = DJB2_KEY;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c;
return hash;
}
static DWORD64 w_djb2(PWSTR str)
{
DWORD64 hash = DJB2_KEY;
int c;
while (c = *str++) {
// lowercase
if (c >= 'A' && c <= 'Z')
c += 0x20;
hash = ((hash << 5) + hash) + c;
}
return hash;
}
HMODULE WINAPI ModuleBaseAddress(const DWORD64 dwModuleName)
{
PLDR_DATA_TABLE_ENTRY pLdrEntry;
PLIST_ENTRY pListHead, pListEntry;
HMODULE hModule = NULL;
PPEB_LDR_DATA pLdrData;
_PPEB pPEB;
do {
if (!pebAddress) {
#ifdef _M_X64
pebAddress = __readgsqword(0x60);
#elif _M_IX86
pebAddress = __readfsdword(0x30);
#endif
}
if (!pebAddress)
break;
pPEB = (_PPEB)pebAddress;
pLdrData = pPEB->pLdr;
pListHead = &(pLdrData->InLoadOrderModuleList);
pListEntry = pListHead->Flink;
while (pListHead != pListEntry) {
pLdrEntry = CONTAINING_RECORD(pListEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
if (dwModuleName == w_djb2(pLdrEntry->BaseDllName.Buffer)) {
hModule = (HMODULE)pLdrEntry->DllBase;
break;
}
//Next entry
pListEntry = pListEntry->Flink;
}
} while (FALSE);
return hModule;
}
HMODULE WINAPI LdrLoadDll(LPWSTR lpLibFileName, const BOOL bAllreadyLoaded)
{
HMODULE hModule = NULL;
UNICODE_STRING pModuleFileName;
unsigned int cbLibFileName;
do {
if (bAllreadyLoaded) {
hModule = ModuleBaseAddress(w_djb2(lpLibFileName));
if (hModule)
break;
}
cbLibFileName = wcslen(lpLibFileName);
if (!cbLibFileName)
break;
pModuleFileName.Buffer = lpLibFileName;
pModuleFileName.Length = (USHORT)(cbLibFileName * sizeof(WCHAR));
pModuleFileName.MaximumLength = pModuleFileName.Length + sizeof(WCHAR);
if (pLdrLoadDll(NULL, 0, &pModuleFileName, &hModule) != ERROR_SUCCESS)
hModule = NULL;
} while (FALSE);
return hModule;
}
FARPROC WINAPI resolve_function(const HMODULE hModule, const DWORD64 dwProcName)
{
PIMAGE_DOS_HEADER image_dos_header;
PIMAGE_NT_HEADERS image_nt_headers;
PIMAGE_EXPORT_DIRECTORY image_export_directory;
PCHAR fwFunction, fwFunctionName = NULL;
PDWORD dwExportName, dwFuncAddress;
LPVOID FuncAddress = NULL;
DWORD i, cbFwFunction, cbForwardedLibName;
HMODULE hForwarded;
PBYTE lpFuncName;
PWORD wOrdinals;
char szForwardedLibraryName[256];
wchar_t wLibraryname[256 * sizeof(WCHAR)];
do {
image_dos_header = (PIMAGE_DOS_HEADER)hModule;
if (image_dos_header->e_magic != IMAGE_DOS_SIGNATURE)
break;
image_nt_headers = (PIMAGE_NT_HEADERS)((PCHAR)hModule + image_dos_header->e_lfanew);
if (image_nt_headers->Signature != IMAGE_NT_SIGNATURE)
break;
image_export_directory = (PIMAGE_EXPORT_DIRECTORY)((PCHAR)hModule + image_nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
if ((!image_export_directory->NumberOfFunctions) || image_export_directory->NumberOfFunctions > 4096)
break;
//Resolve functions exported by name
dwExportName = (PDWORD)(image_export_directory->AddressOfNames + (PCHAR)hModule);
wOrdinals = (PWORD)(image_export_directory->AddressOfNameOrdinals + (PCHAR)hModule);
dwFuncAddress = (PDWORD)(image_export_directory->AddressOfFunctions + (PCHAR)hModule);
for (i = 0; i < image_export_directory->NumberOfNames; i++) {
lpFuncName = (PBYTE)(dwExportName[i] + (PCHAR)hModule);
if (djb2(lpFuncName) == dwProcName) {
FuncAddress = (FARPROC)((PCHAR)hModule + dwFuncAddress[wOrdinals[i]]);
break;
}
}
if (!FuncAddress)
break;
//Handle forwarded functions
fwFunction = (PCHAR)FuncAddress;
cbFwFunction = (DWORD)strlen(fwFunction) + 1;
if (cbFwFunction >= 256)
break;
memcpy(szForwardedLibraryName, fwFunction, cbFwFunction);
for (i = 0; i < cbFwFunction; i++)
{
if (szForwardedLibraryName[i] == '.')
{
szForwardedLibraryName[i] = 0x0;
fwFunctionName = &(fwFunction[i + 1]);
break;
}
}
if (!fwFunctionName) //Function not forwarded
break;
//Convert LibraryName to wchar
cbForwardedLibName = i;
for (i = 0; i < cbForwardedLibName; i++)
{
wLibraryname[i] = szForwardedLibraryName[i];
}
wLibraryname[cbForwardedLibName] = 0x0;
wcscat(wLibraryname, L".dll");
hForwarded = LdrLoadDll(wLibraryname, TRUE);
if (!hForwarded) {
FuncAddress = NULL;
break;
}
FuncAddress = resolve_function(hForwarded, djb2(fwFunctionName));
} while (FALSE);
return FuncAddress;
}
BOOL bInitResolve(void)
{
BOOL bRet = FALSE;
do {
hNtdll = ModuleBaseAddress(0x377D2B522D3B5ED); //ntdll.dll
if (!hNtdll)
break;
pLdrLoadDll = (LdrLoadDll_)resolve_function(hNtdll, 0x726C7A370307DB23); //LdrLoadDll
if (!pLdrLoadDll)
break;
bRet = TRUE;
} while (FALSE);
return bRet;
}