I was trying to do a program that calculates the PTK from the PMK and the MACs and Nonces of the 4 way handshake.
I tried some things but neither of them give me a correct result.
I tried using the code of https://www.perlmonks.org/?node_id=1090649 and also the code of wpa-handshake with python - hashing difficulties and neither of them is giving the correct answer. Even in some situations they give me so much errors, although i did Control C Control V
Update:
This is the code i'm working
#Used for computing HMAC
import hmac
import binascii
#Used to convert from hex to binary
from binascii import a2b_hex, b2a_hex
#Used for computing PMK
from hashlib import pbkdf2_hmac, sha1, md5
def PRF(key, A, B):
#Number of bytes in the PTK
nByte = 64
i = 0
R = b''
#Each iteration produces 160-bit value and 512 bits are required
while(i <= ((nByte * 8 + 159) / 160)):
hmacsha1 = hmac.new(key, A + B + str(i).encode('ascii'), sha1)
R = R + hmacsha1.digest()
i += 1
return R[0:nByte]
#pwd: The password to test
pwd = "kemparajanusha"
#ssid: The ssid of the AP
ssid = "Praneeth"
aNonce = b"ac9871c9ca129468708ca0d554e22f4f8b6eaa6dbaa121d2233bf33cbc29d346"
sNonce = b"5214c4dbe4a567e78b8f30b2b016a2d90ea50c27d408614c1fc0a0934a889ada"
apMac = b"60E327F814A0"
cliMac = b"C0F4E64B6ACF"
A = b"Pairwise key expansion"
B = (min(apMac, cliMac) + max(apMac, cliMac) + min(aNonce, sNonce) + max(aNonce, sNonce))
pmk = pbkdf2_hmac('sha1', pwd.encode('ascii'), ssid.encode('ascii'), 4096, 32)
ptk = PRF(pmk, A, B)
print("\nPMK (Pairwise Master Key):", binascii.hexlify(pmk).decode())
print ("ptk:\t\t",binascii.b2a_hex(ptk),"\n")
Let's see, the code is bringing me an actual value that might seem correct, but the truth is that's not, let me explain:
I'm working with the values of this guy's block https://praneethwifi.in/2019/11/09/4-way-hand-shake-keys-generation-and-mic-verification/. I copied the nonces, the macs and the password and SSID that shows. The "real" PTK is the one that begins with "fb18" and the start of the output of my code is "39e2".
I've seen that the real output and my output has the same lenght but different value.
Important to tell that the PMK that generates my code it's correct (the same PMK that calculates in the blog)