scapy:UDP碎片整理时间戳问题

时间:2011-03-24 16:32:16

标签: python udp pcap scapy

我使用以下脚本(python + scapy)来创建包含碎片UDP数据包的pcap文件的碎片整理版本。

# Read pcap file
in=rdpcap("in.pcap")
# Defragment ...
out = defragment(in)
# Write defragmented pcap file
wrpcap ("out.pcap", out) 

我的问题是碎片整理数据包的pcap时间戳设置为碎片整理的日期,而不是捕获日期。非碎片数据包仍然具有其原始捕获时间戳。

我看了一下inet.py,defragment()和defrag(),但是我不太精通scapy,我想帮助理解它并破解它以保持日期,比如说,最后一个片段,并将其放入碎片整理的数据包......

有人可以帮我解决这个问题吗? 比如,我在哪里可以找到输入数据包中的捕获日期,以及我应该将它放在碎片整理数据包中...

当然,欢迎达到同一目标的任何其他解决方案(我承认......我很着急:( ...)

1 个答案:

答案 0 :(得分:2)

这是inet.py的补丁,它将第一个片段的捕获日期添加到碎片整理的数据包中。

可能有更清晰的解决方案,例如修改Packet.copy()方法和其他一些方法,但是嘿,它符合要求......

*** inet.py     2011-03-29 14:01:19.000000000 +0000
--- inet.py.orig        2011-03-29 07:59:02.000000000 +0000
***************
*** 846,856 ****
          lastp = lst[-1]
          if p.frag > 0 or lastp.flags & 1 != 0: # first or last fragment missing
              missfrag += lst
-             print "missing framgent!"
              continue
!         # Keep 1st fragment capture time (as it is lost in subsequent copies during defragmentation)
!         ptime = p.time
!         p = p.copy() # copy() method do not copy time member (?)
          if Padding in p:
              del(p[Padding].underlayer.payload)
          ip = p[IP]
--- 846,853 ----
          lastp = lst[-1]
          if p.frag > 0 or lastp.flags & 1 != 0: # first or last fragment missing
              missfrag += lst
              continue
!         p = p.copy()
          if Padding in p:
              del(p[Padding].underlayer.payload)
          ip = p[IP]
***************
*** 878,892 ****
              del(ip.len)
              p = p/txt
              p._defrag_pos = max(x._defrag_pos for x in lst)
-             # Put back time in packet
-             p.time= ptime
              defrag.append(p)
      defrag2=[]
      for p in defrag:
          q = p.__class__(str(p))
          q._defrag_pos = p._defrag_pos
-         # Put back time in packet
-         q.time = p.time
          defrag2.append(q)
      final += defrag2
      final += missfrag
--- 875,885 ----
相关问题