Refactor AES key handling

This commit is contained in:
Erik Geiser
2025-05-05 13:34:31 +02:00
parent 3a9cc94321
commit eb7dfe0b78
2 changed files with 16 additions and 22 deletions
-19
View File
@@ -349,22 +349,3 @@ func addKeyToKeytab(kt *keytab.Keytab, username string, domain string, key strin
return nil
}
// ParseAESKey decodes the supplied hex Kerberos AES key and determines the key type.
func ParseAESKey(key string) (keyBytes []byte, keyType int32, err error) {
keyBytes, err = hex.DecodeString(key)
if err != nil {
return nil, 0, fmt.Errorf("decode hex key: %w", err)
}
switch len(keyBytes) {
case 32:
keyType = etypeID.AES256_CTS_HMAC_SHA1_96
case 16:
keyType = etypeID.AES128_CTS_HMAC_SHA1_96
default:
return nil, 0, fmt.Errorf("invalid AES128/AES256 key")
}
return
}
+16 -3
View File
@@ -2,12 +2,14 @@ package dcerpcauth
import (
"context"
"encoding/hex"
"fmt"
"net"
"strings"
"github.com/RedTeamPentesting/adauth"
"github.com/RedTeamPentesting/adauth/pkinit"
"github.com/oiweiwei/gokrb5.fork/v9/iana/etypeID"
"github.com/oiweiwei/go-msrpc/dcerpc"
"github.com/oiweiwei/go-msrpc/smb2"
@@ -131,12 +133,23 @@ func DCERPCCredentials(ctx context.Context, creds *adauth.Credential, options *O
case creds.AESKey != "":
options.debug("Authenticating with AES key")
key, keyType, err := adauth.ParseAESKey(creds.AESKey)
keyBytes, err := hex.DecodeString(creds.AESKey)
if err != nil {
return nil, fmt.Errorf("parse AES key: %w", err)
return nil, fmt.Errorf("decode hex key: %w", err)
}
return credential.NewFromEncryptionKeyBytes(creds.LogonNameWithUpperCaseDomain(), int(keyType), key), nil
var keyType int
switch len(keyBytes) {
case 32:
keyType = int(etypeID.AES256_CTS_HMAC_SHA1_96)
case 16:
keyType = int(etypeID.AES128_CTS_HMAC_SHA1_96)
default:
return nil, fmt.Errorf("invalid AES128/AES256 key: key size is %d bytes", len(keyBytes))
}
return credential.NewFromEncryptionKeyBytes(creds.LogonNameWithUpperCaseDomain(), keyType, keyBytes), nil
case creds.NTHash != "":
options.debug("Authenticating with NT hash")