-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistryContext.cpp
More file actions
295 lines (262 loc) · 8.16 KB
/
RegistryContext.cpp
File metadata and controls
295 lines (262 loc) · 8.16 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
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
#include "HashCommon.h"
namespace
{
const wchar_t *kSalt3822 =
L"Copyright (C) Microsoft. All rights reserved {3822B7CA-C2F4-4889-B8CC-4CE39A8FB81C}";
const wchar_t *kSalt97B6 =
L"Copyright (C) Microsoft. All rights reserved {97B6BCF4-C367-4577-95BE-73BD3053A5E0}";
const wchar_t *kSaltD185 =
L"Copyright (C) Microsoft. All rights reserved {D185E0A1-E265-4724-AA21-3A17B038D72E}";
bool QueryRegString(HKEY root, const std::wstring &subkey, const wchar_t *value_name, std::wstring *out)
{
HKEY key = NULL;
DWORD type = 0;
DWORD size = 0;
if (RegOpenKeyExW(root, subkey.c_str(), 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
{
return false;
}
LONG rc = RegQueryValueExW(key, value_name, NULL, &type, NULL, &size);
if (rc != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ))
{
RegCloseKey(key);
return false;
}
std::vector<wchar_t> buffer(size / sizeof(wchar_t) + 1U, L'\0');
rc = RegQueryValueExW(key,
value_name,
NULL,
&type,
reinterpret_cast<BYTE *>(&buffer[0]),
&size);
RegCloseKey(key);
if (rc != ERROR_SUCCESS)
{
return false;
}
*out = &buffer[0];
return true;
}
bool QueryRegLastWriteTime(HKEY root, const std::wstring &subkey, FILETIME *out)
{
if (out == NULL)
{
return false;
}
HKEY key = NULL;
if (RegOpenKeyExW(root, subkey.c_str(), 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
{
return false;
}
const LONG rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, out);
RegCloseKey(key);
return rc == ERROR_SUCCESS;
}
bool GetCurrentUserSidString(std::wstring *out)
{
HANDLE token = NULL;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
{
return false;
}
DWORD size = 0;
GetTokenInformation(token, TokenUser, NULL, 0, &size);
std::vector<BYTE> buffer(size, 0U);
if (!GetTokenInformation(token, TokenUser, &buffer[0], size, &size))
{
CloseHandle(token);
return false;
}
TOKEN_USER *user = reinterpret_cast<TOKEN_USER *>(&buffer[0]);
LPWSTR sid_text = NULL;
const BOOL ok = ConvertSidToStringSidW(user->User.Sid, &sid_text);
CloseHandle(token);
if (!ok || sid_text == NULL)
{
return false;
}
*out = sid_text;
LocalFree(sid_text);
return true;
}
std::wstring StripOuterBraces(const std::wstring &value)
{
if (value.size() >= 2U && value[0] == L'{' && value[value.size() - 1U] == L'}')
{
return value.substr(1U, value.size() - 2U);
}
return value;
}
bool FormatTimestampHexFromFileTime(const FILETIME &last_write, std::wstring *out)
{
SYSTEMTIME st;
FILETIME normalized;
if (!FileTimeToSystemTime(&last_write, &st) || !SystemTimeToFileTime(&st, &normalized))
{
return false;
}
wchar_t buffer[17];
swprintf(buffer, sizeof(buffer) / sizeof(buffer[0]), L"%08x%08x", normalized.dwHighDateTime, normalized.dwLowDateTime);
*out = buffer;
return true;
}
const wchar_t *PrimarySaltForClass(int mod_class)
{
if (mod_class == 0)
{
return kSalt3822;
}
if (mod_class == 1)
{
return kSaltD185;
}
return kSalt97B6;
}
std::wstring BuildCanonicalInput(const UserChoiceLatestHash::AssocContext &ctx, const wchar_t *salt)
{
std::wstring result;
if (ctx.mod_class == 0)
{
result += salt;
result += ctx.assoc;
result += ctx.timestamp_hex;
result += ctx.machine_id_trimmed;
result += ctx.progid;
result += ctx.sid;
}
else if (ctx.mod_class == 1)
{
result += salt;
result += ctx.timestamp_hex;
result += ctx.assoc;
result += ctx.sid;
result += ctx.machine_id_trimmed;
result += ctx.progid;
}
else
{
result += ctx.sid;
result += ctx.timestamp_hex;
result += salt;
result += ctx.assoc;
result += ctx.machine_id_trimmed;
result += ctx.progid;
}
return result;
}
bool LoadAssociationContext(const std::wstring &assoc, UserChoiceLatestHash::AssocContext *ctx)
{
ctx->assoc = assoc;
ctx->mod_class = -1;
if (!QueryRegString(HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\SQMClient",
L"MachineID",
&ctx->machine_id_raw))
{
return false;
}
ctx->machine_id_trimmed = StripOuterBraces(ctx->machine_id_raw);
if (ctx->machine_id_trimmed.empty() || !GetCurrentUserSidString(&ctx->sid))
{
return false;
}
const bool is_extension = !assoc.empty() && assoc[0] == L'.';
const std::wstring prefix = is_extension
? L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\"
: L"Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\";
const std::wstring base = prefix + assoc;
const wchar_t *choices[2] = { L"UserChoiceLatest", L"UserChoice" };
size_t index = 0U;
for (; index < 2U; ++index)
{
std::wstring choice_key = base + L"\\" + choices[index];
if (QueryRegString(HKEY_CURRENT_USER, choice_key, L"Hash", &ctx->registry_hash))
{
ctx->base_key = base;
ctx->choice_name = choices[index];
break;
}
}
if (index == 2U)
{
return false;
}
const std::wstring progid_key = ctx->base_key + L"\\" + ctx->choice_name + L"\\ProgId";
if (!QueryRegString(HKEY_CURRENT_USER, progid_key, L"ProgId", &ctx->progid)
&& !QueryRegString(HKEY_CURRENT_USER, ctx->base_key + L"\\" + ctx->choice_name, L"ProgId", &ctx->progid))
{
return false;
}
FILETIME last_write;
if (!QueryRegLastWriteTime(HKEY_CURRENT_USER, progid_key, &last_write)
&& !QueryRegLastWriteTime(HKEY_CURRENT_USER, ctx->base_key + L"\\" + ctx->choice_name, &last_write))
{
return false;
}
if (!FormatTimestampHexFromFileTime(last_write, &ctx->timestamp_hex))
{
return false;
}
ctx->mod_class = static_cast<int>(ctx->machine_id_trimmed[ctx->machine_id_trimmed.size() - 1U] % 3);
return true;
}
} // namespace
namespace UserChoiceLatestHash
{
bool LooksLikeAssociationToken(const std::wstring &value)
{
if (value.empty())
{
return false;
}
if (value.find(L' ') != std::wstring::npos || value.find(L'\\') != std::wstring::npos || value.find(L'/') != std::wstring::npos)
{
return false;
}
if (value[0] == L'.')
{
return true;
}
for (size_t i = 0; i < value.size(); ++i)
{
const wchar_t ch = value[i];
if (!(iswalnum(ch) || ch == L'+' || ch == L'-' || ch == L'.'))
{
return false;
}
}
return true;
}
bool VerifyCurrentAssociation(const std::wstring &assoc,
const UserChoiceLatestHash::WorkingSeeds &seeds,
AssocContext *ctx)
{
if (!LoadAssociationContext(assoc, ctx))
{
return false;
}
ctx->canonical_primary = BuildCanonicalInput(*ctx, PrimarySaltForClass(ctx->mod_class));
if (!UserChoiceLatestHash::ComputeHash(ctx->canonical_primary, seeds, false, &ctx->computed_primary, NULL))
{
return false;
}
return true;
}
int PrintVerificationResult(const AssocContext &ctx)
{
const bool match = _wcsicmp(ctx.registry_hash.c_str(), ctx.computed_primary.c_str()) == 0;
std::wcout
<< L"assoc: " << ctx.assoc << L"\n"
<< L"choice: " << ctx.choice_name << L"\n"
<< L"progid: " << ctx.progid << L"\n"
<< L"machine_id: " << ctx.machine_id_trimmed << L"\n"
<< L"sid: " << ctx.sid << L"\n"
<< L"timestamp: " << ctx.timestamp_hex << L"\n"
<< L"registry_hash: " << ctx.registry_hash << L"\n"
<< L"computed_hash: " << ctx.computed_primary << L"\n"
<< L"mod_class: " << ctx.mod_class << L"\n"
<< L"match: " << (match ? L"true" : L"false") << L"\n"
<< L"canonical: " << ctx.canonical_primary << L"\n";
return match ? 0 : 2;
}
} // namespace UserChoiceLatestHash