读取pcapng文件并以json格式获取标头

问题描述 投票:0回答:1

我有一个Wireshark数据包捕获的文件(.pcapng),该文件必须读取到python程序的内存中。希望将数据包转换为json格式(例如,可以在tshark中使用tshark -r cap.pcapng -T json > ip.json),然后我需要将json数据写回到pcapng文件中。请告诉我该怎么做。

python-3.x wireshark packet-capture pcap-ng
1个回答
0
投票

-T json

如果您以问题的形式输出为JSON,将无法转换回pcap。问题在于独立JSON没有字节偏移量或字节来告诉转换器要存储哪些字节以及存储在何处。如果这是您的JSON,则可以根据数据手动编码一个新的pcap,具体取决于JSON值的详细程度。

-T json值看起来像这样:

[
  {
    "_index": "packets-2020-05-09",
    "_type": "doc",
    "_score": null,
    "_source": {
      "layers": {
        "frame": {
          "frame.interface_id": "0",
          "frame.interface_id_tree": {
            "frame.interface_name": "en0",
            "frame.interface_description": "Wi-Fi"
          },
          "frame.encap_type": "1",
          "frame.time": "May  9, 2020 20:03:11.303663000 PDT",
          "frame.offset_shift": "0.000000000",
          "frame.time_epoch": "1589079791.303663000",
          "frame.time_delta": "0.000000000",
          "frame.time_delta_displayed": "0.000000000",
          "frame.time_relative": "0.000000000",
          "frame.number": "1",
          "frame.len": "92",
          "frame.cap_len": "92",
          "frame.marked": "0",
          "frame.ignored": "0",
...

-T jsonraw / -T json -x

这些选项是等效的,并为每个字段输出字节和字节偏移量。 Wireshark具有一个专门用于此目的的内置python实用程序json2pcap,但只会使用这种类型的输出。

[Martin Kacer编写了此实用程序,并在其网站上具有documentation for it。本文提供了逐步指南,将pcap从json转换为pcap。您可以找到源代码并单独使用它:json2pcap.py在Wireshark的github存储库中。

-T jsonraw输出看起来像这样:

[
  {
    "_index": "packets-2020-05-09",
    "_type": "doc",
    "_score": null,
    "_source": {
      "layers": {
        "frame_raw": [
          "cc65adda39706c96cfd87fe70800450000a40000400040061ec4c0a801f69765c18cd13b01bb72f797c64990e967801826c562c300000101080a52be1ece06253aa9170303006b00000000000043350c719c8d15f3339346992e557abac51ade350316082fa92225912552dde4ccec7197c6c7745b91b66747c1c8bea3337656213e59425f35c13fdf1739a174a7528d97b0eb10fce4daaba613840d8ce7f28bbbe094abd97db97da3f8f91b68e4db30d982",
          0,
          178,
          0,
          1
        ],
        "frame": {
          "frame.interface_id": "0",
          "frame.interface_id_tree": {
            "frame.interface_name": "en0",
            "frame.interface_description": "Wi-Fi"
          },
          "frame.encap_type": "1",
          "frame.time": "May  9, 2020 20:02:25.845268000 PDT",
          "frame.offset_shift": "0.000000000",
          "frame.time_epoch": "1589079745.845268000",
          "frame.time_delta": "0.000000000",
...

如您所见,frame_raw具有数据包的完整字节,这在重建中很重要。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.