forked from RedTeamPentesting/adauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_test.go
337 lines (288 loc) · 7.42 KB
/
options_test.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
package adauth_test
import (
"context"
"encoding/hex"
"net"
"strconv"
"testing"
"github.com/RedTeamPentesting/adauth"
)
const (
testUser = "someuser"
testDomain = "domain.tld"
)
func TestUsernameAndDomainParsing(t *testing.T) {
expectedUPN := testUser + "@" + testDomain
expectedLogonName := testDomain + `\` + testUser
expectedImpacketLogonName := testDomain + "/" + testUser
testCases := []struct {
Name string
Opts adauth.Options
}{
{
Name: "UPN",
Opts: adauth.Options{User: testUser + "@" + testDomain},
},
{
Name: "logon-name",
Opts: adauth.Options{User: testDomain + `\` + testUser},
},
{
Name: "impacket-style",
Opts: adauth.Options{User: testDomain + `/` + testUser},
},
{
Name: "pfx",
Opts: adauth.Options{PFXFileName: "testdata/[email protected]"},
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Name, func(t *testing.T) {
creds, err := testCase.Opts.NoTarget()
if err != nil {
t.Fatalf("get credentials: %v", err)
}
if creds.Username != testUser {
t.Errorf("username %q is not %q", creds.Username, testUser)
}
if creds.Domain != testDomain {
t.Errorf("domain %q is not %q", creds.Domain, testDomain)
}
upn := creds.UPN()
if upn != expectedUPN {
t.Errorf("UPN %q is not %q", upn, expectedUPN)
}
logonName := creds.LogonName()
if logonName != expectedLogonName {
t.Errorf("logon name %q is not %q", logonName, expectedLogonName)
}
impacketLogonName := creds.ImpacketLogonName()
if impacketLogonName != expectedImpacketLogonName {
t.Errorf("impacket-style logon name %q is not %q", impacketLogonName, expectedImpacketLogonName)
}
})
}
}
func TestDomainFromTargetHostname(t *testing.T) {
opts := adauth.Options{
User: testUser,
}
creds, _, err := opts.WithTarget(context.Background(), "cifs", "host."+testDomain)
if err != nil {
t.Fatalf("get credentials: %v", err)
}
if creds.Domain != testDomain {
t.Errorf("domain is %q instead of %q", creds.Domain, testDomain)
}
// ignore domain in hostname for local authentication
opts = adauth.Options{
User: "./" + testUser,
}
creds, _, err = opts.WithTarget(context.Background(), "cifs", "host."+testDomain)
if err != nil {
t.Fatalf("get credentials: %v", err)
}
if creds.Domain != "." {
t.Errorf("domain is %q instead of %q", creds.Domain, ".")
}
}
func TestDCTarget(t *testing.T) {
dcHost := "dc." + testDomain
expectedSPN := "ldap/" + dcHost
opts := adauth.Options{
User: testUser + `@` + testDomain,
Resolver: &testResolver{
SRV: map[string]map[string]map[string]struct {
Name string
SRV []*net.SRV
}{
"ldap": {
"tcp": {
testDomain: {
Name: dcHost,
SRV: []*net.SRV{
{Target: dcHost, Port: 389},
},
},
},
},
},
},
}
_, dc, err := opts.WithDCTarget(context.Background(), "ldap")
if err != nil {
t.Fatalf("get DC target: %v", err)
}
if dc.Address() != net.JoinHostPort(dcHost, "389") {
t.Fatalf("DC address is %q instead of %q", dc.Address(), net.JoinHostPort(dcHost, "389"))
}
if dc.AddressWithoutPort() != dcHost {
t.Fatalf("DC address without port is %q instead of %q", dc.Address(), dcHost)
}
if dc.Port != "389" {
t.Errorf("DC port is %q instead of %q", dc.Port, "389")
}
spn, err := dc.SPN(context.Background())
if err != nil {
t.Fatalf("get DC SPN: %v", err)
}
if spn != expectedSPN {
t.Errorf("DC SPN is %q instead of %q", spn, expectedSPN)
}
}
func TestDCTargetWithoutSRVRecord(t *testing.T) {
dcHost := "dc." + testDomain
dcIP := net.ParseIP("10.0.0.1")
expectedSPN := "ldap/" + dcHost
opts := adauth.Options{
User: testUser + `@` + testDomain,
Resolver: &testResolver{
HostToAddr: map[string][]net.IP{
testDomain: {dcIP},
},
AddrToHost: map[string][]string{
dcIP.String(): {dcHost},
},
},
}
_, dc, err := opts.WithDCTarget(context.Background(), "ldap")
if err != nil {
t.Fatalf("get DC target: %v", err)
}
if dc.Address() != net.JoinHostPort(dcHost, "389") {
t.Fatalf("DC address is %q instead of %q", dc.Address(), net.JoinHostPort(dcHost, "389"))
}
if dc.AddressWithoutPort() != dcHost {
t.Fatalf("DC address without port is %q instead of %q", dc.Address(), dcHost)
}
if dc.Port != "389" {
t.Errorf("DC port is %q instead of %q", dc.Port, "389")
}
spn, err := dc.SPN(context.Background())
if err != nil {
t.Fatalf("get DC SPN: %v", err)
}
if spn != expectedSPN {
t.Errorf("DC SPN is %q instead of %q", spn, expectedSPN)
}
}
func TestDCTargetNoReverseLookup(t *testing.T) {
dcIP := net.ParseIP("10.0.0.1")
opts := adauth.Options{
User: testUser + `@` + testDomain,
Resolver: &testResolver{
HostToAddr: map[string][]net.IP{
testDomain: {dcIP},
},
},
}
_, dc, err := opts.WithDCTarget(context.Background(), "ldap")
if err != nil {
t.Fatalf("get DC target: %v", err)
}
if dc.Address() != net.JoinHostPort(dcIP.String(), "389") {
t.Fatalf("DC address is %q instead of %q", dc.Address(), net.JoinHostPort(dcIP.String(), "389"))
}
}
func TestKerberos(t *testing.T) {
upn := testUser + `@` + testDomain
testCases := []struct {
Opts adauth.Options
ShouldUseKerberos bool
}{
{
Opts: adauth.Options{
User: upn,
Password: "pass",
},
ShouldUseKerberos: false,
},
{
Opts: adauth.Options{
User: upn,
AESKey: hex.EncodeToString(make([]byte, 16)),
},
ShouldUseKerberos: true,
},
{
Opts: adauth.Options{
User: upn,
Password: "test",
AESKey: hex.EncodeToString(make([]byte, 16)),
},
ShouldUseKerberos: false,
},
{
Opts: adauth.Options{
User: upn,
Password: "pass",
ForceKerberos: true,
},
ShouldUseKerberos: true,
},
{
Opts: adauth.Options{
User: upn,
PFXFileName: "testdata/[email protected]",
},
ShouldUseKerberos: false,
},
{
Opts: adauth.Options{
User: upn,
// CCache indicates kerberos usage, but only if file is present
// (even though it might be empty)
CCache: "testdata/empty.ccache",
},
ShouldUseKerberos: true,
},
{
Opts: adauth.Options{
User: upn,
// CCache does not matter if file is not even there
CCache: "testdata/doesnotexist",
},
ShouldUseKerberos: false,
},
}
for i, testCase := range testCases {
testCase := testCase
t.Run(strconv.Itoa(i), func(t *testing.T) {
testCase.Opts.DomainController = "dc.domain.tld"
_, target, err := testCase.Opts.WithDCTarget(context.Background(), "ldap")
if err != nil {
t.Fatalf("get target: %v", err)
}
switch {
case testCase.ShouldUseKerberos && !target.UseKerberos:
t.Errorf("target would not use Kerberos even though it should: %#v", testCase.Opts)
case !testCase.ShouldUseKerberos && target.UseKerberos:
t.Errorf("target would use Kerberos even though it should not: %#v", testCase.Opts)
}
})
}
}
func TestCleanNTHash(t *testing.T) {
ntHash := "31d6cfe0d16ae931b73c59d7e0c089c0"
testCases := []string{
ntHash,
":" + ntHash,
"aad3b435b51404eeaad3b435b51404ee:" + ntHash,
}
for i, testCase := range testCases {
testCase := testCase
t.Run(strconv.Itoa(i), func(t *testing.T) {
creds, err := (&adauth.Options{
User: testUser + "@" + testDomain,
NTHash: testCase,
}).NoTarget()
if err != nil {
t.Fatalf("get credentials: %v", err)
}
if creds.NTHash != ntHash {
t.Errorf("NT hash is %q instead of %q", creds.NTHash, ntHash)
}
})
}
}