-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRegistryParser.cs
More file actions
501 lines (433 loc) · 17.1 KB
/
RegistryParser.cs
File metadata and controls
501 lines (433 loc) · 17.1 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace Swarmer
{
public class RegistryParser
{
public RegistryParser()
{
}
public RegistryKeyInfo ParseRegFile(string regFilePath)
{
string[] lines = File.ReadAllLines(regFilePath, Encoding.Unicode);
// Check for REG format header
if (lines.Length == 0 || !lines[0].StartsWith("Windows Registry Editor"))
{
throw new FormatException("Not a valid .reg file. Missing registry editor header.");
}
// Create a root key to represent HKCU
RegistryKeyInfo rootKey = new RegistryKeyInfo
{
Name = "HKEY_CURRENT_USER",
Values = new List<RegistryValueInfo>(),
Subkeys = new List<RegistryKeyInfo>()
};
string currentKeyPath = null;
RegistryKeyInfo currentKey = null;
// For multi-line values
bool collectingMultiLine = false;
string multiLineName = null;
StringBuilder multiLineValue = new StringBuilder();
// Process each line
for (int i = 1; i < lines.Length; i++)
{
string line = lines[i].Trim();
// Skip empty lines
if (string.IsNullOrEmpty(line))
continue;
// If we're collecting a multi-line value
if (collectingMultiLine)
{
// Check if this line continues the multi-line value
if (line.EndsWith("\\"))
{
// Remove the trailing backslash and add to our value
multiLineValue.Append(line.Substring(0, line.Length - 1));
continue;
}
else
{
// This is the last line of the multi-line value
multiLineValue.Append(line);
// Process the complete value
if (currentKey != null)
{
string fullValueLine = multiLineName + "=" + multiLineValue.ToString();
RegistryValueInfo value = ParseValueLine(fullValueLine);
if (value != null)
{
currentKey.Values.Add(value);
}
}
// Reset multi-line tracking
collectingMultiLine = false;
multiLineName = null;
multiLineValue.Clear();
continue;
}
}
// Check for key declaration
if (line.StartsWith("[") && line.EndsWith("]"))
{
// Extract key path
string keyPath = line.Substring(1, line.Length - 2);
// Handle key deletion notation
if (keyPath.StartsWith("-"))
{
// We're creating a new hive, so we can just skip deletion directives
currentKeyPath = null;
currentKey = null;
continue;
}
// Check if it's a HKCU key (which we want)
if (keyPath.Equals("HKEY_CURRENT_USER") || keyPath.Equals("HKCU"))
{
// This is the root key
currentKeyPath = "";
currentKey = rootKey;
}
else if (keyPath.StartsWith("HKEY_CURRENT_USER\\") || keyPath.StartsWith("HKCU\\"))
{
// Remove the HKCU prefix
if (keyPath.StartsWith("HKEY_CURRENT_USER\\"))
keyPath = keyPath.Substring("HKEY_CURRENT_USER\\".Length);
else if (keyPath.StartsWith("HKCU\\"))
keyPath = keyPath.Substring("HKCU\\".Length);
currentKeyPath = keyPath;
// Create or get the key in our structure
currentKey = GetOrCreateKeyPath(rootKey, keyPath.Split('\\'));
}
else
{
// We only want HKCU keys for our hive
currentKeyPath = null;
currentKey = null;
}
}
else if (currentKey != null)
{
// Check for value
int equalsPos = line.IndexOf('=');
if (equalsPos > 0)
{
// Extract name and value parts
string name = line.Substring(0, equalsPos);
string value = line.Substring(equalsPos + 1);
// Check if this is a multi-line value (ends with backslash)
if (value.EndsWith("\\"))
{
// Start tracking a multi-line value
collectingMultiLine = true;
multiLineName = name;
multiLineValue.Clear();
multiLineValue.Append(value.Substring(0, value.Length - 1));
}
else
{
// Process single-line value
RegistryValueInfo valueInfo = ParseValueLine(line);
if (valueInfo != null)
{
currentKey.Values.Add(valueInfo);
}
}
}
}
}
return rootKey;
}
private RegistryKeyInfo GetOrCreateKeyPath(RegistryKeyInfo rootKey, string[] keyParts)
{
RegistryKeyInfo currentKey = rootKey;
foreach (string part in keyParts)
{
if (string.IsNullOrEmpty(part))
continue;
// Look for existing subkey
RegistryKeyInfo subKey = currentKey.Subkeys.Find(k => k.Name.Equals(part, StringComparison.OrdinalIgnoreCase));
if (subKey == null)
{
// Create new subkey
subKey = new RegistryKeyInfo
{
Name = part,
Values = new List<RegistryValueInfo>(),
Subkeys = new List<RegistryKeyInfo>()
};
currentKey.Subkeys.Add(subKey);
}
currentKey = subKey;
}
return currentKey;
}
private RegistryValueInfo ParseValueLine(string line)
{
try
{
// Find the position of the first equals sign
int equalsPos = line.IndexOf('=');
if (equalsPos <= 0)
{
return null;
}
// Extract name and value parts
string namePart = line.Substring(0, equalsPos).Trim();
string valueContent = line.Substring(equalsPos + 1).Trim();
// Parse the name part
string valueName;
if (namePart == "@")
{
valueName = "";
}
else if (namePart.StartsWith("\"") && namePart.EndsWith("\""))
{
// Remove the quotes
valueName = namePart.Substring(1, namePart.Length - 2);
// Unescape the name
valueName = UnescapeRegString(valueName);
}
else
{
// This is an invalid format, but let's try to handle it anyway
valueName = namePart;
}
// Parse the value content
uint valueType;
byte[] valueData;
if (valueContent.StartsWith("dword:"))
{
// DWORD value
valueType = API.REG_DWORD;
string dwordStr = valueContent.Substring(6);
try
{
uint dwordValue = Convert.ToUInt32(dwordStr, 16);
valueData = BitConverter.GetBytes(dwordValue);
}
catch (Exception)
{
return null;
}
}
else if (valueContent.StartsWith("hex:"))
{
// Binary value
valueType = API.REG_BINARY;
string hexData = valueContent.Substring(4);
valueData = ParseHexString(hexData);
}
else if (valueContent.StartsWith("hex("))
{
// Other hex-encoded value
int closingParenPos = valueContent.IndexOf(')');
if (closingParenPos < 0)
{
return null;
}
string typeStr = valueContent.Substring(4, closingParenPos - 4);
string hexData = valueContent.Substring(closingParenPos + 2); // Skip "):""
try
{
valueType = Convert.ToUInt32(typeStr, 16);
}
catch
{
valueType = API.REG_BINARY;
}
valueData = ParseHexString(hexData);
}
else if (valueContent.StartsWith("\"") && valueContent.EndsWith("\""))
{
// String value
valueType = API.REG_SZ;
string strValue = valueContent.Substring(1, valueContent.Length - 2);
// Unescape the string
strValue = UnescapeRegString(strValue);
// Add null terminator
strValue += '\0';
valueData = Encoding.Unicode.GetBytes(strValue);
}
else
{
return null;
}
return new RegistryValueInfo
{
Name = valueName,
Type = valueType,
Data = valueData
};
}
catch (Exception)
{
return null;
}
}
private string UnescapeRegString(string str)
{
// Handle escape sequences in registry strings
StringBuilder result = new StringBuilder(str.Length);
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '\\' && i + 1 < str.Length)
{
// Handle escape sequence
switch (str[i + 1])
{
case '\\': // Backslash
result.Append('\\');
break;
case '"': // Quote
result.Append('"');
break;
case 'r': // Carriage return
result.Append('\r');
break;
case 'n': // Line feed
result.Append('\n');
break;
default: // Other escaped character (just keep it)
result.Append(str[i + 1]);
break;
}
i++; // Skip the escaped character
}
else
{
result.Append(str[i]);
}
}
return result.ToString();
}
private byte[] ParseHexString(string hexString)
{
List<byte> bytes = new List<byte>();
// Remove whitespace and line continuation characters
StringBuilder cleanedHex = new StringBuilder();
for (int i = 0; i < hexString.Length; i++)
{
if (hexString[i] == '\\')
{
// Skip backslash and any following whitespace
i++;
while (i < hexString.Length && char.IsWhiteSpace(hexString[i]))
i++;
// Back up one character since the loop will increment i
i--;
}
else if (!char.IsWhiteSpace(hexString[i]))
{
cleanedHex.Append(hexString[i]);
}
}
// Split by commas
string[] parts = cleanedHex.ToString().Split(',');
foreach (string part in parts)
{
string trimmedPart = part.Trim();
if (string.IsNullOrEmpty(trimmedPart))
continue;
try
{
byte b = Convert.ToByte(trimmedPart, 16);
bytes.Add(b);
}
catch (Exception)
{
}
}
return bytes.ToArray();
}
// Helper method to check if a quote is escaped
private bool IsEscapedQuote(string text, int quotePos)
{
if (quotePos <= 0)
return false;
// Count backslashes before the quote
int backslashCount = 0;
int pos = quotePos - 1;
while (pos >= 0 && text[pos] == '\\')
{
backslashCount++;
pos--;
}
// If there's an odd number of backslashes, the quote is escaped
return backslashCount % 2 != 0;
}
private bool IsHexDigit(char c)
{
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
}
// Helper class to mimic std::string_view from C++
public class StringView
{
private readonly string _source;
private int _start;
private int _length;
public StringView(string source)
{
_source = source;
_start = 0;
_length = source.Length;
}
public int Length => _length;
public bool IsEmpty => _length == 0;
public char this[int index]
{
get
{
if (index < 0 || index >= _length)
throw new IndexOutOfRangeException();
return _source[_start + index];
}
}
public void RemovePrefix(int count)
{
if (count > _length)
count = _length;
_start += count;
_length -= count;
}
public string Substring(int start, int length)
{
if (start < 0 || start >= _length)
throw new IndexOutOfRangeException();
if (length > _length - start)
length = _length - start;
return _source.Substring(_start + start, length);
}
public int IndexOf(string value)
{
int pos = _source.IndexOf(value, _start, _length);
return pos == -1 ? -1 : pos - _start;
}
public bool StartsWith(string value)
{
if (_length < value.Length)
return false;
for (int i = 0; i < value.Length; i++)
{
if (_source[_start + i] != value[i])
return false;
}
return true;
}
}
// Classes to store registry information
public class RegistryValueInfo
{
public string Name { get; set; }
public uint Type { get; set; }
public byte[] Data { get; set; }
}
public class RegistryKeyInfo
{
public string Name { get; set; }
public List<RegistryValueInfo> Values { get; set; }
public List<RegistryKeyInfo> Subkeys { get; set; }
}
}