2014-02-14 139 views
8

嗨,我的應用程序的工作,我必須讀取它的網絡工作現場包 打開它。並以複雜的方式顯示它。轉換TXT分組數據PCAP格式由Wireshark的

問題是我有包,但它是在文本文件,所以要打開它 通過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 
+0

1-從您的結尾直接通過WireShark讀取數據包不可行嗎? 2-你如何得到這個文本文件? – Gyan

+1

您是否嘗試過使用Java 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/

0

如果您已經捕獲原始數據包,你可以與他們直接寫信給PCAP文件格式(見man 5 pcap-savefile),或使用hexdump都/ XXD + text2pcap實用程序ahkcoder建議。 Text2pcap還支持生成虛擬L2-4頭(ethernet,ip,tcp/udp/sctp)。如果你只有文本表示,你可以從它重建數據包(所以,爲你的系統中使用的每個協議生成所有適當的頭文件)或者調整十六進制轉儲部分偏移量(從0000開始),並將其提供給text2pcap 。

+0

有沒有Java解決方案? –

+0

我沒有看到任何。這是一個簡單而具體的問題,所以perl/bash/python/ruby​​可以成爲很好的語言來解決它。 –