Linux中使用wireshark分析tcpdump抓取的資料包

迷倪小魏發表於2018-01-18

很多時候我們的系統部署在Linux系統上面,在一些情況下定位問題就需要檢視各個系統之間傳送資料包文是否正常,下面就簡單講解一下如何使用wireshark分析tcpdump抓取的資料包。網路資料包截獲分析工具。支援針對網路層、協議、主機、網路或埠的過濾。並提供andornot等邏輯語句幫助去除無用的資訊。

 

1、首先,通過yum檢視tcpdumpwireshark所需要的軟體包

[root@wjq2 ~]# yum search tcpdump

Loaded plugins: product-id, refresh-packagekit, security, subscription-manager

This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.

================================================ N/S Matched: tcpdump =================================================

tcpdump.x86_64 : A network traffic monitoring tool

 

  Name and summary matches only, use "search all" for everything.

 

[root@wjq2 ~]# yum search wireshark

Loaded plugins: product-id, refresh-packagekit, security, subscription-manager

This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.

=============================================== N/S Matched: wireshark ================================================

wireshark-gnome.x86_64 : Gnome desktop integration for wireshark and wireshark-usermode

wireshark.i686 : Network traffic analyzer

wireshark.x86_64 : Network traffic analyzer

 

  Name and summary matches only, use "search all" for everything.

 

2、檢視tcpdumpwireshark的軟體包是否安裝,可以發現,tcpdump已經安裝,wireshark沒有安裝

[root@wjq2 ~]# rpm -qa|grep wireshark

[root@wjq2 ~]# rpm -qa | grep tcpdump

tcpdump-4.0.0-3.20090921gitdf3cb4.2.el6.x86_64

 

3、使用yum安裝wireshark

[root@wjq2 tmp]# yum install wireshark* -y

 

[root@wjq2 tmp]# which tcpdump

/usr/sbin/tcpdump

[root@wjq2 tmp]# which wireshark

/usr/sbin/wireshark

 

4、下面對tcpdump命令的使用做一個詳細的說明

 

tcpdump的命令格式

tcpdump的引數眾多,通過man tcpdumptcpdump -h可以檢視tcpdump的詳細說明,這邊只列一些自己常用的引數:

[root@wjq2 tmp]# tcpdump -h

tcpdump version 4.1-PRE-CVS_2012_02_01

libpcap version 1.0.0

Usage: tcpdump [-aAdDefIKlLnNOpqRStuUvxX] [ -B size ] [ -c count ]

                [ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]

                [ -i interface ] [ -M secret ] [ -r file ]

                [ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ]

                [ -y datalinktype ] [ -z command ] [ -Z user ]

                [ expression ]

 

tcpdump [-i 網路卡] -nnAX '表示式'

各引數說明如下:

-i:interface 監聽的網路卡。

-nn:表示以ipport的方式顯示來源主機和目的主機,而不是用主機名和服務。

-A:以ascii的方式顯示資料包,抓取web資料時很有用。

-X:資料包將會以16進位制和ascii的方式顯示。

表示式:表示式有很多種,常見的有:host 主機;port 埠;src host 發包主機;dst host 收包主機。多個條件可以用andor組合,取反可以使用。

 

下面是一些使用的例子

1)不指定任何引數,監聽第一塊網路卡上經過的資料包。主機上可能有不止一塊網路卡,所以經常需要指定網路卡。

tcpdump

 

2)監聽特定網路卡

tcpdump -i eth0

 

3)監聽特定主機:監聽本機跟主機10.1.1.123之間往來的通訊包。

備註:出、入的包都會被監聽。

tcpdump host 10.1.1.123

 

4)特定來源、目標地址的通訊

特定來源

tcpdump src host hostname

特定目標地址

tcpdump dst host hostname

如果不指定srcdst,那麼來源 或者目標 是hostname的通訊都會被監聽

tcpdump host hostname

 

5)特定埠

tcpdump port 3000

 

6)監聽TCP/UDP

伺服器上不同服務分別用了TCPUDP作為傳輸層,假如只想監聽TCP的資料包

tcpdump tcp

 

7)來源主機++TCP

A、監聽來自主機123.207.116.169在埠22上的TCP資料包

tcpdump tcp port 22 and src host 123.207.116.169

 

B、監聽特定主機之間的通訊

tcpdump ip host 210.27.48.1 and 210.27.48.2

 

C、210.27.48.1除了和210.27.48.2之外的主機之間的通訊

tcpdump ip host 210.27.48.1 and ! 210.27.48.2

 

8)稍微詳細點的例子

tcpdump tcp -i eth1 -t -s 0 -c 100 and dst port ! 22 and src net 192.168.1.0/24 -w ./target.cap

 

說明:

tcp: ip icmp arp rarp 和 tcpudpicmp這些選項等都要放到第一個引數的位置,用來過濾資料包的型別

-i eth1 : 只抓經過介面eth1的包

-t : 不顯示時間戳

-s 0 : 抓取資料包時預設抓取長度為68位元組。加上-S 0 後可以抓到完整的資料包

-c 100 : 只抓取100個資料包

dst port ! 22 : 不抓取目標埠是22的資料包

src net 192.168.1.0/24 : 資料包的源網路地址為192.168.1.0/24

-w ./target.cap : 儲存成cap檔案,方便用ethereal(wireshark)分析

 

9)限制抓包的數量

如下,抓到1000個包後,自動退出

tcpdump -c 1000

 

10)儲存到本地

備註:tcpdump預設會將輸出寫到緩衝區,只有緩衝區內容達到一定的大小,或者tcpdump退出時,才會將輸出寫到本地磁碟

 

tcpdump -n -vvv -c 1000 -w /tmp/tcpdump_save.cap

也可以加上-U強制立即寫到本地磁碟(一般不建議,效能相對較差)

 

 

11)儲存tcpdump抓包結果

[root@wjq2 tmp]# tcpdump -i eth0 -w eth0_dump.pcap

tcpdump: WARNING: eth0: no IPv4 address assigned

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

 

^C39 packets captured

39 packets received by filter

0 packets dropped by kernel

[root@wjq2 tmp]# ll -h eth0_dump.pcap

-rw-r--r-- 1 root root 3.4K Jan 18 11:19 eth0_dump.pcap

 

5、使用wireshark分析抓取的資料包:

[root@wjq2 tmp]# wireshark eth0_dump.pcap


上圖中標出三快區域:

紅色框內,是用來顯示簡單的資料包資訊,用tcpdump抓包如時候,預設情況是顯示成這樣的;

綠色框內,是用來顯示選中的資料包的詳細資訊,是按照TCP/IP四層結構顯示的,第一行是資料鏈路層的資訊,第二行是網路層資訊(IP協議),第三行是傳輸層資訊(TCP協議),第四層是應用層資訊(HTTP協議),可以展開第一行用來觀察具體的內容;

藍色框中,是用來顯示此資料包的真實面目。(下圖列更清楚一些)

 

 
作者:SEian.G(苦練七十二變,笑對八十一難)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31015730/viewspace-2150287/,如需轉載,請註明出處,否則將追究法律責任。

相關文章