-
Notifications
You must be signed in to change notification settings - Fork 0
/
reggie.go
247 lines (214 loc) · 6.49 KB
/
reggie.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
//go:build windows
package reggie
import (
"fmt"
"golang.org/x/sys/windows/registry"
"strings"
)
type Reg struct {
RootKey registry.Key // The key in which to access (HKLM, HKCU, etc). Can also be a subkey
ActiveKey registry.Key // The key currently opened, if different from the root key (i.e parent key)
Path string // The path inside RootKey to use
Permission uint32 // The access type for given RootKey and Path
SubKeyMap map[string]*SubKey // Holds the subkeys underneath RootKey
}
type SubKey struct {
Data *Reg // Holds the subkey information
Value map[string]any // Holds the key value data stored in each subkey.
}
// NewReg initialises a Reg struct. Permission can be supplied via go's registry package.
func NewReg(permission uint32) *Reg {
return &Reg{
Permission: permission,
SubKeyMap: make(map[string]*SubKey),
}
}
// NewSubKey initialises a struct for Reg.SubKeyMap. Permission can be supplied via go's registry package.
func NewSubKey(permission uint32) *SubKey {
return &SubKey{
Data: NewReg(permission),
Value: make(map[string]any),
}
}
// OpenKey is used to open a key inside the SubKey struct. Parameter `populateKeyValues` is true or false if you
// want to populate the SubKeyMap map with it's held data.
func (s *SubKey) OpenKey(populateKeyValues bool) (*Reg, error) {
k := Reg{
RootKey: s.Data.RootKey,
Path: s.Data.Path,
Permission: s.Data.Permission,
SubKeyMap: make(map[string]*SubKey),
}
if populateKeyValues {
err := k.FillKeysValues()
if err != nil {
return nil, err
}
}
return &k, nil
}
// FillKeysValues obtains RootKey, enumerates through every subkey in given Path. Each subkey will be attached inside Reg.SubKeyMap
// with its relevant data.
func (r *Reg) FillKeysValues() error {
s, err := r.EnumerateSubKeys(0)
if err != nil {
return err
}
for _, subkey := range s {
p := r.Path + "\\" + subkey
key, err := registry.OpenKey(r.RootKey, p, r.Permission) // Must open each subkey as a new key
if err != nil {
if strings.Contains(err.Error(), "Access is denied") {
return fmt.Errorf("access denied for key: %s", p)
}
return err
}
if r.SubKeyMap == nil {
r.SubKeyMap = make(map[string]*SubKey)
}
if r.SubKeyMap[subkey] == nil {
r.SubKeyMap[subkey] = NewSubKey(r.Permission)
}
r.SubKeyMap[subkey].Data = &Reg{
Path: p,
RootKey: r.RootKey,
ActiveKey: key,
Permission: r.Permission,
}
names, err := key.ReadValueNames(0)
if err != nil {
return err
}
for _, n := range names {
value, err := r.GetValue(key, n)
if err != nil {
return err
}
if value != nil { // Populate if it's not empty
r.SubKeyMap[subkey].Value[n] = value
}
}
}
return nil
}
// GetValue takes a specified registry key and returns the value of the named key `n`.
// This is a generic wrapper function over registry.GetValue
func (r *Reg) GetValue(k registry.Key, n string) (any, error) {
var err error
var v any
_, t, _ := k.GetValue(n, nil)
switch t {
case registry.NONE:
return nil, nil // Allow nil checks
case registry.SZ:
v, _, err = k.GetStringValue(n)
case registry.EXPAND_SZ:
v, _, err = k.GetStringValue(n)
v, err = registry.ExpandString(v.(string))
case registry.DWORD, registry.QWORD:
v, _, err = k.GetIntegerValue(n)
case registry.BINARY:
v, _, err = k.GetBinaryValue(n)
case registry.MULTI_SZ:
v, _, err = k.GetStringsValue(n)
}
if err != nil {
return nil, err
}
return v, nil
}
// CreateKey creates a child key from the Reg.ActiveKey and add it to the current SubKeyMap
func (r *Reg) CreateKey(name string) error {
_, exists, err := registry.CreateKey(r.ActiveKey, name, r.Permission)
if err != nil {
return err
}
if exists {
return fmt.Errorf("key %s already exists", name)
}
if r.SubKeyMap == nil {
r.SubKeyMap = make(map[string]*SubKey)
}
r.SubKeyMap[name] = NewSubKey(r.Permission)
return nil
}
// CreateValue will create a designated key=value pair based on the valueType passed from registry type constants
func (r *Reg) CreateValue(key string, value any, valueType uint32) error {
var err error
switch valueType {
case registry.SZ:
if _, ok := value.(string); !ok {
err = fmt.Errorf("value is not of type string but of type: %T", value)
break
}
err = r.ActiveKey.SetStringValue(key, value.(string))
case registry.EXPAND_SZ:
if _, ok := value.(string); !ok {
err = fmt.Errorf("value is not of type string but of type: %T", value)
break
}
err = r.ActiveKey.SetExpandStringValue(key, value.(string))
case registry.MULTI_SZ:
if _, ok := value.(string); !ok {
err = fmt.Errorf("value is not of type string but of type: %T", value)
break
}
err = r.ActiveKey.SetStringsValue(key, value.([]string))
case registry.BINARY:
if _, ok := value.([]byte); !ok {
err = fmt.Errorf("value is not of type []byte but of type: %T", value)
break
}
err = r.ActiveKey.SetBinaryValue(key, value.([]byte))
case registry.QWORD:
if _, ok := value.(uint64); !ok {
err = fmt.Errorf("value is not of type uint64 but of type: %T", value)
break
}
err = r.ActiveKey.SetQWordValue(key, value.(uint64))
case registry.DWORD:
if _, ok := value.(uint32); !ok {
err = fmt.Errorf("value is not of type uint32 but of type: %T", value)
break
}
err = r.ActiveKey.SetDWordValue(key, value.(uint32))
}
if err != nil {
return err
}
err = r.FillKeysValues()
if err != nil {
return err
}
return nil
}
// EnumerateSubKeys takes the given key in the Reg struct, enumerate
// and find it's subkeys. Amount specifies how many subkeys you want to enumerate.
// The default value is 0 to enumerate all within a given key, anything above 0 will enumerate to the specified amount.
// Amount cannot have more than one element. Behaves the same as specified in registry documentation: https://pkg.go.dev/golang.org/x/sys/windows/registry#Key.ReadSubKeyNames
func (r *Reg) EnumerateSubKeys(amount int) ([]string, error) {
var sKeys []string
key, err := registry.OpenKey(r.RootKey, r.Path, registry.ENUMERATE_SUB_KEYS)
if err != nil {
return nil, err
}
sKeys, err = key.ReadSubKeyNames(amount)
if err != nil {
return nil, err
}
return sKeys, nil
}
// Close will close the current RootKey and ActiveKey. After closing, it will clear the SubKeyMap of the specified Reg
// object
func (r *Reg) Close() (bool, error) {
err := r.ActiveKey.Close()
if err != nil {
return false, err
}
err = r.RootKey.Close()
if err != nil {
return false, err
}
clear(r.SubKeyMap)
return true, nil
}