Security Stuff
  • About
  • HackTheBox
    • Windows
      • Forest
      • Escape
      • Timelapse
      • Support
    • Linux
      • TwoMillion
      • Soccer
      • Pollution
      • Pilgrimage
      • Sandworm
  • Windows
    • Authentication
      • Overview
      • Logon
      • Kerberos
      • Credential
    • Active Directory
      • Domain Service
        • LDAP
        • AD Objects
      • Key Distribution Center
      • Certificate Service
    • Windows Protocols
      • SMB
    • Windows Server
      • MSSQL
    • Execution
      • Windows APIs
      • Remote Access
        • WinRM
    • Credential Access
      • Kerberos Ticket
        • Kerberoasting Attack
        • Golden/Silver Ticket Attack
        • AS-REP Roasting Attack
      • OS Credential Dumping
        • DCsync Attack
      • Certified Pre-Owned
  • Linux
    • Management
      • Package
    • Process
      • Namespace
      • Terminal
  • Web
    • Authentication
      • SAML
      • OAuth
    • Enumeration
  • Defense
    • Windows
      • Windows Event Logs
  • Development
    • Programming Language
    • Database
      • MySQL
    • Virtualization
      • Container
    • Cryptography
      • GnuPG
Powered by GitBook
On this page
  • Introduction
  • Kerberos Tickets
  • Ticket-Granting Ticket
  • GSS API
  • MS-KILE Extension
  • Realm
  • Authentication Service
  • Format
  • AS Exchange
  • TGS Exchange
  • AP Exchange
  • Service Principal Name (SPN)
  • Format
  • Registration
  • Privilege Attribute Certificate (PAC)
  • Keytab File
  • Creation
  1. Windows
  2. Authentication

Kerberos

PreviousLogonNextCredential

Last updated 1 year ago

Introduction

The Kerberos protocol defines how clients interact with a network authentication service.

On the other hand, authorization is accomplished using data.

Kerberos Tickets

Clients obtain tickets from the , which uses Active Directory as its , and they present these tickets to servers, as network credentials.

Ticket-Granting Ticket

Originally, The client used his master key which is derived from the user password to decrypt session keys received from KDC.

To avoid users to enter their passwords frequently, the protocol introduces the usage of the ticket.

When a user logs on, the client for the KDC just as it would request a ticket for any other service. The KDC responds by creating a logon session key and a ticket for the KDC's full .

GSS API

MS-KILE Extension

Microsoft extend the Keberos authorization data to provide the server with additional information such as:

  • Group membership

  • Claims

  • Interactive logon information

  • Integrity levels

Realm

A collection of key distribution centers (KDCs) with a common set of principals, as described in [RFC4120] section 1.2.

An administrative boundary that uses one set of authentication servers to manage and deploy a single set of unique identifiers.

A realm is a unique logon space.

Authentication Service

Kerberos V5 is composed of three exchanges:

  • The Authentication Service (AS) exchange

  • The Ticket-Granting Service (TGS) exchange

  • The Client/Server Authentication Protocol (AP) exchange

The AS exchange and TGS exchange are transported by Kerberos implementations. The AP exchange is passive and relies on an upper-layer application protocol to carry the AP exchange messages.

Format

Also, we can consume the code in Impakct packages to understand the protocol:

from pyasn1.type import tag, namedtype, univ, constraint, char, useful
...
class KDC_REQ(univ.Sequence):
    componentType = namedtype.NamedTypes(
        _vno_component(1),
        _msg_type_component(2, (constants.ApplicationTagNumbers.AS_REQ.value,
                                constants.ApplicationTagNumbers.TGS_REQ.value)),
        _sequence_optional_component('padata', 3,
                                     univ.SequenceOf(componentType=PA_DATA())),
        _sequence_component('req-body', 4, KDC_REQ_BODY())
        )

class AS_REQ(KDC_REQ):
    tagSet = _application_tag(constants.ApplicationTagNumbers.AS_REQ.value)

class TGS_REQ(KDC_REQ):
    tagSet = _application_tag(constants.ApplicationTagNumbers.TGS_REQ.value)

AS Exchange

Pre-authentication

    # This should be the PREAUTH_FAILED packet or the actual TGT if the target principal has the
    # 'Do not require Kerberos preauthentication' set
    preAuth = True
    try:
        asRep = decoder.decode(r, asn1Spec = KRB_ERROR())[0]
    except:
        # Most of the times we shouldn't be here, is this a TGT?
        asRep = decoder.decode(r, asn1Spec=AS_REP())[0]
        # Yes
        preAuth = False
        # Let's build the timestamp
        timeStamp = PA_ENC_TS_ENC()

        now = datetime.datetime.utcnow()
        timeStamp['patimestamp'] = KerberosTime.to_asn1(now)
        timeStamp['pausec'] = now.microsecond

        # Encrypt the shyte
        encodedTimeStamp = encoder.encode(timeStamp)

        # Key Usage 1
        # AS-REQ PA-ENC-TIMESTAMP padata timestamp, encrypted with the
        # client key (Section 5.2.7.2)
        encriptedTimeStamp = cipher.encrypt(key, 1, encodedTimeStamp, None)

The KDC returns a TGT and a session key the client can use to encrypt and authenticate communication with the KDC for ticket-granting service (TGS) requests in the TGS exchange, without reusing the persistent key.

Ticket-Granting Ticket

Session Key

    # So, we have the TGT, now extract the new session key and finish
    cipherText = asRep['enc-part']['cipher']
...
    # Key Usage 3
    # AS-REP encrypted part (includes TGS session key or
    # application session key), encrypted with the client key
    # (Section 5.4.2)
    try:
        plainText = cipher.decrypt(key, 3, cipherText)
    except InvalidChecksum as e:
        # probably bad password if preauth is disabled
        if preAuth is False:
            error_msg = "failed to decrypt session key: %s" % str(e)
            raise SessionKeyDecryptionError(error_msg, asRep, cipher, key, cipherText)
        raise
    encASRepPart = decoder.decode(plainText, asn1Spec = EncASRepPart())[0]

    # Get the session key and the ticket
    cipher = _enctype_table[encASRepPart['key']['keytype']]
    sessionKey = Key(cipher.enctype,encASRepPart['key']['keyvalue'].asOctets())

TGS Exchange

Kerberos Authenticator

A timestamp encrypted with the TGS session key derived in the KRB_AS_REP used to demonstrate the knowledge of the session key in the accompanying ticket.

    # Key Usage 7
    # TGS-REQ PA-TGS-REQ padata AP-REQ Authenticator (includes
    # TGS authenticator subkey), encrypted with the TGS session
    # key (Section 5.5.1)
    encryptedEncodedAuthenticator = cipher.encrypt(sessionKey, 7, encodedAuthenticator, None)
...
    apReq['authenticator']['cipher'] = encryptedEncodedAuthenticator
    encodedApReq = encoder.encode(apReq)
...
    tgsReq['padata'][0]['padata-type'] = int(constants.PreAuthenticationDataTypes.PA_TGS_REQ.value)
    tgsReq['padata'][0]['padata-value'] = encodedApReq

Ticket-Granting Ticket

Session Key

As in the KRB_AS_REP message, a new session key is contained in the encryption part, encrypted by the TGS session key this time, of the KRB_TGS_REP message.

AP Exchange

Optionally, the client might request that the server verify its own identity. If mutual authentication is requested, the server returns the client's timestamp from the authenticator encrypted with the session key.

Service Principal Name (SPN)

A unique identifier associates a service instance with a service sign-in account.

Format

An SPN must be unique in a forest in which it is registered.

<service class>/<host>:<port>/<service name>

Common service classes can be found here:

Registration

Typically, SPN registration is done by a service installation program running with domain administrator privileges.

Privilege Attribute Certificate (PAC)

The Privilege Attribute Certificate (PAC) was created to provide the authorization data, which the Kerberos protocol doesn't provide, for Kerberos Protocol Extensions [MS-KILE].

Into the PAC structure [MS-KILE] encodes authorization information, which consists of group memberships, additional credential information, profile, and policy information, and supporting security metadata.

The Kerberos protocol allows a field within the Kerberos ticket to carry authorization information, and Windows uses that field to carry information about Windows groups.

Keytab File

Creation

We can use the command ktpass on windows to

  • configure the server principal name for the host or service in Active Directory Domain Services (AD DS) and

  • generate a .keytab file that contains the shared secret key of the service.

C:\> ktpass /princ host/User1.contoso.com@CONTOSO.COM /mapuser User1 /pass MyPas$w0rd /out machine.keytab /crypto all /ptype KRB5_NT_PRINCIPAL /mapop set

On Linux, we can use command kutil.

>ktutil
ktutil: addent -password -p username/domain.com@DOMAIN.COM -k <kvno> -e rc4-hmac
ktutil: wkt ./keytab.file
ktutil: quit

Applications that use AP exchange messages directly are typically called "kerberized" applications. Most applications use the (GSS-API) and can even be wrapped by higher-level abstractions such as Simple Authentication and Security Layer (SASL) .

When an application wants to use Kerberos-based authentication, it uses either the higher-level API to invoke Kerberos directly; or it uses SPNEGO , which in turn invokes Kerberos.

The ticket formats are defined in .

The exchange message format is defined in using the ASN.1.

Both KRB_TGS_REQ and KRB_AS_REQ have a common structure as the message.

On the other hand, the message format is used for the reply from the KDC for either an initial (AS) request or a subsequent (TGS) request.

The client presents its principal name and present pre-authentication information ([RFC4120] sections and ) in the KRB_AS_REQ message to request a ticket-granting ticket (TGT) from the KDC ([RFC4120] section 5.3).

By , we can see that the first KRB_AS_REQ message contains PA-PAC-REQUEST as the padata, then a second KRB_AS_REQ is sent with pa-enc-timestamp padata if the Kerberos client receives an error message when pre-authentication is required.

Refer to in the Kerberos implementation of function getKerberosTGT in Impacket, we see the Kerberos client is expecting a Kerberos error message:

Then it using the client's key to prepare the padata to be sent in the second KRB_AS_REQ to demonstrate its knowledge of the user's key.

The ticket, with the logon session key embedded in it, is .

The logon session key is contained in , encrypted with the user's master key derived from the user's logon password, in the message.

Refer to in Impacket, we can see that the Kerberos client can decrypt the encrypted part to get the logon session key for further usage in :

The client , a , and the in the request sent to the KDC for a service ticket for the server.

The authenticator is embedded in a KRB_AP_REQ carried by the KRB_TGS_REQ message as a PA_TGS_REQ padata field, refer to the .

Implementation in the function of the Impacket package:

Contained in the padata of an encoded AP-REQ request body as seen in the .

Implementation in the function of the Impacket package:

The KDC validates the TGT ([RFC4120] section 5.3) and the ([RFC4120] section 5.5.1). If these are valid, the KDC returns a ([RFC4120] section 5.3) and session key the client can use to encrypt communication with the server.

See the example code in .

The new session key is used in the AP exchange, for example like .

The client presents the ticket ([RFC4120] section 5.3) and a new authenticator ([RFC4120] section 5.5.1). The server will decrypt the ticket, validate the authenticator, and can use any ([RFC4120] section 5.2.6) contained in the ticket for access control.

SSPI
[MS-SPNG]
[RFC4120] section 5.3
[RFC4120] section 5.4
KRB_KDC_REQ
KRB_KDC_REP
shall
5.2.7
7.5.2
the code
computes the encrypted timestamp
encrypted with the KDC's master key
an encrypted part
KRB_AS_REP
presents the TGT
Kerberos authenticator
service principal name (SPN)
[RFC4120] section 5.5.1
getKerberosTGS
[RFC4120] section 5.2.7.1
getKerberosTGS
authenticator
service ticket
Impacket
this
authorization data
the code implementation
the TGS exchange
Kerberos Key Distribution Center (KDC)
Privilege Attribute Certificate (PAC)
requests a ticket
ticket-granting service
[RFC2222]
Ticket-Granting Tickets - Win32 appsMicrosoftLearn
Logo
[MS-KILE]: Network LogonMicrosoftLearn
Logo
[MS-KILE]: KILE SynopsisMicrosoftLearn
Logo
[MS-KILE]: IntroductionMicrosoftLearn
Logo
[MS-PAC]: Privilege Attribute Certificate Data StructureMicrosoftLearn
Logo
[MS-KILE]: Kerberos Network Authentication Service (V5) SynopsisMicrosoftLearn
Logo
RFC 4120: The Kerberos Network Authentication Service (V5)IETF Datatracker
RFC 4120: The Kerberos Network Authentication Service (V5)IETF Datatracker
Service principal names - Win32 appsdocsmsft
Logo
[MS-KILE]: NamingMicrosoftLearn
Logo
Name Formats for Unique SPNs - Win32 appsMicrosoftLearn
Logo
SPNsActive Directory Security
How a Service Registers its SPNs - Win32 appsMicrosoftLearn
Logo
[MS-PAC]: IntroductionMicrosoftLearn
Logo
ktpassMicrosoftLearn
Logo
Logo
Logo
tracing the packets seen in the Kerberos process
account database
Generic Security Service Application Program Interface