-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeKey.cs
186 lines (132 loc) · 5.43 KB
/
NodeKey.cs
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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
namespace ntregsharp
{
public class NodeKey
{
public NodeKey (BinaryReader hive)
{
ReadNodeStructure (hive);
ReadChildrenNodes (hive);
ReadChildValues (hive);
}
public void EditNodeName (FileStream hive, string newName)
{
byte[] name = System.Text.Encoding.UTF8.GetBytes (System.Text.Encoding.UTF8.GetString (System.Text.Encoding.ASCII.GetBytes (newName)));
if (name.Length > this.NameLength)
throw new Exception ("Strings larger than the original are not currently supported");
//I need to figure out what this actual math is and make it one line
//I stole it from below when I must have been smarter/drunk
hive.Position = this.AbsoluteOffset + 52;
hive.Position += (this.AbsoluteOffset + 72) - hive.Position;
byte[] nameLenBytes = new byte[] { (byte)hive.ReadByte (), (byte)hive.ReadByte () };
short curLength = BitConverter.ToInt16 (nameLenBytes, 0);
if (curLength != this.NameLength)
throw new Exception ("Error in reading");
hive.Position += 2;
int k = this.NameLength - name.Length;
hive.Write (name, 0, name.Length);
for (int i = 0; i < k; i++)
hive.WriteByte (0x00);
}
private void ReadNodeStructure (BinaryReader hive)
{
this.AbsoluteOffset = hive.BaseStream.Position;
byte[] buf = hive.ReadBytes (4);
if (buf [0] != 110 || buf [1] != 107)
throw new NotSupportedException ("Bad nk header");
long startingOffset = this.AbsoluteOffset + 4;
this.IsRootKey = (buf [2] == 0x2c) ? true : false;
this.Timestamp = DateTime.FromFileTime (BitConverter.ToInt64 (hive.ReadBytes (8), 0));
hive.BaseStream.Position += 4;
this.ParentOffset = BitConverter.ToInt32 (hive.ReadBytes (4), 0);
this.SubkeysCount = BitConverter.ToInt32 (hive.ReadBytes (4), 0);
hive.BaseStream.Position += 4;
this.LFRecordOffset = BitConverter.ToInt32 (hive.ReadBytes (4), 0);
hive.BaseStream.Position += 4;
this.ValuesCount = BitConverter.ToInt32 (hive.ReadBytes (4), 0);
this.ValueListOffset = BitConverter.ToInt32 (hive.ReadBytes (4), 0);
this.SecurityKeyOffset = BitConverter.ToInt32 (hive.ReadBytes (4), 0);
this.ClassnameOffset = BitConverter.ToInt32 (hive.ReadBytes (4), 0);
hive.BaseStream.Position += (startingOffset + 68) - hive.BaseStream.Position;
this.NameLength = BitConverter.ToInt16 (hive.ReadBytes (2), 0);
this.ClassnameLength = BitConverter.ToInt16 (hive.ReadBytes (2), 0);
buf = hive.ReadBytes (this.NameLength);
this.Name = System.Text.Encoding.UTF8.GetString (buf);
}
private void ReadChildrenNodes (BinaryReader hive)
{
this.ChildNodes = new List<NodeKey> ();
if (this.LFRecordOffset != -1) {
hive.BaseStream.Position = 4096 + this.LFRecordOffset + 4;
byte[] buf = hive.ReadBytes (2);
//ri
if (buf [0] == 0x72 && buf [1] == 0x69) {
int count = BitConverter.ToInt16 (hive.ReadBytes (2), 0);
for (int i = 0; i < count; i++) {
long pos = hive.BaseStream.Position;
int offset = BitConverter.ToInt32 (hive.ReadBytes (4), 0);
hive.BaseStream.Position = 4096 + offset + 4;
buf = hive.ReadBytes (2);
if (!(buf [0] == 0x6c && (buf [1] == 0x66 || buf [1] == 0x68)))
throw new Exception ("Bad LF/LH record at: " + hive.BaseStream.Position);
ParseChildNodes (hive);
hive.BaseStream.Position = pos + 4; //go to next record list
}
}
//lf or lh
else if (buf [0] == 0x6c && (buf [1] == 0x66 || buf [1] == 0x68))
ParseChildNodes (hive);
else
throw new Exception ("Bad LF/LH/RI Record at: " + hive.BaseStream.Position);
}
}
private void ParseChildNodes (BinaryReader hive)
{
int count = BitConverter.ToInt16 (hive.ReadBytes (2), 0);
long topOfList = hive.BaseStream.Position;
for (int i = 0; i < count; i++) {
hive.BaseStream.Position = topOfList + (i * 8);
int newoffset = BitConverter.ToInt32 (hive.ReadBytes (4), 0);
hive.BaseStream.Position += 4;
//byte[] check = hive.ReadBytes(4);
hive.BaseStream.Position = 4096 + newoffset + 4;
NodeKey nk = new NodeKey (hive) { ParentNodeKey = this };
this.ChildNodes.Add (nk);
}
hive.BaseStream.Position = topOfList + (count * 8);
}
private void ReadChildValues (BinaryReader hive)
{
this.ChildValues = new List<ValueKey> ();
if (this.ValueListOffset != -1) {
hive.BaseStream.Position = 4096 + this.ValueListOffset + 4;
for (int i = 0; i < this.ValuesCount; i++) {
hive.BaseStream.Position = 4096 + this.ValueListOffset + 4 + (i * 4);
int offset = BitConverter.ToInt32 (hive.ReadBytes (4), 0);
hive.BaseStream.Position = 4096 + offset + 4;
this.ChildValues.Add (new ValueKey (hive));
}
}
}
public long AbsoluteOffset { get; set; }
public List<NodeKey> ChildNodes { get; set; }
public List<ValueKey> ChildValues { get; set; }
public DateTime Timestamp { get; set; }
public int ParentOffset { get; set; }
public int SubkeysCount { get; set; }
public int LFRecordOffset { get; set; }
public int ClassnameOffset { get; set; }
public int SecurityKeyOffset { get; set; }
public int ValuesCount { get; set; }
public int ValueListOffset { get; set; }
public short NameLength { get; set; }
public bool IsRootKey { get; set; }
public short ClassnameLength { get; set; }
public string Name { get; set; }
public byte[] ClassnameData { get; set; }
public NodeKey ParentNodeKey { get; set; }
}
}