WIFI之WPA2四步握手简单分析

一、名词介绍

PMK (Pairwise Master Key)

PTK (Pairwise Transient Key)
Pairwise Transient key is used to encrypt all unicast traffic between a client station and the access point. PTK is unique between a client station and access point. To generate PTK, client device and access point need the following information.
PTK = PRF (PMK + Anonce + SNonce + Mac (AA)+ Mac (SA))
Anonce is a random number generated by an access point (authenticator), Snonce a random number generated by the client device (supplicant). MAC addresses of supplicant (client device) and MAC address of authenticator (access point). PRF is a pseudo-random function which is applied to all the input.


GTK (Group Temporal Key)

二、原理分析

2.1 原理图

 

2.2 从代码层面分析一下交互过程

// AP
//状态机  PTKSTART也就是说要协商ptk了 即ap要向sta发送M1了
SM_STATE(WPA_PTK, PTKSTART)

//关键日志
wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
			"sending 1/4 msg of 4-Way Handshake")

//中间是对 pmkid的处理

//发送 M1 主要发送的是sm->ANonce
wpa_send_eapol(sm->wpa_auth, sm,
		       WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
		       sm->ANonce, pmkid, pmkid_len, 0, 0);
// STA
//sta 处理 M1
wpa_supplicant_process_1_of_4

//关键日志
wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
		"Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);

//sta 获取 pmk
res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);

//sta自己计算ptk 并保存到 M3时校验
/* Calculate PTK which will be stored as a temporary PTK until it has
	 * been verified when processing message 3/4. */
	ptk = &sm->tptk;
	if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
		goto failed;

//sta 准备发送 M2
if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
				       kde, kde_len, ptk) < 0)
		goto failed;

//填充 key data 和 SNonce
key_mic = (u8 *) (reply + 1);
	WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
	os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
	os_free(rsn_ie_buf);

	os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);

//关键日志
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
	return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
				  key_mic);
//关键日志
wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
		   " ver=%d mic_len=%d key_mgmt=0x%x",
		   MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);

//计算 key_mic
if (mic_len) {
		if (key_mic && (!ptk || !ptk->kck_len))
			goto out;

		if (key_mic &&
		    wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
				      msg, msg_len, key_mic)) {
			wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
				"WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
				ver, sm->key_mgmt);
			goto out;
		}
		if (ptk)
			wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
					ptk->kck, ptk->kck_len);
		wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
			    key_mic, mic_len);
	} 

//发送M2
ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);

	if (wpa_s->l2) {
		return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
	}
// AP
//关键日志
wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
		   " key_info=0x%x type=%u mic_len=%zu key_data_length=%u",
		   MAC2STR(sm->addr), key_info, key->type,
		   mic_len, key_data_length);

SM_STATE(WPA_PTK, PTKINITNEGOTIATING)

//关键日志
wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
			"sending 3/4 msg of 4-Way Handshake");

//发送 M3
wpa_send_eapol(sm->wpa_auth, sm,
		       (secure ? WPA_KEY_INFO_SECURE : 0) |
		       (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
			   WPA_KEY_INFO_MIC : 0) |
		       WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
		       WPA_KEY_INFO_KEY_TYPE,
		       _rsc, sm->ANonce, kde, pos - kde, 0, encr);
// STA
//关键日志
wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
		"Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);

//解析校验 IE

//sta 准备发送 M4
	if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
				       &sm->ptk) < 0) {
		goto failed;
	}

//关键日志
	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
	return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
				  key_mic);

// M4 的发送接口和 M2 一样 详见 M2 后半部分

三、抓包分析

M1

802.1x Authentication
  Protocol Version:     2 [34]
  Packet Type:          3  EAPOL - Key [35]
  Body Length:          95 [36-37]
  EAPOL - Key
    Type:               2  RSN key descriptor [38]
    Key Information:    %0000000010001010 [39-40]
                        xx...... ........ Reserved
                        ..0..... ........ SMK Handshake is not supported
                        ...0.... ........ Key Data Not Encrypted
                        ....0... ........ No Request to initiate Handshake
                        .....0.. ........ No Error
                        ......0. ........ Not Secure
                        .......0 ........ Message does NOT contain Key MIC
                        ........ 1....... Key ACK
                        ........ .0...... Install: 802.1X component shall not configure the temporal key
                        ........ ..xx.... Reserved
                        ........ ....1... Key Type: Pairwise Key
                        ........ .....010 Vers: HMAC-SHA1-128 is the EAPOL-Key MIC / NIST AES key wrap is the EAPOL-key enc algorithm
    Key Length:         16  CCMP [41-42]
    Replay Counter:     1 [43-50]
    Key Nonce:          0xE110A1C3123C72426DF5F2DA71880A733C3F55AA424E633BBFA5D1D35028C56A [51-82]
    EAPOL-Key IV:       0x00000000000000000000000000000000 [83-98]
    Key RSC:            0x0000000000000000 [99-106]
    Key ID:             0x0000000000000000 [107-114]
    Key MIC:            0x00000000000000000000000000000000 [115-130]
    Key Data Len:       0 [131-132]

M2

802.1x Authentication
  Protocol Version:     1 [34]
  Packet Type:          3  EAPOL - Key [35]
  Body Length:          117 [36-37]
  EAPOL - Key
    Type:               2  RSN key descriptor [38]
    Key Information:    %0000000100001010 [39-40]
                        xx...... ........ Reserved
                        ..0..... ........ SMK Handshake is not supported
                        ...0.... ........ Key Data Not Encrypted
                        ....0... ........ No Request to initiate Handshake
                        .....0.. ........ No Error
                        ......0. ........ Not Secure
                        .......1 ........ Message contains Key MIC
                        ........ 0....... No Key ACK
                        ........ .0...... Install: 802.1X component shall not configure the temporal key
                        ........ ..xx.... Reserved
                        ........ ....1... Key Type: Pairwise Key
                        ........ .....010 Vers: HMAC-SHA1-128 is the EAPOL-Key MIC / NIST AES key wrap is the EAPOL-key enc algorithm
    Key Length:         0 [41-42]
    Replay Counter:     1 [43-50]
    Key Nonce:          0x8AD1A997AD7F4ABF00FB1FB008EA734D3EABF9BA1B824F754521AC961422761A [51-82]
    EAPOL-Key IV:       0x00000000000000000000000000000000 [83-98]
    Key RSC:            0x0000000000000000 [99-106]
    Key ID:             0x0000000000000000 [107-114]
    Key MIC:            0xD8D3398B2FBA88DDE9158437A3BBDD2F [115-130]
    Key Data Len:       22 [131-132]

M3

802.1x Authentication
  Protocol Version:     2 [34]
  Packet Type:          3  EAPOL - Key [35]
  Body Length:          151 [36-37]
  EAPOL - Key
    Type:               2  RSN key descriptor [38]
    Key Information:    %0001001111001010 [39-40]
                        xx...... ........ Reserved
                        ..0..... ........ SMK Handshake is not supported
                        ...1.... ........ Key Data Encrypted
                        ....0... ........ No Request to initiate Handshake
                        .....0.. ........ No Error
                        ......1. ........ Secure
                        .......1 ........ Message contains Key MIC
                        ........ 1....... Key ACK
                        ........ .1...... Install: 802.1X component shall configure the temporal key
                        ........ ..xx.... Reserved
                        ........ ....1... Key Type: Pairwise Key
                        ........ .....010 Vers: HMAC-SHA1-128 is the EAPOL-Key MIC / NIST AES key wrap is the EAPOL-key enc algorithm
    Key Length:         16  CCMP [41-42]
    Replay Counter:     2 [43-50]
    Key Nonce:          0xE110A1C3123C72426DF5F2DA71880A733C3F55AA424E633BBFA5D1D35028C56A [51-82]
    EAPOL-Key IV:       0x00000000000000000000000000000000 [83-98]
    Key RSC:            0x0000000000000000 [99-106]
    Key ID:             0x0000000000000000 [107-114]
    Key MIC:            0x94A1B736144437AC1309D352C0E93695 [115-130]
    Key Data Len:       56 [131-132]
    Key Data:           (56 bytes) [133-188]

M4

802.1x Authentication
  Protocol Version:     1 [34]
  Packet Type:          3  EAPOL - Key [35]
  Body Length:          95 [36-37]
  EAPOL - Key
    Type:               2  RSN key descriptor [38]
    Key Information:    %0000001100001010 [39-40]
                        xx...... ........ Reserved
                        ..0..... ........ SMK Handshake is not supported
                        ...0.... ........ Key Data Not Encrypted
                        ....0... ........ No Request to initiate Handshake
                        .....0.. ........ No Error
                        ......1. ........ Secure
                        .......1 ........ Message contains Key MIC
                        ........ 0....... No Key ACK
                        ........ .0...... Install: 802.1X component shall not configure the temporal key
                        ........ ..xx.... Reserved
                        ........ ....1... Key Type: Pairwise Key
                        ........ .....010 Vers: HMAC-SHA1-128 is the EAPOL-Key MIC / NIST AES key wrap is the EAPOL-key enc algorithm
    Key Length:         0 [41-42]
    Replay Counter:     2 [43-50]
    Key Nonce:          0x0000000000000000000000000000000000000000000000000000000000000000 [51-82]
    EAPOL-Key IV:       0x00000000000000000000000000000000 [83-98]
    Key RSC:            0x0000000000000000 [99-106]
    Key ID:             0x0000000000000000 [107-114]
    Key MIC:            0x457FCFCD9CADF04EC7F32DCF30835B61 [115-130]
    Key Data Len:       0 [131-132]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值