Repository navigation
kerberos: check the AP-REP timestamp in KerberosSSP initiator - #5305
musthaf-bughunter wants to merge 2 commits into
Conversation
RFC4120 sect 3.2.5 requires the client to check that the AP-REP ctime and cusec match the authenticator it sent. Without this, an AP-REP recorded for an earlier AP-REQ made with the same ticket completes mutual authentication. AI-Assisted: yes (Claude Opus 5.5)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #5305 +/- ##
=======================================
Coverage 78.99% 78.99%
=======================================
Files 375 375
Lines 98802 98807 +5
=======================================
+ Hits 78044 78050 +6
+ Misses 20758 20757 -1
🚀 New features to boost your workflow:
|
| # the server". Otherwise, an AP-REP for an earlier AP-REQ that used | ||
| # the same ticket would be accepted. |
There was a problem hiding this comment.
| # the server". Otherwise, an AP-REP for an earlier AP-REQ that used | |
| # the same ticket would be accepted. | |
| # the server". |
There was a problem hiding this comment.
Applied. I dropped the rest of that sentence too, since the suggestion left it dangling and the RFC quote already says why.
gpotter2
left a comment
There was a problem hiding this comment.
Thanks for the PR ! some minor comments
| from scapy.layers.gssapi import GSS_S_DEFECTIVE_TOKEN | ||
|
|
||
| st = KRB_Ticket(realm="DOMAIN.LOCAL", sname=PrincipalName.fromSPN("cifs/dc1"), encPart=EncryptedData()) | ||
| st.encPart.encrypt(srvkey, EncTicketPart( | ||
| transited=TransitedEncoding(trType=ASN1_INTEGER(0), contents=ASN1_STRING(b"")), | ||
| key=EncryptionKey.fromKey(tktkey), | ||
| crealm="DOMAIN.LOCAL", | ||
| cname=PrincipalName.fromUPN("User1@DOMAIN.LOCAL"), | ||
| authtime=ASN1_GENERALIZED_TIME(now - timedelta(hours=1)), | ||
| starttime=ASN1_GENERALIZED_TIME(now - timedelta(hours=1)), | ||
| endtime=ASN1_GENERALIZED_TIME(now + timedelta(hours=9)), | ||
| )) | ||
| krbcli = KerberosSSP(UPN="User1@DOMAIN.LOCAL", SPN="cifs/dc1", ST=KRB_Ticket(bytes(st)), KEY=tktkey) | ||
| krbsrv = KerberosSSP(SPN="cifs/dc1", KEY=srvkey) | ||
|
|
||
| with mock.patch('scapy.layers.kerberos.datetime', mock.MagicMock(now=lambda tz=None: now)): | ||
| clicontext, tok, _ = krbcli.GSS_Init_sec_context(None, req_flags=GSS_C_FLAGS.GSS_C_MUTUAL_FLAG) | ||
| _, first_ap_rep, status = krbsrv.GSS_Accept_sec_context(None, tok) | ||
| assert status == GSS_S_COMPLETE | ||
| _, _, status = krbcli.GSS_Init_sec_context(clicontext, first_ap_rep) | ||
| assert status == GSS_S_COMPLETE | ||
|
|
||
| # A second AP-REQ with the same ticket, answered with the first AP-REP | ||
| with mock.patch('scapy.layers.kerberos.datetime', mock.MagicMock(now=lambda tz=None: now + timedelta(seconds=1))): | ||
| clicontext, tok, _ = krbcli.GSS_Init_sec_context(None, req_flags=GSS_C_FLAGS.GSS_C_MUTUAL_FLAG) | ||
| _, _, status = krbcli.GSS_Init_sec_context(clicontext, first_ap_rep) | ||
|
|
||
| assert status == GSS_S_DEFECTIVE_TOKEN |
There was a problem hiding this comment.
Can you use the KrbRandomPatcher in this test? (instead of doing the mocks manually) thanks
There was a problem hiding this comment.
Done. The first exchange now runs under KrbRandomPatcher, and the second AP-REQ is built outside it so its authenticator time differs from the pinned 2024 date (the initiator doesn't check the ticket's endtime, so the stale ticket doesn't get in the way). The IAKERB step still needs the extra date patch on top of KrbRandomPatcher, since the captured AP-REP has a 2026 authenticator time.
Description
KerberosSSP.GSS_Init_sec_contextdecrypts the AP-REP with the ticket session key and accepts it without comparing itsctime/cusecto the authenticator it sent (kerberos.py#L5838-L5845), which RFC4120 sect 3.2.5 requires. The ticket session key is the same for every AP-REQ made with a given service ticket (ST=/KEY=, ccache,Ticketer), so an AP-REP recorded from an earlier exchange completes mutual authentication of a later one. The client then uses the old subkey and sequence number, so a man-in-the-middle can impersonate the server and replay its old signed or sealed messages.The patch stores the authenticator time in the context and returns
GSS_S_DEFECTIVE_TOKENwhen the AP-REP does not match, except in passive mode. The IAKERB test fed a captured AP-REP to an AP-REQ built with the mocked 2024 date, so that step now mocks the capture time. A regression test is added tokerberos.uts.