TExcelTAD
Home › Wireless, IoT & Mobile Security › The WPA2 4-Way Handshake and EAPOL-Key Frames

The WPA2 4-Way Handshake and EAPOL-Key Frames

When your phone joins a WPA2 or WPA3 network, four short frames fly between it and the access point before any data moves. They prove that both sides hold the same secret without sending it, derive fresh keys for this session, and deliver the key for broadcast traffic. Almost every Wi-Fi attack of the last decade targeted these four frames or their implementations: offline passphrase guessing captures them, KRACK replayed one of them. This lesson follows the handshake frame by frame; the next lesson computes its keys.

Roles and keys

  • The supplicant is the client (SPA, its MAC address), the authenticator is the access point (AA).
  • Both already hold the PMK (pairwise master key, 256 bits). In WPA2-Personal it comes from the passphrase and the SSID; in Enterprise from the 802.1X/EAP exchange; in WPA3 from SAE.
  • From the PMK, both MAC addresses and two random nonces (ANonce from the AP, SNonce from the client) each side derives the PTK (pairwise transient key) = KCK (key confirmation key, for MICs) ‖ KEK (key encryption key, wraps the group key) ‖ TK (temporal key, encrypts unicast data with CCMP).
  • The AP also holds the GTK (group temporal key) for broadcast and multicast frames, and with PMF the IGTK.

The lab

Press play to see the four messages, the key derivation and what an observer in radio range collects. The second setting switches to WPA3-SAE, which adds two exchanges before the 4-way handshake (WPA3-SAE).

client (supplicant)SPA 02:…:02access pointAA 02:…:01 "LabNet"M1ANonce 82879b97…400eM2SNonce 491394f0…522bMIC(KCK)M3ANonceMIC(KCK){GTK}KEKM4MIC(KCK)data{frames}TKpassphrase + SSID↓ PBKDF2-HMAC-SHA1 4096 iterationsPMK (256 bits)↓ PRF-384 with MACs + noncesPTK = KCK‖KEK‖TKKCK: MIC keyKEK: wraps GTKTK: encrypts dataobserver capturedSSID, AA, SPAM1: ANonceM2: SNonce + MICM3: MICWith M1 + M2 captured, any guess can be tested offline: guess → PMK → PTK → MIC.A weak passphrase falls to a word list, a long random one does not. SAE ends this.
Message Direction Carries Proves / delivers
M1 AP → client ANonce, replay counter r nothing yet (no MIC); the client can now compute the PTK
M2 client → AP SNonce, the client's RSN element, MIC under KCK the client knows the PMK; the AP computes the PTK and checks the MIC
M3 AP → client ANonce, replay counter r+1, Install, MIC, the GTK encrypted with KEK the AP knows the PMK; the client gets the GTK; the AP's RSN element is repeated so a downgrade in the beacon is noticed
M4 client → AP MIC confirmation; both install TK

Note what is not in any frame: the PMK, the PTK or the passphrase. Only the group keys (the GTK, and with PMF the IGTK) are encrypted inside the handshake; everything else is in the clear, protected by MICs.

EAPOL-Key frames byte by byte

The handshake travels as EAPOL-Key frames (IEEE 802.1X EAPOL packet type 3) inside 802.11 data frames, with the LLC/SNAP type 88 8E. The fields, in network byte order: descriptor type (2 = RSN), Key Information (2 bytes of flags), key length, replay counter (8), key nonce (32), key IV (16), key RSC (8), reserved (8), MIC (16), key data length (2), key data.

The Key Information flags tell the messages apart: bits 0–2 are the descriptor version (2 = HMAC-SHA1 MIC and AES key wrap), then Pairwise (0x0008), Install (0x0040), Ack (0x0080), MIC (0x0100), Secure (0x0200), Encrypted Key Data (0x1000). The program builds the four messages with placeholder nonces and MICs, then parses them as a capture tool does:

python · try it yourself
import struct

# Key Information bits (IEEE 802.11 EAPOL-Key frame)
BITS = {0x0008: "Pairwise", 0x0040: "Install", 0x0080: "Ack", 0x0100: "MIC",
        0x0200: "Secure", 0x1000: "EncKeyData"}

def eapol_key(key_info, replay, nonce=bytes(32), mic=bytes(16), key_data=b""):
    body = struct.pack(">BHH", 2, key_info, 16) + struct.pack(">Q", replay) + nonce
    body += bytes(16) + bytes(8) + bytes(8) + mic + struct.pack(">H", len(key_data)) + key_data
    return struct.pack(">BBH", 2, 3, len(body)) + body      # 802.1X version 2, type 3 = EAPOL-Key

def parse(frame):
    ver, ptype, length = struct.unpack_from(">BBH", frame, 0)
    desc, info, keylen, replay = struct.unpack_from(">BHHQ", frame, 4)
    nonce = frame[17:49]
    mic = frame[81:97]
    kd_len, = struct.unpack_from(">H", frame, 97)
    flags = [n for b, n in BITS.items() if info & b]
    return info & 7, flags, replay, nonce, mic, kd_len

def which(flags, nonce_zero):
    f = set(flags)
    if "Ack" in f and "MIC" not in f: return "M1"
    if "Ack" in f and "Install" in f: return "M3"
    if "MIC" in f and "Secure" not in f: return "M2"
    if "MIC" in f and "Secure" in f and nonce_zero: return "M4"
    return "?"

ANONCE = bytes.fromhex("aa" * 32)            # placeholders; real nonces are fresh 32-byte values from a CSPRNG
SNONCE = bytes.fromhex("55" * 32)
MIC = bytes.fromhex("11" * 16)               # placeholder MIC; the next lesson computes a real one
handshake = [
    ("AP -> STA", eapol_key(0x008a, 1, ANONCE)),
    ("STA -> AP", eapol_key(0x010a, 1, SNONCE, MIC, key_data=bytes(22))),          # RSN element of the client
    ("AP -> STA", eapol_key(0x13ca, 2, ANONCE, MIC, key_data=bytes(56))),          # wrapped GTK
    ("STA -> AP", eapol_key(0x030a, 2, bytes(32), MIC)),
]
for direction, fr in handshake:
    ver, flags, replay, nonce, mic, kd = parse(fr)
    name = which(flags, nonce == bytes(32))
    print(f"{name} {direction}  len {len(fr):3}  desc v{ver}  replay {replay}  nonce {nonce[:4].hex()}..  "
          f"MIC {'set' if mic != bytes(16) else 'none'}  keydata {kd:2}  [{' '.join(flags)}]")
M1 AP -> STA  len  99  desc v2  replay 1  nonce aaaaaaaa..  MIC none  keydata  0  [Pairwise Ack]
M2 STA -> AP  len 121  desc v2  replay 1  nonce 55555555..  MIC set  keydata 22  [Pairwise MIC]
M3 AP -> STA  len 155  desc v2  replay 2  nonce aaaaaaaa..  MIC set  keydata 56  [Pairwise Install Ack MIC Secure EncKeyData]
M4 STA -> AP  len  99  desc v2  replay 2  nonce 00000000..  MIC set  keydata  0  [Pairwise MIC Secure]

Reading it the way an analyst does:

  • M1 and M3 carry the Ack bit (the AP expects an answer); M3 also has Install, Secure and an encrypted key data field (56 bytes: the wrapped GTK plus framing).
  • M2 and M4 come from the client and carry a MIC; M4 has the Secure bit and an all-zero nonce.
  • The replay counter grows with each AP request (1, then 2), and the client echoes it. A client must reject an M1 or M3 whose counter isn't larger than the last one it accepted. How implementations handled a retransmitted M3 is exactly where KRACK found its bug (KRACK).

Why each part is there

Element Attack it prevents
fresh nonces from both sides replaying an old handshake gives an old, useless PTK; every session gets new keys
MIC on M2 an impostor client without the PMK can't complete the handshake
MIC on M3 an impostor AP (evil twin) without the PMK can't make the client install keys
RSN element echoed in M2 and M3 an attacker who edits beacons to offer weaker ciphers is detected (downgrade protection)
replay counter stops old M1/M3 frames from being replayed
GTK under KEK only clients with the right PTK get the broadcast key

The handshake authenticates knowledge of the PMK, and that is its limit. In WPA2-Personal every client shares the passphrase, hence the PMK: any legitimate member of the network can run a perfect evil twin, and with a captured handshake an outsider can test passphrase guesses offline. Both problems are taken up in later lessons.

How handshake problems are detected

APs log failed handshakes. When M2's MIC doesn't verify, the client used a different PMK: hostapd reports AP-STA-POSSIBLE-PSK-MISMATCH. One device failing twice is a user who typed the passphrase wrongly; many failures from many MAC addresses in a few minutes is online guessing, someone trying passphrases against the live AP while changing their MAC address. The log below is synthetic:

python · try it yourself
from collections import defaultdict
log = """2026-01-15T10:00:01 wlan0: AP-STA-CONNECTED 02:11:22:33:44:55
2026-01-15T10:02:10 wlan0: AP-STA-POSSIBLE-PSK-MISMATCH 02:aa:bb:cc:dd:01
2026-01-15T10:02:12 wlan0: AP-STA-POSSIBLE-PSK-MISMATCH 02:aa:bb:cc:dd:01
2026-01-15T10:02:15 wlan0: AP-STA-CONNECTED 02:aa:bb:cc:dd:01
2026-01-15T10:05:00 wlan0: AP-STA-POSSIBLE-PSK-MISMATCH 02:aa:bb:cc:dd:02
2026-01-15T10:05:02 wlan0: AP-STA-POSSIBLE-PSK-MISMATCH 02:aa:bb:cc:dd:03
2026-01-15T10:05:04 wlan0: AP-STA-POSSIBLE-PSK-MISMATCH 02:aa:bb:cc:dd:04
2026-01-15T10:05:06 wlan0: AP-STA-POSSIBLE-PSK-MISMATCH 02:aa:bb:cc:dd:05
2026-01-15T10:05:08 wlan0: AP-STA-POSSIBLE-PSK-MISMATCH 02:aa:bb:cc:dd:06""".splitlines()
windows = defaultdict(lambda: {"fails": 0, "macs": set()})
for line in log:
    ts, _, event, mac = line.split()
    if event == "AP-STA-POSSIBLE-PSK-MISMATCH":
        minute = int(ts[14:16])
        w = windows[f"{ts[11:13]}:{minute - minute % 5:02d}"]      # 5-minute windows
        w["fails"] += 1
        w["macs"].add(mac)
for name, w in sorted(windows.items()):
    alert = w["fails"] >= 5 and len(w["macs"]) >= 3
    print(f"{name}  {w['fails']} failures from {len(w['macs'])} MAC(s)"
          + ("  ALERT: guessing with changing MACs?" if alert else "  (one device, wrong passphrase typed?)"))
10:00  2 failures from 1 MAC(s)  (one device, wrong passphrase typed?)
10:05  5 failures from 5 MAC(s)  ALERT: guessing with changing MACs?

Other signals: repeated M1/M3 retransmissions to one client (radio problems, or an attacker interfering), handshakes from clients that never complete DHCP, and a burst of reconnections right after a deauthentication flood (PMF).

How the handshake is protected in practice

  • Patch clients and APs: the KRACK fixes (2017) changed how retransmitted messages are handled.
  • Use PMF, so forged deauthentications can't force repeated handshakes.
  • Use WPA3-SAE or Enterprise, where a captured handshake gives no offline test and each user has their own credentials.
  • Alert on PSK-mismatch bursts and consider temporary blocking per MAC (knowing MACs can be changed).

Pitfalls

  • Thinking the handshake "sends the password encrypted": no secret travels; the MICs prove knowledge of the PMK.
  • Treating a successful handshake as proof that the AP is genuine in WPA2-Personal: everyone with the passphrase can impersonate it.
  • Parsing EAPOL fields with the wrong byte order: they are big-endian, unlike the little-endian 802.11 header.

Practice

1. Which message lets the AP verify the client, and what does it check?

M2. With SNonce from M2 the AP computes the PTK and verifies the MIC over M2 with the KCK. A valid MIC means the client used the same PMK.

2. A capture shows M1 and M2 but no M3. Name two explanations.

The MIC in M2 failed (wrong passphrase: the AP stops, and logs a PSK mismatch), or the frames after M2 were lost or not captured (the capture device was out of range). Check the AP log to tell them apart.

3. Extend the parser to flag an M3 whose replay counter isn't larger than M1's.

Keep last = None; for M1 store its replay counter; for M3, if replay <= last, print a warning. In the sample data M3 has 2 and M1 has 1, so nothing is flagged; change M3's counter to 1 to see the warning.

Test yourself

What does the MIC in M2 prove?

  1. That the AP is genuine
  2. That the client derived the PTK from the same PMK
  3. That the passphrase is strong
  4. That the GTK was received

Which key is encrypted inside the 4-way handshake?

  1. The PMK
  2. The PTK
  3. The GTK, wrapped with the KEK in M3
  4. The passphrase

Which Key Information flags identify M1?

  1. MIC and Secure
  2. Pairwise and Ack, without MIC
  3. Install and Secure
  4. Only Secure

Many PSK-mismatch events from different MAC addresses in a few minutes most likely indicate

  1. A firmware update
  2. Online passphrase guessing with changing MACs
  3. Normal roaming
  4. A DHCP problem