ネットワーク上で動作するアプリケーションを開発している場合、またはそのようなアプリケーションの動作をデバッグしている場合は、ネットワーク プロトコルの仕組みを十分に理解しておくと、作業が非常に容易になります。そのような知識の主な情報源は RFC であり、幸いなことに、RFC は長い間パブリック ドメインにありました。また、テキスト以外は何も入っていないので、コンソールブラウザのリンクからでも読むことができます。
それにもかかわらず、おそらく Habr の読者のほとんどは、少なくとも 1 つの RFC、さらにはRFC-2616 の全文を読んだことがないでしょう。 ... 官僚的な文書の猛烈なスタイルに加えて、言語の壁が障害になる可能性があります。さらに、ほとんどの場合、プロトコル アーキテクチャの特定の側面 (フィールドの長さとタイプ、リターン コード、ヘッダー内の位置など) を理解する必要があります。これを行うために、最初から最後まですべてを読む必要はありません。
この場合に備えて、 Python で記述されたかなり単純なコンソール アプリケーションであるProtocol が作成されました。それには 2 つの目的があります。
- 開発者とエンジニアに、最も一般的なネットワーク プロトコルのヘッダー ダイアグラムをコマンド ラインから直接簡単かつ簡単に表示する機能を提供します。
- 研究者やエンジニアに、独自のカスタム プロトコルの ASCII ヘッダー ダイアグラムをすばやく作成する機能を提供します。
インストールと構成
ソース コードは GitHubにあり、標準の方法でダウンロードできます。
git clone https://github.com/luismartingarcia/protocol.git
構成は、root ユーザー権限を持つコマンドによって実行されます。
setup.py install
現在、このプログラムには Linux または Python ディストリビューション用の標準インストール メカニズムがないため、ファイルのコピー先を覚えておくことをお勧めします。
running install running build running build_scripts creating build creating build/scripts-3.9 copying and adjusting protocol -> build/scripts-3.9 copying constants.py -> build/scripts-3.9 copying specs.py -> build/scripts-3.9 changing mode of build/scripts-3.9/protocol from 644 to 755 changing mode of build/scripts-3.9/constants.py from 644 to 755 changing mode of build/scripts-3.9/specs.py from 644 to 755 running install_scripts copying build/scripts-3.9/protocol -> /usr/bin copying build/scripts-3.9/constants.py -> /usr/bin copying build/scripts-3.9/specs.py -> /usr/bin changing mode of /usr/bin/protocol to 755 changing mode of /usr/bin/constants.py to 755 changing mode of /usr/bin/specs.py to 755 running install_egg_info Writing /usr/lib/python3.9/site-packages/protocol-0.1-py3.9.egg-info
プロトコルを実行
プログラムには 2 つの主要な操作モードがあります。
- protocol <既存のプロトコルの名前>;
- protocol <ネイティブプロトコル仕様>。
以下の標準プロトコルが利用可能です
ethernet : Ethernet
8021q : IEEE 802.1q
dot1q : IEEE 802.1q
tcp : Transmission Control Protocol (TCP)
udp : User Datagram Protocol (TCP)
ip : Internet Protocol (IP), version 4.
ipv6 : Internet Protocol (IP), version 6.
icmp : Internet Control Message Protocol (ICMP)
icmp-destination: : ICMP Destination Unreachable
icmp-time : ICMP Time Exceeded
icmp-parameter : ICMP Parameter Problem
icmp-source : ICMP Source Quench
icmp-redirect : ICMP Redirect
icmp-echo : ICMP Echo Request/Reply
icmp-timestamp : ICMP Timestamp Request/Reply
icmp-information : ICMP Information Request/Reply
icmpv6 : ICMP for IPv6 (ICMPv6)
icmpv6-destination : ICMPv6 Destination Unreachable
icmpv6-big : ICMPv6 Packet Too Big
icmpv6-time : ICMPv6 Time Exceeded
icmpv6-parameter : ICMPv6 Parameter Problem
icmpv6-echo : ICMPv6 Echo Request/Reply
icmpv6-rsol : ICMPv6 Router Solicitation
icmpv6-radv : ICMPv6 Router Advertisement
icmpv6-nsol : ICMPv6 Neighbor Solicitation
icmpv6-nadv : ICMPv6 Neighbor Advertisement
icmpv6-redirect : ICMPv6 Redirect
ユーティリティの使用例を次に示します。
|12:09:41|admin@redeye:[~]> protocol ip 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |Version| IHL |Type of Service| Total Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identification |Flags| Fragment Offset | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Time to Live | Protocol | Header Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Destination Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |12:09:45|admin@redeye:[~]> protocol ipv6 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |Version| Traffic Class | Flow Label | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Payload Length | Next Header | Hop Limit | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | + + | | + Source Address + | | + + | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | + + | | + Destination Address + | | + + | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1 つのコマンドで、一度に複数のプロトコルをスペースで区切って指定できます。
|12:34:16|admin@redeye:[~]> protocol icmp-time icmp-timestamp 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Type | Code | Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Unused | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | + Internet Header + 64 bits of Original Data Datagram + | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Type | Code | Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identifier | Sequence Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Originate Timestamp | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Receive Timestamp | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Transmit Timestamp | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
プロトコルは機知に富んでおり、プロトコルの名前を完全に入力する必要はありません。本当に怠けている場合は、たとえば、dot1q の代わりにドットと入力できます。ただし、オートコンプリートの場合と同様に、あいまいな選択の場合は、選択肢の 1 つを選択することをお勧めします。
|12:43:25|admin@redeye:[~]> protocol icmpv6-r
Ambiguous protocol specifier 'icmpv6-r'. Did you mean any of these?
icmpv6-radv
icmpv6-redirect
icmpv6-rsol
独自開発
プロトコル仕様の最も重要な機能は、フィールド リストです。次のように、独自のプロトコルにそれらを指定できます。
|12:55:16|mikayel@redeye:[~]> protocol «Source:16,Reserved:40,RTT:8» 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ | Reserved | RTT | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
追加オプションにより、ASCII チャートのフォーマット機能が拡張されます。1 行あたりのビット数を指定し、区切り文字と擬似グラフィックスの文字を設定できます。疑似グラフィック オプションを指定した前のコマンドは、次のようになります。
|16:02:05|mikayel@redeye:[~]> protocol «Source:16,Reserved:40,RTT:8?\ numbers=y,startchar=*,endchar=*,evenchar=-,oddchar=-,sepchar=|» 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 *---------------------------------------------------------------* | Source | | *-------------------------------* *---------------* | Reserved | RTT | *---------------------------------------------------------------*
プロトコル + tshark
tshark に保存されているネットワーク トラフィックの pcap/pcapng ファイルを開くと、ネットワーク プロトコルの構造に関するより完全な情報を取得できます 。tsharkだけを見る と、木の森が見えず、プロトコルと一緒に 必要なものが表示されます。
|16:08:05|admin@redeye:[~]> sudo tshark -r /tmp/exmp.pcap -V
Transmission Control Protocol, Src Port: 48378, Dst Port: 443, Seq: 1, Ack: 1, Len: 0
Source Port: 48378
Destination Port: 443
[Stream index: 0]
[TCP Segment Len: 0]
Sequence Number: 1 (relative sequence number)
Sequence Number (raw): 1120003294
[Next Sequence Number: 1 (relative sequence number)]
Acknowledgment Number: 1 (relative ack number)
Acknowledgment number (raw): 4090007166
1000 .... = Header Length: 32 bytes (8)
Flags: 0x010 (ACK)
000. .... .... = Reserved: Not set
...0 .... .... = Nonce: Not set
.... 0... .... = Congestion Window Reduced (CWR): Not set
.... .0.. .... = ECN-Echo: Not set
.... ..0. .... = Urgent: Not set
.... ...1 .... = Acknowledgment: Set
.... .... 0... = Push: Not set
.... .... .0.. = Reset: Not set
.... .... ..0. = Syn: Not set
.... .... ...0 = Fin: Not set
[TCP Flags: ·······A····]
Window: 501
[Calculated window size: 501]
[Window size scaling factor: -1 (unknown)]
Checksum: 0xfbe8 [unverified]
[Checksum Status: Unverified]
Urgent Pointer: 0
Options: (12 bytes), No-Operation (NOP), No-Operation (NOP), Timestamps
TCP Option — No-Operation (NOP)
Kind: No-Operation (1)
TCP Option — No-Operation (NOP)
Kind: No-Operation (1)
TCP Option — Timestamps: TSval 2014817649, TSecr 2606727549
Kind: Time Stamp Option (8)
Length: 10
Timestamp value: 2014817649
Timestamp echo reply: 2606727549
|16:28:51|admin@redeye:[~]> protocol tcp
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Offset| Res. | Flags | Window |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
もちろん、Protocol や tshark のようなプログラムは一次ソースの読み取りに取って代わることはできませんし、またそうすべきではありません。e 豪華<0x80><0xA4><0xA4><0xA4> ただし、RFC は、プログラマーやシステム管理者の時間を大幅に節約できます。
Macleod のクラウド サーバー は高速で安全です。
上記のリンクを使用するか、バナーをクリックして登録すると、任意の構成のサーバーをレンタルした最初の 1 か月間で 10% の割引が受けられます。