網(wǎng)絡(luò)異常時如何抓取數(shù)據(jù)包
本文主要介紹了在Linux和Windows環(huán)境下如何使用抓包工具進(jìn)行抓包的操作步驟。
背景信息
如果源服務(wù)器訪問目標(biāo)服務(wù)器時出現(xiàn)異常,您可以抓包獲取最原始的交互數(shù)據(jù)進(jìn)行排查分析。在介紹常用的抓包工具以及如何抓包的詳細(xì)信息前,請參見以下文檔排查和分析問題。
Linux環(huán)境中的抓包工具
Linux環(huán)境中通常使用TCPDump工具進(jìn)行抓包和分析,TCPDump工具是所有Linux發(fā)行版本預(yù)裝的數(shù)據(jù)包抓取和分析工具。有關(guān)TCPDump工具的獲取和安裝方法,請參見TCPDump官方文檔。
關(guān)于tcpdump命令的說明如下所示(命令中區(qū)分大小寫)。
tcpdump [ -AbdDefhHIJKlLnNOpqStuUvxX# ] [ -B buffer_size ]
[ -c count ]
[ -C file_size ] [ -G rotate_seconds ] [ -F file ]
[ -i interface ] [ -j tstamp_type ] [ -m module ] [ -M secret ]
[ --number ] [ -Q in|out|inout ]
[ -r file ] [ -V file ] [ -s snaplen ] [ -T type ] [ -w file ]
[ -W filecount ]
[ -E spi@ipaddr algo:secret,... ]
[ -y datalinktype ] [ -z postrotate-command ] [ -Z user ]
[ --time-stamp-precision=tstamp_precision ]
[ --immediate-mode ] [ --version ]
[ expression ]
-s:用于設(shè)置數(shù)據(jù)包抓取長度。如果-s為0,則表示自動選擇合適的長度來抓取數(shù)據(jù)包。
-w:用于將抓包結(jié)果導(dǎo)出到文件,而不是在控制臺進(jìn)行分析和打印輸出。
-i:用于指定需要監(jiān)聽的接口(網(wǎng)卡)。
-vvv:用于輸出詳細(xì)的交互數(shù)據(jù)。
expression:是一個正則表達(dá)式,用于過濾報文,主要包含如下幾類:
指定類型的關(guān)鍵字:包括host(主機(jī))、net(網(wǎng)絡(luò))和port(端口)。
指定傳輸方向的關(guān)鍵字:包括src(源)、dst(目標(biāo))、dst or src(源或目標(biāo))和dst and src(源和目標(biāo))。
指定協(xié)議的關(guān)鍵字:包括ICMP、IP、ARP、RARP、TCP和UDP等協(xié)議類型。
關(guān)于其他參數(shù)說明及用法,請參見tcpdump的Manpage。
關(guān)于tcpdump命令常見用法和示例輸出的詳細(xì)信息如下:
執(zhí)行以下命令,抓取eth0網(wǎng)卡22端口的交互數(shù)據(jù)。
tcpdump -s 0 -i eth0 port 22
系統(tǒng)顯示類似如下。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 20:24:59.414951 IP 172.xx.xx.226.ssh > 42.xx.xx.107.43414: Flags [P.], seq 442372:442536, ack 53, win 141, length 164 20:24:59.415002 IP 172.xx.xx.226.ssh > 42.xx.xx.107.43414: Flags [P.], seq 442536:442700, ack 53, win 141, length 164 20:24:59.415052 IP 172.xx.xx.226.ssh > 42.xx.xx.107.43414: Flags [P.], seq 442700:442864, ack 53, win 141, length 164 20:24:59.415103 IP 172.xx.xx.226.ssh > 42.xx.xx.107.43414: Flags [P.], seq 442864:443028, ack 53, win 141, length 164
執(zhí)行以下命令,抓取eth1網(wǎng)卡發(fā)送給22端口的交互數(shù)據(jù),并在控制臺輸出詳細(xì)交互信息。
tcpdump -s 0 -i eth1 -vvv port 22
系統(tǒng)顯示類似如下。
tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes 20:24:20.991006 IP (tos 0x10, ttl 64, id 22747, offset 0, flags [DF], proto TCP (6), length 316) 172.xx.xx.226.ssh > 42.xx.xx.107.43414: Flags [P.], cksum 0x2504 (incorrect -> 0x270d), seq 133624:133900, ack 1, win 141, length 276 20:24:20.991033 IP (tos 0x0, ttl 53, id 2348, offset 0, flags [DF], proto TCP (6), length 92) 42.xx.xx.107.43414 > 172.xx.xx.226.ssh: Flags [P.], cksum 0x4759 (correct), seq 1:53, ack 129036, win 15472, length 52
執(zhí)行以下命令,抓取eth1網(wǎng)卡發(fā)送至指定IP地址的PING交互數(shù)據(jù),并輸出詳細(xì)交互數(shù)據(jù)。
tcpdump -s 0 -i eth1 -vvv dst 223.xx.xx.5 and icmp
系統(tǒng)顯示類似如下。
tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes 20:26:00.368958 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto ICMP (1), length 84) 172.xx.xx.226 > public1.alidns.com: ICMP echo request, id 55097, seq 341, length 64 20:26:01.369996 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto ICMP (1), length 84) 172.xx.xx.226 > public1.alidns.com: ICMP echo request, id 55097, seq 342, length 64 20:26:02.371058 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto ICMP (1), length 84) 172.xx.xx.226 > public1.alidns.com: ICMP echo request, id 55097, seq 343, length 64 20:26:03.372181 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto ICMP (1), length 84) 172.xx.xx.226 > public1.alidns.com: ICMP echo request, id 55097, seq 344, length 64
執(zhí)行以下命令,抓取系統(tǒng)內(nèi)所有接口數(shù)據(jù)并保存到指定文件。
tcpdump -i any -s 0 -w test.cap
說明您如果通過
cat
、vim
命令查看保存的文件,都顯示為亂碼,此時,您可以執(zhí)行tcpdump -r test.cap
命令,查看信息,您也可以使用Wireshark工具查看信息。系統(tǒng)顯示類似如下。
tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
Windows環(huán)境中的抓包工具
Windows環(huán)境中一般使用免費(fèi)的較為流行的Wireshark開源工具進(jìn)行抓包和分析。請參見Wireshark官方網(wǎng)站,獲取并安裝Wireshark工具,然后進(jìn)行抓包。
安裝并打開Wireshark工具。
選擇捕獲 > 選項。
在Wireshark 捕獲接口頁面,根據(jù)接口名稱或?qū)?yīng)的IP地址選擇需要進(jìn)行抓包的網(wǎng)卡,然后單擊開始。
抓取足量數(shù)據(jù)包后,選擇捕獲 > 停止。
選擇文件 > 保存,將抓包結(jié)果保存到指定文件。
更多有關(guān)Wireshark工具使用和數(shù)據(jù)分析方法,請參見Wireshark官方網(wǎng)站。
抓包分析流程
出現(xiàn)異常時,您可以抓取數(shù)據(jù)包進(jìn)行分析。抓包時請確保從源服務(wù)器和目標(biāo)服務(wù)器同時并發(fā)操作,以便進(jìn)行對比分析,具體操作步驟如下:
確認(rèn)源服務(wù)器和目標(biāo)服務(wù)器進(jìn)行數(shù)據(jù)交互通過的網(wǎng)卡。
如果源服務(wù)器通過NAT共享方式訪問公網(wǎng),則訪問淘寶IP地址庫,獲取本地網(wǎng)絡(luò)對應(yīng)的公網(wǎng)IP地址。
利用前文所述工具,從源服務(wù)器對目標(biāo)服務(wù)器地址的目標(biāo)端口和網(wǎng)卡進(jìn)行抓包,或者進(jìn)行完整抓包。
利用前文所述工具,從目標(biāo)服務(wù)器對源服務(wù)器地址和網(wǎng)卡進(jìn)行抓包,或者進(jìn)行完整抓包,然后進(jìn)行分析。