-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathresolve.c
executable file
·136 lines (115 loc) · 3.06 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
#include "resolve.h"
/*
resolve_hash_name
uses the SuperFastHash algorithm to create an unsigned 32-bit hash
*/
UINT32 resolve_hash_name(LPCSTR cszName)
{
if (cszName == NULL)
{
return 0;
}
SIZE_T uNameLen = strnlen_s(cszName, RESOLVE_NAME_MAX);
if (uNameLen == 0)
{
return 0;
}
UINT32 u32Hash = 0, u32Buf = 0;
PBYTE pbData = (PBYTE)cszName;
INT iRemain = (uNameLen & 3);
uNameLen >>= 2;
for (SIZE_T i = uNameLen; i > 0; i--)
{
u32Hash += *(const UINT16*)pbData;
u32Buf = (*(const UINT16*)(pbData + 2) << 11) ^ u32Hash;
u32Hash = (u32Hash << 16) ^ u32Buf;
pbData += (2 * sizeof(UINT16));
u32Hash += u32Hash >> 11;
}
switch (iRemain)
{
case 1:
u32Hash += *pbData;
u32Hash ^= u32Hash << 10;
u32Hash += u32Hash >> 1;
break;
case 2:
u32Hash += *(const UINT16*)pbData;
u32Hash ^= u32Hash << 11;
u32Hash += u32Hash >> 17;
break;
case 3:
u32Hash += *(const UINT16*)pbData;
u32Hash ^= u32Hash << 16;
u32Hash ^= pbData[sizeof(UINT16)] << 18;
u32Hash += u32Hash >> 11;
break;
}
u32Hash ^= u32Hash << 3;
u32Hash += u32Hash >> 5;
u32Hash ^= u32Hash << 4;
u32Hash += u32Hash >> 17;
u32Hash ^= u32Hash << 25;
u32Hash += u32Hash >> 6;
return u32Hash;
}
/*
resolve_find finds the entry by walking the EAT
*/
BOOL resolve_find(PRESOLVE_ENTRY pEntry)
{
if (pEntry->lpAddr != NULL)
{
return TRUE;
}
HMODULE hLibrary = LoadLibrary(pEntry->cszwMod);
if (hLibrary == NULL)
{
return FALSE;
}
PIMAGE_DOS_HEADER pDOSHdr = (PIMAGE_DOS_HEADER)hLibrary;
if (pDOSHdr->e_magic != IMAGE_DOS_SIGNATURE)
{
return FALSE;
}
PIMAGE_NT_HEADERS pNTHdr = (PIMAGE_NT_HEADERS)RESOLVE_REL_CALC(hLibrary, pDOSHdr->e_lfanew);
if (pNTHdr->Signature != IMAGE_NT_SIGNATURE)
{
return FALSE;
}
if (
(pNTHdr->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0 ||
pNTHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress == 0 ||
pNTHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size == 0
)
{
return FALSE;
}
PIMAGE_EXPORT_DIRECTORY pIED = (PIMAGE_EXPORT_DIRECTORY)RESOLVE_REL_CALC(hLibrary,
pNTHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
PDWORD pdwAddress = (PDWORD)RESOLVE_REL_CALC(hLibrary, pIED->AddressOfFunctions);
PDWORD pdwNames = (PDWORD)RESOLVE_REL_CALC(hLibrary, pIED->AddressOfNames);
PWORD pwOrd = (PWORD)RESOLVE_REL_CALC(hLibrary, pIED->AddressOfNameOrdinals);
for (DWORD i = 0; i < pIED->AddressOfFunctions; i++)
{
UINT32 u32FuncHash = resolve_hash_name((LPCSTR)RESOLVE_REL_CALC(hLibrary, pdwNames[i]));
if (u32FuncHash == pEntry->u32Hash)
{
pEntry->lpAddr = (PVOID)RESOLVE_REL_CALC(hLibrary, pdwAddress[pwOrd[i]]);
return TRUE;
}
}
return FALSE;
}
/*
resolve_init populates the given table
*/
BOOL resolve_init(PRESOLVE_TABLE pTbl, SIZE_T uCount)
{
for (SIZE_T i = 0; i < uCount; i++)
{
if (!resolve_find((PRESOLVE_ENTRY)&pTbl->reEntries[i]))
return FALSE;
}
return TRUE;
}