【问题标题】:convert txt packet data to pcap format to open it by Wireshark将txt数据包数据转换为pcap格式,通过Wireshark打开
【发布时间】:2014-03-13 23:22:45
【问题描述】:

您好,我正在开发必须读取实时数据包的应用程序 从网络 工作。并以复杂的方式展示它。

但是问题是我有数据包但它在文本文件中,所以打开它 通过 Wireshark,我必须将其转换为 .pcap 格式

那么如何将文本中的数据包转换为 pcap 格式。

我的文本文件格式如下图,

Frame:
Frame:          number = 0
Frame:       timestamp = 2014-02-13 09:39:11.288
Frame:     wire length = 174 bytes
Frame: captured length = 174 bytes
Frame:
Eth:  ******* Ethernet - "Ethernet" - offset=0 (0x0) length=14 
Eth: 
Eth:      destination = 01:00:5e:7f:ff:fa
Eth:                    .... ..0. .... .... = [0] LG bit
Eth:                    .... ...0 .... .... = [0] IG bit
Eth:           source = ec:9a:74:4d:8e:03
Eth:                    .... ..0. .... .... = [0] LG bit
Eth:                    .... ...0 .... .... = [0] IG bit
Eth:             type = 0x800 (2048) [ip version 4]
Eth: 
Ip:  ******* Ip4 - "ip version 4" - offset=14 (0xE) length=20 protocol suite=NETWORK
Ip: 
Ip:          version = 4
Ip:             hlen = 5 [5 * 4 = 20 bytes, No Ip Options]
Ip:         diffserv = 0x0 (0)
Ip:                    0000 00.. = [0] code point: not set
Ip:                    .... ..0. = [0] ECN bit: not set
Ip:                    .... ...0 = [0] ECE bit: not set
Ip:           length = 160
Ip:               id = 0x4CD1 (19665)
Ip:            flags = 0x0 (0)
Ip:                    0.. = [0] reserved
Ip:                    .0. = [0] DF: do not fragment: not set
Ip:                    ..0 = [0] MF: more fragments: not set
Ip:           offset = 0
Ip:              ttl = 0 [time to live]
Ip:             type = 17 [next: User Datagram]
Ip:         checksum = 0xB0AA (45226) [correct]
Ip:           source = 124.125.80.90
Ip:      destination = 239.255.255.250
Ip: 
Udp:  ******* Udp offset=34 (0x22) length=8 
Udp: 
Udp:           source = 58845
Udp:      destination = 1900
Udp:           length = 140
Udp:         checksum = 0x5154 (20820) [correct]
Udp: 
Data:  ******* Payload offset=42 (0x2A) length=132 
Data: 
002a: 4d 2d 53 45  41 52 43 48  20 2a 20 48  54 54 50 2f    M-SEARCH * HTTP/
003a: 31 2e 31 0d  0a 48 6f 73  74 3a 32 33  39 2e 32 35    1.1..Host:239.25
004a: 35 2e 32 35  35 2e 32 35  30 3a 31 39  30 30 0d 0a    5.255.250:1900..
005a: 53 54 3a 75  72 6e 3a 73  63 68 65 6d  61 73 2d 75    ST:urn:schemas-u
006a: 70 6e 70 2d  6f 72 67 3a  64 65 76 69  63 65 3a 57    pnp-org:device:W
007a: 41 4e 43 6f  6e 6e 65 63  74 69 6f 6e  44 65 76 69    ANConnectionDevi
008a: 63 65 3a 31  0d 0a 4d 61  6e 3a 22 73  73 64 70 3a    ce:1..Man:"ssdp:
009a: 64 69 73 63  6f 76 65 72  22 0d 0a 4d  58 3a 33 0d    discover"..MX:3.
00aa: 0a 0d 0a 00 

【问题讨论】:

  • 1- 从您的一端直接通过 WireShark 读取数据包不可行吗? 2- 你是如何得到这个文本文件的?
  • 您是否尝试过使用 Java pcap 库?

标签: java text converter pcap


【解决方案1】:

Wireshark 为文本文件提供了一个命令行 pcap 转换器:

https://www.wireshark.org/docs/man-pages/text2pcap.html

AutoHotkey 解决方案:

; Change appropriate file locations
Run, %A_ProgramFiles%\ethereal\text2pcap.exe c:\test.txt c:\testconv.cap,%A_ProgramFiles%\ethereal

如果您想做一个完全自动化的解决方案,您可以修改此功能,该功能会主动监视目录以进行文件更改/创建。

http://www.autohotkey.com/board/topic/41653-watchdirectory/

【讨论】:

    【解决方案2】:

    如果您捕获了原始数据包,您可以将它们直接写入 pcap 文件格式(请参阅man 5 pcap-savefile)或按照ahkcoder 的建议使用 hexdump/xxd+text2pcap 实用程序。 Text2pcap 还支持生成虚拟 L2-4 标头(以太网、ip、tcp/udp/sctp)。

    如果您只有文本表示,您可以从中重建数据包(因此,为系统中使用的每个协议生成所有适当的标头)或调整十六进制转储部分偏移量(从 0000 开始)并将其提供给 text2pcap。

    【讨论】:

    • 有Java解决方案吗??
    • 我没有看到任何东西。这是一个简单而具体的问题,因此 perl/bash/python/ruby 可以是很好的语言来解决它。
    猜你喜欢
    • 2018-11-06
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多