如何使用tshark将pcap转换为十六进制流?

时间:2019-09-09 06:43:28

标签: python wireshark pcap tshark

我只想在Python中做到这一点

首先,我阅读pcap文件并在Python中使用了以下命令:

with open("pcap_files/DCERPC.pcap", 'rb') as f:
    content = f.read()
binascii.hexlify(content)

自从读取整个文件以来,我没有得到确切的十六进制流。

因此,是否可以使用tshark将pcap复制到十六进制流中?

编辑:

在图像wireshark capture中,有很多帧,我在其中选择一个并复制为十六进制流。在tshark或xxd中执行此操作的命令是什么?

1 个答案:

答案 0 :(得分:0)

在使用python时,您可能需要看看利用tshark的PyShark

设置:创建文件

为了演示目的,我们创建一个单数据包文件:

bash-5.0$ tshark -w temp.pcap -c 10
Capturing on 'Wi-Fi: en0'
1 
1 packet dropped from Wi-Fi: en0

解析十六进制

有很多解析十六进制的方法。您选择哪种方法将取决于您要执行的操作。 Tshark将向您显示数据包,而hexdump和xxd将向您显示每个字节,包括捕获格式字节。要了解包格式字节和文件格式字节之间的区别,这篇关于解构pcap format的文章可能会有所帮助。 Wireshark也可以使用View -> "Reload as File Format/Capture"来做到这一点。

使用tshark获取十六进制

要从tshark获取每个数据包的十六进制,请使用-T json,然后找到“ frame_raw”字段。

bash-5.0$ tshark -x -r temp.pcap -T json
[
  {
    "_index": "packets-2019-09-10",
    "_type": "pcap_file",
    "_score": null,
    "_source": {
      "layers": {
        "frame_raw": ["cc65adda39706c96cfd87fe7080045000028910000004006ca3fc0a801f69765c58cc08001bb2f5a0b8169ef01ab501008001f930000",
          0,
          54,
          0,
          1
        ],
        "frame": {
          "frame.encap_type": "1",
          "frame.time": "Sep 10, 2019 18:57:29.571371000 PDT",
          "frame.offset_shift": "0.000000000",
...

在python中使用tshark获取十六进制

import json
import subprocess as sp

def get_tshark_hexstreams(capture_path: str) -> list:
    """Get the frames in a capture as a list of hex strings."""
    cmds = ["tshark", "-x", "-r", capture_path, "-T", "json"]
    frames_text = sp.check_output(cmds, text=True)
    frames_json = json.loads(frames_text)
    hexstreams = [frame["_source"]["layers"]["frame_raw"][0] for frame in frames_json]
    return hexstreams

output = get_tshark_hexstreams('temp.pcap')
print(output)
['6c96cfd87fe7cc65adda'...
 'cc65adda39706c96cfd8'...
 'ffffffffffff60a44c24'...
 ...

每个有问题的更新进行编辑。