tshark 基本操作

安裝 Wireshark

tshark 是 Wireshark 的命令行界面,因此需安裝 Wireshark

1
scoop install extras/wireshark

安裝完成後,移動至 Wireshark 安裝目錄下點擊 npcap-installer.exe 進行 npcap 安裝

查詢版本

查詢目前 tshark 版本

1
tshark -v

網路介面

查詢網路介面

1
tshark -D

擷取封包

擷取封包資料,並匯出 pcap

1
tshark -i [網路介面編號] -b duration:[每隔幾秒執行] -c [指定擷取筆數] -w [匯出檔案].pcap

讀取 pcap 檔案

1
tshark -r [匯出檔案].pcap

顯示指定欄位,來源 IP、目的 IP 、 TCP port

1
tshark -i [網路介面編號] -T fields -e ip.src -e ip.dst -e tcp.port

解析 TLS

設定環境變數讓瀏覽器緩存 SSL KEY 至指定檔案

1
setx SSLKEYLOGFILE "C:\path\to\sslkeys.log"

緩存封包

1
tshark -i [網路介面編號] -w https_traffic.pcapng

使用指定 SSL KEY 讀取檔案

1
tshark -r https_traffic.pcapng -o tls.keylog_file:"C:\path\to\sslkeys.log"

解析 Body

解析 HTTP Body,存入變數 hex

1
2
3
4
$hex = tshark -r https_traffic.pcapng `
-Y 'http.request.method == "[HTTP的方法]" && http.request.uri == \"[URL連結]\"' `
-T fields -e http.file_data `
-o tls.keylog_file:"C:\path\to\sslkeys.log"

建立方法解析 Hex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function Convert-HexToUtf8String {
param([string]$hex)

# 移除所有非十六進位字元(保險做法)
$cleanHex = $hex -replace '[^0-9a-fA-F]', ''

if ($cleanHex.Length % 2 -ne 0) {
throw "Hex string 長度為奇數,無法正確解碼。"
}

$bytes = for ($i = 0; $i -lt $cleanHex.Length; $i += 2) {
[Convert]::ToByte($cleanHex.Substring($i, 2), 16)
}

return [System.Text.Encoding]::UTF8.GetString($bytes)
}

Hex 轉文字

1
2
$decoded = Convert-HexToUtf8String -hex $hex
echo $decoded

📜 參考資料

  1. tshark(1) Manual Page