【问题标题】:Tshark - can't display just data of custom protocolTshark - 不能只显示自定义协议的数据
【发布时间】:2012-04-28 15:12:52
【问题描述】:

我有一个在端口 8888(不,它不是 http)和 TCP 之上运行的自定义协议。我已经将数据包流捕获到 PCAP 文件中。问题是现在我不能只显示它的数据部分。

我已尝试使用以下命令:

tshark -r test.pcap -R 'tcp.port==8888 && tcp.len>0' -T fields -e "tcp.data"

但它显示一个空字符串。 tcp.data字段不是保存TCP数据包的数据吗?

如何只显示我需要的数据?

【问题讨论】:

  • 你可以试试tpcick
  • @StephaneChazelas,tcpick 有一个限制:它不支持 ipv6

标签: linux networking tcp wireshark tcpdump


【解决方案1】:

Wireshark 中有“分析/跟踪 TCP 流”功能。

只需从数据包列表中选择 TCP 数据包,然后“Follow TCP stream”.... Wireshark 就会显示所选连接的 TCP 会话。

编辑:

tcp.data 不存在。请改用data.data

tshark -r mon.pcap -R "(tcp.port == 8888) && (tcp.len > 0)" -T  fields -e data.data

如果wireshark 知道使用端口(8888)的协议,那么以前的协议就行不通了。 但以下技巧有效:

tshark -r mon.pcap -R "(tcp.port == 8888) && (tcp.len > 0)" -T  fields -d tcp.port==8888,echo -e echo.data

【讨论】:

  • 是的,我知道,但我不想使用它,因为它有点不切实际,因为它只显示所选连接的 TCP 对话。我想显示所选 IP 的整个 TCP 对话。如果您知道在 Wireshark 中执行此操作的方法,我会很乐意使用它。
  • 感谢您更新答案,它真的很有帮助。第一个命令打印协议的数据部分,但是是十六进制的。并且由于它是 ASCII 协议,因此以 ascii 表示形式打印可打印字符会更好,而不是用点“.”打印。输出示例为:“45:58:49:54:0d:ff”,即“EXIT??”可以在这里看到:dolcevie.com/js/converter.html。所以基本上这就是现在需要做的所有事情。你知道 tshark 是否支持这个?
  • 我不知道。但请记住:TCP 可以进行重传。因此,有时输出中可能会出现部分重复的数据。
  • 我回应@eleanor 的问题。有data——一整包字符串作为十六进制半字节,还有data.data,它看起来像一个字节序列(或数组)。如何获得“EXIT”的 ASCII 而不是一串长的十六进制半字节?当然,tshark 内部必须有一个解决方案,或者我可以通过管道将我的输出传输到 ...???
【解决方案2】:

好的,我已经编写了一个 python 脚本来完成所需的工作。我知道代码可能会更好一些,但它可以工作,这就是我现在所需要的。

#!/usr/bin/python
import subprocess
import sys
import binascii

""" Input arguments. """
if len(sys.argv) != 3 and len(sys.argv) != 4 and len(sys.argv)!= 5:
  print "[*] You didn't specify the right command line arguments."
  print "Usage:\n"
  print "\t"+sys.argv[0]+" <pcap> <port> [<src_ip> <dst_ip>]"
  print
  exit(-1)

args = len(sys.argv)
if args == 3:
  pcap = sys.argv[1]
  port = sys.argv[2]
elif args == 4:
  pcap = sys.argv[1]
  port = sys.argv[2]
  srcip = sys.argv[3]
elif args == 5:
  pcap = sys.argv[1]
  port = sys.argv[2]
  srcip = sys.argv[3]
  dstip = sys.argv[4]


""" Use tshark to read pcap file. """
targs = []
targs.append("tshark")
targs.append("-r"+pcap)
f = "-R (tcp.port=="+port+") && (tcp.len>0)"
if args == 4:
  f=f+" && (ip.src == "+srcip+")"
elif args == 5:
  f=f+" && (ip.src == "+srcip+" and ip.dst == "+dstip+")"
targs.append(f)
targs.append("-Tfields")
targs.append("-edata.data")
p = subprocess.Popen(targs, stderr=subprocess.PIPE, stdout=subprocess.PIPE)

while True:
  """ Read a line of output from the tshark output- """
  out = p.stdout.readline()

  """ Print only non-empty lines."""
  if out != '':
    """ Parse the line appropriately and print printable characters. """
    chars = out.split(':')
    for c in chars:
      if c >= '20' and c <= '7e':
    try:
          cc = binascii.unhexlify(c)
    except:
      pass
    sys.stdout.write(cc)
      else:
        sys.stdout.write('.')
    print

  """ When there is no more data, break out of infinite loop."""
  if out == '' and p.poll() != None:
    break

p.stdout.close()

我们可以用三种不同的方式调用脚本:

第一:

python tshark.py temp.pcap 8888

第二:

python tshark.py temp.pcap 8888 "10.1.1.2" 

第三:

python tshark.py temp.pcap 8888 "10.1.1.2" "10.1.1.3"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 2011-01-30
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多