designetwork

ネットワークを軸としたIT技術メモ

tsharkディスプレイフィルタ(tcp.payload)でTLS1.1通信をキャプチャする

TLS1.1サポート終了にあたり、tsharkによるパケットキャプチャでTLS1.1通信を検知する。

ポイントは 以下のオプション

  • -Y "tcp.payload[x:x]" TCPペイロードでディスプレイフィルタ
  • -Mモリー枯渇対策
  • -w - | tshark -r - DISK枯渇対策 ( ディスプレイフィルタは -b, -w 不可 )

動作確認バージョン

バージョン・設定により表示が変わるので要調整。
TShark (Wireshark) 3.6.13 (v3.6.13-0-g9aa9aca9c1c1)

詳細バージョン(クリックで展開)

C:\Users\Administrator>"C:\Program Files\Wireshark\tshark.exe" --version
TShark (Wireshark) 3.6.13 (v3.6.13-0-g9aa9aca9c1c1)

Copyright 1998-2023 Gerald Combs <gerald@wireshark.org> and contributors.
License GPLv2+: GNU GPL version 2 or later <https://www.gnu.org/licenses/gpl-2.0.html>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Compiled (64-bit) using Microsoft Visual Studio 2019 (VC++ 14.32, build 31332),
with libpcap, with GLib 2.66.4, with zlib 1.2.11, with Lua 5.2.4, with GnuTLS
3.6.3 and PKCS #11 support, with Gcrypt 1.8.3, with MIT Kerberos, with MaxMind
DB resolver, with nghttp2 1.44.0, with brotli, with LZ4, with Zstandard, with
Snappy, with libxml2 2.9.10, with libsmi 0.4.8.

Running on 64-bit Windows Server 2016 (1607), build 14393, with Intel(R) Xeon(R)
Silver 4112 CPU @ 2.60GHz (with SSE4.2), with 32425 MB of physical memory, with
GLib 2.66.4, with Npcap version 1.60, based on libpcap version 1.10.2-PRE-GIT,
with c-ares 1.17.0, with GnuTLS 3.6.3, with Gcrypt 1.8.3, with nghttp2 1.44.0,
with brotli 1.0.9, with LZ4 1.9.3, with Zstandard 1.4.0, with LC_TYPE=C, binary
plugins supported (0 loaded).

参考

参考にさせていただきました。ありがとうございます。

syanaise-soudan.com

sig9.org

kagasu.hatenablog.com

継続取得コマンド

"C:\Program Files\Wireshark\tshark" -i "1" -f "host 3.113.127.38" -M 100000 -l -w - 2>>"%USERPROFILE%\Downloads\stderr.log" | "C:\Program Files\Wireshark\tshark" -r - -Y "tcp.payload[1:2] == 03:02 && tcp.payload[9:2] == 03:02" -o gui.column.format:"Packet,%m,Time,%Yt,Source,%us,SrcPort,%uS,Destination,%ud,DstPort,%uD,Info,%i" -M 1000000 -l >> "%USERPROFILE%\Downloads\tls1.1.log"

結果例

 7947 2025-03-17 20:51:14.109253 x.x.x.x 51013 3.113.127.38 443 Client Hello
 7951 2025-03-17 20:51:14.109584 3.113.127.38 443 x.x.x.x 51013 Server Hello

tsharkコマンドオプション

tsharkのManual Pageは以下の通り

www.wireshark.org

キャプチャフィルタ(-f)とディスプレイフィルタ(-Y)

キャプチャフィルタとディスプレイフィルタは以下の通りかかるタイミングが異なる。TLSバージョン(TCPペイロード)はキャプチャフィルタでは扱えないため、ディスプレイフィルタで絞り込む。

Tshark | Capture Filters

Tshark | Display Filters

オプション解説

それぞれのオプションの設定意図を解説する。

"C:\Program Files\Wireshark\tshark" \
-i "1" \
-f "host 3.113.127.38" \
-M 100000 \
-l \
-w - \
2>>"%USERPROFILE%\Downloads\stderr.log" \
| \
"C:\Program Files\Wireshark\tshark" \
-r - \
-Y "tcp.payload[1:2] == 03:02 && tcp.payload[9:2] == 03:02" \
-o gui.column.format:"Packet,%m,Time,%Yt,Source,%us,SrcPort,%uS,Destination,%ud,DstPort,%uD,Info,%i" \
-M 1000000 \
-l \
>> "%USERPROFILE%\Downloads\tls1.1.log"

-f キャプチャフィルタ IPのみフィルタ

-M 100000 (<auto session reset>)モリー枯渇対策

未指定の場合、パケットの関連付け解析のために保持しモリーが枯渇する。一定パケットごとにresetしてメモリーを解放させる。

-l 標準出力バッファオフ

標準出力へのバッファをオフにしてリアルタイムに処理されるようにする。

-w - | tshark -r - Tempファイル DISK枯渇対策

パイプではなく

tshark -i "1" -f "host 3.113.127.38" -Y "tcp.payload[1:2] == 03:02 && tcp.payload[9:2] == 03:02" -o gui.column.format:"..." -M 1000000 -l >> "%USERPROFILE%\Downloads\tls1.1.log"

とした際に、以下のエラーでtsharkが停止した。キャプチャフィルタでフィルタしたパケットをTempファイルとして書き出しており、DISKが枯渇した。

 ** (tshark:3328) 21:44:09.073366 [Main MESSAGE] -- Capture started.
 ** (tshark:3328) 21:44:09.073797 [Main MESSAGE] -- File: "C:\Users\ADMINI~1\AppData\Local\Temp\2\wireshark_Embedded LOM 1 Port 26M9K32.pcapng"
8  ** (tshark:3328) 06:29:04.779256 [GLib ERROR] -- ../src/glib-2-b84205c786.clean/glib/gmem.c:112: failed to allocate 8388608 bytes

対策として、以下で解説されている通り キャプチャフィルタ -> 標準出力 -> 標準入力 -> ディスプレイフィルタ としてTempファイルを作成しない

Tshark | Pipes

2>>"%USERPROFILE%\Downloads\stderr.log" 標準エラーをリダイレクト

-Y "tcp.payload[1:2] == 03:02 && tcp.payload[9:2] == 03:02" TCPペイロードでフィルタ

ディスプレイフィルタではTLSデータを扱えず

tls.record.version == 0x0302 && tls.handshake.version == 0x0302

といったフィルタができなかった。

そのため、TLSではなく単なるTCPとして扱い tcp.payload によりバイト列でフィルタする。

tls.record.version: tcp.payload[1:2] == 03:02
tls.handshake.version: tcp.payload[9:2] == 03:02

TCPペイロードバイト列

 0  1  2  3  4  5  6  7  8  9 10 11 12
------------------------------------------
16 03 02 00 a3 01 00 00 9f 03 02 67 de ...

(任意 出力形式) -o gui.column.format:"Packet,%m,Time,%Yt,Source,%us,SrcPort,%uS,Destination,%ud,DstPort,%uD,Info,%i"

ログファイルの出力形式を任意で変更する。 形式は tshark -G column-formats で確認可能。

>> "%USERPROFILE%\Downloads\tls1.1.log" 標準出力

結果として以下のように出力される

 7947 2025-03-17 20:51:14.109253 x.x.x.x 51013 3.113.127.38 443 Client Hello
 7951 2025-03-17 20:51:14.109584 3.113.127.38 443 x.x.x.x 51013 Server Hello

まとめ - tsharkディスプレイフィルタ(tcp.payload)でTLS1.1通信をキャプチャする

tsharkオプション -M, -l, -w - | tshark -r -, -Y "tcp.payload[1:2] == 03:02 && tcp.payload[9:2] == 03:02" により、メモリー・DISK枯渇に対応した上で、ディスプレイフィルタによるTLS1.1通信の継続キャプチャができるようになった。

"C:\Program Files\Wireshark\tshark" -i "1" -f "host 3.113.127.38" -M 100000 -l -w - 2>>"%USERPROFILE%\Downloads\stderr.log" | "C:\Program Files\Wireshark\tshark" -r - -Y "tcp.payload[1:2] == 03:02 && tcp.payload[9:2] == 03:02" -o gui.column.format:"Packet,%m,Time,%Yt,Source,%us,SrcPort,%uS,Destination,%ud,DstPort,%uD,Info,%i" -M 1000000 -l >> "%USERPROFILE%\Downloads\tls1.1.log"