我想創建一個使用scapy的新圖層,我創建了一個新圖層,但是當我將它發送給另一臺計算機時,它已經丟失,wireshark也無法識別它。 我該如何解決這個問題?如何用scapy創建新圖層或新協議?
class OMER(Packet):
name = "OMER"
fields_desc = [StrLenField("Omer", "", None)]
我想創建一個使用scapy的新圖層,我創建了一個新圖層,但是當我將它發送給另一臺計算機時,它已經丟失,wireshark也無法識別它。 我該如何解決這個問題?如何用scapy創建新圖層或新協議?
class OMER(Packet):
name = "OMER"
fields_desc = [StrLenField("Omer", "", None)]
當你創建一個新的協議或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...
正如你可以看到scapy知道的協議,因此可以正確解析數據。現在,如果你嘗試檢查與wireshark
的「net.pcap」文件,你會看到以下內容:
wireshark不知道你的協議,因此它不能正確分析。如你所知,即使你在另一個設備上發送這些數據包(實際上你也必須實現一些其他的設備),那麼這個設備也必須知道你的協議,否則它也必須知道你的協議,不然它將無法正確解析它。這就是爲什麼當你試圖從一臺計算機發送數據包到另一臺計算機時,接收器無法成功解碼它們。
您是否將新層/協議綁定到上一層/協議?例如。 bind_layers(Ether,OMER)如果OMER層緊鄰Ether層之後。
您如何期待['wireshark'](https://www.wireshark.org/)能夠理解和解碼您自己的協議規範?要解決這個問題,你可以編寫一個本地解碼器。欲瞭解更多信息,請閱讀['scapy'文檔](https://scapy.readthedocs.io/en/latest/build_dissect.html)... – coder
我試圖閱讀這些信息,但我並沒有深刻理解我應該怎麼做你能解釋一下嗎?另一個問題是,當我要求查看數據包時,我將包從一臺計算機發送到另一臺計算機,爲什麼當我要求查看數據包時,除了我的圖層 – Omer