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



我只在Python中尝试过这个:

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

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

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

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

编辑:

在图像中 线鲨捕获 ,有很多帧,我选择一个并复制它具有十六进制流。在 tshark 或 xxd 中执行此操作的命令是什么?

当你使用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 格式的文章可能会有所帮助。Wireshark也能够做到这一点View -> "Reload as File Format/Capture".

用鲨鱼获得十六进制

要从 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",
...

在蟒蛇中用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'...
...

根据有问题的更新进行编辑。

最新更新