[英]How to replace the IP addresses within a PCAP file using Scapy
我想用Scapy替換給定 PCAP 文件中的 IP 地址。
PCAP 文件(例如eth0.pcap
)包含 Wireshark 的捕獲。 我的消息不是代碼,它顯示了 2 個 IP 地址之間的數據流。 我想用另外 2 個地址替換原來的 2 個地址。
之前給出:
message1: 192.168.10.10-->192.168.20.20
message2: 192.168.20.20-->192.168.10.10
我想替換文件中的所有包:
192.168.10.10
(第一個包的來源)和8.8.8.8
192.168.20.20
(第一個包的目的地)和1.1.1.1
所以之后有:
message1: 172.10.10.10-->172.10.20.20
message2: 172.10.20.20-->172.10.10.10
我該怎么做?
所以你想修改數據包捕獲輸出,比如PCAP 文件格式。 libpcap庫使用此格式將捕獲的數據包記錄到文件中。
Scapy 可以讀取和寫入 PCAP 文件,請參閱 PCAP 的Scapy 文檔。
要替換此文件中的 IP 地址,您必須使用rdpcap()
函數將其數據包讀入對象模型。
然后您可以打印每個數據包的 IP 地址(需要替換)。
或者您也可以替換內存中對象模型中的 IP。 然后使用wrpcap()
函數寫回完整的模型。
我在 GitHub 上使用了從PCAP 到 Mermaid 解析器的示例 PCAP 文件dhcp.pcap
:
from scapy.all import *
# load the PCAP file using rdpcap
packets = rdpcap('dhcp.pcap')
# Let's iterate through every packet
for packet in packets:
source_ip = packet.getlayer(IP).src
destination_ip = packet.getlayer(IP).dst
print(f"{source_ip} --> {destination_ip}")
# TODO: replace in model and write to PCAP file using wrpcap
更新:
通過一些簡化和調試打印改進了 Talentldk 的解決方案:
all
導入全部)wrpcap
編寫讀取模型replace
函數的替換字典,其中將字典條目(元組)傳遞給函數,使用解包運算符*
作為前綴,導致 2 個單獨的參數(鍵被替換為值)iter
來處理所有替換(這里是 2 個條目),其中下一個項目可以由next
函數繪制from scapy.all import *
# rdpcap loads in our pcap file
packets = rdpcap('dhcp.pcap')
# define search
first_src_ip = packets[0][IP].src
first_dst_ip = packets[0][IP].dst
# define new ip address to use as replacement
ip_replacement = {f"{first_src_ip}" : '8.8.8.8', f"{first_dst_ip}" : '1.1.1.1'}
print(f"replacement: {ip_replacement}")
# Let's iterate through every packet
for i, packet in enumerate(packets):
source_ip = packet.getlayer(IP).src
destination_ip = packet.getlayer(IP).dst
print(f"[{i:3}] original: {source_ip} --> {destination_ip}")
# replace in model
replacement = iter(ip_replacement.items())
source_ip = source_ip.replace(*next(replacement))
destination_ip = destination_ip.replace(*next(replacement))
print(f"[{i:3}] replaced: {source_ip} --> {destination_ip}")
wrpcap("dhcp_replaced.pcap", packets)
印刷:
replacement: {'0.0.0.0': '8.8.8.8', '255.255.255.255': '1.1.1.1'}
[ 0] original: 0.0.0.0 --> 255.255.255.255
[ 0] replaced: 8.8.8.8 --> 1.1.1.1
[ 1] original: 192.168.0.1 --> 192.168.0.10
[ 1] replaced: 192.168.0.1 --> 192.168.0.10
[ 2] original: 0.0.0.0 --> 255.255.255.255
[ 2] replaced: 8.8.8.8 --> 1.1.1.1
[ 3] original: 192.168.0.1 --> 192.168.0.10
[ 3] replaced: 192.168.0.1 --> 192.168.0.10
hc_dev 的回答引導我找到解決方案。 我試圖理解它並使它在我的問題中起作用。
下面是我的代碼:
from scapy.all import *
from scapy.utils import PcapWriter
packets = rdpcap('ftp.pcap')
new_cap = PcapWriter("ftp_new.pcap")
srcIp=packets[0][IP].src
dstIP=packets[0][IP].dst
#define new ip address I want use
ip1='8.8.8.8'
ip2='1.1.1.1'
#replace orinigal address
for p in packets:
if(p[IP].src == srcIp):
p[IP].src = ip1
elif(p[IP].src == dstIP):
p[IP].src = ip2
if(p[IP].dst == srcIp):
p[IP].dst = ip1
elif(p[IP].dst == dstIP):
p[IP].dst = ip2
new_cap.write(p)
wrpcap("ftp_new.pcap", packets)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.