-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (130 loc) · 3.77 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/weoses/cryptopro-cert-export/registry2"
"golang.org/x/sys/windows/registry"
"golang.org/x/text/encoding/charmap"
)
// HKEY_LOCAL_MACHINE\S-1-5-21-2408434269-2496968396-1739759940-6064\Keys\
func main() {
container := ""
userSid := ""
outputFolder := "."
flag.StringVar(&container, "container", "", "Container name to export")
flag.StringVar(&userSid, "sid", "", "User sid")
flag.StringVar(&outputFolder, "out", ".", "Output folder")
flag.Parse()
log.Printf("Arg: container = '%s'\n", container)
log.Printf("Arg: userSid = '%s'\n", userSid)
log.Printf("Arg: outputFolder = '%s'\n", outputFolder)
log.Println("Opening cryptopro root (WOW64)...")
cproot, err := registry2.HKLMPath(
registry.QUERY_VALUE|registry.ENUMERATE_SUB_KEYS,
`SOFTWARE\WOW6432Node\Crypto Pro\Settings\Users`)
if err != nil {
log.Println("Opening cryptopro root (x64)...")
cproot, err = registry2.HKLMPath(
registry.QUERY_VALUE|registry.ENUMERATE_SUB_KEYS,
`SOFTWARE\WOW6432Node\Crypto Pro\Settings\Users`)
if err != nil {
log.Fatal(err)
}
}
defer cproot.Close()
log.Println("Reading users nodes...")
userRoots := cproot.SubKeys(
registry.QUERY_VALUE|registry.ENUMERATE_SUB_KEYS,
func(s string) bool {
log.Printf("Found user %s ...\n", s)
return userSid == "" || userSid == s
},
func(s string, err error) {
log.Printf("Error open subkey for user %s - %s...\n", s, err.Error())
},
)
log.Printf("User nodes count - %d \n", len(userRoots))
defer func() {
for _, rk := range userRoots {
rk.Close()
}
}()
for _, userRoot := range userRoots {
log.Println("Reading containers...")
keyRoot, err := userRoot.Resolve("Keys")
if err != nil {
log.Printf("Cant read user Keys dir - %s / Keys", userRoot.GetKeyFullPath())
continue
}
defer keyRoot.Close()
containerRoots := keyRoot.SubKeys(
registry.QUERY_VALUE|registry.ENUMERATE_SUB_KEYS,
func(s string) bool {
log.Printf("Found Container %s ...\n", s)
return container == "" || container == s
},
func(s string, err error) {
log.Printf("Error open subkey for Container %s - %s...\n", s, err.Error())
},
)
defer func() {
for _, rk := range containerRoots {
rk.Close()
}
}()
prefixes := map[string]int{}
for _, containerRoot := range containerRoots {
log.Printf("Full container name - %s", containerRoot.GetKeyFullPath())
keyName := containerRoot.GetKeyName()
keys, err := containerRoot.ValueNames()
if err != nil {
log.Printf("No values in %s - %s\n", keyName, err.Error())
continue
}
folder := ""
for _, char := range []rune(keyName)[0:8] {
valByte, _ := charmap.Windows1251.EncodeRune(char)
// its a magic....
if valByte > 127 {
valByte = valByte - 20
valByte = valByte % 26
valByte = valByte + 97
}
if valByte < 35 {
valByte = valByte % 26
valByte = valByte + 97
}
valConverted := charmap.Windows1251.DecodeByte(valByte)
folder += string(valConverted)
}
log.Printf("Encoded Container name - %s \n", folder)
v := prefixes[folder]
folder = fmt.Sprintf("%s.%03d", folder, v)
v++
prefixes[folder] = v
folder = filepath.Join(outputFolder, folder)
os.MkdirAll(folder, 0755)
for _, name := range keys {
data, err := containerRoot.GetBinaryValue(name)
if err != nil {
log.Printf("Cant read %s from reg - %s\n", name, err.Error())
continue
}
filename := filepath.Join(folder, name)
file, err := os.Create(filename)
if err != nil {
log.Printf("Cant write %s file - %s\n", name, err.Error())
continue
}
defer file.Close()
log.Printf("Saving %s file\n", filename)
io.Copy(file, bytes.NewBuffer(data))
}
}
}
}