拒绝成为免费劳动力:检测含有挖矿脚本的WiFi热点
字数 1199 2025-08-18 11:37:02
检测含有挖矿脚本的WiFi热点教学文档
1. 背景介绍
近年来,公共WiFi网络被植入挖矿脚本的事件频发,例如星巴克店内无线网络被发现植入了恶意代码,利用用户设备挖掘门罗币(XMR)。攻击者可以通过MITM(中间人)攻击植入JavaScript代码,使WiFi网络内的所有设备帮助其挖矿。
2. CoinHive挖矿程序
CoinHive是一个提供门罗币挖掘JS脚本的平台,攻击者可以将其脚本植入网站中。当用户访问网页加载JS后,便会利用用户设备的运算资源挖掘门罗币。
2.1 CoinHive特点
- 提供多种部署方式:JS代码、人机验证、Wordpress插件等
- 部署简单,只需将示例代码放入网站的html中
- 可通过将
authedmine.min.js替换为coinhive.min.js来隐藏用户提示
3. 开放式WiFi网络的特性
开放式WiFi网络(无密码)存在两大安全威胁:
- 攻击者可轻易建立同名钓鱼WiFi(客户端会自动连接)
- 通信数据未加密容易被嗅探
虽然WPA3将添加对开放式WiFi的通信数据加密,但在WPA3普及前,这类攻击仍会长期存在。
4. 检测原理
利用开放式WiFi"通信数据未加密"的特性,通过监听明文的802.11数据帧,当发现挖矿脚本特征时进行告警。
5. 检测工具实现
5.1 搭建测试热点(可选)
为了测试检测工具,可以先建立一个包含挖矿代码的开放式WiFi网络:
方法一:完整方案
- 使用无线网卡Hostapd建立软AP
- Dnsmasq提供DHCP及DNS服务
- 本地Nginx提供Web服务并植入CoinHive代码
- 通过iptables配置Captive Portal(强制认证登陆页面)
方法二:简化方案
- 使用随身WiFi或家庭路由器建立热点
- 配置认证页面指向本地Web服务(或手动访问网页)
5.2 监听明文802.11数据帧
- 将无线网卡配置为Monitor模式:
ifconfig wlan0 down
iwconfig wlan0 mode monitor
ifconfig wlan0 up
iwconfig wlan0 channel 11
- 使用Wireshark观察802.11帧,过滤HTTP Response包:
http.response
- 过滤CoinHive特征代码:
data-text-lines contains CoinHive.Anonymous
- 从wlan.sa字段获取热点MAC地址,结合Beacon或Probe帧获取热点名称
5.3 使用Scapy编写恶意热点识别框架
5.3.1 安装依赖
sudo apt install python-pip
pip install scapy
pip install scapy_http
5.3.2 获取热点列表
from scapy.all import *
from scapy.layers import http
iface = "wlan0"
ap_dict = {}
def BeaconHandler(pkt):
if pkt.haslayer(Dot11):
if pkt.type == 0 and pkt.subtype == 8:
if pkt.addr2 not in ap_dict.keys():
ap_dict[pkt.addr2] = pkt.info
sniff(iface=iface, prn=BeaconHandler, timeout=1)
5.3.3 监听含有关键字的HTTP数据包
filter_response = "tcp src port 80"
def HTTPHandler(pkt):
if pkt.haslayer('HTTP'):
if "CoinHive.Anonymous" in pkt.load:
mac = pkt.addr2
if mac in ap_dict.keys():
ssid = ap_dict[mac]
reason = "Coinhive_miner"
print "Find Rogue AP: %s(%s) -- %s" %(ssid, mac, reason)
else:
print mac
sniff(iface=iface, prn=HTTPHandler, filter=filter_response, timeout=5)
5.3.4 监听模式及信道切换
import os
print "[+] Set iface %s to monitor mode" %(iface)
os.system("ifconfig " + iface + " down")
os.system("iwconfig " + iface + " mode monitor")
os.system("ifconfig " + iface + " up")
channels = [1,6,11] # 2.4GHz中互不干扰的三个主要信道
print "[+] Sniffing on channel " + str(channels)
while True:
for channel in channels:
os.system("iwconfig " + iface + " channel " + str(channel))
# 在此处添加监听代码
6. 扩展检测规则
可以在HTTPHandler函数中扩展更多的检测规则,例如:
def HTTPHandler(pkt):
if pkt.haslayer('HTTP'):
http_load = pkt.load
if "CoinHive.Anonymous" in http_load:
# 检测CoinHive挖矿脚本
reason = "Coinhive_miner"
elif "cryptonight" in http_load.lower():
# 检测其他使用cryptonight算法的挖矿脚本
reason = "Cryptonight_miner"
elif "webassembly" in http_load.lower() and "mine" in http_load.lower():
# 检测使用WebAssembly的挖矿脚本
reason = "WASM_miner"
if reason:
mac = pkt.addr2
if mac in ap_dict.keys():
ssid = ap_dict[mac]
print "Find Rogue AP: %s(%s) -- %s" %(ssid, mac, reason)
else:
print mac
7. 防御措施
- 使用VPN加密所有网络流量
- 安装广告拦截插件(如Adblock),可屏蔽已知挖矿脚本
- 避免连接开放式WiFi,或使用移动数据网络
- 定期检查设备CPU使用率,异常高使用率可能是挖矿脚本在运行
- 使用本文介绍的检测工具定期扫描周围WiFi网络
8. 完整代码参考
完整实现代码可参考原作者的GitHub仓库(文中提到的链接)。