forked from RedTeamPentesting/adauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredentials_test.go
193 lines (171 loc) · 5.33 KB
/
credentials_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
package adauth_test
import (
"context"
"encoding/hex"
"net"
"strconv"
"strings"
"testing"
"github.com/RedTeamPentesting/adauth"
"github.com/jcmturner/gokrb5/v8/iana/etypeID"
"github.com/jcmturner/gokrb5/v8/iana/nametype"
"github.com/jcmturner/gokrb5/v8/types"
)
func TestKeytab(t *testing.T) {
expectedNTHash := hex.EncodeToString(make([]byte, 16))
expectedAES256Key := hex.EncodeToString(make([]byte, 32))
expectedAES128Key := hex.EncodeToString(make([]byte, 16))
principal := types.NewPrincipalName(nametype.KRB_NT_PRINCIPAL, testUser)
testCases := []struct {
Cred adauth.Credential
ShouldHaveRC4Key bool
ShouldHaveAES128Key bool
ShouldHaveAES256Key bool
}{
{
Cred: adauth.Credential{},
ShouldHaveRC4Key: false,
ShouldHaveAES128Key: false,
ShouldHaveAES256Key: false,
},
{
Cred: adauth.Credential{
NTHash: expectedNTHash,
},
ShouldHaveRC4Key: true,
ShouldHaveAES128Key: false,
ShouldHaveAES256Key: false,
},
{
Cred: adauth.Credential{
NTHash: expectedNTHash,
AESKey: expectedAES128Key,
},
ShouldHaveRC4Key: true,
ShouldHaveAES128Key: true,
ShouldHaveAES256Key: false,
},
{
Cred: adauth.Credential{
NTHash: expectedNTHash,
AESKey: expectedAES256Key,
},
ShouldHaveRC4Key: true,
ShouldHaveAES128Key: false,
ShouldHaveAES256Key: true,
},
{
Cred: adauth.Credential{
AESKey: expectedAES128Key,
},
ShouldHaveRC4Key: false,
ShouldHaveAES128Key: true,
ShouldHaveAES256Key: false,
},
{
Cred: adauth.Credential{
AESKey: expectedAES256Key,
},
ShouldHaveRC4Key: false,
ShouldHaveAES128Key: false,
ShouldHaveAES256Key: true,
},
}
for i, testCase := range testCases {
testCase := testCase
t.Run(strconv.Itoa(i), func(t *testing.T) {
testCase.Cred.Username = testUser
testCase.Cred.Domain = testDomain
keyTab, err := testCase.Cred.Keytab()
if err != nil {
t.Fatalf("create keytab: %v", err)
}
rc4Key, _, rc4Err := keyTab.GetEncryptionKey(principal, strings.ToUpper(testDomain), 0, etypeID.RC4_HMAC)
switch {
case testCase.ShouldHaveRC4Key && rc4Err != nil:
t.Errorf("expected RC4 key but got error: %v", rc4Err)
case testCase.ShouldHaveRC4Key && len(rc4Key.KeyValue) != 16:
t.Errorf("RC4 key has %d bytes instead of %d", len(rc4Key.KeyValue), 16)
case testCase.ShouldHaveRC4Key && rc4Key.KeyType != etypeID.RC4_HMAC:
t.Errorf("RC4 key type is %d instead of %d", rc4Key.KeyType, etypeID.RC4_HMAC)
case !testCase.ShouldHaveRC4Key && (rc4Err == nil || len(rc4Key.KeyValue) > 0):
t.Errorf("RC4 key should not exist")
}
aes128Key, _, aes128Err := keyTab.GetEncryptionKey(
principal, strings.ToUpper(testDomain), 0, etypeID.AES128_CTS_HMAC_SHA1_96)
switch {
case testCase.ShouldHaveAES128Key && aes128Err != nil:
t.Errorf("expected AES128 key but got error: %v:\n%#v\n", aes128Err, keyTab.Entries)
case testCase.ShouldHaveAES128Key && len(aes128Key.KeyValue) != 16:
t.Errorf("AES128 key has %d bytes instead of %d", len(aes128Key.KeyValue), 16)
case testCase.ShouldHaveAES128Key && aes128Key.KeyType != etypeID.AES128_CTS_HMAC_SHA1_96:
t.Errorf("AES128 key type is %d instead of %d", aes128Key.KeyType, etypeID.AES128_CTS_HMAC_SHA1_96)
case !testCase.ShouldHaveAES128Key && (aes128Err == nil || len(aes128Key.KeyValue) > 0):
t.Errorf("AES128 key should not exist")
}
aes256Key, _, aes256Err := keyTab.GetEncryptionKey(
principal, strings.ToUpper(testDomain), 0, etypeID.AES256_CTS_HMAC_SHA1_96)
switch {
case testCase.ShouldHaveAES256Key && aes256Err != nil:
t.Errorf("expected AES256 key but got error: %v", aes256Err)
case testCase.ShouldHaveAES256Key && len(aes256Key.KeyValue) != 32:
t.Errorf("AES256 key has %d bytes instead of %d", len(aes256Key.KeyValue), 32)
case testCase.ShouldHaveAES256Key && aes256Key.KeyType != etypeID.AES256_CTS_HMAC_SHA1_96:
t.Errorf("AES256 key type is %d instead of %d", aes256Key.KeyType, etypeID.AES256_CTS_HMAC_SHA1_96)
case !testCase.ShouldHaveAES256Key && (aes256Err == nil || len(aes256Key.KeyValue) > 0):
t.Errorf("AES256 key should not exist")
}
})
}
}
func TestSetDC(t *testing.T) {
creds := adauth.Credential{
Username: testUser,
Domain: testDomain,
Resolver: &testResolver{},
}
_, err := creds.DC(context.Background(), "host")
if err == nil {
t.Fatalf("expected creds.DC() to fail initially")
}
dcHostname := "dc." + testDomain
creds.SetDC(dcHostname)
dc, err := creds.DC(context.Background(), "host")
if err != nil {
t.Fatalf("get DC: %v", err)
}
if dc.Address() != dcHostname {
t.Fatalf("DC address is %q instead of %q", dc.Address(), dcHostname)
}
}
func TestLookupDC(t *testing.T) {
dcHostname := "dc." + testDomain
creds := adauth.Credential{
Username: testUser,
Domain: testDomain,
Resolver: &testResolver{
SRV: map[string]map[string]map[string]struct {
Name string
SRV []*net.SRV
}{
"kerberos": {
"tcp": {
testDomain: {
Name: dcHostname,
SRV: []*net.SRV{
{Target: dcHostname, Port: 88},
},
},
},
},
},
},
}
dc, err := creds.DC(context.Background(), "host")
if err != nil {
t.Fatalf("get DC: %v", err)
}
if dc.AddressWithoutPort() != dcHostname {
t.Fatalf("DC address is %q instead of %q", dc.Address(), dcHostname)
}
}