TLS Records and the ClientHello
TLS encrypts almost everything, but not the first message. The ClientHello, the client's opening offer, travels in the clear in TLS 1.2 and 1.3 alike: which server the client wants (SNI), which protocol versions, cipher suites and key-exchange groups it supports, which application protocol it hopes to speak (ALPN). For defenders that one packet is a goldmine: it names the destination of encrypted traffic, it reveals outdated clients, and its exact shape fingerprints the software that sent it, including malware. This lesson parses the ClientHello in $SAMPLES/pcap/tls.pcap byte by byte and with tshark. TLS itself is introduced in TLS; the handshake's cryptography is the subject of Applied Cryptography.
Records and handshake messages
TLS data travels in records: a 5-byte header (content type, legacy version, length) followed by the fragment. Content type 22 is a handshake message, 23 application data, 21 an alert, 20 change cipher spec. Inside a handshake record, each message has a 4-byte header (type, 3-byte length): type 1 is ClientHello, 2 ServerHello, 11 Certificate.
| Part of the ClientHello | Bytes | Security relevance |
|---|---|---|
| legacy_version | 2 | always 0x0303 (TLS 1.2) in TLS 1.3 clients, for middlebox compatibility; the real versions are in an extension |
| random | 32 | client nonce |
| legacy_session_id | 0–32 | random in TLS 1.3 (compatibility mode) |
| cipher_suites | list | offered algorithms; old or weak ones are a finding |
| extensions | list | server_name (SNI, type 0), supported_groups (10), signature_algorithms (13), ALPN (16), supported_versions (43), psk_key_exchange_modes (45), key_share (51) |
Parsing the sample ClientHello
The 214 bytes below are the TCP payload of frame 4 of tls.pcap (the client's first data segment), copied in. The parser walks the record, the handshake header and every extension.
import struct
data = bytes.fromhex(
"16030100d1010000cd030365ea7998470db82d51a71b0c93332aa49223b66d205f9ba18fd710f60bba034b20"
"2a5e0cbf11c85092256dcc13ef82a8c68fd129140ef33437b08550cea5ecef3c000a130113021303c02fc030"
"0100007a0000000d000b0000086c61622e74657374000a00080006001d00170018000d000e000c04030804"
"04010503080508070010000e000c02683208687474702f312e31002b00050403040303002d000201010033"
"00260024001d0020e45443e79f34f7420f13b200e13189f15da68d24b60a2fed87de0cb03f5b214e")
SUITES = {0x1301: "TLS_AES_128_GCM_SHA256", 0x1302: "TLS_AES_256_GCM_SHA384", 0x1303: "TLS_CHACHA20_POLY1305_SHA256",
0xC02F: "ECDHE_RSA_AES_128_GCM_SHA256", 0xC030: "ECDHE_RSA_AES_256_GCM_SHA384"}
GROUPS = {0x001D: "x25519", 0x0017: "secp256r1", 0x0018: "secp384r1"}
VERSIONS = {0x0304: "TLS 1.3", 0x0303: "TLS 1.2", 0x0302: "TLS 1.1", 0x0301: "TLS 1.0"}
ctype, rver, rlen = struct.unpack("!BHH", data[:5])
print(f"record: type {ctype} (handshake), legacy version 0x{rver:04x}, length {rlen}")
hs = data[5:5 + rlen]
htype, hlen = hs[0], int.from_bytes(hs[1:4], "big")
p = 4
cver = struct.unpack("!H", hs[p:p + 2])[0]; p += 2 + 32 # version, client nonce
sid_len = hs[p]; p += 1 + sid_len
n = struct.unpack("!H", hs[p:p + 2])[0]; p += 2
suites = [struct.unpack("!H", hs[i:i + 2])[0] for i in range(p, p + n, 2)]; p += n
comp_len = hs[p]; p += 1 + comp_len
ext_total = struct.unpack("!H", hs[p:p + 2])[0]; p += 2
print(f"handshake type {htype} (ClientHello), length {hlen}, legacy_version 0x{cver:04x}, session id {sid_len} bytes")
print("cipher suites:", ", ".join(SUITES.get(s, hex(s)) for s in suites))
end = p + ext_total
while p < end:
etype, elen = struct.unpack("!HH", hs[p:p + 4]); body = hs[p + 4:p + 4 + elen]; p += 4 + elen
if etype == 0:
print(" ext 0 server_name:", body[5:].decode())
elif etype == 10:
print(" ext 10 supported_groups:", [GROUPS.get(g, hex(g)) for (g,) in struct.iter_unpack("!H", body[2:])])
elif etype == 16:
names, q = [], 2
while q < len(body):
names.append(body[q + 1:q + 1 + body[q]].decode()); q += 1 + body[q]
print(" ext 16 ALPN:", names)
elif etype == 43:
print(" ext 43 supported_versions:", [VERSIONS[v] for (v,) in struct.iter_unpack("!H", body[1:])])
elif etype == 51:
group, klen = struct.unpack("!HH", body[2:6])
print(f" ext 51 key_share: {GROUPS[group]}, {klen}-byte public key")
else:
print(f" ext {etype} ({elen} bytes)")record: type 22 (handshake), legacy version 0x0301, length 209
handshake type 1 (ClientHello), length 205, legacy_version 0x0303, session id 32 bytes
cipher suites: TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256, ECDHE_RSA_AES_128_GCM_SHA256, ECDHE_RSA_AES_256_GCM_SHA384
ext 0 server_name: lab.test
ext 10 supported_groups: ['x25519', 'secp256r1', 'secp384r1']
ext 13 (14 bytes)
ext 16 ALPN: ['h2', 'http/1.1']
ext 43 supported_versions: ['TLS 1.3', 'TLS 1.2']
ext 45 (2 bytes)
ext 51 key_share: x25519, 32-byte public key
Everything here is readable by anyone on the path: the client wants lab.test, speaks TLS 1.3 or 1.2, prefers HTTP/2, and has already sent an X25519 public key so the server can finish the key exchange in one round trip. The key share is public by design; the secret comes from combining it with the server's private key.
The same with tshark
tshark decodes the same fields, and computes two client fingerprints: JA3 (an MD5 over the version, cipher suites, extensions, groups and point formats) and its successor JA4 (a readable prefix such as t13d0507h2: TCP, TLS 1.3, SNI is a domain, 5 suites, 7 extensions, then the first and last characters of the first ALPN value, h2; followed by hashes of the sorted suites and extensions).
cd $SAMPLES/pcap
tshark -r tls.pcap -Y 'tls.handshake.type == 1' -T fields -E header=y \
-e ip.dst -e tls.handshake.extensions_server_name -e tls.handshake.extensions_alpn_str \
-e tls.handshake.extensions.supported_version -e tls.handshake.ciphersuite
echo "--- fingerprints:"
tshark -r tls.pcap -Y 'tls.handshake.type == 1' -T fields -e tls.handshake.ja3 -e tls.handshake.ja4ip.dst tls.handshake.extensions_server_name tls.handshake.extensions_alpn_str tls.handshake.extensions.supported_version tls.handshake.ciphersuite
10.10.0.10 lab.test h2,http/1.1 0x0304,0x0303 0x1301,0x1302,0x1303,0xc02f,0xc030
--- fingerprints:
9f093ce75416f083f2c887d8c093feb7 t13d0507h2_c96ac5133cd7_17a634208175
How defenders use it
- Where encrypted traffic goes. SNI names the site even when the content is encrypted. Proxies and firewalls filter on it; logging it (Zeek's
ssl.log, firewall logs) shows which services hosts talk to. - Outdated clients. ClientHellos offering only TLS 1.0/1.1, export or RC4/3DES suites identify software that needs updating.
- Fingerprinting. Each TLS library and configuration produces a characteristic JA3/JA4. A "browser" User-Agent with a fingerprint of a scripting library, or a fingerprint known from a malware family, is a strong signal. Fingerprints change when software updates, and attackers can imitate popular ones, so use them as one signal among several.
- Anomalies. SNI that doesn't match the certificate the server returns, IP-literal or empty SNI towards Internet hosts, ALPN values never seen before.
Weaknesses and their fixes
| Problem | What it allows | Fix |
|---|---|---|
| SNI in clear | observers (and censors) learn the site | Encrypted Client Hello (ECH): the real ClientHello is encrypted to a key published in DNS (HTTPS records) |
| old versions still accepted | downgrade to TLS 1.0/1.1 and their weaknesses | servers accept TLS 1.2+ only (RFC 8996 deprecated 1.0 and 1.1); TLS 1.3's downgrade sentinel in the server random |
| weak suites (CBC with SHA-1, RSA key exchange, 3DES) | no forward secrecy, padding-oracle and Sweet32-class attacks | offer AEAD suites with ECDHE only; TLS 1.3 removes the rest |
| no certificate validation in the client | man-in-the-middle with any certificate | validate the chain and the name; pin in special cases (OpenSSL s_client shows the check) |
Detection of the problems: inventory the versions and suites your servers negotiate (scanners such as testssl.sh or sslyze, in your own scope), and the versions your clients offer (from ClientHello logs).
The TLS 1.3 lab shows what a passive observer of one connection gets. Only the ClientHello and ServerHello are grey (cleartext): the SNI shop.example.com, the key shares and the ALPN offer. Everything from EncryptedExtensions on, the certificate included, is under handshake or application keys. Switch Encrypted Client Hello on and the readout "Observer reads SNI" changes to "no (ECH)": the visible ClientHello carries only the outer name public.example.net. Record sizes and timing stay visible either way.
Pitfalls
- Reading the record's
0x0301or the ClientHello's0x0303as the client's TLS version. TLS 1.3 support is only visible insupported_versions. - Treating SNI as authenticated. A client can put any name in it; what the server proves is in its certificate.
- Blocking by JA3 hash alone: one browser update changes it for millions of legitimate clients.
Practice
1. How many extensions does the ClientHello carry, and how does JA4 show that?
Seven (types 0, 10, 13, 16, 43, 45, 51). In t13d0507h2 the digits 05 and 07 are the counts of cipher suites and extensions.
2. Why does a TLS 1.3 ClientHello already contain a public key?
TLS 1.3 completes the handshake in one round trip: the client guesses the server's preferred group (here x25519) and sends its share immediately. If the server prefers another group it asks again with a HelloRetryRequest.
3. A host sends ClientHellos with SNI cdn.lab.test but the server's certificate is for an unrelated name. What could this be?
Domain fronting or a tunnelling tool hiding the real destination, a misconfigured CDN, or an interception proxy. Correlate with the certificate, the destination IP's owner, the JA4 fingerprint and the process on the host.
Test yourself
Which TLS extension carries the name of the server the client wants?
- ALPN (16)
- server_name / SNI (0)
- key_share (51)
- supported_versions (43)
Where does a TLS 1.3 client say it supports TLS 1.3?
- The record version 0x0301
- The ClientHello legacy_version 0x0303
- The supported_versions extension
- The cipher suite list only
What does a JA3 or JA4 fingerprint identify?
- The server's certificate
- The client TLS implementation and configuration, from the shape of its ClientHello
- The user's password
- The DNS server used
Which technology hides the SNI from observers?
- HSTS
- Encrypted Client Hello (ECH)
- OCSP stapling
- DNSSEC
In the TLS 1.3 lab's full handshake, which messages can a passive observer read?
- All of them
- Only the ClientHello and the ServerHello
- Only the Certificate
- None