-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
339 lines (295 loc) · 8.77 KB
/
main.go
File metadata and controls
339 lines (295 loc) · 8.77 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
package main
import (
"bufio"
"bytes"
"encoding/hex"
"fmt"
"log"
"net/http"
"os"
"strings"
"golang.org/x/sys/windows/registry"
"golang.org/x/text/encoding/unicode"
)
// http://www.sentinelchicken.com/data/TheWindowsNTRegistryFileFormat.pdf
type FileData struct {
Value string
Key string
Type uint32
}
var issues int
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
for _, RegFilePath := range RegFilePaths {
file, err := os.Open(RegFilePath)
if err != nil {
log.Println(err)
continue
}
var scanner *bufio.Scanner
contentType := DetectContentType(file)
file.Seek(0, 0)
switch contentType {
case "utf-8":
scanner = bufio.NewScanner(file)
case "utf-16le":
scanner = bufio.NewScanner(unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder().Reader(file))
default:
file.Close()
fmt.Fprintln(os.Stderr, "ContentType:", contentType)
os.Exit(1)
}
var WorkingRegPath = new(Data)
var index uint
var line string
for scanner.Scan() {
index++
if index == 1 {
continue // skip "Windows Registry Editor Version"
}
if rawLine := scanner.Text(); len(rawLine) > 1 {
// fmt.Printf("[%d] %s\n", index, rawLine)
if Verbose {
fmt.Printf("[%d] %s\n", index, rawLine)
}
commentIndex := strings.Index(rawLine, ";")
if commentIndex != -1 {
rawLine = rawLine[:commentIndex] // remove comments
}
if strings.HasSuffix(rawLine, "\\") {
line += strings.TrimSuffix(strings.TrimSpace(rawLine), "\\")
continue
}
line += strings.TrimSpace(rawLine) // remove spaces
if line == "" {
continue // skip empty lines
}
if line[0] == 91 { // [
WorkingRegPath = new(Data)
line = line[1 : len(line)-1]
var rootkeySplit = strings.SplitN(line, `\`, 2)
WorkingRegPath.StringPath = rootkeySplit[1]
if rootkeySplit[0][0] == 45 { // [-HKEY_LOCAL_MACHINE\...
WorkingRegPath.DelRootKey = true
rootkeySplit[0] = rootkeySplit[0][1:] // remove "-"
}
if val, ok := FindRootString[rootkeySplit[0]]; ok {
WorkingRegPath.RootKey = val
} else {
line = ""
log.Panicf("unknown: %#v\n", rootkeySplit)
}
if WorkingRegPath.OpenHandle {
line = ""
WorkingRegPath.Close()
}
if err := WorkingRegPath.Open(); err != nil {
switch {
case err == registry.ErrNotExist && WorkingRegPath.DelRootKey:
WorkingRegPath.InfoHeader = true
WorkingRegPath.Log(Info, "\n["+line+"]")
case err == registry.ErrNotExist:
WorkingRegPath.InfoHeader = true
WorkingRegPath.Log(Err, "\n["+line+"] ;"+err.Error())
default:
WorkingRegPath.InfoHeader = true
WorkingRegPath.Log(Panic, "\n["+line+"] ;"+err.Error())
}
line = ""
continue
}
if WorkingRegPath.DelRootKey { // Path exists
WorkingRegPath.InfoHeader = true
WorkingRegPath.Log(Err, "\n["+line+"]")
line = ""
WorkingRegPath.Close()
continue
}
line = ""
} else {
if !WorkingRegPath.OpenHandle {
WorkingRegPath.Log(Err, line)
line = ""
continue
}
equalPos := strings.Index(line, "=")
if equalPos == -1 {
line = ""
continue
}
fileData := FileData{
Key: strings.Trim(line[:equalPos], `"`),
Value: line[equalPos+1:],
}
fileData.Type = GetClassification(fileData.Value)
if fileData.Key == "@" {
fileData.Key = "" // default value
}
switch fileData.Type {
case registry.NONE:
_, _, err := WorkingRegPath.RegPath.GetValue(fileData.Key, nil)
if err == registry.ErrNotExist {
WorkingRegPath.Log(Info, line)
} else {
WorkingRegPath.Log(Err, line)
}
case registry.SZ:
fileData.Value = strings.ReplaceAll(fileData.Value, `\"`, `"`)
fileData.Value = strings.ReplaceAll(fileData.Value, `\\`, `\`)
if fileData.Value[0] == 34 && fileData.Value[len(fileData.Value)-1] == 34 {
fileData.Value = fileData.Value[1 : len(fileData.Value)-1]
}
regValue, _, err := WorkingRegPath.RegPath.GetStringValue(fileData.Key)
switch err {
case nil: // none
case registry.ErrNotExist:
WorkingRegPath.Log(Err, line) // Entry not found
line = ""
continue
case registry.ErrUnexpectedType:
WorkingRegPath.Log(Err, line+" ;unexpected key value type") // Entry not in the same format
line = ""
continue
default: // Error
log.Println(err)
log.Fatal(line)
}
if regValue == fileData.Value {
WorkingRegPath.Log(Info, line)
} else {
WorkingRegPath.Log(Warn, line+" ;current: "+regValue)
}
case registry.DWORD:
RegFileValue := strings.TrimLeft(fileData.Value[6:], "0") // Deletes the dword: and all preceding 0s
regValue, _, err := WorkingRegPath.RegPath.GetIntegerValue(fileData.Key)
switch err {
case nil: // none
case registry.ErrNotExist:
WorkingRegPath.Log(Err, line) // Entry not found
line = ""
continue
case registry.ErrUnexpectedType:
WorkingRegPath.Log(Err, line+" ;unexpected key value type") // Entry not in the same format
line = ""
continue
default:
log.Println(err)
log.Fatal(line)
}
if CompareDWord(DecToHexstring(regValue), RegFileValue) {
WorkingRegPath.Log(Info, line)
} else {
WorkingRegPath.Log(Warn, line+" ;current: 0x"+DecToHexstring(regValue))
}
case registry.EXPAND_SZ: // REG_EXPAND_SZ
regValue, _, err := WorkingRegPath.RegPath.GetStringValue(fileData.Key)
switch err {
case nil: // none
case registry.ErrNotExist:
WorkingRegPath.Log(Err, line) // Entry not found
line = ""
continue
case registry.ErrUnexpectedType:
WorkingRegPath.Log(Err, line+" ;unexpected key value type") // Entry not in the same format
line = ""
continue
default:
log.Println(err)
log.Fatal(line)
}
regValue = strings.ReplaceAll(regValue, `\\`, `\`) // escape func?
if regValue == BinstringToString(fileData.Value[7:]) { // without "hex(2):"
WorkingRegPath.Log(Info, line)
} else {
WorkingRegPath.Log(Warn, line+" ;current: "+regValue)
}
case registry.BINARY: // Binär
regValue, _, err := WorkingRegPath.RegPath.GetBinaryValue(fileData.Key)
switch err {
case nil: // none
case registry.ErrNotExist:
WorkingRegPath.Log(Err, line) // Entry not found
line = ""
continue
case registry.ErrUnexpectedType:
WorkingRegPath.Log(Err, line+" ;unexpected key value type") // Entry not in the same format
line = ""
continue
default:
log.Println(err)
log.Fatal(line)
}
data, err := hex.DecodeString(strings.ReplaceAll(fileData.Value[strings.Index(fileData.Value, ":")+1:], ",", ""))
if err != nil {
WorkingRegPath.Log(Err, line) // Entry not in the same format
line = ""
continue
}
if bytes.EqualFold(regValue, data) {
WorkingRegPath.Log(Info, line)
} else {
WorkingRegPath.Log(Warn, line)
}
line = ""
case registry.QWORD: // QWORD
fallthrough
case registry.MULTI_SZ: // MULTI_SZ
regValue, _, err := WorkingRegPath.RegPath.GetIntegerValue(fileData.Key)
switch err {
case nil: // none
case registry.ErrNotExist:
WorkingRegPath.Log(Err, line) // Entry not found
line = ""
continue
case registry.ErrUnexpectedType:
WorkingRegPath.Log(Err, line+" ;unexpected key value type") // Entry not in the same format
line = ""
continue
default:
log.Println(err)
log.Fatal(line)
}
b := BinStringToByteArray(fileData.Value[7:])
i := ByteArrayToUint64(b)
if i == regValue {
WorkingRegPath.Log(Info, line)
} else {
WorkingRegPath.Log(Warn, fmt.Sprintf("%s ;current: %s", line, ByteArrayToBinString(Uint64ToByteArray(regValue))))
}
line = ""
default:
log.Printf("// TODO: unknown typ: %#v\n", fileData)
continue
}
line = ""
}
} else {
line = ""
}
}
if err := scanner.Err(); err != nil {
fmt.Printf("Invalid input: %s\n", err)
}
file.Close()
}
if issues != 0 {
fmt.Printf("; issues: %d\n; %s\n", issues, strings.Join(RegFilePaths, ","))
}
if !Exit {
fmt.Printf("\nPress 'Enter' to exit...\n")
fmt.Scanln()
}
}
func DetectContentType(file *os.File) string {
buffer := make([]byte, 512)
n, err := file.Read(buffer)
if err != nil {
log.Panicln("Error:", err)
}
contentType := http.DetectContentType(buffer[:n])
index := strings.Index(contentType, "; charset=")
if index == -1 {
return ""
}
return contentType[index+10:]
}