基于Linux的Python实现ARP检测防护软件设计模块包含能够获取局域网活动主机的MAC地址、IP地址能够检测防护ARP攻击行为并记录保存能够显示出ARP攻击源的MAC地址、IP地址。
实现思路:
-
使用Python的scapy库实现ARP协议的数据包构造和解析,通过发送ARP请求获取局域网内活动主机的MAC地址和IP地址。
-
监听ARP协议数据包,对收到的ARP数据包进行解析和分析,检测是否存在ARP攻击行为,如ARP欺骗、ARP投毒等攻击。
-
对检测到的ARP攻击行为进行记录和保存,记录攻击源的MAC地址、IP地址等信息。
-
实现一个简单的图形化界面,显示ARP攻击源的MAC地址和IP地址,方便用户进行查看和分析。
模块设计:
- 获取局域网活动主机的MAC地址、IP地址模块:
使用Python的scapy库实现ARP协议的数据包构造和解析,通过发送ARP请求获取局域网内活动主机的MAC地址和IP地址。
- 检测防护ARP攻击行为模块:
使用Python的scapy库监听ARP协议数据包,对收到的ARP数据包进行解析和分析,检测是否存在ARP攻击行为,如ARP欺骗、ARP投毒等攻击。
- 记录保存ARP攻击信息模块:
对检测到的ARP攻击行为进行记录和保存,记录攻击源的MAC地址、IP地址等信息。
- 显示ARP攻击源的MAC地址、IP地址模块:
实现一个简单的图形化界面,显示ARP攻击源的MAC地址和IP地址,方便用户进行查看和分析。
总体流程:
-
启动程序,进行局域网扫描,获取活动主机的MAC地址和IP地址。
-
监听ARP协议数据包,对收到的ARP数据包进行解析和分析,检测是否存在ARP攻击行为。
-
如果检测到ARP攻击行为,记录攻击源的MAC地址、IP地址等信息。
-
在图形化界面上显示ARP攻击源的MAC地址和IP地址。
-
用户可通过图形化界面进行查看和分析ARP攻击源的信息。
参考代码:
- 获取局域网活动主机的MAC地址、IP地址模块:
from scapy.all import ARP, Ether, srp
def get_active_hosts(ip_range):
active_hosts = []
for ip in ip_range:
arp = ARP(pdst=ip)
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
packet = ether/arp
result = srp(packet, timeout=3, verbose=0)[0]
if len(result) > 0:
active_hosts.append({'ip': result[0][1].psrc, 'mac': result[0][1].hwsrc})
return active_hosts
- 检测防护ARP攻击行为模块:
from scapy.all import ARP, sniff
def arp_sniffer_callback(packet):
if ARP in packet and packet[ARP].op in (1, 2):
arp_cache = {}
arp_cache['src_mac'] = packet[ARP].hwsrc
arp_cache['src_ip'] = packet[ARP].psrc
arp_cache['dst_mac'] = packet[ARP].hwdst
arp_cache['dst_ip'] = packet[ARP].pdst
# 检测是否存在ARP攻击行为
if arp_cache['src_mac'] != get_mac_by_ip(arp_cache['src_ip']):
print('ARP欺骗攻击!攻击源MAC地址:', arp_cache['src_mac'], '攻击源IP地址:', arp_cache['src_ip'])
if arp_cache['dst_mac'] != get_mac_by_ip(arp_cache['dst_ip']):
print('ARP投毒攻击!攻击目标MAC地址:', arp_cache['dst_mac'], '攻击目标IP地址:', arp_cache['dst_ip'])
def arp_sniffer():
sniff(prn=arp_sniffer_callback, filter="arp", store=0)
- 记录保存ARP攻击信息模块:
def save_arp_attack_info(arp_cache):
with open('arp_attack_info.txt', 'a') as f:
f.write('攻击源MAC地址:' + arp_cache['src_mac'] + ' 攻击源IP地址:' + arp_cache['src_ip'] + '\n')
- 显示ARP攻击源的MAC地址、IP地址模块:
import tkinter as tk
class App:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.frame.pack()
self.text = tk.Text(self.frame, height=20, width=50)
self.text.pack()
self.update_text()
def update_text(self):
with open('arp_attack_info.txt', 'r') as f:
content = f.read()
self.text.delete(1.0, tk.END)
self.text.insert(tk.END, content)
self.master.after(1000, self.update_text)
if __name__ == '__main__':
root = tk.Tk()
app = App(root)
root.mainloop()
完整代码:
from scapy.all import ARP, Ether, srp, sniff
import tkinter as tk
def get_active_hosts(ip_range):
active_hosts = []
for ip in ip_range:
arp = ARP(pdst=ip)
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
packet = ether/arp
result = srp(packet, timeout=3, verbose=0)[0]
if len(result) > 0:
active_hosts.append({'ip': result[0][1].psrc, 'mac': result[0][1].hwsrc})
return active_hosts
def get_mac_by_ip(ip):
for host in active_hosts:
if host['ip'] == ip:
return host['mac']
return None
def arp_sniffer_callback(packet):
if ARP in packet and packet[ARP].op in (1, 2):
arp_cache = {}
arp_cache['src_mac'] = packet[ARP].hwsrc
arp_cache['src_ip'] = packet[ARP].psrc
arp_cache['dst_mac'] = packet[ARP].hwdst
arp_cache['dst_ip'] = packet[ARP].pdst
# 检测是否存在ARP攻击行为
if arp_cache['src_mac'] != get_mac_by_ip(arp_cache['src_ip']):
print('ARP欺骗攻击!攻击源MAC地址:', arp_cache['src_mac'], '攻击源IP地址:', arp_cache['src_ip'])
save_arp_attack_info(arp_cache)
if arp_cache['dst_mac'] != get_mac_by_ip(arp_cache['dst_ip']):
print('ARP投毒攻击!攻击目标MAC地址:', arp_cache['dst_mac'], '攻击目标IP地址:', arp_cache['dst_ip'])
save_arp_attack_info(arp_cache)
def arp_sniffer():
sniff(prn=arp_sniffer_callback, filter="arp", store=0)
def save_arp_attack_info(arp_cache):
with open('arp_attack_info.txt', 'a') as f:
f.write('攻击源MAC地址:' + arp_cache['src_mac'] + ' 攻击源IP地址:' + arp_cache['src_ip'] + '\n')
class App:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.frame.pack()
self.text = tk.Text(self.frame, height=20, width=50)
self.text.pack()
self.update_text()
def update_text(self):
with open('arp_attack_info.txt', 'r') as f:
content = f.read()
self.text.delete(1.0, tk.END)
self.text.insert(tk.END, content)
self.master.after(1000, self.update_text)
if __name__ == '__main__':
ip_range = ['192.168.1.' + str(i) for i in range(1, 255)]
active_hosts = get_active_hosts(ip_range)
print(active_hosts)
arp_sniffer()
root = tk.Tk()
app = App(root)
root.mainloop()
``
原文地址: https://www.cveoy.top/t/topic/ffzl 著作权归作者所有。请勿转载和采集!