-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathregln.c
514 lines (382 loc) · 13 KB
/
regln.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*++
Copyright (c) 1999 by Antoni Sawicki
Module Name:
regln.c
Abstract:
Manage Windows Registry Symbolic Links
Author:
Antoni Sawicki <[email protected]>
Credits:
Antoni Sawicki <[email protected]>
Tomasz Nowak <[email protected]>
Mark Russinovitch <[email protected]>
Special thaks to [email protected] for recent code fixes
License:
Apache 2.0
--*/
#include <windows.h>
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#define REGLN_LINK_VALUE_NAME L"SymbolicLinkValue"
#define REGLN_OPTION_CREATE_LINK (0x00000002L)
#define REGLN_OPTION_OPEN_LINK (0x00000100L)
#define REGLN_OBJ_CASE_INSENSITIVE (0x00000040L)
typedef struct _PROG_ARGS {
ULONG Volatile;
ULONG Delete;
WCHAR SourceKey[1024];
WCHAR DestinationKey[1024];
} PROG_ARGS;
typedef PROG_ARGS *PPROG_ARGS;
typedef struct _UNICODE_STRING {
WORD Length;
WORD MaximumLength;
PWSTR Buffer;
} UNICODE_STRING;
typedef UNICODE_STRING *PUNICODE_STRING;
typedef struct _OBJECT_ATTRIBUTES {
ULONG Length;
HANDLE RootDirectory;
PUNICODE_STRING ObjectName;
ULONG Attributes;
PVOID SecurityDescriptor;
PVOID SecurityQualityOfService;
} OBJECT_ATTRIBUTES;
typedef OBJECT_ATTRIBUTES *POBJECT_ATTRIBUTES;
NTSTATUS (__stdcall *NtCreateKey)(
PHANDLE KeyHandle,
ULONG DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes,
ULONG TitleIndex,
PUNICODE_STRING Class,
ULONG CreateOptions,
PULONG Disposition
);
NTSTATUS (__stdcall *NtSetValueKey)(
HANDLE KeyHandle,
PUNICODE_STRING ValueName,
ULONG TitleIndex,
ULONG Type,
PVOID Data,
ULONG DataSize
);
NTSTATUS (__stdcall *NtDeleteKey)(
HANDLE KeyHandle
);
NTSTATUS (__stdcall *NtClose)(
HANDLE Handle
);
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
VOID
Usage(
void
)
/*++
Routine Description:
Display usage information and version info
Terminate programm
--*/
{
wprintf(L"REGLN v2.2a, Copyright (c) 1999 by Antoni Sawicki\n\n");
wprintf(L"Usage: regln [-v] <link_key> <target_key>\n");
wprintf(L" regln -d <link_key>\n\n");
wprintf(L"Where: <link_key> is the new registry link key\n");
wprintf(L" <target_key> is an existing registry key being linked to\n");
wprintf(L" -v = volatile, exist in memory only\n");
wprintf(L" -d = delete link\n\n");
wprintf(L"Examples:\n");
wprintf(L" regln HKLM\\SOFTWARE\\BigCompany HKLM\\SOFTWARE\\Microsoft\n");
wprintf(L" regln -d \\Registry\\Machine\\SOFTWARE\\BigCompany\n\n");
ExitProcess(0);
}
VOID
DEBUG(
WCHAR *msg,
...
)
/*++
Routine Description:
Print debug message if debug is enabled
--*/
{
va_list valist;
if(_wgetenv(L"REGLN_DEBUG")) {
wprintf(L"DEBUG: ");
va_start(valist, msg);
vwprintf(msg, valist);
va_end(valist);
putwchar(L'\n');
}
}
VOID
ERRPT(
int EmitUsage,
WCHAR *msg,
...
)
/*++
Routine Description:
Display User Error Information (eg. wrong parameter)
Display actual error message
Display usage information if flag specified
Terminate program
--*/
{
va_list valist;
LPWSTR errBuff=NULL;
DWORD err;
wprintf(L"ERROR: ");
va_start(valist, msg);
vwprintf(msg, valist);
va_end(valist);
err=GetLastError();
if(err) {
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR) &errBuff, 64, NULL);
wprintf(L"%08X : %s", err, errBuff);
}
wprintf(L"\n\n");
if(EmitUsage)
Usage();
ExitProcess(1);
}
VOID
NTAPI_Init(
void
)
/*++
Routine Description:
This routine finds entry points for a few NTAPI functions
--*/
{
if(!(NtCreateKey = (void *) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtCreateKey" )))
ERRPT(0, L"Unable to initialize NTAPI\n");
if(!(NtDeleteKey = (void *) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtDeleteKey" )))
ERRPT(0, L"Unable to initialize NTAPI\n");
if(!(NtSetValueKey = (void *) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtSetValueKey" )))
ERRPT(0, L"Unable to initialize NTAPI\n");
if(!(NtClose = (void *) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtClose" )))
ERRPT(0, L"Unable to initialize NTAPI\n");
}
VOID
WinToNtPath(
WCHAR *Src,
size_t DstSize,
WCHAR *Dst
)
/*++
Routine Description:
Convert Win32 Registry Root Key Name to NT Namespace Path
HKEY_LOCAL_MACHINE\Key -> \Registry\Machine\Key
--*/
{
if(wcsnicmp(Src, L"HKEY_LOCAL_MACHINE\\", 19)==0 || wcsnicmp(Src, L"HKLM\\", 5)==0)
_snwprintf(Dst, DstSize, L"\\Registry\\Machine%s", wcschr(Src, L'\\'));
else if(wcsnicmp(Src, L"HKEY_USERS\\", 11)==0 || wcsnicmp(Src, L"HKUS\\", 5)==0 || wcsnicmp(Src, L"HKU\\", 4)==0)
_snwprintf(Dst, DstSize, L"\\Registry\\User%s", wcschr(Src, L'\\'));
else
ERRPT(1, L"WinToNtPtah: Wrong Win32 Registry Root Key Name.");
DEBUG(L"WinToNtPath converted \"%s\" [len=%d] to \"%s\" [len=%d] [max=%d]", Src, wcslen(Src), Dst, wcslen(Dst), DstSize);
}
VOID
ParseArgs(
int argc,
WCHAR **argv,
PPROG_ARGS args
)
/*++
Routine Description:
Parse arguments specified to Regln main() module
Output is returned through PROG_ARGS structure
--*/
{
int argn;
DEBUG(L"argc: %d", argc);
for(argn=1; argn<argc; argn++) {
DEBUG(L"argv[%d]: %s", argn, argv[argn]);
// this is an option
if(argv[argn][0]==L'/' || argv[argn][0]==L'-') {
if(argv[argn][1]) {
DEBUG(L"option, len=%d", wcslen(argv[argn]));
switch(argv[argn][1]) {
case L'v':
case L'V':
DEBUG(L"Volatile set");
args->Volatile=1;
break;
case L'd':
case L'D':
DEBUG(L"Delete set");
args->Delete=1;
break;
case L'h':
case L'H':
Usage();
break;
default:
ERRPT(1, L"Unknown option: %s", argv[argn]);
break;
}
}
else Usage();
}
// this is a parameter
else {
DEBUG(L"parameter, len=%d", wcslen(argv[argn]));
if(wcslen(argv[argn])>5) {
if(!wcslen(args->SourceKey)) {
if(wcsnicmp(L"HK", argv[argn], 2)==0)
WinToNtPath(argv[argn], sizeof(args->SourceKey)/sizeof(WCHAR), args->SourceKey);
else if(wcsnicmp(L"\\Registry\\", argv[argn], 10)==0)
wcsncpy(args->SourceKey, argv[argn], sizeof(args->SourceKey)/sizeof(WCHAR));
else
ERRPT(1, L"Unknown Registry Root Key Format.");
DEBUG(L"SourceKey set to \"%s\" [len=%d] [max=%d]", args->SourceKey, wcslen(args->SourceKey), sizeof(args->SourceKey)/sizeof(WCHAR));
}
else if(!wcslen(args->DestinationKey)) {
if(wcsnicmp(L"HK", argv[argn], 2)==0)
WinToNtPath(argv[argn], sizeof(args->DestinationKey)/sizeof(WCHAR), args->DestinationKey);
else if(wcsnicmp(L"\\Registry\\", argv[argn], 10)==0)
wcsncpy(args->DestinationKey, argv[argn], sizeof(args->DestinationKey)/sizeof(WCHAR));
else
ERRPT(1, L"Unknown Registry Root Key Format.");
DEBUG(L"DestinationKey set to \"%s\" [len=%d] [max=%d]", args->DestinationKey, wcslen(args->DestinationKey), sizeof(args->DestinationKey)/sizeof(WCHAR));
}
else
ERRPT(1, L"Too many parameters. Giving up.");
}
else
ERRPT(1, L"Parameter %d [%s] too short. Giving up.", argn, argv[argn]);
}
}
// check validity
if(args->Delete) {
if(!wcslen(args->SourceKey) || wcslen(args->DestinationKey))
ERRPT(1, L"Delete requires one parameter.");
if(args->Volatile)
ERRPT(1, L"Options -d, -v cannot be specified together.");
}
else {
if(!wcslen(args->SourceKey) || !wcslen(args->DestinationKey))
ERRPT(1, L"Two parameters required.");
}
if(args->Delete)
DEBUG(L">> Will delete link key %s", args->SourceKey);
else
DEBUG(L">> Will create %s link key %s -> %s", (args->Volatile) ? L"volatile" : L"permanent", args->SourceKey, args->DestinationKey);
}
VOID
RegLnDeleteLink(
WCHAR *LinkKeyName
)
/*++
Routine Description:
Delete a registry link
Arguments:
Registry Link Key Name as NT Namespace Path
--*/
{
DWORD Disposition, Status;
HANDLE NtKeyHandle;
UNICODE_STRING NtKeyName;
OBJECT_ATTRIBUTES NtObjAttr;
ZeroMemory(&NtKeyName, sizeof(UNICODE_STRING));
ZeroMemory(&NtObjAttr, sizeof(OBJECT_ATTRIBUTES));
NtKeyName.Buffer = LinkKeyName;
NtKeyName.Length = wcslen(LinkKeyName) * sizeof(WCHAR);
NtObjAttr.ObjectName = &NtKeyName;
NtObjAttr.Attributes = REGLN_OBJ_CASE_INSENSITIVE | REGLN_OPTION_OPEN_LINK;
NtObjAttr.RootDirectory = NULL;
NtObjAttr.SecurityDescriptor = NULL;
NtObjAttr.SecurityQualityOfService = NULL;
NtObjAttr.Length = sizeof(OBJECT_ATTRIBUTES);
Status=NtCreateKey(&NtKeyHandle, KEY_ALL_ACCESS, &NtObjAttr, 0, NULL, REG_OPTION_NON_VOLATILE, &Disposition);
if(!NT_SUCCESS(Status))
ERRPT(0, L"Link deletion failed (NtCreateKey) [%08X]\n", Status);
DEBUG(L"NtCreateKey Succeeded");
Status=NtDeleteKey(NtKeyHandle);
if(!NT_SUCCESS(Status)) {
NtClose(NtKeyHandle);
ERRPT(0, L"Link deletion failed (NtDeleteKey) [%08X]\n", Status);
}
DEBUG(L"NtDeleteKey Succeeded");
Status=NtClose(NtKeyHandle);
if(!NT_SUCCESS(Status))
ERRPT(0, L"NtClose failed. [%08X]\n", Status);
DEBUG(L"Link Delete Succeeded");
}
VOID
RegLnCreateLink(
WCHAR *LinkKeyName,
WCHAR *TargetKeyName,
BOOL Volatile
)
/*++
Routine Description:
Create a registry link
Arguments:
Registry Link Key Name as NT Namespace Path
Registry Target Key Name as NT Namespace Path
Volatile true/false
--*/
{
ULONG Disposition;
DWORD Status;
HANDLE NtKeyHandle;
UNICODE_STRING NtKeyName, NtValueName;
OBJECT_ATTRIBUTES NtObjAttr;
ZeroMemory(&NtKeyName, sizeof(UNICODE_STRING));
ZeroMemory(&NtValueName, sizeof(UNICODE_STRING));
ZeroMemory(&NtObjAttr, sizeof(OBJECT_ATTRIBUTES));
NtKeyName.Buffer = LinkKeyName;
NtKeyName.Length = wcslen(LinkKeyName) * sizeof(WCHAR);
NtValueName.Buffer = REGLN_LINK_VALUE_NAME;
NtValueName.Length = wcslen(REGLN_LINK_VALUE_NAME) * sizeof(WCHAR);
NtObjAttr.ObjectName = &NtKeyName;
NtObjAttr.Attributes = REGLN_OBJ_CASE_INSENSITIVE;
NtObjAttr.RootDirectory = NULL;
NtObjAttr.SecurityDescriptor = NULL;
NtObjAttr.SecurityQualityOfService = NULL;
NtObjAttr.Length = sizeof(OBJECT_ATTRIBUTES);
Status=NtCreateKey(&NtKeyHandle, KEY_ALL_ACCESS, &NtObjAttr, 0, NULL, (Volatile) ? REG_OPTION_VOLATILE|REGLN_OPTION_CREATE_LINK : REG_OPTION_NON_VOLATILE|REGLN_OPTION_CREATE_LINK, &Disposition);
if(!NT_SUCCESS(Status))
ERRPT(0, L"Unable to create registry key \"%s\" (NtCreateKey) [%08X]", LinkKeyName, Status);
DEBUG(L"NtCreateKey Succeeded");
Status=NtSetValueKey(NtKeyHandle, &NtValueName, 0, REG_LINK, TargetKeyName, wcslen(TargetKeyName) * sizeof(WCHAR));
if(!NT_SUCCESS(Status)) {
NtClose(NtKeyHandle);
ERRPT(0, L"Unable to set link value for [REG_LINK]%s=\"%s\" (NtSetValueKey) [%08X]", REGLN_LINK_VALUE_NAME, TargetKeyName, Status);
}
DEBUG(L"NtSetValueKey Succeeded");
Status=NtClose(NtKeyHandle);
if(!NT_SUCCESS(Status))
ERRPT(0, L"NtClose failed. [%08X]\n", Status);
DEBUG(L"Link Creation Succeeded");
}
int
wmain(
int argc,
WCHAR **argv
)
/*++
Routine Description:
This routine is the main program for Regln.
The arguments accepted by Regln are:
/d Delete registry link
/v Volatile operation
link key Registry link being created or deleted
target key Registry key being linked to
--*/
{
PROG_ARGS RegLnArgs;
ZeroMemory(&RegLnArgs, sizeof(PROG_ARGS));
NTAPI_Init();
ParseArgs(argc, argv, &RegLnArgs);
if(RegLnArgs.Delete)
RegLnDeleteLink(RegLnArgs.SourceKey);
else
RegLnCreateLink(RegLnArgs.SourceKey, RegLnArgs.DestinationKey, RegLnArgs.Volatile);
return 0;
};