我正在尝试发送一个以前记录的流量(以pcap格式捕获)。目前,我被困在剥离原始以太网层。流量是在另一台主机上捕获的,我基本上需要更改IP和以太网层src和dst。我设法替换IP层并重新计算校验和,但以太网层给我带来了麻烦。
有没有人有过从捕获文件重新发送数据包并对IP和以太网层(src和dst)应用更改的经验?此外,捕获是相当大的几Gb,在如此大的流量下性能如何?
发布于 2012-01-13 06:41:01
检查此示例
from scapy.all import *
from scapy.utils import rdpcap
pkts=rdpcap("FileName.pcap") # could be used like this rdpcap("filename",500) fetches first 500 pkts
for pkt in pkts:
pkt[Ether].src= new_src_mac # i.e new_src_mac="00:11:22:33:44:55"
pkt[Ether].dst= new_dst_mac
pkt[IP].src= new_src_ip # i.e new_src_ip="255.255.255.255"
pkt[IP].dst= new_dst_ip
sendp(pkt) #sending packet at layer 2评论:
sniff(offline="filename")读取数据包,您可以使用类似于此sniff(offline="filename",prn=My_Function)的prn参数在这种情况下,My_Functions将应用于每个嗅探到的pkt ip="1.1.1.1"等,如上所述。完成同样的任务很简单很简单,但它并不比c
整个“项目/程序”,你可以用C/Wincap创建高性能的函数并编译成动态链接库,然后你可以把这个动态链接库导入到你的python程序中,你可以在一个python程序中使用它。这样,您就可以从非常简单的python/Scapy中获得好处,并且只需用c编写特定的函数,就可以更快地完成工作,并将代码集中在一起并具有可扩展性( maintainable )。
发布于 2014-01-23 20:10:26
如果我是您,我会让Scapy处理Ether层,并使用send()函数。例如:
ip_map = {"1.2.3.4": "10.0.0.1", "1.2.3.5": "10.0.0.2"}
for p in PcapReader("filename.cap"):
if IP not in p:
continue
p = p[IP]
# if you want to use a constant map, only let the following line
p.src = "10.0.0.1"
p.dst = "10.0.0.2"
# if you want to use the original src/dst if you don't find it in ip_map
p.src = ip_map.get(p.src, p.src)
p.dst = ip_map.get(p.dst, p.dst)
# if you want to drop the packet if you don't find both src and dst in ip_map
if p.src not in ip_map or p.dst not in ip_map:
continue
p.src = ip_map[p.src]
p.dst = ip_map[p.dst]
# as suggested by @AliA, we need to let Scapy compute the correct checksum
del(p.chksum)
# then send the packet
send(p)发布于 2012-01-05 22:25:04
好的,我想出了以下几点(很抱歉我的Python)。希望它能帮助一些人。还有一种可能更简单的方案,其中来自pcap文件的所有数据包都被读取到内存中,但这可能会导致大型捕获文件出现问题。
from scapy.all import *
global src_ip, dst_ip
src_ip = 1.1.1.1
dst_ip = 2.2.2.2
infile = "dump.pcap"
try:
my_reader = PcapReader(infile)
my_send(my_reader)
except IOError:
print "Failed reading file %s contents" % infile
sys.exit(1)
def my_send(rd, count=100):
pkt_cnt = 0
p_out = []
for p in rd:
pkt_cnt += 1
np = p.payload
np[IP].dst = dst_ip
np[IP].src = src_ip
del np[IP].chksum
p_out.append(np)
if pkt_cnt % count == 0:
send(PacketList(p_out))
p_out = []
# Send remaining in final batch
send(PacketList(p_out))
print "Total packets sent %d" % pkt_cnhttps://stackoverflow.com/questions/8726881
复制相似问题