ESP32-S3 WiFi Deauth检测方案:无线安全防护实践
Deauth攻击物联网设备的隐形杀手你的ESP32物联网设备突然集体掉线。你检查了路由器、检查了电源、检查了代码——一切正常。但设备就是连不上WiFi。10分钟后设备自动重连成功一切恢复正常。你以为只是网络波动。实际上可能有人在你设备旁边发了几十个小包——Deauth帧。这是一种零成本、零技术的WiFi攻击任何ESP32都能执行。Deauth原理WiFi管理帧Management Frame是明文传输的不需要加密。其中包括Deauthentication Frame告诉客户端你要断开连接Disassociation Frame告诉客户端你已经断开了攻击者伪造一个来自AP路由器的Deauth帧发送给设备设备收到后以为路由器要它断开于是乖乖断开。关键问题802.11管理帧在WPA2下不加密任何人都能伪造。检测方案用ESP32-S3做一个独立的Deauth检测器放在物联网设备附近。检测到Deauth帧后通过MQTT告警上报。硬件只需要一个ESP32-S3开发板不需要额外器件。代码// deauth_detector.ino#includeWiFi.h#includeesp_wifi.h#includeesp_wifi_types.hconstchar*WIFI_SSIDmonitor_network;constchar*MQTT_SERVERmqtt.example.com;constintMQTT_PORT1883;// WiFi数据包回调voidwifi_promiscuous_cb(void*buf,wifi_promiscuous_pkt_type_ttype){if(type!WIFI_PKT_MGMT)return;wifi_promiscuous_pkt_t*pkt(wifi_promiscuous_pkt_t*)buf;wifi_mgmt_frame_hdr_t*hdr(wifi_mgmt_frame_hdr_t*)pkt-payload;// 0x0C Deauth, 0x0A Disassociateif(hdr-frame_control.type0(hdr-frame_control.subtype0x0C||hdr-frame_control.subtype0x0A)){uint8_t*srchdr-address2;uint8_t*dsthdr-address1;uint8_treasonpkt-payload[sizeof(wifi_mgmt_frame_hdr_t)];Serial.printf(ALERT: Deauth detected!\n);Serial.printf( From: %02X:%02X:%02X:%02X:%02X:%02X\n,src[0],src[1],src[2],src[3],src[4],src[5]);Serial.printf( To: %02X:%02X:%02X:%02X:%02X:%02X\n,dst[0],dst[1],dst[2],dst[3],dst[4],dst[5]);Serial.printf( Reason: %d\n,reason);// 触发告警trigger_alert(src,dst,reason);}}voidsetup(){Serial.begin(115200);// 设置WiFi为混杂模式WiFi.mode(WIFI_MODE_NULL);esp_wifi_set_promiscuous(true);esp_wifi_set_promiscuous_rx_cb(wifi_promiscuous_cb);// 设置信道跟被监控设备一致esp_wifi_set_channel(6,WIFI_SECOND_CHAN_NONE);Serial.println(Deauth detector started on channel 6);}voidloop(){// 主循环不做事全靠回调delay(1000);}告警上报voidtrigger_alert(uint8_t*src,uint8_t*dst,uint8_treason){// 切换到STA模式连接WiFi发送告警// 或者通过LoRa/4G模组发送// 简化版通过串口输出由另一个ESP32转发charjson[256];snprintf(json,sizeof(json),{\type\:\deauth\,\src\:\%02X:%02X:%02X:%02X:%02X:%02X\,\dst\:\%02X:%02X:%02X:%02X:%02X:%02X\,\reason\:%d,\ts\:%ld},src[0],src[1],src[2],src[3],src[4],src[5],dst[0],dst[1],dst[2],dst[3],dst[4],dst[5],reason,millis());Serial.printf(ALERT_JSON: %s\n,json);}统计功能加上统计功能记录每小时的Deauth次数#defineSTATS_HOUR24typedefstruct{uint32_tdeauth_count;uint32_tdisassoc_count;time_tlast_alert;}hour_stats_t;hour_stats_tstats[STATS_HOUR];voidupdate_stats(uint8_tsubtype){inthour(millis()/3600000)%STATS_HOUR;if(subtype0x0C){stats[hour].deauth_count;}else{stats[hour].disassoc_count;}stats[hour].last_alertmillis();}// 每小时通过MQTT上报统计voidreport_stats(){charjson[512];// ... 构建JSON并发送}防御方案方案1开启PMF802.11wPMFProtected Management Frames是802.11w标准对管理帧加密。开启后伪造的Deauth帧会被丢弃。ESP32端wifi_config_tconf;memset(conf,0,sizeof(conf));strcpy((char*)conf.sta.ssid,WIFI_SSID);strcpy((char*)conf.sta.password,WIFI_PASS);conf.sta.pmf_cfg.capabletrue;// 支持PMFconf.sta.pmf_cfg.requiredfalse;// 不强制要求兼容旧APesp_wifi_set_config(WIFI_IF_STA,conf);路由器端也要开启PMF/WPA3。大多数2024年后的路由器都支持。效果Deauth帧被加密验证拦截攻击失效。方案2自动重连告警如果路由器不支持PMF至少做到快速恢复// 检测WiFi断连事件WiFi.onEvent([](WiFiEvent_t event,WiFiEventInfo_t info){if(eventSYSTEM_EVENT_STA_DISCONNECTED){uint8_treasoninfo.disconnect_reason;// reason 1 Unspecified// reason 4 Deauth// reason 7 Class frame from non-associatedif(reason1||reason4||reason7){// 可能是Deauth攻击send_alert(wifi_disconnect,reason);}// 立即重连WiFi.reconnect();}},SYSTEM_EVENT_STA_DISCONNECTED);方案3多信道检测攻击者在哪个信道发Deauth检测器就切到哪个信道voidchannel_hop(){staticuint8_tch1;ch;if(ch13)ch1;esp_wifi_set_channel(ch,WIFI_SECOND_CHAN_NONE);}// 每500ms切换一次信道Ticker channel_ticker;channel_ticker.attach(0.5,channel_hop);这样可以覆盖1-13信道不会漏检。实际部署在虎王科技的物联网项目中Deauth检测器部署方案每个设备密集区域放一个检测器检测器通过LoRa/4G上报告警到云平台云平台收到告警后通知运维人员检测器和被保护设备在同一信道实测正常环境每24小时0-2个Deauth帧路由器偶尔发送的正常断开攻击环境每秒5-20个Deauth帧检测器立即告警误报率接近0Deauth帧数量异常时才告警关于合法性检测Deauth帧是合法的——你只是在监听自己的网络环境。发送Deauth帧在大多数国家/地区是违法的——包括中国《网络安全法》明确禁止干扰他人网络。ESP32 Marauder有发送Deauth的功能但我们在实际使用中只用检测功能不主动发送。安全测试应该在自己的设备和网络上进行。总结Deauth攻击是物联网WiFi安全中最容易被忽视的威胁。攻击成本为零一个ESP32就能做防御成本也不高开启PMF或部署检测器。关键措施路由器和设备都开启PMF设备实现断连检测自动重连告警在设备密集区域部署Deauth检测器虎王科技的ESP32 Marauder MicroPython版本包含了Deauth检测功能在GitHub上开源github.com/huwangkeji/ESP32Marauder-MicroPython沧州虎王科技专注物联网软硬件开发、通信设备、嵌入式系统与设备管理平台。