目录

  • 介绍
  • 普通版
    • 代码
    • GIF演示
  • plus版
    • 代码
    • GIF演示
  • 更多
  • 防御
    • 电脑管家

介绍

最近发现火绒的IP黑名单功能不能防护ARP攻击,只能防护二层以上的网络行为,就花了点时间开发了一个自动化工具,用来检测自己是否遭受了ARP欺骗攻击。我这里开发了2个脚本,普通版和plus版,两者的区别就是后者支持了取证功能。

普通版

代码

脚本介绍:

  1. 脚本每隔一段时间(这里我是设置为30分钟)检查一次ARP表,如果发现存在相同的MAC地址,就意味着极有可能发生了ARP欺骗攻击
  2. 一旦检测到MAC地址重复,脚本就会查询重复mac地址对应的网卡名称,以弹窗的方式展示给用户
  3. 当用户关闭了弹窗,脚本会把可疑的ARP记录和所有的ARP记录,都存放到用户的桌面上,方便用户查看
  4. 我是使用了pyinstaller打包脚本来演示的:pyinstaller.exe -F .\反ARP欺骗.py -w
import re
from subprocess import PIPE, Popen
import tkinter.messagebox
import os
import time# 根据获取到的IP网卡地址,获知对应的网卡名字
def ip_to_name(ip):command1 = r'ipconfig'interface_name1 = ''  # 获取到网卡的名字p = Popen(command1, stdout=PIPE, stderr=PIPE)stdout1, stderr1 = p.communicate()ipconfig = stdout1.decode('gbk')a = ipconfig.split('网适配器')del (a[0])for i1 in a:if '断开连接' not in i1 and ip in i1:interface_msg = i1.split('子网掩码')[0]  # 获取每个网卡的网络信息,断开连接的网卡不予展示interface_name1 = interface_msg.split('连接特定的 DNS 后缀')[0].replace(':', '').strip()  # 获取到网卡的名字return interface_name1def work():desktop_path = os.path.join(os.path.expanduser("~"), 'Desktop')  # 获取桌面路径path_result = os.path.join(desktop_path, 'evil_arp.txt')  # 用来存储终端中输出的内容到一个文本中,方便用户使用数据,此为文本路径path_result1 = os.path.join(desktop_path, 'arp.txt')  # 用来存储终端中输出的内容到一个文本中,方便用户使用数据,此为文本路径evil_arp = ''  # 用来记录所有的可疑arp表项flag = 0command = r'arp -a'p = Popen(command, stdout=PIPE, stderr=PIPE)stdout, stderr = p.communicate()arp_form = stdout.decode('gbk')arp_list = arp_form.split('接口:')for i2 in arp_list:  # 把ARP表按照网卡进行切分ip_and_mac = {}  # 用来存放arp小表中的IP和MAC地址mac_list = []  # 用来存储一个ARP小表中的所有mac,借此检查有无重复的mac,进而得知ARP攻击interface_arp = i2.split('---')[0].strip()  # 获取每个ARP小表的网卡地址# 从每一个ARP小表中提取出IP地址和MAC地址,将其绑定为字典obj = re.compile(r"(?P<ip>(\d+\x2e){3}\d+)\s+(?P<mac>(\w\w-){5}\w\w)", re.S)result = obj.finditer(i2)for i3 in result:# print(i3.group('ip'), i3.group('mac'), sep=' ')ip_and_mac[i3.group('ip')] = i3.group('mac')# 判断有误重复的MAC地址for i4 in ip_and_mac.values():if i4 == 'ff-ff-ff-ff-ff-ff':passelse:if i4 not in mac_list:mac_list.append(i4)else:flag = 1interface_name = ip_to_name(interface_arp)  # 获知网卡名字repeat_ip = [k for k, v in ip_and_mac.items() if v == i4]  # 获知重复MAC对应的IPmsg = ''for ri in repeat_ip:msg += ri + '\t' + i4 + '\n'alert_msg = interface_name + '网卡 发现ARP攻击!\n' + msgevil_arp += alert_msg + '\n'tkinter.messagebox.showerror('ARP攻击!ARP记录已存放至桌面!', alert_msg)# 导出结果if flag:# 导出所有的ARP记录a = open(path_result, 'w', encoding='utf8')for r in evil_arp:a.write(r)a.close()# 导出可疑的ARP记录arp_record = r'arp -a > ' + path_result1os.system(arp_record)if __name__ == '__main__':'''脚本功能:每隔一段时间查询一次ARP表,一旦发现ARP异常,就会导出结果到桌面'''while True:work()time.sleep(30*60)  # 每隔30分钟运行一次

GIF演示

plus版

代码

脚本介绍:这个脚本相较于普通版,就是添加了一个取证的功能,一旦检测到ARP欺骗,脚本会调用tshark开启抓包20秒,数据包的名字就是出现ARP欺骗的网卡的名字。(tshark在我之前的文章中已经多次运用介绍过,这里不再赘述)
打包成exe的命令:pyinstaller.exe -F .\反ARP欺骗PLUS版.py -w

import re
from subprocess import PIPE, Popen
import tkinter.messagebox
import os
import time# 根据获取到的IP网卡地址,获知对应的网卡名字
def ip_to_name(ip):command1 = r'ipconfig'interface_name1 = ''  # 获取到网卡的名字p = Popen(command1, stdout=PIPE, stderr=PIPE)stdout1, stderr1 = p.communicate()ipconfig = stdout1.decode('gbk')a = ipconfig.split('网适配器')del (a[0])for i1 in a:if '断开连接' not in i1 and ip in i1:interface_msg = i1.split('子网掩码')[0]  # 获取每个网卡的网络信息,断开连接的网卡不予展示interface_name1 = interface_msg.split('连接特定的 DNS 后缀')[0].replace(':', '').strip()  # 获取到网卡的名字return interface_name1# 开启取证抓包
def qu_zheng(interface_name):desktop_path = os.path.join(os.path.expanduser("~"), 'Desktop')  # 获取桌面路径pcap_name = interface_name + '.pcap'path_pcap = os.path.join(desktop_path, pcap_name)path_pcap = '"' + path_pcap + '"'command = r'tshark.exe -D'p = Popen(command, stdout=PIPE, stderr=PIPE)stdout, stderr = p.communicate()tshark = stdout.decode('utf-8')tshark = tshark.split(')')for i in tshark:if interface_name in i:interface_code = i.split('(')[0].split('.')[-1].strip()  # 网卡对应的接口名称os.system('tshark.exe -i ' + interface_code + ' -f arp -a duration:20 -w ' + path_pcap)  # 抓包20秒# 检测ARP攻击
def work():desktop_path = os.path.join(os.path.expanduser("~"), 'Desktop')  # 获取桌面路径path_result = os.path.join(desktop_path, 'evil_arp.txt')  # 用来存储终端中输出的内容到一个文本中,方便用户使用数据,此为文本路径path_result1 = os.path.join(desktop_path, 'arp.txt')  # 用来存储终端中输出的内容到一个文本中,方便用户使用数据,此为文本路径evil_arp = ''  # 用来记录所有的可疑arp表项flag = 0interface_name = ''  # 网卡的名字evil_interface_name = []  # 用来存储所有出现ARP欺骗问题的网卡command = r'arp -a'p = Popen(command, stdout=PIPE, stderr=PIPE)stdout, stderr = p.communicate()arp_form = stdout.decode('gbk')arp_list = arp_form.split('接口:')for i2 in arp_list:  # 把ARP表按照网卡进行切分ip_and_mac = {}  # 用来存放arp小表中的IP和MAC地址mac_list = []  # 用来存储一个ARP小表中的所有mac,借此检查有无重复的mac,进而得知ARP攻击interface_arp = i2.split('---')[0].strip()  # 获取每个ARP小表的网卡地址# 从每一个ARP小表中提取出IP地址和MAC地址,将其绑定为字典obj = re.compile(r"(?P<ip>(\d+\x2e){3}\d+)\s+(?P<mac>(\w\w-){5}\w\w)", re.S)result = obj.finditer(i2)for i3 in result:# print(i3.group('ip'), i3.group('mac'), sep=' ')ip_and_mac[i3.group('ip')] = i3.group('mac')# 判断有误重复的MAC地址for i4 in ip_and_mac.values():if i4 == 'ff-ff-ff-ff-ff-ff':passelse:if i4 not in mac_list:mac_list.append(i4)else:flag = 1interface_name = ip_to_name(interface_arp)  # 获知网卡名字evil_interface_name.append(interface_name)repeat_ip = [k for k, v in ip_and_mac.items() if v == i4]  # 获知重复MAC对应的IPmsg = ''for ri in repeat_ip:msg += ri + '\t' + i4 + '\n'alert_msg = interface_name + '网卡 发现ARP攻击!\n' + msgevil_arp += alert_msg + '\n'tkinter.messagebox.showerror('ARP攻击!ARP记录已存放至桌面!', alert_msg)# 导出结果if flag:# 导出所有的ARP记录a = open(path_result, 'w', encoding='utf8')for r in evil_arp:a.write(r)a.close()# 导出可疑的ARP记录arp_record = r'arp -a > ' + path_result1os.system(arp_record)# 这里走个形式,网络上一般就一个网卡会存在ARP欺骗,为了满足实验的需求,这里检查所有的网卡for i in evil_interface_name:qu_zheng(i)if __name__ == '__main__':'''脚本功能:每隔一段时间查询一次ARP表,一旦发现ARP异常,就会导出结果到桌面,并且抓包取证'''while True:work()time.sleep(30*60)

GIF演示

更多

由于打包成exe的时候,使用了-w,因此工具会一直运行在后台,打开任务管理器才能关闭工具。

防御

在开发脚本的时候我想了很多防御的姿势,但最终结果证明,个人电脑最好的防御措施,要么是采用"ARP双向绑定"的方法,要么是使用电脑管家(自带ARP防火墙的那种),别的找到什么好的办法。如果读者有好的思路的话,欢迎交流分享。

电脑管家

测试发现电脑管家的能力也各不相同,如下图
腾讯电脑管家

360

python实现——ARP攻击检测相关推荐

  1. 安全测试(四) Wi-Fi安全 渗透测试 网络安全 公共场的免费Wi-Fi真的安全吗?个人信息被监控窃听风险 如: ARP攻击 SSL攻击 DNS安全 加密方式等 枚举实战应用案例 信息窃听获取

    文章目录 一.前言 二.Wi-Fi 安全知识普及 2.1 Wi-Fi(Wireless Fidelity)是什么? 2.2 Wi-Fi 安全吗? 2.3 Wi-Fi 安全加密方式介绍 2.3.1  W ...

  2. 这里介绍个arp攻击工具

    网络神兵 WinArpAttacker 3.7 比网络终结者_P2P终结者_网络执法官_聚生网管等强 是网管的一个好工具,可以查看攻击者的IP地址及MAC地址及其攻击行为,还有自我保护功能,自动解除A ...

  3. 校园网ARP攻击的防御

    2013年5月21日下午至5月22日,ARP攻击校园网,导致全校大面积不了上网,领导急,压力大,整理下文章. 由于ARP欺骗攻击,利用了ARP协议的设计缺陷,光靠包过滤.IP+MAC+端口绑定等传统办 ...

  4. python实现局域网攻击软件_使用python的scapy库进行局域网内的断网攻击(基于ARP协议)...

    (使用python scapy库进行网络攻击) ARP协议 ARP协议的基本功能就是通过目标设备的IP地址,查询目标设备的MAC地址,以保证通信的进行. ARP攻击的局限性: ARP攻击仅能在以太网( ...

  5. python arp脚本_Python scapy 实现一个简易 arp 攻击脚本

    scapy是python写的一个功能强大的交互式数据包处理程序,可用来发送.嗅探.解析和伪造网络数据包,常常被用到网络攻击和测试中. scapy的安装在Linux非常便利,但在Windows下比较复杂 ...

  6. python网站攻击脚本_Python scapy 实现一个简易 arp 攻击脚本

    scapy是python写的一个功能强大的交互式数据包处理程序,可用来发送.嗅探.解析和伪造网络数据包,常常被用到网络攻击和测试中. scapy的安装在Linux非常便利,但在Windows下比较复杂 ...

  7. python对局域网所有人进行arp攻击

    python使用ARP进行同一局域网所有人进行攻击** 攻击原理 arp作为一个底层协议,我们使用他进行攻击可以避免任何杀毒软件,最关键的就是这种攻击方式简单,并且对局域网造成的伤害也是极高,可以盗取 ...

  8. python电脑攻击_python写的ARP攻击代码实例 -电脑资料

    注:使用这个脚本需要安装scapy 包 最好在linux平台下使用,因为scapy包在windows上安装老是会有各种问题 复制代码代码如下: #coding:utf-8 #example :sudo ...

  9. ARP攻击python实现

    1. 前言 代码参考:https://github.com/byt3bl33d3r/arpspoof/blob/master/arpspoof/arpspoof.py 题目描述: 任务三:网络协议堆栈 ...

最新文章

  1. 备份一张iPhone拍照写入exif中的orientation图片
  2. 算法学习:后缀数组 height的求取
  3. linux显示界面指令规范,linux的常规操作命令
  4. mysql 数值类型 长度_mysql中的数据类型的长度
  5. Influxdb安装、启动influxdb控制台、常用命令、Influx命令使用、Influx-sql使用举例、Influxdb的数据格式、Influxdb客户端工具
  6. Python2寿命只剩一个月啦!还不快赶紧学起Python3酷炫到爆的新特性!
  7. rsync 远程同步——安全高效的异地备份策略
  8. CasperJS基于PhantomJS抓取页面
  9. Pytorch框架中SGD&Adam优化器以及BP反向传播入门思想及实现
  10. Java中的对象、private关键字、this关键字、构造方法
  11. PowerDesigner模型分类
  12. 根据ip做客户端gps定位
  13. 【View基础知识】TouchSlop、VelocityTracker、GestureDetector、Scroller
  14. NC 主子表增加页签
  15. NOIP模拟赛 麻将
  16. 2022年湖南省社会工作者考试综合实务(初级)练习题及答案
  17. JAVA与MAVEN打包
  18. altera FPGA介紹
  19. 新博客, 新方向, 新动力, 心坚持
  20. Headless CMS - 打破“设计优先”的怪圈

热门文章

  1. 你可能不知道的children(React)
  2. 在MacBook 13“机器上无法安装Boot Camp x64驱动程序问题的处理
  3. 开发微信公众平台--新建新浪云sae部署服务器
  4. 达梦数据库的事务管理
  5. 详解scheduleAtFixedRate与scheduleWithFixedDelay原理
  6. java 数据库 触发器
  7. oracle prepare耗时,Android MediaPlayer的prepare()方法太耗时问题
  8. Tomcat服务器启动Bug:Error running ‘Tomcat 9.0.37‘: Address localhost:1099 is already in use
  9. 到哪里找PPT所需图标
  10. 金融行业软件测试面试题(含答案)| 零基础入门指南