forked from RedTeamPentesting/adauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredentials.go
348 lines (298 loc) · 9.98 KB
/
credentials.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
338
339
340
341
342
343
344
345
346
347
348
package adauth
import (
"context"
"crypto/rsa"
"crypto/x509"
"encoding/hex"
"fmt"
"net"
"os"
"strings"
"time"
"github.com/RedTeamPentesting/adauth/othername"
"github.com/jcmturner/gokrb5/v8/config"
"github.com/jcmturner/gokrb5/v8/credentials"
"github.com/jcmturner/gokrb5/v8/iana/etypeID"
"github.com/jcmturner/gokrb5/v8/keytab"
"software.sslmate.com/src/go-pkcs12"
)
// Credential represents Active Directory credentials.
type Credential struct {
// Username is the username without the domain.
Username string
// Password contains the users cleartext password if available.
Password string
// Domain holds the user's domain.
Domain string
// NTHash holds the user's NT hash or Kerberos RC4 key if available.
NTHash string
// AESKey holds the user's Kerberos AES128 or AES256 key if available.
AESKey string
// CCache contains the path to the user's CCache file.
CCache string
// ClientCert holds a client certificate for Kerberos or LDAP authentication if available.
ClientCert *x509.Certificate
// ClientCertKey holds the private key that corresponds to ClientCert.
ClientCertKey *rsa.PrivateKey
// CACerts holds CA certificates that were loaded alongside the ClientCert.
CACerts []*x509.Certificate
dc string
// PasswordIsEmptyString is true when an empty Password field should not be
// interpreted as a missing password but as a password that happens to be
// empty.
PasswordIsEmtpyString bool
// CCacheIsFromEnv indicates whether the CCache was set explicitly or
// implicitly through an environment variable.
CCacheIsFromEnv bool
// Resolver can be used to set an alternative DNS resolver. If empty,
// net.DefaultResolver is used.
Resolver Resolver
}
// CredentialFromPFX creates a Credential structure for certificate-based
// authentication based on a PFX file.
func CredentialFromPFX(
username string, domain string, pfxFile string, pfxPassword string,
) (*Credential, error) {
pfxData, err := os.ReadFile(pfxFile)
if err != nil {
return nil, fmt.Errorf("read PFX: %w", err)
}
return CredentialFromPFXBytes(username, domain, pfxData, pfxPassword)
}
// CredentialFromPFX creates a Credential structure for certificate-based
// authentication based on PFX data.
func CredentialFromPFXBytes(
username string, domain string, pfxData []byte, pfxPassword string,
) (*Credential, error) {
cred := &Credential{
Username: username,
Domain: domain,
}
key, cert, caCerts, err := pkcs12.DecodeChain(pfxData, pfxPassword)
if err != nil {
return nil, fmt.Errorf("decode PFX: %w", err)
}
rsaKey, ok := key.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("PFX key is not an RSA private key but %T", rsaKey)
}
cred.ClientCert = cert
cred.ClientCertKey = rsaKey
cred.CACerts = caCerts
user, domain, err := othername.UserAndDomain(cert)
if err == nil {
if cred.Username == "" {
cred.Username = user
}
if cred.Domain == "" {
cred.Domain = domain
}
}
return cred, nil
}
// UPN is the user principal name (username@domain).
func (c *Credential) UPN() string {
return c.Username + "@" + c.Domain
}
// LogonName is the legacy logon name (domain\username).
func (c *Credential) LogonName() string {
return c.Domain + `\` + c.Username
}
// LogonNameWithUpperCaseDomain is like LogonName with the domain capitalized
// for compatibility with the Kerberos library (DOMAIN\username).
func (c *Credential) LogonNameWithUpperCaseDomain() string {
return strings.ToUpper(c.Domain) + `\` + c.Username
}
// ImpacketLogonName is the Impacket-style logon name (domain/username).
func (c *Credential) ImpacketLogonName() string {
return c.Domain + "/" + c.Username
}
// SetDC configures a specific domain controller for this credential.
func (c *Credential) SetDC(dc string) {
c.dc = dc
}
// DC returns the domain controller of the credential's domain as a target.
func (c *Credential) DC(ctx context.Context, protocol string) (*Target, error) {
if c.dc != "" {
return newTarget(protocol, c.dc, true, c.CCache, c.Resolver), nil
}
if c.Domain == "" {
return nil, fmt.Errorf("domain unknown")
}
_, addrs, err := ensureResolver(c.Resolver, nil).LookupSRV(ctx, "kerberos", "tcp", c.Domain)
if err != nil {
return nil, fmt.Errorf("lookup %q service of domain %q: %w", "kerberos", c.Domain, err)
}
if len(addrs) == 0 {
return nil, fmt.Errorf("no %q services were discovered for domain %q", "kerberos", c.Domain)
}
return newTarget(protocol, strings.TrimRight(addrs[0].Target, "."), true, c.CCache, c.Resolver), nil
}
func (c *Credential) mustUseKerberos() bool {
return c.Password == "" && c.NTHash == "" && (c.CCache != "" || c.AESKey != "")
}
// Keytab returns the Kerberos keytab containing the AES key and/or NT hash if
// they were supplied. If a password is supplied, the keys/hashes are not
// derived and the keytab will be empty.
func (c *Credential) Keytab() (*keytab.Keytab, error) {
kt := newKeytab()
if c.AESKey != "" {
err := addKeyToKeytab(kt, c.Username, c.Domain, c.AESKey, true, 1)
if err != nil {
return nil, fmt.Errorf("add AES key: %w", err)
}
}
if c.NTHash != "" {
err := addKeyToKeytab(kt, c.Username, c.Domain, c.NTHash, false, 1)
if err != nil {
return nil, fmt.Errorf("add RC4 key: %w", err)
}
}
return kt, nil
}
// KerberosConfig returns the Kerberos configuration for the credential's domain.
func (c *Credential) KerberosConfig(ctx context.Context) (*config.Config, error) {
dc, err := c.DC(ctx, "krbtgt")
if err != nil {
return nil, fmt.Errorf("find DC: %w", err)
}
krbConf := config.New()
krbConf.LibDefaults.DefaultRealm = strings.ToUpper(c.Domain)
krbConf.LibDefaults.AllowWeakCrypto = true
krbConf.LibDefaults.DNSLookupRealm = false
krbConf.LibDefaults.DNSLookupKDC = false
krbConf.LibDefaults.TicketLifetime = time.Duration(24) * time.Hour
krbConf.LibDefaults.RenewLifetime = time.Duration(24*7) * time.Hour
krbConf.LibDefaults.Forwardable = true
krbConf.LibDefaults.Proxiable = true
krbConf.LibDefaults.RDNS = false
krbConf.LibDefaults.UDPPreferenceLimit = 1 // Force use of tcp
if c.NTHash != "" {
// use RC4 for pre-auth but AES256 for ephemeral keys, otherwise we get
// unsupported GSSAPI tokens during LDAP SASL handshake
krbConf.LibDefaults.DefaultTGSEnctypeIDs = []int32{etypeID.AES256_CTS_HMAC_SHA1_96}
krbConf.LibDefaults.DefaultTktEnctypeIDs = []int32{etypeID.RC4_HMAC}
krbConf.LibDefaults.PermittedEnctypeIDs = []int32{etypeID.AES256_CTS_HMAC_SHA1_96}
krbConf.LibDefaults.PreferredPreauthTypes = []int{int(etypeID.RC4_HMAC)}
} else {
krbConf.LibDefaults.DefaultTGSEnctypeIDs = []int32{etypeID.AES256_CTS_HMAC_SHA1_96}
krbConf.LibDefaults.DefaultTktEnctypeIDs = []int32{
etypeID.AES256_CTS_HMAC_SHA1_96, etypeID.AES128_CTS_HMAC_SHA1_96, etypeID.RC4_HMAC,
}
krbConf.LibDefaults.PermittedEnctypeIDs = []int32{etypeID.AES256_CTS_HMAC_SHA1_96}
krbConf.LibDefaults.PreferredPreauthTypes = []int{
int(etypeID.AES256_CTS_HMAC_SHA1_96), int(etypeID.AES128_CTS_HMAC_SHA1_96), int(etypeID.RC4_HMAC),
}
}
krbConf.Realms = []config.Realm{
{
Realm: strings.ToUpper(c.Domain),
DefaultDomain: strings.ToUpper(c.Domain),
AdminServer: []string{dc.AddressWithoutPort()},
KDC: []string{net.JoinHostPort(dc.AddressWithoutPort(), "88")},
KPasswdServer: []string{net.JoinHostPort(dc.AddressWithoutPort(), "464")},
MasterKDC: []string{dc.AddressWithoutPort()},
},
{
Realm: c.Domain,
DefaultDomain: c.Domain,
AdminServer: []string{dc.AddressWithoutPort()},
KDC: []string{net.JoinHostPort(dc.AddressWithoutPort(), "88")},
KPasswdServer: []string{net.JoinHostPort(dc.AddressWithoutPort(), "464")},
MasterKDC: []string{dc.AddressWithoutPort()},
},
}
krbConf.DomainRealm = map[string]string{
"." + c.Domain: strings.ToUpper(c.Domain),
c.Domain: strings.ToUpper(c.Domain),
}
return krbConf, nil
}
func splitUserIntoDomainAndUsername(user string) (domain string, username string) {
switch {
case strings.Contains(user, "@"):
parts := strings.Split(user, "@")
if len(parts) == 2 {
return parts[1], parts[0]
}
return "", user
case strings.Contains(user, `\`):
parts := strings.Split(user, `\`)
if len(parts) == 2 {
return parts[0], parts[1]
}
return "", user
case strings.Contains(user, "/"):
parts := strings.Split(user, "/")
if len(parts) == 2 {
return parts[0], parts[1]
}
return "", user
default:
return "", user
}
}
func newKeytab() *keytab.Keytab {
kt := &keytab.Keytab{}
err := kt.Unmarshal([]byte{
// header.
0x05, // first-byte
0x02, // version
0x00, 0x00, 0x00, 0x00, // entry-length
})
if err != nil {
panic(err.Error())
}
return kt
}
func addKeyToKeytab(kt *keytab.Keytab, username string, domain string, key string, aes bool, kvno uint32) error {
keyBytes, err := hex.DecodeString(key)
if err != nil {
return fmt.Errorf("decode AES key: %w", err)
}
var keyType int32
switch len(keyBytes) {
case 32:
keyType = etypeID.AES256_CTS_HMAC_SHA1_96
case 16:
if aes {
keyType = etypeID.AES128_CTS_HMAC_SHA1_96
} else {
keyType = etypeID.RC4_HMAC
}
default:
return fmt.Errorf("invalid AES128/AES256 key")
}
tmp := &keytab.Keytab{}
err = tmp.Unmarshal([]byte{
// header
0x05, // first-byte
0x02, // version
0x00, 0x00, 0x00, 0x11, // entry-length
// principal
0x00, 0x00, // num components
0x00, 0x00, // realm length
0x00, 0x00, 0x00, 0x00, // name type
// key
0x00, 0x00, 0x00, 0x00, // timestamp
0x00, // kvno8
0x00, 0x00, // key type
0x00, 0x00, // key length
})
if err != nil {
return fmt.Errorf("invalid dummy data: %w", err)
}
e := tmp.Entries[0]
krbCreds := credentials.New(username, domain)
e.Principal.NumComponents = int16(len(krbCreds.CName().NameString))
e.Principal.Components = krbCreds.CName().NameString
e.Principal.Realm = strings.ToUpper(krbCreds.Realm())
e.Principal.NameType = krbCreds.CName().NameType
e.Timestamp = time.Now()
e.KVNO8 = 0
e.Key.KeyType = keyType
e.Key.KeyValue = keyBytes
e.KVNO = kvno
kt.Entries = append(kt.Entries, e)
return nil
}