# krack_sim.py - Simulates reinstallation of an already-used PTK
import hashlib, os
class AccessPoint:
def __init__(self):
self.anonce = os.urandom(32)
self.ptk = None
def send_msg3(self, snonce):
# Normally would install PTK, but here we resend msg3 to trigger reinstall
self.ptk = hashlib.sha256(b"PMK" + self.anonce + snonce).digest()
print("Installed PTK")
return self.ptk
class Client:
def __init__(self):
self.snonce = os.urandom(32)
self.ptk = None
def receive_msg3(self, ap):
self.ptk = ap.send_msg3(self.snonce)
# Vulnerability: if AP resends msg3, nonce reuse may reset counters
print("Client installed PTK")
ap = AccessPoint()
client = Client()
client.receive_msg3(ap) # first install
client.receive_msg3(ap) # reinstallation! Nonce reused, replay possible
Wi‑Fi 客户端未能正确跟踪四次握手中的消息 3。攻击者可重放消息 3,导致客户端重装已在使用中的成对瞬态密钥(PTK),并重置 nonce 与重放计数器。这使攻击者能够解密和伪造数据帧。
运行模拟:
python krack_sim.py
该脚本表明 PTK 被重装,凸显了 nonce 重用问题。