-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregsnap.cpp
More file actions
190 lines (155 loc) · 4.43 KB
/
regsnap.cpp
File metadata and controls
190 lines (155 loc) · 4.43 KB
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
/* Stream Archive I/O utility, Copyright (C) Olof Lagerkvist 2004-2008
*
* regsnap.cpp
* Creates registry database snapshots.
*/
#ifndef _UNICODE
#define _UNICODE
#endif
#ifndef _DLL
#define _DLL
#endif
#ifndef UNICODE
#define UNICODE
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define WIN32_NO_STATUS
#include <windows.h>
#include <intsafe.h>
#undef WIN32_NO_STATUS
#include <ntdll.h>
#include <winstrct.h>
#include <wio.h>
#include "strarc.hpp"
bool
StrArc::CreateRegistrySnapshots()
{
HKEY hKeyHiveList;
LONG lRegErr;
lRegErr = RegOpenKey(HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\Control\\hivelist",
&hKeyHiveList);
if (lRegErr != ERROR_SUCCESS)
{
SetLastError(lRegErr);
return false;
}
LPWSTR wczKeyName = (LPWSTR)Buffer;
DWORD dwKeyNameSize = 32767;
DWORD dwIndex = 0;
for (;;)
{
YieldSingleProcessor();
DWORD dwKeyNameReturnedSize = dwKeyNameSize;
DWORD dwFileNameReturnedSize = FullPath.MaximumLength -
sizeof(REGISTRY_SNAPSHOT_FILE_EXTENSION);
DWORD dwReturnedDataType;
lRegErr = RegEnumValue(hKeyHiveList,
dwIndex++,
wczKeyName,
&dwKeyNameReturnedSize,
NULL,
&dwReturnedDataType,
(LPBYTE)FullPath.Buffer,
&dwFileNameReturnedSize);
if (lRegErr == ERROR_NO_MORE_ITEMS)
{
RegCloseKey(hKeyHiveList);
return true;
}
if (lRegErr != ERROR_SUCCESS)
{
SetLastError(lRegErr);
return false;
}
if ((dwReturnedDataType != REG_SZ) | (lRegErr == ERROR_MORE_DATA))
{
oem_printf(stderr,
"Wrong data type %1!u! for hive %2!ws!. "
"Should be %3!u!. This hive will not be backed up.%%n",
dwReturnedDataType, wczKeyName, REG_SZ);
continue;
}
FullPath.Buffer[dwFileNameReturnedSize] = 0;
FullPath.Length = (USHORT)(wcslen(FullPath.Buffer) << 1);
if (FullPath.Length == 0)
continue;
NTSTATUS status =
RtlAppendUnicodeToString(&FullPath, REGISTRY_SNAPSHOT_FILE_EXTENSION);
if (!NT_SUCCESS(status))
{
oem_printf(stderr,
"Registry file path is too long: '%1!wZ!'%%n"
"Some of the snapshots will not be backed up.%%n",
&FullPath);
continue;
}
if (bVerbose)
oem_printf(stderr,
"'%1!ws!' -> '%2!wZ!'%%n",
wczKeyName,
&FullPath);
else if (bListFiles)
oem_printf(stdout, "%1!ws!%%n", wczKeyName);
if (bListOnly)
continue;
UNICODE_STRING key_name;
OBJECT_ATTRIBUTES object_attributes;
HANDLE hKey;
HANDLE hFile;
RtlInitUnicodeString(&key_name, wczKeyName);
InitializeObjectAttributes(&object_attributes,
&key_name,
OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
NULL,
NULL);
status = NtCreateKey(&hKey,
KEY_READ,
&object_attributes,
0,
NULL,
REG_OPTION_BACKUP_RESTORE,
NULL);
if (!NT_SUCCESS(status))
{
WErrMsgA errmsg(RtlNtStatusToDosError(status));
oem_printf(stderr, "Error opening key '%1!ws!': %2%%n",
wczKeyName, errmsg);
continue;
}
hFile =
NativeCreateFile(NULL,
&FullPath,
GENERIC_WRITE,
OBJ_CASE_INSENSITIVE,
NULL,
NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_SUPERSEDE,
0,
FALSE);
if (hFile == INVALID_HANDLE_VALUE)
{
WErrMsgA errmsg;
NtClose(hKey);
oem_printf(stderr,
"Error creating file '%2!wZ!': %1%%n",
errmsg,
&FullPath);
continue;
}
status = NtSaveKey(hKey, hFile);
NtClose(hKey);
NtClose(hFile);
if (!NT_SUCCESS(status))
{
WErrMsgA errmsg(RtlNtStatusToDosError(status));
oem_printf(stderr, "Error creating snapshot for '%1!ws!': %2%%n",
wczKeyName, errmsg);
continue;
}
}
}