-
Notifications
You must be signed in to change notification settings - Fork 6
/
engine.c
311 lines (264 loc) · 9.47 KB
/
engine.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*************************************************************
* Registry Redirector
*
* (C)oded by [email protected], 2010
*
* This application redirects registry calls to Registry dump
* files, so that you can "shim" your application to a foreign
* registry.
*
* This may become handy if you want to check the registry
* of another machine with various checking tools that are
* designed for the checking of a live System.
* Note that the application must use the normal WIN32 API
* for registry Access. If the app is checking via Native API,
* this won't work.
*
* Be aware that reads AND WRITES are redirected, so only
* use this tool on copies of your original Registry files.
* It may be a good idea to use this tool in a PE environment.
*
*************************************************************
* Module: engine.c
* Descr.: Simple routines for Registry remapping of all keys
* License: GPL 3
* Date : 12.02.2010
* Changelog:
*************************************************************/
// ------------------------- INCLUDES -------------------------
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include "engine.h"
#include "symlink.h"
// ------------------------- DEFINES --------------------------
#define MY_REGKEY "Software\\RegRemap"
// ------------------------ VARIABLES -------------------------
static DWORD m_dwLastError = ERROR_SUCCESS;
static char m_szLastErr[256] = {0};
// ------------------------ PROTOTYPES ------------------------
static void Eng_Error (DWORD dwErr, char *pszFormat, ...) ;
static BOOL MapHKLMKey (char *pszUID, HKEY hKey, char *pszName, char *pszFile);
//-----------------------------------------------------------------
// Public
//-----------------------------------------------------------------
/* Eng_EnablePrivilege
*
* Description : Assigns the given process the needed privilege
* Parameters : hProcess - Process to assign Privilege to
* lpPrivilegeName - Name of Privilege token to assign
* Returns : TRUE on success, FALSE on failure
*/
BOOL Eng_EnablePrivilege (HANDLE hProcess, LPCTSTR lpPrivilegeName)
{
HANDLE hTok;
TOKEN_PRIVILEGES tp;
BOOL bRet = FALSE;
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (OpenProcessToken (hProcess, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hTok))
{
LookupPrivilegeValue (NULL, lpPrivilegeName, &tp.Privileges[0].Luid);
if (!(bRet = AdjustTokenPrivileges (hTok, FALSE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, 0)))
{
Eng_Error (GetLastError(), "Cannot adjust privilege %s: ", lpPrivilegeName);
}
CloseHandle (hTok);
}
else
{
Eng_Error (GetLastError(), "Cannot open Process token: ");
}
return bRet;
}
//-----------------------------------------------------------------
/* Eng_MapHKCU
*
* Description : Remaps HKEY_CURRENT_USER to given Registry file
* Parameters : pszUID - UID for our virtual remap-Key (can be anything)
* phInject - Injector instance handle to add remapping to
* pszFile - Registry file to load
* Returns : TRUE on success, FALSE on failure
*/
BOOL Eng_MapHKCU (char *pszUID, HYPINJECT *phInject, char *pszFile)
{
DWORD dwErr;
char szKey[MAX_PATH];
if (!pszUID) return TRUE;
wsprintf (szKey, "EXT_%s_USER001", pszUID);
if ((dwErr = RegLoadKey (HKEY_USERS, szKey, pszFile)) == ERROR_SUCCESS)
{
char szKeyCLS[MAX_PATH];
// Link current user to the first user loaded
Injector_Override (phInject, HKEY_CURRENT_USER, HKEY_USERS, szKey);
wsprintf (szKeyCLS, "%s\\Software\\Classes", szKey);
Injector_Override (phInject, HKEY_CLASSES_ROOT, HKEY_USERS, szKeyCLS);
return TRUE;
}
else
{
Eng_Error (dwErr, "Cannot load user hive %s from %s: ", szKey, pszFile);
return FALSE;
}
return FALSE;
}
//-----------------------------------------------------------------
/* Eng_UnmapHKCU
*
* Description : Unmaps assigned Keys previously loaded with Eng_MapHKCU
* Parameters : pszUID - UID for our virtual remap-Key used in Eng_MapHKCU
*/
void Eng_UnmapHKCU(char *pszUID)
{
char szKey[MAX_PATH];
wsprintf (szKey, "EXT_%s_USER001", pszUID);
RegUnLoadKey (HKEY_USERS, szKey);
}
//-----------------------------------------------------------------
/* Eng_MapHKLM
*
* Description : Maps 3 Files as HKEY_LOCAL_MACHINE for pszUID to our virtual registry
* Don't forget to unmap to delete the leftovers in the registry to avoid corruption!
* You can set pszUID i.e. to the PID of the app you wanna hook
* Parameters : pszUID - UID for our virtual remap-Key used in Eng_MapHKCU
* phInject - Injector instance handle to add remapping to
* pszSYSTEM - File for SYSTEM hive to attach, can be NULL
* pszSOFTWARE- File for SOFTWARE hive to attach, can be NULL
* pszSAM - File for SAM hive to attach, can be NULL
* Returns : TRUE on success, FALSE on failure
*/
BOOL Eng_MapHKLM (char *pszUID, HYPINJECT *phInject, char *pszSYSTEM, char *pszSOFTWARE, char *pszSAM)
{
char szKey[MAX_PATH];
HKEY hKey, hSubKey;
DWORD dwErr;
/* If we have nothing to do, succeed anyway */
if (!pszSYSTEM && !pszSOFTWARE && !pszSAM)
return TRUE;
/* Create fake registry root */
wsprintf (szKey, MY_REGKEY"\\%s\\HKEY_LOCAL_MACHINE", pszUID);
if ((dwErr = RegCreateKeyEx(HKEY_LOCAL_MACHINE, szKey, 0, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS,
NULL, &hKey, NULL)) != ERROR_SUCCESS)
{
Eng_Error (dwErr, "Cannot create fake registry root in %s: ", MY_REGKEY"\\%s\\HKEY_LOCAL_MACHINE");
return FALSE;
}
/* Map base keys */
if (!MapHKLMKey (pszUID, hKey, "SYSTEM", pszSYSTEM) ||
!MapHKLMKey (pszUID, hKey, "SOFTWARE", pszSOFTWARE) ||
!MapHKLMKey (pszUID, hKey, "SAM", pszSAM))
{
Eng_UnmapHKLM(pszUID);
RegCloseKey(hKey);
return FALSE;
}
/* Create additional volatile symlinks */
if (pszSYSTEM)
{
if (CreateSymLinkKey (hKey, "SYSTEM", "CurrentControlSet", &hSubKey) == ERROR_SUCCESS)
{
char szLinkTarget[MAX_PATH];
wsprintf (szLinkTarget, "%s\\SYSTEM\\ControlSet001", szKey);
SetSymLink (hSubKey, HKEY_LOCAL_MACHINE, szLinkTarget);
RegCloseKey (hSubKey);
}
}
RegCloseKey(hKey);
Injector_Override (phInject, HKEY_LOCAL_MACHINE, HKEY_LOCAL_MACHINE, szKey);
return TRUE;
}
//-----------------------------------------------------------------
/* Eng_UnmapHKLM
*
* Description : Unmaps assigned Keys previously loaded with Eng_MapHKLM
* Parameters : pszUID - UID for our virtual remap-Key used in Eng_MapHKLM
*/
void Eng_UnmapHKLM(char *pszUID)
{
char szKey[MAX_PATH], szLink[MAX_PATH], *p;
char *aszKeys[]={"SYSTEM", "SOFTWARE", "SAM"};
int i;
// First remove the symlinks
p = szLink + wsprintf (szLink, MY_REGKEY"\\%s\\HKEY_LOCAL_MACHINE\\", pszUID);
lstrcat (szLink, "SYSTEM\\CurrentControlSet");
DeleteSymLink (HKEY_LOCAL_MACHINE, szLink);
*p = 0;
for (i=0; i<sizeof(aszKeys)/sizeof(aszKeys[0]); i++)
{
lstrcat (szLink, aszKeys[i]);
DeleteSymLink (HKEY_LOCAL_MACHINE, szLink);
*p = 0;
}
// Delete our mapping key
*(p-1)=0;
RegDeleteKey (HKEY_LOCAL_MACHINE, szLink);
wsprintf (szLink, MY_REGKEY"\\%s", pszUID);
RegDeleteKey (HKEY_LOCAL_MACHINE, szLink);
// Unload external hives
for (i=0; i<sizeof(aszKeys)/sizeof(aszKeys[0]); i++)
{
wsprintf (szKey, "EXT_%s_HKLM_%s", pszUID, aszKeys[i]);
RegUnLoadKey (HKEY_LOCAL_MACHINE, szKey);
}
}
//-----------------------------------------------------------------
/* Eng_GetLastError
*
* Description: Get last error that occured in this module
* Parameters : pdwLastErr - Optional pointer to a variable that receives error code
* Returns : Error message
*/
char *Eng_GetLastError(DWORD *pdwLastErr)
{
if (pdwLastErr) *pdwLastErr = m_dwLastError;
return m_szLastErr;
}
//-----------------------------------------------------------------
// Static
//-----------------------------------------------------------------
static void Eng_Error (DWORD dwErr, char *pszFormat, ...)
{
char *p;
va_list ap;
m_dwLastError = dwErr;
va_start(ap, pszFormat);
_vsnprintf(m_szLastErr, sizeof(m_szLastErr), pszFormat, ap);
va_end(ap);
p = m_szLastErr + FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, m_dwLastError,
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), m_szLastErr, 0, NULL);
wsprintf (p, " (%08X)", m_dwLastError);
}
//-----------------------------------------------------------------
static BOOL MapHKLMKey (char *pszUID, HKEY hKey, char *pszName, char *pszFile)
{
char szKey[MAX_PATH];
HKEY hSubKey;
DWORD dwErr;
BOOL bRet = TRUE;
if (pszFile)
{
wsprintf (szKey, "EXT_%s_HKLM_%s", pszUID, pszName);
if ((dwErr = RegLoadKey (HKEY_LOCAL_MACHINE, szKey, pszFile)) != ERROR_SUCCESS)
{
Eng_Error (dwErr, "Cannot load key %s from file %s: ", pszName, pszFile);
return FALSE;
}
if ((dwErr = RegCreateKeyEx(hKey, pszName, 0, NULL,
REG_OPTION_VOLATILE |
REG_OPTION_CREATE_LINK,
KEY_ALL_ACCESS | KEY_CREATE_LINK,
NULL, &hSubKey, NULL)) != ERROR_SUCCESS)
{
Eng_Error (dwErr, "Cannot create link %s: ", pszName);
return FALSE;
}
if ((dwErr = SetSymLink (hSubKey, HKEY_LOCAL_MACHINE, szKey)) != ERROR_SUCCESS)
{
Eng_Error (dwErr, "Cannot setup symlink %s in %s: ", szKey, pszName);
bRet = FALSE;
}
RegCloseKey (hSubKey);
}
return bRet;
}