[{"content":"A stack overflow is a write past the end of a buffer whose layout the compiler decided, followed by a return instruction that trusts whatever it finds at the top of the stack. Everything else, the shellcode, the ROP chain, the defeated ASLR, is engineering on top of that one broken assumption.\nThe frame x86-64 on Linux follows the System V ABI. The first six integer arguments go in rdi, rsi, rdx, rcx, r8, and r9; the rest go on the stack. The return value comes back in rax. Before a call, the stack pointer is 16-byte aligned. The call pushes an 8-byte return address, so a function starts with rsp % 16 == 8.\nA non-optimized function looks like this:\npush rbp ; save the caller\u0026#39;s frame pointer mov rbp, rsp ; establish this frame sub rsp, 0x50 ; make room for locals ... leave ; mov rsp, rbp; pop rbp ret ; pop the return address into rip The stack grows downward, so the frame occupies the addresses below rbp. A local buffer sits at some negative offset, and the saved return address sits at rbp + 8:\nhigher addresses +----------------------+ | caller\u0026#39;s frame | +----------------------+ | return address | \u0026lt;- rbp + 8 +----------------------+ | saved rbp | \u0026lt;- rbp +----------------------+ | local buffer[64] | \u0026lt;- rbp - 64 +----------------------+ lower addresses read() writes upward from the start of the buffer. Give it more than 64 bytes and it walks straight through the saved frame pointer and into the return address, because nothing in the function checks where the buffer ends.\nThe overflow A minimal example:\n#include \u0026lt;unistd.h\u0026gt; __attribute__((noinline)) void win(void) { const char msg[] = \u0026#34;you win\\n\u0026#34;; write(1, msg, sizeof(msg) - 1); } int main(void) { char buf[64]; read(0, buf, 256); return 0; } Compiled for demonstration, with the protections turned off:\ngcc -O0 -fno-stack-protector -no-pie -o vuln vuln.c That build is deliberately weak. -fno-stack-protector removes the canary, -no-pie keeps the code at fixed addresses, and NX is left enabled because the interesting version of this exploit does not need an executable stack. Modern GCC will also warn about the obvious overflow; a warning is not a mitigation. A quick check with pwntools would report:\nRELRO: Partial RELRO Stack: No canary found NX: NX enabled PIE: No PIE The distance from the start of buf to the saved return address is 64 bytes of buffer plus 8 bytes of saved rbp. With that layout the offset is 72. Compilers are free to insert padding, so the reliable way to find it is a cyclic pattern: send a repeating sequence, crash, and read the value that landed in rip.\nfrom pwn import * io = process(\u0026#34;./vuln\u0026#34;) io.send(cyclic(128)) io.wait() core = io.corefile print(hex(core.regs.rip)) print(cyclic_find(core.regs.rip)) Once the offset is known, the return address is just the next eight bytes in the payload. Point it at win and the process prints the message instead of crashing:\nfrom pwn import * elf = ELF(\u0026#34;./vuln\u0026#34;, checksec=False) io = process(\u0026#34;./vuln\u0026#34;) payload = b\u0026#34;A\u0026#34; * 72 payload += p64(elf.sym.win) io.send(payload) print(io.recvall().decode()) That is a ret2win, the hello world of control-flow hijacking, and the last easy step. Real targets do not ship with a function that prints a flag, and the mitigations below turn \u0026ldquo;jump to a convenient address\u0026rdquo; into a multi-stage problem.\nNX and ROP If the stack is writable but not executable, injected shellcode fails at the first instruction fetch. The kernel maps the stack read-write, the CPU refuses to execute from it, and the process dies with a segmentation fault before any of the attacker\u0026rsquo;s bytes run.\nThe standard answer is return-oriented programming. Instead of injecting code, the attacker reuses code that is already mapped executable: short instruction sequences ending in ret, called gadgets. A gadget does one small thing, such as loading a value into rdi, and then returns to the next address on the stack.\nA chain is just a list of addresses and values laid out where the return address used to be:\n+----------------------+ | address of ret gadget| \u0026lt;- alignment fix +----------------------+ | address of pop rdi | +----------------------+ | address of \u0026#34;/bin/sh\u0026#34; | +----------------------+ | address of system | +----------------------+ | ... | +----------------------+ When the vulnerable function returns, rsp points at the first entry. The ret instruction pops it into rip. The CPU executes the gadget, hits another ret, and pops the next entry. The stack has become a program counter.\nTo call system(\u0026quot;/bin/sh\u0026quot;), the chain needs the string pointer in rdi and then a jump to system. Finding the gadgets is mechanical:\nROPgadget --binary vuln | grep \u0026#34;pop rdi\u0026#34; ropper --file vuln --search \u0026#34;pop rdi; ret\u0026#34; One detail causes more failed exploits than any other: stack alignment. On function entry, the ABI expects rsp % 16 == 8, because a normal call leaves it that way. After the vulnerable function\u0026rsquo;s leave; ret, the first gadget sees rsp % 16 == 0. Each pop and each ret moves the pointer by eight bytes, which flips that parity. A chain that goes straight from pop rdi; ret into system arrives with the stack misaligned, and glibc\u0026rsquo;s system hits a movaps instruction on an address that is not 16-byte aligned. The result is a crash inside libc that looks nothing like the overflow.\nThe fix is one extra ret at the front of the chain. It consumes eight bytes and restores the parity that a real call would have produced. It is a one-gadget tax on almost every ret2libc exploit.\nWith a libc leak and the base address known, the full chain is:\npayload = b\u0026#34;A\u0026#34; * 72 payload += p64(ret_gadget) # align rsp to 16 bytes payload += p64(pop_rdi) payload += p64(binsh_addr) # next(libc.search(b\u0026#34;/bin/sh\u0026#34;)) payload += p64(libc.sym.system) The binsh_addr and libc.sym.system values are only correct after the leak. Without a leak, ASLR randomizes libc on every run and the chain points into nothing.\nThe remaining mitigations Each mitigation removes a class of exploit, which is why real targets require chaining several weaknesses.\nA stack canary is a random value the compiler places between the locals and the saved rbp, read from thread-local storage at function entry and checked before returning. A linear overflow has to overwrite it, and the check fails before the return address is used. Bypasses exist: leak the canary through a format string or an over-read, brute-force it byte by byte on a server that forks without re-randomizing, or overwrite something the program uses before the check. -fstack-protector-strong covers more functions than the original heuristic, including arrays and locals whose address is taken.\nASLR moves the stack, the heap, libc, and, with -pie, the main executable. A fixed p64(elf.sym.win) becomes a guess. Exploits then need an information leak: a format string, an uninitialized buffer, a partial pointer, or a side channel. Partial overwrites are popular because the low 12 bits of a page offset do not change, so a one- or two-byte overwrite can redirect within the same region without knowing the base.\nFull RELRO with BIND_NOW makes the GOT read-only after relocation, which kills the classic \u0026ldquo;overwrite a GOT entry with system\u0026rdquo; technique. Partial RELRO leaves part of the GOT writable. Neither affects a pure ROP chain that only reads existing code, which is why RELRO matters more for some exploit classes than others.\nIntel CET keeps a second, hardware-protected copy of return addresses. A ret whose target does not match the shadow stack raises a control-protection fault, which breaks the core mechanic of ROP. ARM\u0026rsquo;s pointer authentication signs return addresses with a per-process key, and BTI restricts where indirect branches can land. CET and pointer authentication go after ROP itself instead of one of its inputs, but they depend on CPU, kernel, and compiler support all being enabled, and they push attention toward JOP and data-only attacks without eliminating control-flow hijacking.\nCompiler flags and code review The mitigation list is short and most of it is a compiler flag:\ngcc -O2 -fstack-protector-strong -D_FORTIFY_SOURCE=3 \\ -fPIE -pie -Wl,-z,relro,-z,now \\ -fcf-protection=full -o app app.c -fstack-protector-strong catches the common overflow patterns. -D_FORTIFY_SOURCE=3 adds checked variants of libc functions where the compiler can see the object size. -fPIE -pie makes the executable position-independent. Full RELRO plus BIND_NOW removes the writable GOT. -fcf-protection=full emits CET landing pads; shadow stacks are enforced when the CPU, kernel, and loader all support them.\nFlags are not a substitute for bounds checking, and they are not uniformly available. Code that parses untrusted input in C or C++ needs review at the points where a length from the input reaches a copy, an index, or an allocation size. The bugs that matter are usually one line: an off-by-one in a loop bound, a signed/unsigned comparison, a length field trusted before validation, an integer multiplication that wraps before it reaches malloc.\nSanitizers find these during testing. AddressSanitizer catches the overflow at the moment it happens, with a stack trace that points at the write rather than wherever the corruption eventually crashes. UndefinedBehaviorSanitizer catches the signed overflow and the bad shift that made the length wrong in the first place. Fuzzing finds the inputs that reach them.\nThe larger fix is language choice. Rust, Go, Java, and C# do not let a write past a slice or an array bounds check turn into a return-address overwrite. They have their own bugs, and unsafe blocks exist for a reason, but the specific chain described here, a linear write, a saved return address, and a ret, does not occur in safe code. For new code that parses network input, that is a stronger guarantee than any combination of compiler flags.\nWhy the pattern persists The exploit is old enough to be taught in a semester course, and it still maps cleanly onto real vulnerabilities. The frame layout has not changed. The ABI has not changed. The ret instruction still trusts the stack. Mitigations raised the cost and forced attackers to find a leak first, but they did not change the underlying property: a program that lets input decide where it writes has already given up control of its instruction pointer, whether or not the attacker has figured out where to point it yet.\nIn a code review, the questions that matter are about where a length comes from, who checked it against the destination, and what happens on the error path.\n","permalink":"https://syycorax.xyz/posts/stack-smashing-to-rop/","summary":"How a bounds check failure becomes control of the instruction pointer, what each mitigation blocks, and why ROP still works on a stack that cannot execute.","title":"Stack Smashing to ROP: What a Return Address Is Worth"},{"content":"Kerberos gets filed under authentication, but the protocol is a ticket distribution system. The password is used once, at the beginning, to prove possession of a key. After that, every access decision is made by presenting a ticket that was encrypted and signed by someone further up the chain.\nThat design is what makes single sign-on work. It is also what makes ticket theft so useful to an attacker: a ticket is a bearer credential, and the protocol has no way to ask whether the bearer is the entity it was issued to.\nPrincipals, realms, and the KDC A handful of names cover almost everything. A principal is an identity, written alice@CORP.EXAMPLE for a user or HTTP/web.corp.example@CORP.EXAMPLE for a service. The realm is the administrative domain, usually the DNS domain in uppercase. The KDC is the Key Distribution Center, which runs two logical services, the Authentication Service (AS) and the Ticket Granting Service (TGS); in Active Directory, every domain controller is a KDC.\nThe krbtgt account is special: its key encrypts ticket-granting tickets. It has no interactive logon, and its password is normally never used by a human. An SPN is a service principal name, the identifier a client asks for when it wants a ticket to a particular service.\nThe important structural point is that the KDC never talks to the service during a normal logon. It issues a ticket that the service can decrypt with a key it already shares with the KDC. Authentication is offline from the service\u0026rsquo;s perspective.\nFrom password to key Kerberos does not send the password. It converts it into a long-term key using a string-to-key function, then uses that key to encrypt a timestamp.\nFor AES in Active Directory, the conversion is PBKDF2-HMAC-SHA1 with 4096 iterations, salted with the realm and principal name. The result is an AES-128 or AES-256 key. RC4-HMAC uses a different, much cheaper derivation, which is one reason RC4 tickets are so much easier to attack offline.\nThe etype names are slightly misleading. aes256-cts-hmac-sha1-96 means AES-256 in CTS mode with an HMAC-SHA1-96 integrity check. It is still a reasonable choice, but the \u0026ldquo;SHA1\u0026rdquo; in the name is not a typo.\nBecause the key is derived from the password, changing the password changes the key. It does not, however, invalidate tickets that were already issued. A TGT is encrypted with the krbtgt key, not the user\u0026rsquo;s key, and the client\u0026rsquo;s copy of the session key lives in memory. Resetting a user\u0026rsquo;s password in response to an incident does not log them out of Kerberos. Only expiring the tickets, or resetting the key that protects them, does that.\nThe two-ticket flow Every interactive logon uses the same sequence. The client talks to the AS once, then to the TGS for each service it needs.\nclient KDC (AS) KDC (TGS) service | | | | | AS-REQ | | | | preauth: enc(timestamp) | | | |-----------------------------\u0026gt;| | | | AS-REP | | | | TGT (encrypted with krbtgt) | | | | session key (encrypted | | | | with client key) | | | |\u0026lt;-----------------------------| | | | | | | | TGS-REQ: TGT + authenticator + SPN | | |--------------------------------------------------\u0026gt;| | | TGS-REP: service ticket (encrypted with | | | service key) + service session key | | |\u0026lt;--------------------------------------------------| | | | | | | AP-REQ: service ticket + authenticator | | |------------------------------------------------------------------------\u0026gt;| | AP-REP: optional mutual authentication | | |\u0026lt;------------------------------------------------------------------------| The client cannot read its own TGT. The encrypted ticket blob is opaque to it. What the client holds is the TGT plus a session key, and that session key arrived encrypted under the client\u0026rsquo;s long-term key. The TGT is for the KDC; the session key is for proving to the KDC that the client is the same client that received the TGT.\nThe AS-REQ usually contains pre-authentication. The client encrypts a timestamp with its long-term key (PA-ENC-TIMESTAMP). The KDC decrypts it to verify that the client knows the key before issuing anything. Without pre-authentication, anyone can ask for a TGT for any principal and receive material encrypted with that principal\u0026rsquo;s key, which can then be attacked offline. That is AS-REP roasting, and it exists because some accounts are configured with \u0026ldquo;Do not require Kerberos preauthentication.\u0026rdquo;\nThe authenticator is what prevents replay. In the TGS-REQ and AP-REQ, the client encrypts a timestamp with the session key. The receiver checks the timestamp against its clock and keeps a cache of recently seen authenticators. The default tolerance in Active Directory is five minutes, which is why clock drift produces KRB_AP_ERR_SKEW and why NTP matters more than most people think.\nWhat is inside a ticket A ticket is an encrypted blob with a small plaintext header. The header names the client, the service, the ticket flags, and the session key\u0026rsquo;s encryption type. The encrypted part contains the session key, the client\u0026rsquo;s identity, the validity window, and the authorization data.\nIn Active Directory, that authorization data is the PAC: the user\u0026rsquo;s SID, the group SIDs, logon hours, and a set of checksums. The PAC has two signatures, one keyed with the service\u0026rsquo;s long-term key and one keyed with the krbtgt key. A service can verify the first; the second is supposed to let it detect a PAC that was forged or altered by someone who only knows the service key.\nThere is a gap here that matters later. Services generally do not call the KDC to validate every ticket. They decrypt the ticket with their own key, check the PAC signature, and proceed. The KDC is not consulted, because the whole point of the ticket is that it can be verified without one.\nService tickets and SPNs The TGS-REQ names the service by its SPN. The KDC looks up which account owns that SPN, encrypts the service ticket with that account\u0026rsquo;s key, and returns it along with a fresh session key for the client and service.\nThis creates a direct relationship between an SPN and a credential. If an SPN is registered on a user account, the service ticket is encrypted with that user\u0026rsquo;s password-derived key. If it is registered on a computer account, it uses the machine account key, which rotates automatically. If it is registered on a group Managed Service Account, the key is 240 bytes of random data that rotates on a schedule.\nThat relationship is why SPN hygiene matters: every SPN points at an account whose key can decrypt tickets for that service.\nDelegation models Kerberos has three delegation models, and they all extend the same bearer-ticket problem.\nUnconstrained delegation lets a service impersonate a user to any service in the domain. When a user authenticates to a service marked TRUSTED_FOR_DELEGATION, the KDC returns a forwardable copy of the user\u0026rsquo;s TGT alongside the service ticket, and the client passes it to the service. That service can then present the TGT anywhere. Compromise of such a service is equivalent to compromise of every user who has ever authenticated to it, including domain admins.\nConstrained delegation limits the service to a fixed list of target SPNs. The service uses S4U2Self to obtain a ticket to itself on behalf of the user, then S4U2Proxy to exchange it for a ticket to an allowed target. The list is the control. If the list includes a service that itself has broad rights, the constraint is weaker than it looks.\nResource-based constrained delegation moves the control to the target. The target computer\u0026rsquo;s msDS-AllowedToActOnBehalfOfOtherIdentity attribute names who may impersonate to it. Anyone with write access to that attribute can grant themselves the right, which is why it shows up in privilege-escalation paths.\nThese are features with sharp edges, and they are worth auditing before an incident rather than during one.\nCommon attacks Kerberoasting abuses the fact that any authenticated user can request a service ticket for any SPN. The ticket is encrypted with the service account\u0026rsquo;s key, and the requesting user can take it away and attack it offline. Service accounts with weak passwords and RC4 enabled fall quickly. AES-256 tickets are still attackable, just at a much higher cost per guess. Kerberoasting depends on a weak key, so the fix is to make the key unguessable: group Managed Service Accounts or long random passwords, and no RC4 tickets.\nAS-REP roasting is the same idea at the AS. If an account does not require pre-authentication, an attacker can request a TGT for it and receive a blob encrypted with the account\u0026rsquo;s key, without proving anything. Those accounts are rare, but they are trivially findable and often forgotten.\nGolden tickets are forged TGTs. The attacker needs the krbtgt key, usually obtained by replicating credentials from a domain controller (DCSync) after compromising a domain admin. With that key, they can mint a TGT for any user, with any SID, any group membership, and any lifetime. The KDC will accept it because the signature is valid. It will then issue real service tickets, properly encrypted with each service\u0026rsquo;s key, so the services see nothing unusual.\nThat last point is why golden tickets are so hard to detect. The service tickets are genuine. The only anomaly is that the TGT was never issued by the KDC, and most services have no way to know that. A forged TGT with a ten-year lifetime can sit in an environment for years if nobody looks at ticket metadata.\nSilver tickets skip the KDC entirely. The attacker forges a service ticket using a service account or computer account key, sets the PAC to whatever groups they want, and presents it directly. The service decrypts it with its own key and, if it does not validate the KDC\u0026rsquo;s PAC signature, accepts it. Silver tickets are narrower than golden tickets, limited to one service, but they also survive a krbtgt reset, because the krbtgt key was never involved. Only rotating the service account\u0026rsquo;s key invalidates them.\nHardening the keys and privileges Reset the krbtgt password twice, separated by at least one maximum ticket lifetime (ten hours by default in Active Directory), with domain controller replication completed in between. The first reset moves the old key into the \u0026ldquo;previous\u0026rdquo; slot, which is still accepted. The second reset removes it. Tickets minted with the original key stop working at the TGS once that key is gone. A single reset is not enough.\nRotate service account keys. Use gMSAs where possible, long random passwords where not, and stop putting SPNs on ordinary user accounts. A kerberoastable service account with a 12-character password is a liability regardless of how strong the domain password policy is.\nDisable RC4. Every RC4 ticket is a much cheaper offline attack than its AES equivalent, and modern domains have no reason to issue them. Audit the exceptions before flipping the switch, because old appliances are usually the reason RC4 is still enabled.\nEnable Kerberos armoring (FAST) where the domain supports it. It wraps the AS exchange in a key derived from the client\u0026rsquo;s machine account, which removes the easy offline capture that makes password guessing practical.\nPrivilege paths need the same treatment. Remove unconstrained delegation from anything that is not a tier-0 service. Audit constrained delegation target lists and resource-based delegation ACLs. Put privileged accounts in Protected Users, which among other things blocks delegation and restricts credential caching. Keep domain admins off workstations.\nMonitoring helps, within limits. The useful events are 4768 (TGT issued), 4769 (service ticket issued), 4770 (TGT renewed), 4771 (pre-authentication failed), and 4662 (directory object access, which is where DCSync shows up). A golden ticket often appears as a 4769 with no matching 4768 for the same logon, or as a TGT with an implausible lifetime. A kerberoasting burst looks like many 4769 events with RC4 encryption for accounts that should be using AES. None of these is conclusive on its own, and all of them depend on logs from every domain controller being collected somewhere they can be correlated.\nA canary account with an SPN and no real purpose is cheap and effective for kerberoasting. A fake privileged account with pre-authentication disabled and a known-bad password can serve the same role for AS-REP roasting. Golden tickets are harder to trap, but a TGT with a lifetime measured in years is not subtle if anyone is looking.\nWhy tickets remain the weak point Kerberos does exactly what it says: it issues bearer tickets that are valid until they expire, and it delegates trust to anyone holding the right key. The difficulty is operational. A domain accumulates delegation settings, stale SPNs, legacy encryption types, and service accounts with passwords from 2011, and each of those is a place where the key is weaker than the protocol assumes.\nThe mental model worth keeping is simple. Authentication happens once, at the AS. Everything after that is possession. If an attacker gets possession of a key that can mint tickets, the distinction between a legitimate ticket and a forged one is a signature that most services never check with anyone. That is the design, and it is why the krbtgt key and the service account keys deserve the same protection as domain admin credentials.\n","permalink":"https://syycorax.xyz/posts/kerberos-tickets/","summary":"What the KDC hands out, why a golden ticket works without touching a domain controller, and which defenses change the economics of the attack.","title":"Kerberos: Tickets All the Way Down"},{"content":"TLS 1.3 gets described as \u0026ldquo;one round trip,\u0026rdquo; which is true and also unhelpful. The useful part is that the ServerHello is the last message sent in the clear, and every key after it is derived from a transcript of bytes both sides have already seen.\nThat design removes a lot of old machinery. RSA key transport, CBC modes, compression, renegotiation, and DSA are gone. What remains is a small set of AEAD ciphers, an ephemeral key exchange, and a hash-based key schedule. The moving parts are fewer than in TLS 1.2, but they are linked more tightly, so it pays to know the order.\nBefore the handshake: the record layer TLS is a record protocol carrying a handshake protocol. Every record has a five-byte header:\ntype(1) legacy_version(2) length(2) Handshake messages use type 22, alerts 21, application data 23. After the keys are installed, a record\u0026rsquo;s outer type is almost always 23, even when the plaintext inside is a handshake message. The real content type is appended to the plaintext before encryption and stripped after decryption.\nThe plaintext that gets sealed is:\ncontent || content_type(1) || zero padding The trailing zeros are padding, and their count is implicit: everything after the content type that is zero is padding. This is how TLS 1.3 hides the true length of handshake messages without a separate length field inside the ciphertext.\nEach record is encrypted with AES-GCM or ChaCha20-Poly1305. The nonce is the static IV XORed with a 64-bit sequence number, and the additional authenticated data is the record header. For AES-GCM the IV is 12 bytes; the sequence number starts at zero in each direction and never repeats. The maximum plaintext per record is 16 KiB.\nClientHello: the only plaintext request The client starts with a ClientHello that looks, at the field level, a lot like TLS 1.2:\nlegacy_version set to 0x0303, because middleboxes get confused otherwise. 32 random bytes. A legacy session ID, often present purely for compatibility. A list of cipher suites, now only AEAD suites such as TLS_AES_128_GCM_SHA256 and TLS_CHACHA20_POLY1305_SHA256. Compression methods, always just null. Extensions. Two extensions carry the version negotiation. supported_versions lists 0x0304 for TLS 1.3. key_share carries one or more ephemeral public keys, usually X25519: 32 bytes each. signature_algorithms tells the server which signature schemes the client can verify. server_name carries SNI in the clear unless Encrypted Client Hello is in use, which is still unevenly deployed.\nThe client is not yet authenticated, and it has not proved it can complete a key exchange. It has only offered a public key.\nIf the server does not like the offered group, it can send a HelloRetryRequest, which is a ServerHello with a special random value. The client then sends a second ClientHello with a corrected key share. That is the only case where the handshake takes an extra round trip.\nServerHello and the encrypted flight The server picks a cipher suite, a key share, and a version. Its ServerHello is still plaintext. Everything after it is not:\nServerHello {EncryptedExtensions} ALPN, SNI acknowledgement {CertificateRequest} optional, for client auth {Certificate} the chain {CertificateVerify} signature over the transcript {Finished} HMAC over the transcript The braces mark what is encrypted. A passive observer sees the server\u0026rsquo;s certificate length and timing, not its contents. That closes a long-standing leak: in TLS 1.2, anyone on the path could read the certificate and learn which internal hostname or which customer domain was being visited.\nThe CertificateVerify signature is not over the certificate alone. It covers the handshake transcript:\nsignature = Sign(server_key, 64 bytes of 0x20 || \u0026#34;TLS 1.3, server CertificateVerify\u0026#34; || 0x00 || Transcript-Hash(Handshake Context, Certificate)) The 64 spaces and the context string prevent a signature from one protocol being replayed in another. The client verifies it with the public key from the leaf certificate, then verifies the certificate chain separately.\nFinished is an HMAC, not a signature. Both sides compute:\nfinished_key = HKDF-Expand-Label(base_key, \u0026#34;finished\u0026#34;, \u0026#34;\u0026#34;, Hash.length) verify_data = HMAC(finished_key, Transcript-Hash(...)) The client\u0026rsquo;s Finished is encrypted under the client\u0026rsquo;s handshake traffic key. After that, application data can flow. The server can send application data as soon as it has sent its own Finished; the client can send it after its Finished.\nThe key schedule TLS 1.3 derives keys in stages rather than negotiating a single master secret. Each stage mixes in more material and produces traffic secrets for a specific phase.\n0 (or PSK) | v HKDF-Extract = Early Secret | Derive-Secret(., \u0026#34;derived\u0026#34;) | (EC)DHE -------\u0026gt; HKDF-Extract = Handshake Secret | +---------+----------+ | | \u0026#34;c hs traffic\u0026#34; \u0026#34;s hs traffic\u0026#34; | | client hs keys server hs keys | Derive-Secret(., \u0026#34;derived\u0026#34;) | 0 ------\u0026gt; HKDF-Extract = Master Secret | +---------+----------+ | | \u0026#34;c ap traffic\u0026#34; \u0026#34;s ap traffic\u0026#34; | | client app keys server app keys HKDF-Extract(salt, input) is a single HMAC. HKDF-Expand-Label(secret, label, context, length) expands it with the TLS label prefix and the transcript hash where the stage calls for one. A traffic secret becomes an AEAD key and IV through two more expansions:\nkey = HKDF-Expand-Label(secret, \u0026#34;key\u0026#34;, \u0026#34;\u0026#34;, key_length) iv = HKDF-Expand-Label(secret, \u0026#34;iv\u0026#34;, \u0026#34;\u0026#34;, iv_length) The transcript hash is the running hash of all handshake messages, in order, with the handshake message headers included. It is what binds the keys to the exact negotiation. Change one byte of the ClientHello and the Finished MAC no longer verifies, because the transcript changed and every derived value changed with it.\nThis is also why the early secret is weak on its own. Before the (EC)DHE share is mixed in, it is derived only from a PSK or from zeros. The handshake secret is the first one with fresh entropy from both sides. The master secret adds another extraction so that compromise of the handshake keys does not directly yield the application keys.\nWhat the client is supposed to check The handshake proves the server holds the private key for the leaf certificate. It does not prove that the certificate is the right one for this host. That is the client\u0026rsquo;s job, and it is where custom TLS code tends to fail.\nA correct chain validation does all of this:\nBuild a path. Leaf to intermediate to a root in the trust store. Servers normally omit the root; clients must not require it. Verify each signature. Each certificate is signed by the next one\u0026rsquo;s key. The algorithm and parameters must be acceptable, not just mathematically valid. Check validity. notBefore and notAfter on every certificate in the path, against a clock you trust. Match the name. The subjectAltName extension, dNSName entries for hostnames, iPAddress for IPs. Wildcards match one leftmost label only: *.example.com matches api.example.com and does not match api.eu.example.com or example.com. The legacy CN fallback is gone from modern validators. Check basic constraints. CAs need CA:TRUE, and any pathLenConstraint must hold. The leaf must not be a CA. Check key usage and EKU. The leaf needs digitalSignature, because TLS 1.3 has no key encipherment. If an EKU is present, it must include serverAuth, and the chain must not contain an EKU that excludes it. Check revocation. CRL or OCSP, or a stapled response. Most clients soft-fail when the revocation source is unreachable, which means an attacker who can block the OCSP responder gets the old behavior. Apply local policy. Minimum key sizes, allowed curves, name constraints, and for public CAs, Certificate Transparency log inclusion. Steps 1 through 3 are what most libraries mean by \u0026ldquo;verify.\u0026rdquo; Step 4 is separate, and forgetting it is the classic bug: a chain can be entirely valid and still belong to a different hostname. In OpenSSL the check is X509_check_host, and s_client exposes it as -verify_hostname; in Go it is tls.Config.ServerName plus VerifyHostname if you are verifying manually; in Python, ssl.create_default_context() does the right thing only if you pass server_hostname to wrap_socket.\nA short Python client that gets all of this from the standard library:\nimport socket import ssl ctx = ssl.create_default_context() ctx.minimum_version = ssl.TLSVersion.TLSv1_3 with socket.create_connection((\u0026#34;example.com\u0026#34;, 443), timeout=10) as raw: with ctx.wrap_socket(raw, server_hostname=\u0026#34;example.com\u0026#34;) as tls: print(tls.version()) # TLSv1.3 print(tls.cipher()) # (\u0026#39;TLS_AES_256_GCM_SHA384\u0026#39;, \u0026#39;TLSv1.3\u0026#39;, 256) for kind, value in tls.getpeercert()[\u0026#34;subjectAltName\u0026#34;]: print(kind, value) create_default_context() enables verification and, because server_hostname is supplied, the hostname check too. Drop server_hostname and it refuses to wrap the socket; turn off check_hostname as well and the handshake succeeds with no name check at all. That second shape turns up in internal tooling often enough to be worth testing for.\n0-RTT and replay TLS 1.3 allows a client that has a valid session ticket to send application data in its first flight, before the server has replied. The client derives an early secret from the PSK, encrypts the data under early traffic keys, and includes it after the ClientHello.\nThe performance win is real for repeat connections. The security properties are worse than the rest of the handshake, in two specific ways.\nFirst, early data has no fresh key exchange. It is protected by the PSK alone, so it lacks forward secrecy; if the PSK is later compromised, recorded early data can be decrypted. Second, it is replayable. An attacker who captures the ClientHello and the early data can send them again, and the server has no cryptographic way to tell the copy from the original. TLS stacks implement anti-replay using single-use tickets and a time window, but that state is per-server, and it gets harder in a load-balanced deployment where the ticket may be presented to a different node.\nThe practical rule: only send idempotent requests in early data, never anything that changes state or money, and configure the server to reject early data it cannot prove is fresh. Most applications are better off disabling 0-RTT entirely until they have a reason.\nInspecting a connection openssl s_client will show the plaintext flight and then a wall of encrypted records, which is a decent illustration of where the boundary falls:\nopenssl s_client -connect example.com:443 -servername example.com \\ -tls1_3 -state -msg The -msg output shows the handshake message types and lengths. -state prints the state machine transitions. After ServerHello, the message bodies become opaque because the handshake keys are already installed.\nTo see inside, capture with Wireshark and set SSLKEYLOGFILE before running the client. Browsers and curl honor it; the file contains the per-connection traffic secrets in NSS key log format. With those, Wireshark decrypts the stream and you can compare the transcript hash it computes against the one in the Finished message.\nFor certificates specifically:\nopenssl s_client -connect example.com:443 -servername example.com \\ -showcerts \u0026lt;/dev/null 2\u0026gt;/dev/null | openssl x509 -noout -text The output is long, but the fields that matter are near the top: validity, SAN, basic constraints, EKU, and the signature algorithm. If a chain fails on one machine and works on another, compare the trust stores first, then the clocks, then the intermediate certificates the server is sending. A missing intermediate is the most common cause of \u0026ldquo;it works in the browser and nowhere else,\u0026rdquo; because browsers cache intermediates and most command-line clients do not.\nTLS 1.3 is a good protocol to read at the byte level because it is compact enough to hold in your head. The handshake is one flight each way, the key schedule is a handful of HMACs, and the certificate rules are the same ones that were always there. The fragile part is everything the client decides to trust after the math checks out.\n","permalink":"https://syycorax.xyz/posts/tls-1-3-handshake/","summary":"A walk through the TLS 1.3 flight, the HKDF key schedule, and the certificate checks that most custom clients get subtly wrong.","title":"TLS 1.3: The Handshake, Byte by Byte"},{"content":"Every reverse proxy is a parser. So is every application server sitting behind it. Most of the time the two agree about where one HTTP request ends and the next one begins, and nothing interesting happens. Request smuggling is the name for what happens when they don\u0026rsquo;t.\nThe bug lives in the framing. Most defenses inspect payloads, which is why they miss it.\nWhere the parsers meet A typical deployment looks like this:\nclient ──TLS──\u0026gt; CDN / load balancer ──HTTP/1.1──\u0026gt; origin (auth, routing, (application, rate limits) business logic) The front end makes decisions. It authenticates the user, applies rate limits, decides whether /admin is reachable, and forwards the request. The back end trusts that whatever arrives has already been through that gauntlet.\nBoth components read the same bytes off a reused connection. If they disagree about message boundaries, the front end\u0026rsquo;s decisions apply to a different sequence of requests than the one the back end executes.\nConnection reuse is the delivery mechanism. Without keep-alive or a backend connection pool, a desynchronized parser would only corrupt its own connection and the damage would stop there. With pooling, the bytes left over by one client can prefix the request of the next.\nContent-Length and chunked encoding HTTP/1.1 has two framing mechanisms, and they were never meant to appear together.\nContent-Length is a decimal byte count. The receiver reads exactly that many bytes after the header block and calls it a body. It cannot stream a body whose length is not known in advance.\nTransfer-Encoding: chunked sends the body as a sequence of chunks:\n8\\r\\n SMUGGLED\\r\\n 0\\r\\n \\r\\n The hex number is the chunk size in bytes. Each chunk\u0026rsquo;s data is followed by CRLF. A zero-length chunk ends the body, and an optional trailer section follows before the final blank line.\nRFC 9112 is clear about the collision: if a message carries both headers, Transfer-Encoding wins, the Content-Length must be removed before forwarding, and a server that receives both \u0026ldquo;ought to be handled as an error.\u0026rdquo; The word \u0026ldquo;ought\u0026rdquo; is doing a lot of work in real deployments. Different languages, different HTTP libraries, different middleware layers, and a few decades of lenient parsing mean the message that one hop considers malformed is often accepted by the next. Any disagreement between the two parsers is enough to work with.\nCL.TE: front end counts, back end chunks Take this request:\nPOST /search HTTP/1.1 Host: shop.example Content-Length: 44 Transfer-Encoding: chunked 0 GET /admin HTTP/1.1 Host: internal A front end that honors Content-Length reads 44 bytes of body, forwards the request, and considers the transaction finished. A back end that honors Transfer-Encoding reads the 0 chunk, consumes the empty trailer, and ends the request right there. The GET /admin is still in the socket buffer. It is a complete request, and the back end will parse it as the next one on that connection. The front end authorized the first request and never logged the second one.\nA more common shape leaves an incomplete prefix instead of a full request:\nPOST / HTTP/1.1 Host: shop.example Content-Length: 13 Transfer-Encoding: chunked 0 SMUGGLED Here the 13 bytes are 0\\r\\n\\r\\nSMUGGLED. The front end forwards them. The back end ends the chunked body at 0\\r\\n\\r\\n and leaves SMUGGLED in the buffer, where it becomes the first bytes of whichever request arrives next. That request now starts with SMUGGLEDGET /... and usually ends in a 400. Replace the filler with a crafted partial request and the next request on the connection becomes an extension of the attacker\u0026rsquo;s.\nThe attack takes its name from which side uses which framing. In CL.TE, the front end uses Content-Length and the back end uses chunked encoding.\nTE.CL: the same trick, reversed POST / HTTP/1.1 Host: shop.example Content-Length: 3 Transfer-Encoding: chunked 8 SMUGGLED 0 The front end sees chunked, reads an 8-byte chunk and the terminating zero chunk, and forwards the whole thing. The back end sees Content-Length: 3, reads 8\\r\\n as the body, and leaves SMUGGLED\\r\\n0\\r\\n\\r\\n behind.\nThe leftover here begins with chunk data, which limits what the attacker can place there. A real TE.CL payload sizes the chunk so that its contents start with a valid request line, and arranges for the trailing chunk terminator to fall inside the request that follows. It is fiddlier than CL.TE but the principle is identical.\nWhen both ends support chunked encoding but disagree about whether a particular Transfer-Encoding header counts, the variant is called TE.TE. The disagreement moves from \u0026ldquo;which framing wins\u0026rdquo; to \u0026ldquo;is this header even valid,\u0026rdquo; which opens more room than it sounds.\nHeader obfuscation Parsers disagree about malformed input long before they disagree about RFC semantics. A few variants that have caused real splits:\nHeader as sent Why parsers diverge Transfer-Encoding: chunked The baseline. Everyone agrees. Transfer-Encoding : chunked Whitespace before the colon is invalid, but plenty of parsers trim it while others ignore the header entirely. Transfer-Encoding: xchunked Not a registered coding. Some parsers reject it, some treat it as unknown and fall back to Content-Length. Transfer-Encoding: chunked, identity chunked is not the final coding, which the RFC forbids. Some normalize, some reject, some pick the first token. Two Transfer-Encoding headers One parser uses the first, another the last, another joins them. Content-Length: 5, 5 Duplicate or list-valued lengths are handled differently: first value, last value, or rejection. Transfer-Encoding:\\tchunked Tab instead of space. Same story as the space-before-colon case. None of these should be accepted by a strict implementation. In practice, the front end is often an appliance or CDN edge with its own parser, the back end is Node, Python, Java, or Go, and the two were never tested against each other with hostile input.\nHTTP/2 changes the framing rules, which is why \u0026ldquo;we use HTTP/2\u0026rdquo; feels like an answer. It isn\u0026rsquo;t, unless the upgrade is end to end. An edge that terminates HTTP/2 and speaks HTTP/1.1 to the origin has to translate, and translation is where H2.CL and H2.TE appear: the client sends a content-length header inside HTTP/2, the edge copies it into the HTTP/1.1 request, and the origin now sees two framing signals again.\nWhat the attacker gets The leftover bytes are a write primitive into the back end\u0026rsquo;s request stream. The first effect is an access control bypass: the front end authorizes one request and the back end executes another. If /admin is blocked at the edge but reachable from the origin\u0026rsquo;s network segment, a smuggled GET /admin never passes through the edge\u0026rsquo;s rules, and authentication checks that live in the proxy become decorative.\nThe same primitive captures credentials. Smuggle a request whose body is declared much larger than the bytes sent. The next request on that backend connection, usually a real user\u0026rsquo;s request with cookies and headers, gets appended to the attacker\u0026rsquo;s body and stored wherever that endpoint writes data. A login form or a support contact endpoint is enough, and the attacker later reads the captured request from the application itself.\nBehind a shared cache, the smuggled prefix can make the cache store the attacker\u0026rsquo;s response under a legitimate URL. The next visitor gets the poisoned object, which is a much larger blast radius than one connection.\nThere is also the plain response-queue confusion: the back end produces two responses for one front-end request. Depending on how the proxy maps responses to requests, the attacker\u0026rsquo;s second request can receive the response intended for someone else. Even without a cache in the path, that is a data leak.\nTesting safely Detection is mostly differential. You send a request that one parser considers complete and the other considers incomplete, then measure what happens. If the back end is left waiting for bytes that will never arrive, the response is delayed until a timeout, and that delay is the signal.\nFor a suspected CL.TE pair, send a chunked body that declares a chunk larger than what you provide:\nPOST /probe HTTP/1.1 Host: target.example Content-Length: 4 Transfer-Encoding: chunked 1 A A front end using Content-Length forwards the four bytes and waits for the response. A back end using chunked encoding reads a one-byte chunk, then waits for the CRLF that should follow it, which never comes. The request hangs. If the same request returns immediately, the pair is probably not CL.TE.\nFor TE.CL, invert it: declare a Content-Length larger than the chunked body you send, for example 50, while the body is the five bytes 0\\r\\n\\r\\n. A chunked front end considers the body complete and forwards it. A length-counting back end waits for 45 more bytes.\nA few practical rules keep this from becoming an outage:\nTest against your own deployment, with a unique marker header so you can find the traffic later. Use an absurdly short timeout and a single connection. Do not run timing probes in parallel; you will measure your own queueing. Confirm the delay is reproducible and lands near a known timeout value rather than varying with load. Treat a single 400 or 501 as inconclusive. WAFs, rate limiters, and request-size caps produce those too. Do not test with a payload that captures real user traffic unless you have written authorization and a way to delete what you collect. PortSwigger\u0026rsquo;s HTTP Request Smuggler extension automates the permutations, and the research by James Kettle is still the best description of the exploitation space. Both are worth reading before pointing anything at a production pair.\nMitigations that miss the cause A WAF is at the wrong layer. By the time it inspects a request, the edge parser has already decided where that request ends. The smuggled bytes are, from the WAF\u0026rsquo;s point of view, part of a body it may not even inspect, or a second request it never sees.\nTLS is irrelevant. The bytes are authenticated and encrypted; they are also perfectly legal HTTP as far as each individual parser is concerned.\nDisabling backend keep-alive works, in the sense that it removes the shared queue. It also removes most of the performance benefit of having a proxy, and it doesn\u0026rsquo;t help if the desync is between two internal hops.\n\u0026ldquo;Use HTTP/2\u0026rdquo; only helps if every hop speaks it natively. Any HTTP/1.1 downgrade puts the ambiguity back, and H2.CL/H2.TE are the proof.\nWhat to change The durable fixes are parser-level and deployment-level, not payload-level:\nReject any request carrying both Content-Length and Transfer-Encoding with a 400. Do it at every hop that parses HTTP. When forwarding, remove Content-Length if Transfer-Encoding is present, exactly as RFC 9112 requires. Reject whitespace before the header colon, duplicate conflicting Content-Length headers, and unknown transfer codings. Strictness is a feature here. Run the same HTTP parser on both sides of every trust boundary, or at least test the two you have as a pair rather than in isolation. Prefer HTTP/2 end to end and audit what your edge does when it downgrades. Watch for bursts of 400s, 408s, and aborted backend connections. A desync in progress looks like a parser arguing with itself. You cannot prove the absence of this bug by reading one codebase, because it lives in the seam between two implementations. What matters is how many parsers see this byte stream and whether anyone has evidence that they agree.\n","permalink":"https://syycorax.xyz/posts/http-request-smuggling/","summary":"How a disagreement over Content-Length and Transfer-Encoding turns a keep-alive connection into a request queue that one client can write into.","title":"HTTP Request Smuggling: Two Parsers, One Socket"}]