如何使用Scapy替换PCAP文件中的IP地址
我想用 scapy 。
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.8
- 和
192.168.20.20.20 < /code>(第一个软件包的目的地)带有
1.1.1.1
,以后有:
message1: 172.10.10.10-->172.10.20.20
message2: 172.10.20.20-->172.10.10.10
我该如何处理?
I want to replace the IP addresses in a given PCAP file with Scapy.
The PCAP file (e.g. eth0.pcap
) contains captures by Wireshark.
My message is not code, it shows the data flow between 2 IP addresses. I want to replace the original 2 addresses with another 2 addresses.
Example
Given before:
message1: 192.168.10.10-->192.168.20.20
message2: 192.168.20.20-->192.168.10.10
I want to replace for all packages within the file:
192.168.10.10
(source of first package) with8.8.8.8
- and
192.168.20.20
(destination of first package) with1.1.1.1
So that afterwards there are:
message1: 172.10.10.10-->172.10.20.20
message2: 172.10.20.20-->172.10.10.10
How can I do this with?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
hc_dev的答案引导我实现了解决方案。
我试图理解它并使其在我的问题中起作用。
以下是我的代码:
hc_dev's answer guided me towards the solution.
I tried to understand it and make it work in my issue.
Below is my code:
因此,您想修改数据包捕获输出,例如 pcap文件格式。
libpcap 库使用此格式将捕获的数据包记录为文件。
Scapy可以读写PCAP文件,请参阅 for pcap for pcap 。
读,写,打印,替换
以替换本文件中的IP地址,您必须使用
rdpcap()
函数将其数据包读为对象模型。然后,您可以打印每个数据包的IP地址(需要替换)。
或者,您也可以在内存中的对象模型中替换IP。
然后使用
wrpcap()
函数写入完整的模型。打印IP地址的示例
我使用了示例PCAP文件 dhcp.pcap 来自
另请参见
更新:
如何在您的情况下替换
精制 dractageldk的解决方案 有一些简化和调试的打印机:
wrpcap
编写读取模型,replace> replace> replace
function plot dique(tuple) )通过 undack-operator传递给功能。 *
作为前缀产生2个单独的参数( key 被 value )iter
在dict的项目上处理所有替代品(此处2个条目),其中可以通过下一个项目
函数打印下一个项目:
So you want to modify packet capture output, like in PCAP file format.
This format is used by libpcap library to record captured packets to a file.
Scapy can read and write PCAP files, see the Scapy docs for PCAP.
Read, write, print, replace
To replace the IP addresses within this file, you have to read its packets into an object-model using the
rdpcap()
function.Then you can print the IP addresses for each packet (with desired replacement).
Or you can also replace the IP within the object-model in memory.
Then write the complete model back using the
wrpcap()
function.Example to print IP addresses
I used the example PCAP file
dhcp.pcap
from PCAP to Mermaid parser on GitHub:See also
Update:
How to replace in your case
Refined talentldk's solution with some simplification and debug-prints:
all
imports all)wrpcap
to write the read modelreplace
function where dict entry (a tuple) is passed to function using the unpack-operator*
as prefix resulting in 2 separate arguments (key is replaced by value)iter
over the dict's items to process all replacements (here 2 entries) where the next item can be drawn bynext
functionPrints: