2017-09-16 128 views
2

我想創建一個使用scapy的新圖層,我創建了一個新圖層,但是當我將它發送給另一臺計算機時,它已經丟失,wireshark也無法識別它。 我該如何解決這個問題?如何用scapy創建新圖層或新協議?

class OMER(Packet): 
    name = "OMER" 
    fields_desc = [StrLenField("Omer", "", None)] 
+0

您如何期待['wireshark'](https://www.wireshark.org/)能夠理解和解碼您自己的協議規範?要解決這個問題,你可以編寫一個本地解碼器。欲瞭解更多信息,請閱讀['scapy'文檔](https://scapy.readthedocs.io/en/latest/build_dissect.html)... – coder

+0

我試圖閱讀這些信息,但我並沒有深刻理解我應該怎麼做你能解釋一下嗎?另一個問題是,當我要求查看數據包時,我將包從一臺計算機發送到另一臺計算機,爲什麼當我要求查看數據包時,除了我的圖層 – Omer

回答

1

當你創建一個新的協議或scapy一個新層,其他的網絡工具,如wireshark(及其他),因爲它們是知道你協議的細節的將不能夠自動解析它正確。

如果你想試驗一個新的協議,你將不得不創建你自己的本地解碼器。下面的例子,即使其最小的,這表明上述所有的:


#!/usr/bin/env python 

from scapy.all import * 

# create a simple protocol 
# (example similar with the one in the scapy docs...) 
class Exmpl(Packet): 
    name = "myprotocol" 
    fields_desc=[ ShortField("fld1",5), 
        XByteField("fld2",3) ] 

from scapy.utils import PcapWriter 

# write data in a pcap file to examine later with 
# 1 -> scapy 
# 2 -> wireshark 
print '\n[+] Writing net.pcap file...' 
cap = PcapWriter("net.pcap", append=True, sync=True) 
for i in range(10): 
    packet = Exmpl(fld1=i) 
    cap.write(packet) 

# read the data and examine them with scapy 
# scapy is aware of the "Exmpl" protocol (e.g. its fields etc...) 
# and how to decode it, while wireshark is not 
print '[+] Examining net.pcap file...\n' 
packets = rdpcap('net.pcap') 
for p in packets: 
    Exmpl(str(p)).show() 

以上腳本的輸出將是這樣的:

[+] Writing net.pcap file... 
[+] Examining net.pcap file... 

###[ myprotocol ]### 
    fld1  = 0 
    fld2  = 0x3 
###[ myprotocol ]### 
    fld1  = 1 
    fld2  = 0x3 
###[ myprotocol ]### 
    fld1  = 2 
    fld2  = 0x3 
...skip... 

正如你可以看到知道的協議,因此可以正確解析數據。現在,如果你嘗試檢查與wireshark的「net.pcap」文件,你會看到以下內容:

enter image description here

不知道你的協議,因此它不能正確分析。如你所知,即使你在另一個設備上發送這些數據包(實際上你也必須實現一些其他的設備),那麼這個設備也必須知道你的協議,否則它也必須知道你的協議,不然它將無法正確解析它。這就是爲什麼當你試圖從一臺計算機發送數據包到另一臺計算機時,接收器無法成功解碼它們。

+0

以外,我如何讓另一臺計算機知道我的協議? – Omer

+0

您必須將'Exmpl'導入到另一臺計算機的腳本中,並按代碼段中所示解析pcap。順便說一下,這是一種隱藏網絡中數據的方式,因爲沒有人可以知道你的協議的細節。查看[此維基鏈接](https://en.wikipedia.org/wiki/Communications_protocol),其中包含有關協議標準的更多信息。 – coder

+0

好吧,如果我寫這個代碼它取消像ip或tcp的另一層?我可以寫IP()/ UDP()/ my_protocol()嗎? – Omer

0

您是否將新層/協議綁定到上一層/協議?例如。 bind_layers(Ether,OMER)如果OMER層緊鄰Ether層之後。