TExcelTAD
Home › Wireless, IoT & Mobile Security › KRACK (2017): Key Reinstallation and the Patch

KRACK (2017): Key Reinstallation and the Patch

For thirteen years the 4-way handshake was considered proven secure, and the proofs were right: they showed that both sides derive the same fresh key and nobody else does. In October 2017 Mathy Vanhoef and Frank Piessens published KRACK (key reinstallation attacks): almost every Wi-Fi client could be made to install the same key twice, and reinstalling a key reset the counter that CCMP uses as a nonce. The proofs had not modelled when a key is installed, and reused nonces break AES-CTR and AES-GCM the same way everywhere. KRACK is a protocol-and-implementation bug, fixed by patches; this lesson shows the mechanism on a toy model, how it is detected, and what the patch changed.

The mechanism

  1. The client receives M3, installs the TK and sends M4. From now on it encrypts data with CCMP, using a 48-bit packet number (PN) that starts at 0 and increases with every frame. The PN is the nonce: it must never repeat under one key.
  2. If the AP doesn't receive M4, the standard says it retransmits M3. An attacker in a machine-in-the-middle position between client and AP (for example on another channel, relaying frames) can make sure M4 never arrives.
  3. The client accepts the retransmitted M3, as the standard allowed, and installs the same TK again, which resets its PN to 0.
  4. The client's next frames reuse packet numbers already used under the same key. In counter mode, the same key and nonce give the same keystream, so the XOR of two ciphertexts equals the XOR of their plaintexts. If one plaintext is known or predictable, the other is revealed, and with TKIP or GCMP frames could also be forged.

Variants hit the group-key handshake (GTK and IGTK reinstallation), the fast-BSS-transition handshake of 802.11r (an AP-side bug), and others: ten CVE numbers from CVE-2017-13077 to CVE-2017-13088. One implementation was much worse: wpa_supplicant 2.4 and later (2.6 through a variant of the attack), used by Linux and by Android 6.0 and later, cleared the key from memory after installing it, so a reinstallation installed an all-zero key.

The model below is a toy: a client that encrypts with "CCMP without the MAC" (AES-CTR with the PN in the counter block) under the lab TK from the key-chain lesson. It receives M3 twice, as in step 3, once unpatched and once with the fix:

python · try it yourself
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

TK = bytes.fromhex("a94472bf31a98b7444b3df754f11cc2d")      # the TK from the key-chain lesson (lab value)

def ccmp_like(pn, plaintext):
    """CCMP's encryption part is AES-CTR with a counter block built from the packet number (PN).
    Simplified: counter block = PN (6 bytes) + zero padding; the CBC-MAC part is left out."""
    ctr = pn.to_bytes(6, "big") + bytes(10)
    enc = Cipher(algorithms.AES(TK), modes.CTR(ctr)).encryptor()
    return enc.update(plaintext) + enc.finalize()

class Client:
    def __init__(self, patched):
        self.patched, self.installed, self.pn, self.sent = patched, False, 0, []
    def on_m3(self):
        if self.patched and self.installed:
            return "M3 again: key already installed, PN kept"     # the 2017 fix
        self.installed, self.pn = True, 0                        # (re)install TK and reset PN
        return "M3: install TK, PN reset to 0"
    def send(self, text):
        self.pn += 1
        self.sent.append((self.pn, ccmp_like(self.pn, text)))

for patched in (False, True):
    c = Client(patched)
    print(("PATCHED" if patched else "UNPATCHED") + " client")
    print("  " + c.on_m3())
    c.send(b"GET /status HTTP/1.1")                 # a frame whose content is easy to predict
    c.send(b"user=alice&pin=4821!!")
    print("  " + c.on_m3())                         # M3 retransmitted (M4 was lost or blocked)
    c.send(b"ccc")
    c.send(b"user=alice&pin=9356!!")
    pns = [pn for pn, _ in c.sent]
    print("  packet numbers used:", pns, "-> reuse!" if len(set(pns)) < len(pns) else "-> all unique")
    (pn_a, c1), (pn_b, c2) = c.sent[1], c.sent[3]
    if pn_a == pn_b:   # same PN, same keystream: knowing frame 2's plaintext reveals frame 4's
        known = b"user=alice&pin=4821!!"
        print("  frame 4 recovered without the key:", bytes(x ^ y ^ z for x, y, z in zip(c1, c2, known)))
UNPATCHED client
  M3: install TK, PN reset to 0
  M3: install TK, PN reset to 0
  packet numbers used: [1, 2, 1, 2] -> reuse!
  frame 4 recovered without the key: b'user=alice&pin=9356!!'
PATCHED client
  M3: install TK, PN reset to 0
  M3 again: key already installed, PN kept
  packet numbers used: [1, 2, 3, 4] -> all unique

The unpatched client sends frames 3 and 4 with PNs 1 and 2 again. Knowing frame 2's content (protocols are full of predictable bytes: headers, DHCP, ARP, HTTP requests), an observer XORs and reads frame 4 without ever learning the key. The patched client ignores the second installation and keeps counting.

What KRACK did not do

  • It did not reveal the passphrase or the PMK. Changing the Wi-Fi password was useless as a fix.
  • It needed a nearby attacker with a machine-in-the-middle position on the radio, against a specific client.
  • Traffic protected end to end (HTTPS, SSH, a VPN) stayed protected: the attack removed only the Wi-Fi layer.
  • Mostly clients were vulnerable; access points were affected through 802.11r fast transition and when they acted as clients (mesh, repeaters).

How it was and is detected

  • Testing your own devices. The researchers published test scripts that play the role of a misbehaving AP and check whether a client reinstalls keys or accepts replayed group keys. They belong in a lab with your own devices, like the rest of this course, and are now part of many vendors' certification tests.
  • Version and patch audits: device inventories compared with vendor advisories for the KRACK CVEs. Unpatched IoT devices from 2017 and earlier are still found today.
  • Sensors watching nonces. A receiver must drop frames whose PN isn't larger than the last one it accepted; a wireless IDS can flag a client whose PNs jump back to small values right after a retransmitted M3. The log below is synthetic:
python · try it yourself
observed = [  # (time, client, event, packet number of the client's frame) from a synthetic WIDS sensor log
    ("10:00:00.10", "02:00:00:0a:0a:05", "EAPOL M3", None), ("10:00:00.12", "02:00:00:0a:0a:05", "data", 1),
    ("10:00:00.20", "02:00:00:0a:0a:05", "data", 2), ("10:00:01.10", "02:00:00:0a:0a:05", "EAPOL M3 (retry)", None),
    ("10:00:01.15", "02:00:00:0a:0a:05", "data", 1), ("10:00:01.30", "02:00:00:0a:0a:07", "data", 9),
]
last_pn = {}
for t, mac, event, pn in observed:
    if pn is None:
        print(f"{t} {mac} receives {event} from the AP")
        continue
    if mac in last_pn and pn <= last_pn[mac]:
        print(f"{t} {mac} data PN {pn} <= last {last_pn[mac]}: ALERT nonce reuse (key reinstalled?)")
    last_pn[mac] = max(pn, last_pn.get(mac, 0))
10:00:00.10 02:00:00:0a:0a:05 receives EAPOL M3 from the AP
10:00:01.10 02:00:00:0a:0a:05 receives EAPOL M3 (retry) from the AP
10:00:01.15 02:00:00:0a:0a:05 data PN 1 <= last 2: ALERT nonce reuse (key reinstalled?)

A PN that goes backwards right after a repeated M3 is exactly the KRACK pattern. Retransmitted M3s alone are common (radio loss), which is why the PN check, not the retry, raises the alert.

How it was fixed

  • Clients: don't reinstall a key that is already in use. A retransmitted M3 is answered with M4 again, but the key, the PN and the replay counter are left alone. Group keys are tracked the same way so an old GTK can't be reinstalled. The IEEE 802.11 standard was clarified accordingly.
  • Access points: patch 802.11r (fast transition) handling; hostapd gained an option to stop retransmitting EAPOL-Key frames, which helps protect unpatched clients at the cost of some failed connections on noisy channels.
  • Defence in depth: application-layer encryption (TLS everywhere, VPNs on untrusted networks) is why a Wi-Fi-layer break exposed less than it would have years earlier.
  • WPA3 didn't remove the 4-way handshake: it still runs after SAE, so the KRACK fixes apply to WPA3 clients too.

Pitfalls

  • "A proof says it's secure": proofs cover the model, and implementations add state the model doesn't include (when keys are installed, retransmissions, timers).
  • Treating KRACK as fixed everywhere: firmware of cameras, printers and old phones often never got the patch. Inventory and isolate them.
  • Changing the Wi-Fi passphrase as a response to a protocol bug: patch instead.

Practice

1. Why does resetting the packet number expose plaintext, even though AES itself is not broken?

The PN is CCMP's nonce. With the same key and the same nonce, AES-CTR produces the same keystream, so two ciphertexts XOR to the XOR of their plaintexts. A known plaintext then reveals the other one. AES was never attacked.

2. Frame 3 of the unpatched client also reused a packet number. Which earlier frame does it pair with, and what would an observer need to read it?

Frame 1, which also used PN 1. XORing the two ciphertexts gives the XOR of the plaintexts; since frame 1 (GET /status HTTP/1.1) is predictable, frame 3's first bytes follow. Every frame sent after the reinstallation is exposed this way whenever its partner frame is predictable.

3. An IoT camera from 2016 has no firmware updates. What do you do about KRACK?

Treat its Wi-Fi link as unencrypted: put it on an isolated IoT network with no access to other devices, make sure its own traffic is protected end to end (or replace it), and track it in the inventory as an accepted risk.

Test yourself

What did a KRACK-vulnerable client do wrong?

  1. It revealed the PMK
  2. It reinstalled an already-installed key when M3 was retransmitted, resetting the nonce
  3. It used WEP
  4. It accepted unencrypted data

Did changing the Wi-Fi passphrase protect against KRACK?

  1. Yes
  2. No: the attack didn't reveal or depend on the passphrase; patching did
  3. Only for WPA3
  4. Only with long passphrases

Which signal indicates a key reinstallation to a sensor?

  1. A beacon
  2. A client's packet numbers going back to small values right after a retransmitted M3
  3. A DHCP request
  4. High signal strength

Why was HTTPS traffic still protected during KRACK?

  1. HTTPS uses WEP
  2. It is encrypted end to end above the Wi-Fi layer
  3. KRACK only affected DNS
  4. Browsers block Wi-Fi attacks