Kerberos: AS, TGS and AP Exchanges
Kerberos is the default authentication protocol of Active Directory and the reason a domain user types a password once a day. It replaces NTLM's "prove it to every server, and let the server ask the DC" with tickets: the DC, acting as key distribution center (KDC), issues the user a ticket-granting ticket once, then service tickets on request, and servers check those tickets without contacting the DC at all. Nearly every AD attack with a famous name lives somewhere in this flow: AS-REP roasting in the first exchange, Kerberoasting in the second, silver and golden tickets in what servers and the KDC trust, delegation in how tickets are forwarded. You will understand all of them if you can follow one ticket through the protocol, which is what this lesson does, with real encryption and toy keys.
The message table and the KDC's clock-skew check are in LDAP and Kerberos on the Wire. Kerberos is RFC 4120; Microsoft's extensions are in MS-KILE and MS-PAC.
Three exchanges, three keys
| Exchange | Request proves | Reply contains | Encrypted with |
|---|---|---|---|
| AS (authentication service) | the client knows K_alice (pre-authentication: an encrypted timestamp) |
the TGT and session key S1 |
TGT: K_krbtgt; S1 copy for the client: K_alice |
| TGS (ticket-granting service) | the client holds a TGT and S1 (an authenticator encrypted with S1) |
a service ticket for the requested SPN and session key S2 |
ticket: the service account's key K_fs01; S2 copy: S1 |
| AP (application) | the client holds the ticket and S2 |
(optionally) a reply proving the server's identity | authenticator: S2 |
Each ticket carries a PAC (privilege attribute certificate) with the user's SID and group SIDs, which the service uses to build the access token (the next lesson).
The Kerberos lab plays the six messages one by one, with every encrypted part coloured by the key that protects it; its service is a SQL server (MSSQLSvc/sql01) instead of FS01. Watch which keys never leave their owner: alice's password-derived key Kc only ever encrypts a timestamp and S1, the TGT is opaque to her because it is under krbtgt, and the service ticket is under the service's key Ks. The readouts also name the DC events each exchange produces: 4768 for the AS exchange and 4769 for the TGS exchange, while the AP exchange is logged on the service host (4624), not on the DC. The attack markers are for the lessons on roasting and forged tickets.
One ticket, step by step
The program below runs the three exchanges with real authenticated encryption (AES-GCM from cryptography) and toy keys: each long-term key is derived from a lab password with PBKDF2 and a salt of realm plus name, much like the real algorithm, which the next lesson computes exactly. Session keys and nonces come from a fixed counter so the output is the same every run; a real KDC draws session keys at random and real Kerberos encrypts with AES-CTS and HMAC-SHA1-96, with a key-usage number in each encryption just like the usage argument here.
import json, base64, hashlib
from datetime import datetime, timedelta
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.exceptions import InvalidTag
REALM = "CORP.LAB.TEST"
def long_term_key(password, name):
return PBKDF2HMAC(hashes.SHA256(), 32, (REALM + name).encode(), 4096).derive(password.encode())
counter = 0
def fresh(n): # deterministic "randomness" for a reproducible demo
global counter; counter += 1
return hashlib.sha256(b"demo-%d" % counter).digest()[:n]
def seal(key, obj, usage):
nonce = fresh(12)
return nonce + AESGCM(key).encrypt(nonce, json.dumps(obj).encode(), str(usage).encode())
def unseal(key, blob, usage):
return json.loads(AESGCM(key).decrypt(blob[:12], blob[12:], str(usage).encode()))
b64 = lambda b: base64.b64encode(b).decode()
keys = {"alice": long_term_key("Winter2026!", "alice"),
"krbtgt": long_term_key("krbtgt-lab-only-value", "krbtgt"),
"cifs/fs01.corp.lab.test": long_term_key("FS01$-machine-secret", "fs01quot;)}
now = datetime(2026, 1, 15, 9, 0, 0)
# 1 AS-REQ: pre-authentication with an encrypted timestamp
k_client = long_term_key("Winter2026!", "alice") # computed on WS01 from the typed password
as_req = {"cname": "alice", "padata": seal(k_client, {"ts": now.isoformat()}, 1)}
# KDC: checks the timestamp with its copy of alice's key
ts = datetime.fromisoformat(unseal(keys["alice"], as_req["padata"], 1)["ts"])
assert abs(ts - now) <= timedelta(minutes=5)
s1 = fresh(32)
pac = {"user_sid_rid": 1104, "group_rids": [513, 1120]} # Domain Users + Finance
tgt = seal(keys["krbtgt"], {"cname": "alice", "key": b64(s1), "pac": pac, "end": (now + timedelta(hours=10)).isoformat()}, 2)
as_rep = {"ticket": tgt, "enc_part": seal(keys["alice"], {"key": b64(s1)}, 3)}
print(f"1-2 AS : TGT {len(tgt)} bytes (opaque to the client), S1 delivered under alice's key")
# client: recovers S1; the TGT itself stays opaque
s1_client = base64.b64decode(unseal(k_client, as_rep["enc_part"], 3)["key"])
try:
unseal(k_client, tgt, 2)
except InvalidTag:
print(" client tries to read its own TGT: InvalidTag (only krbtgt's key opens it)")
# 3 TGS-REQ: TGT + authenticator encrypted with S1
spn = "cifs/fs01.corp.lab.test"
tgs_req = {"ticket": tgt, "sname": spn, "auth": seal(s1_client, {"cname": "alice", "ts": now.isoformat()}, 7)}
inner = unseal(keys["krbtgt"], tgs_req["ticket"], 2) # KDC opens the TGT: no database lookup of alice's key
s1_kdc = base64.b64decode(inner["key"])
assert unseal(s1_kdc, tgs_req["auth"], 7)["cname"] == inner["cname"]
s2 = fresh(32)
ticket = seal(keys[spn], {"cname": inner["cname"], "key": b64(s2), "pac": inner["pac"]}, 2)
tgs_rep = {"ticket": ticket, "enc_part": seal(s1_kdc, {"key": b64(s2)}, 8)}
print(f"3-4 TGS: service ticket for {spn} under the service account's key, S2 under S1")
# 5 AP-REQ: the service needs only its own key
s2_client = base64.b64decode(unseal(s1_client, tgs_rep["enc_part"], 8)["key"])
ap_req = {"ticket": ticket, "auth": seal(s2_client, {"cname": "alice", "ts": now.isoformat()}, 11)}
t = unseal(keys[spn], ap_req["ticket"], 2)
a = unseal(base64.b64decode(t["key"]), ap_req["auth"], 11)
print(f"5 AP : FS01 accepts {a['cname']}, groups from PAC {t['pac']['group_rids']}, without contacting the KDC")1-2 AS : TGT 189 bytes (opaque to the client), S1 delivered under alice's key
client tries to read its own TGT: InvalidTag (only krbtgt's key opens it)
3-4 TGS: service ticket for cifs/fs01.corp.lab.test under the service account's key, S2 under S1
5 AP : FS01 accepts alice, groups from PAC [513, 1120], without contacting the KDC
Follow the keys and the famous attacks fall into place:
- The AS-REP's
enc_partis encrypted with alice's key. If an account doesn't require pre-authentication, anyone can send an AS-REQ for it and get material encrypted with its password-derived key: AS-REP roasting. - The service ticket is encrypted with the service account's key, and any authenticated user may request a ticket for any SPN. If that account is a user with a weak password, the ticket is material for offline guessing: Kerberoasting (Kerberoasting and AS-REP Roasting).
- The KDC opened the TGT without looking up alice: it trusts whatever the TGT says, because only
krbtgt's key could have made it. Whoever has thekrbtgtkey can make TGTs for anyone with any groups: a golden ticket. FS01 trusts the PAC in a ticket under its own key the same way: with that key, a silver ticket (Golden and Silver Tickets).
How Kerberos use is detected
On domain controllers (with the Kerberos subcategories of the audit policy on):
| Event | Meaning | Useful fields |
|---|---|---|
| 4768 | a TGT was requested (AS exchange) | account, client address, ticket encryption type, pre-authentication type (0 = none), result code |
| 4771 | pre-authentication failed | account, client address, failure code (0x18 wrong password, 0x25 clock skew) |
| 4769 | a service ticket was requested (TGS exchange) | account, service name, encryption type, failure code, transited services (delegation) |
| 4770 | a service ticket was renewed |
The AP exchange happens on the server, which records the resulting logon as 4624 with Authentication Package: Kerberos. The detections of the coming lessons are patterns over these fields: RC4 where AES is expected, many SPNs per user per minute, pre-authentication type 0, a service logon with no matching 4769 on any DC.
How it is hardened
- AES only for Kerberos; RC4 off where every system supports AES (encryption types).
- Pre-authentication required for every account; service accounts as gMSAs with long random passwords.
- Synchronised clocks (the PDC emulator from a reliable source, members from the domain hierarchy).
- Rotate the
krbtgtkey on a schedule and after any DC compromise. - Kerberos armoring (FAST) to protect the AS exchange itself (Protected Users and Armoring).
Pitfalls
- Thinking the client can read its tickets. It can't; it holds them and the session keys. Tools that "show tickets" display their metadata and the parts the client can decrypt.
- Expecting the server to ask the DC. By default it doesn't; PAC validation against the DC is optional and rarely done, which is why silver tickets work.
- Long ticket lifetimes. A TGT is valid for 10 hours and renewable for 7 days by default; group removals and disabled accounts take effect only as tickets are renewed or reissued.
- Using IP addresses instead of names: no SPN matches an IP, so the client falls back to NTLM.
Practice
1. Which key does the KDC use to open the TGT in step 3, and why doesn't it need alice's key there?
K_krbtgt. The TGT carries the session key S1 and alice's PAC; whatever is inside was put there by a KDC, so it doesn't need to recheck alice's password.
2. Change the timestamp the client seals in as_req to (now + timedelta(minutes=6)).isoformat() and keep the KDC's check. What fails, and with which event in a real domain?
The assert on the five-minute skew. A real KDC returns KRB_AP_ERR_SKEW and logs 4771 with failure code 0x25.
3. Why can any authenticated user obtain a service ticket encrypted with a service account's key?
The KDC doesn't decide authorization: it issues a ticket for any SPN to any authenticated user, and the service decides access from the PAC. That design is what makes Kerberoasting possible, and why service-account passwords must be long and random.
Test yourself
Which key encrypts a ticket-granting ticket?
- The user's key
- The krbtgt account's key
- The service account's key
- The session key S1
Which event on a DC records a service ticket request, including the service name and encryption type?
- 4768
- 4769
- 4771
- 4624
In the AP exchange, what does the service need to validate the ticket?
- A call to the DC for every request
- Only its own long-term key
- The user's password
- The krbtgt key
What does Kerberos pre-authentication prove in the AS-REQ?
- That the client knows its password-derived key, by sending an encrypted timestamp
- That the client has a TGT
- That the server is genuine
- Nothing
In the Kerberos lab, which key encrypts the service ticket in the TGS-REP (message 4)?
- alice's key Kc
- The krbtgt key
- The service account's key Ks
- The service session key S2