-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreghive.go
440 lines (398 loc) · 9.54 KB
/
reghive.go
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
package reghive
import (
"fmt"
"strings"
"sync"
"github.com/JoshuaDoes/crunchio"
hivex "github.com/gabriel-samfira/go-hivex"
)
//Reghive holds a session for a registry hive
type Reghive struct {
Hive *hivex.Hivex
Path string
Root *Key
}
var (
_ = fmt.Errorf
hivesLock sync.Mutex
hives []*Reghive
)
//Open returns a handle to a specified registry hive, cloning a handle for the same path
func Open(path string) (*Reghive, error) {
for i := 0; i < len(hives); i++ {
if hives[i].Path == path {
return hives[i], nil
}
}
hive, err := hivex.NewHivex(path, hivex.WRITE)
if err != nil {
return nil, err
}
root, err := hive.Root()
if err != nil {
return nil, err
}
rh := &Reghive{Hive: hive, Path: path}
key, err := rh.DecodeKey(root)
if err != nil {
return nil, err
}
rh.Root = key
if hives == nil {
hives = make([]*Reghive, 0)
}
hives = append(hives, rh)
return rh, nil
}
//Close closes the handle for the registry hive, nullifying usage of it
func (rh *Reghive) Close() error {
path := rh.Path
if err := rh.Hive.Close(); err != nil {
return err
}
rh.Hive = nil
rh.Path = ""
rh.Root = nil
if hives != nil && len(hives) > 0 {
hivesLock.Lock()
index := -1
for i := 0; i < len(hives); i++ {
if hives[i].Path == path {
index = i
break
}
}
if index > -1 {
hives = append(hives[:index], hives[index+1:]...)
}
hivesLock.Unlock()
}
return nil
}
func (rh *Reghive) sync() error {
_, err := rh.Hive.Commit()
return err
}
//PathSplit splits a key path into an array of nodes by name (without checking against hives, used internally)
func PathSplit(key string) []string {
if key == "" || key == "/" {
return make([]string, 0)
}
//Node keys are case insensitive, and so will be this search
key = strings.ToLower(key)
//Split the requested path into each node key to nest into
keys := strings.Split(key, "/")
//Strip path node shenanigans
if keys[0] == "" { //path starts with /
keys = keys[1:]
}
if keys[len(keys)-1] == "" { //path ends with /
keys = keys[:len(keys)-1]
}
return keys
}
//GetKey returns a specified key (case insensitive)
func (rh *Reghive) GetKey(name string) (*Key, error) {
keys := PathSplit(name)
if len(keys) <= 0 {
return rh.Root, nil
}
//Loop over each key in the path, nesting deeper into the node structure to find our target key
key := rh.Root
for i := 0; i < len(keys); i++ {
child, err := key.GetChild(keys[i])
if err != nil {
return nil, err
}
key = child
}
return key, nil
}
//MakeKey makes a specified key if it doesn't exist (including any parent keys) and returns it (case sensitive)
func (rh *Reghive) MakeKey(name string) (*Key, error) {
keys := PathSplit(name)
if len(keys) <= 0 {
return nil, ERROR_ROOT_MAKE
}
key := rh.Root
new := 0
for i := 0; i < len(keys); i++ {
new = i
child, err := key.GetChild(keys[i])
if err != nil {
break
}
key = child
}
for i := new; i < len(keys); i++ {
child, err := key.MakeChild(keys[i])
if err != nil {
return nil, err
}
key = child
}
return key, nil
}
//DeleteKey removes a specified key and its children (case insensitive)
func (rh *Reghive) DeleteKey(name string) error {
key, err := rh.GetKey(name)
if err != nil {
return err
}
keyName, err := key.GetName()
if err != nil {
return err
}
parent, err := key.GetParent()
if err != nil {
return err
}
if err := parent.DeleteChild(keyName); err != nil {
return err
}
return rh.sync()
}
//DecodeKey decodes the specified node into a key
func (rh *Reghive) DecodeKey(node int64) (*Key, error) {
k := &Key{Reghive: rh, Node: node}
return k, nil
}
//DecodeValue decodes the specified node into a value
func (rh *Reghive) DecodeValue(node, value int64) (*Value, error) {
k, err := rh.DecodeKey(node)
if err != nil {
return nil, err
}
v := newValue(k)
valueKey, err := rh.Hive.NodeValueKey(value)
if err != nil {
return nil, err
}
if valueKey == "" {
valueKey = "@"
}
v.Name = valueKey
valueType, valueBytes, err := rh.Hive.ValueValue(value)
if err != nil {
return nil, err
}
v.Type = RegValueType(valueType)
v.Write(valueBytes)
v.ready = true
return v, nil
}
//Key holds a node (AKA key) from a registry hive
type Key struct {
Reghive *Reghive
Node int64
}
//GetName returns the name of this key
func (k *Key) GetName() (string, error) {
name, err := k.Reghive.Hive.NodeName(k.Node)
return name, err
}
//GetParent returns the parent key to this child
func (k *Key) GetParent() (*Key, error) {
parent, err := k.Reghive.Hive.NodeParent(k.Node)
if err != nil {
return nil, err
}
return k.Reghive.DecodeKey(parent)
}
//GetChildNames returns a list of all values for this key in order
func (k *Key) GetChildNames() ([]string, error) {
nodes, err := k.Reghive.Hive.NodeChildren(k.Node)
if err != nil {
return nil, err
}
children := make([]string, len(nodes))
for i := 0; i < len(nodes); i++ {
name, err := k.Reghive.Hive.NodeName(nodes[i])
if err != nil {
return nil, err
}
children[i] = name
}
return children, nil
}
//GetChild returns a specified child key (case insensitive)
func (k *Key) GetChild(name string) (*Key, error) {
node, err := k.Reghive.Hive.NodeGetChild(k.Node, name)
if err != nil {
return nil, err
}
if node == 0 {
return nil, ERROR_CHILD_MISSING
}
return k.Reghive.DecodeKey(node)
}
//MakeChild makes a specified child key (case sensitive)
func (k *Key) MakeChild(name string) (*Key, error) {
node, err := k.Reghive.Hive.NodeAddChild(k.Node, name)
if err != nil {
return nil, err
}
if err := k.Reghive.sync(); err != nil {
return nil, err
}
return k.Reghive.DecodeKey(node)
}
//DeleteChild recursively removes a specified child key (case insensitive)
func (k *Key) DeleteChild(name string) error {
node, err := k.Reghive.Hive.NodeGetChild(k.Node, name)
if err != nil {
return err
}
if _, err := k.Reghive.Hive.NodeDeleteChild(node); err != nil {
return err
}
return k.Reghive.sync()
}
//GetValueNames returns a list of all values for this key in order
func (k *Key) GetValueNames() ([]string, error) {
nodes, err := k.Reghive.Hive.NodeValues(k.Node)
if err != nil {
return nil, err
}
values := make([]string, len(nodes))
for i := 0; i < len(nodes); i++ {
name, err := k.Reghive.Hive.NodeValueKey(nodes[i])
if err != nil {
return nil, err
}
values[i] = name
}
return values, nil
}
//GetValue finds and returns a value (case insensitive)
func (k *Key) GetValue(name string) (*Value, error) {
if name == "@" {
name = ""
}
node, err := k.Reghive.Hive.NodeGetValue(k.Node, name)
if err != nil {
return nil, err
}
return k.Reghive.DecodeValue(k.Node, node)
}
//MakeValue
func (k *Key) MakeValue(name string) (*Value, error) {
v := newValue(k)
v.SetName(name)
v.ready = true
if err := v.sync(); err != nil {
return nil, err
}
return k.GetValue(name)
}
//DeleteValue deletes a value from a key (case insensitive)
func (k *Key) DeleteValue(name string) error {
node, err := k.Reghive.Hive.NodeGetValue(k.Node, name)
if err != nil {
return err
}
values, err := k.Reghive.Hive.NodeValues(k.Node)
if err != nil {
return err
}
index := -1
for i := 0; i < len(values); i++ {
if values[i] == node {
index = i
break
}
}
if index > -1 {
values = append(values[:index], values[index+1:]...)
}
hiveValues := make([]hivex.HiveValue, len(values))
for i := 0; i < len(values); i++ {
valueKey, err := k.Reghive.Hive.NodeValueKey(values[i])
if err != nil {
return err
}
valType, valBytes, err := k.Reghive.Hive.ValueValue(values[i])
if err != nil {
return err
}
hiveValues[i] = hivex.HiveValue{
Type: int(valType),
Key: valueKey,
Value: valBytes,
}
}
if _, err := k.Reghive.Hive.NodeSetValues(k.Node, hiveValues); err != nil {
return err
}
return k.Reghive.sync()
}
//Value holds a value from a key that can be changed or written to
type Value struct {
*crunchio.Buffer
Key *Key
Name string
Type RegValueType
ready bool //true if allowed to sync changes to hive
}
func newValue(k *Key) *Value {
return &Value{crunchio.NewBuffer(), k, "", RegNone, false}
}
func (v *Value) sync() error {
if !v.ready {
return nil
}
hv := v.hiveValue()
if _, err := v.Key.Reghive.Hive.NodeSetValue(v.Key.Node, hv); err != nil {
return err
}
return v.Key.Reghive.sync()
}
func (v *Value) hiveValue() hivex.HiveValue {
hv := hivex.HiveValue{
Type: int(v.Type),
Key: v.Name,
Value: v.Bytes(),
}
if len(hv.Value) == 0 { //Default to REG_NONE with a single byte so hivex can still make a pointer
hv.Value = make([]byte, 1)
hv.Type = int(RegNone)
}
return hv
}
//SetType changes the type of the value
func (v *Value) SetType(valType RegValueType) error {
v.Type = valType
return v.sync()
}
//SetName changes the name of the value
func (v *Value) SetName(name string) error {
if name == "@" {
name = ""
}
v.Name = name
return v.sync()
}
//SetValue changes the data of the value using a supported input type
//Data must be one of the following types:
//
// >> TODO: Add registry value types again now that we're using crunchio <<
//
//- BCDDevice (BCD_DEVICE)
//- BCDDescType (BCD_DESCTYPE)
//- byte, bool, int, uint (DWORD_LITTLE)
//- []byte (BINARY)
//- string (SZ)
//- []string (TODO)
//- int16, int32, uint16, uint32 (DWORD_LITTLE)
//- int64, uint64 (QWORD)
//- float32, float64 (BINARY)
//Alternatively, data may satisfy anything supported by crunchio's WriteAbstract method
func (v *Value) SetValue(data interface{}) error {
switch data.(type) {
case BCDDevice:
return ERROR_VALUE_TYPE //TODO: Encode BCDDevice to []byte
case BCDDescType:
return ERROR_VALUE_TYPE //TODO: Encode BCDDescType to []byte
}
return v.WriteAbstract(data)
}