Linux 基礎教程 30-tcpdump命令-2

Surpassme發表於2018-07-30

    在上一節講了一些tcpdump常用功能,本期我們繼續學習tcpdump後續功能。

流量過濾

    tcpdump不僅支援單個過濾表示式過濾,也還支援多個過濾表示式。但需要注意的是傳入的過濾表示式如含有Shell萬用字元,需將過濾表示式放在單引號內,以預防Shell對其進行解釋和通配。如果需要深入學習過濾表示式,可以使用以下命令進行檢視:

man pcap-filter

1、只抓取UDP資料包

[root@localhost ~]# tcpdump -i ens5f1 -c 2 `udp`
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
14:37:38.852304 IP gateway.35428 > 239.255.255.250.ssdp: UDP, length 395
14:37:38.852458 IP gateway.35428 > 239.255.255.250.ssdp: UDP, length 467
2 packets captured
15 packets received by filter
0 packets dropped by kernel

tcpdump另外還支援如下的協議型別:

ip、ip6、arp、tcp、rarp

2、抓取固定來源的資料包
    對一臺主機而言,後臺執行的網路有很多個,所連線的伺服器也會有很多個。那如果我們只想檢視某一個伺服器和主機的資料包情況了?在tcpdump中也支援這樣的資料包抓包,但需要設定src(source)dst(destination)就可以了,同時tcpdum也支援各條件間邏輯運算,如andor等。
    如果tcpdump未指定dst和src,則預設全匹配,而不區分是dst還是src

[root@localhost ~]# tcpdump -i ens5f1 -t -c 5 `udp` or `dst 114.114.114.114` or `src 192.168.8.1`
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
IP6 fe80::b072:b447:2702:3fca.55407 > ff02::1:3.hostmon: UDP, length 33
IP 192.168.8.101.52468 > 224.0.0.252.hostmon: UDP, length 33
IP localhost.localdomain.52349 > gateway.domain: 14923+ PTR? 3.0.0.0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.f.f.ip6.arpa. (90)
IP gateway.domain > localhost.localdomain.52349: 14923 NXDomain 0/1/0 (154)
IP localhost.localdomain.43621 > gateway.domain: 25497+ PTR? a.c.f.3.2.0.7.2.7.4.4.b.2.7.0.b.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa. (90)
5 packets captured
14 packets received by filter
0 packets dropped by kernel

3、抓取特定埠的資料包

[root@localhost ~]# tcpdump -i ens5f1 -c 10 `src port 53 and udp or dst port 80`
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
15:05:42.618107 IP localhost.localdomain.32980 > tsa03s06-in-f14.1e100.net.http: Flags [S], seq 712530441, win 29200, options [mss 1460,sackOK,TS val 792506576 ecr 0,nop,wscale 7], length 0
15:05:42.623275 IP gateway.domain > localhost.localdomain.51286: 19244 1/0/0 PTR tsa03s06-in-f14.1e100.net. (85)
15:05:42.627834 IP gateway.domain > localhost.localdomain.57579: 54387 NXDomain* 0/1/0 (101)
15:05:42.632411 IP gateway.domain > localhost.localdomain.37002: 35102 NXDomain* 0/1/0 (101)
15:05:43.259504 IP gateway.domain > localhost.localdomain.44708: 24032 1/0/0 A 116.62.82.159 (50)
15:05:43.261047 IP gateway.domain > localhost.localdomain.44708: 36078 0/1/0 (107)
15:05:43.337416 IP gateway.domain > localhost.localdomain.37886: 32328 1/0/0 A 118.178.109.187 (53)
15:05:43.339220 IP gateway.domain > localhost.localdomain.37886: 47697 0/1/0 (110)
15:05:43.344380 IP gateway.domain > localhost.localdomain.45436: 31901 6/0/0 CNAME images0.cnblogs.com.cdn.dnsv1.com., CNAME 1069954.p23.tc.cdntip.com., A 180.101.217.192, A 180.101.217.119, A 180.101.217.115, A 180.153.100.201 (181)
15:05:43.356015 IP gateway.domain > localhost.localdomain.45436: 24484 2/1/0 CNAME images0.cnblogs.com.cdn.dnsv1.com., CNAME 1069954.p23.tc.cdntip.com. (174)
10 packets captured
13 packets received by filter
0 packets dropped by kernel

tcpdump還支援的過濾型別如下所示:

- port:埠
- host:主機,如`host 112.65.12.245`
- portranger:埠範圍,如`dst portrange 0-22`
- net:網路段,如`dst net 192.168`
  如未設定過濾型別,則預設為主機(host)

4、抓取屬於兩臺伺服器之間的資料包

[root@localhost ~]# tcpdump -i ens5f1 -c 10 `host 192.168.8.8 and (www.cnblogs.com or www.baidu.com)`
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
15:23:03.918266 IP localhost.localdomain.47234 > 115.239.210.27.https: Flags [P.], seq 660681023:660681721, ack 812631894, win 238, length 698
15:23:03.930901 IP 115.239.210.27.https > localhost.localdomain.47234: Flags [.], ack 698, win 944, length 0
15:23:03.939766 IP 115.239.210.27.https > localhost.localdomain.47234: Flags [.], seq 1:1461, ack 698, win 944, length 1460
15:23:03.939783 IP localhost.localdomain.47234 > 115.239.210.27.https: Flags [.], ack 1461, win 261, length 0
15:23:03.939950 IP 115.239.210.27.https > localhost.localdomain.47234: Flags [.], seq 1461:2921, ack 698, win 944, length 1460
15:23:03.939961 IP localhost.localdomain.47234 > 115.239.210.27.https: Flags [.], ack 2921, win 284, length 0
15:23:03.940137 IP 115.239.210.27.https > localhost.localdomain.47234: Flags [P.], seq 2921:4030, ack 698, win 944, length 1109
15:23:03.940146 IP localhost.localdomain.47234 > 115.239.210.27.https: Flags [.], ack 4030, win 307, length 0
15:23:03.940484 IP 115.239.210.27.https > localhost.localdomain.47234: Flags [P.], seq 4030:4155, ack 698, win 944, length 125
15:23:03.940496 IP localhost.localdomain.47234 > 115.239.210.27.https: Flags [.], ack 4155, win 307, length 0
10 packets captured
62 packets received by filter
0 packets dropped by kernel

常用協議資料包格式

    在此對協議不做深入講解,僅列出TCP/IP的資料包格式,有興趣的話,可以自行查閱相關資料。

  • IP協議資料包格式如下所示:
    IP協議資料包.jpg

  • 常用協議編號如下所示:
    常用協議編號.jpg

  • TCP協議資料包格式如下所示:

TCP協議資料包.jpg

  • UDP協議資料包格式如下所示:

UDP協議資料包.jpg

抓包進階

    在明白各常用協議的資料包格式後,我們先來看看以下幾個示例:
1、抓取本機與百度之間TCP三次握手中的第一個網路包,且排除主機為cnblogs的資料包

[root@localhost ~]# tcpdump -i ens5f1 -c 5 `tcp[tcpflags] & tcp-syn != 0 and dst host www.baidu.com and not dst host www.cnblogs.com`
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
15:39:26.311628 IP localhost.localdomain.53062 > 115.239.211.112.https: Flags [S], seq 735716502, win 29200, options [mss 1460,sackOK,TS val 794530269 ecr 0,nop,wscale 7], length 0
15:39:26.505795 IP localhost.localdomain.53078 > 115.239.211.112.https: Flags [S], seq 4031881731, win 29200, options [mss 1460,sackOK,TS val 794530463 ecr 0,nop,wscale 7], length 0
15:39:26.505863 IP localhost.localdomain.53080 > 115.239.211.112.https: Flags [S], seq 867030794, win 29200, options [mss 1460,sackOK,TS val 794530463 ecr 0,nop,wscale 7], length 0
15:39:29.222164 IP localhost.localdomain.53100 > 115.239.211.112.https: Flags [S], seq 4207431041, win 29200, options [mss 1460,sackOK,TS val 794533180 ecr 0,nop,wscale 7], length 0
15:39:29.235246 IP localhost.localdomain.53102 > 115.239.211.112.https: Flags [S], seq 548060444, win 29200, options [mss 1460,sackOK,TS val 794533193 ecr 0,nop,wscale 7], length 0
5 packets captured
15 packets received by filter
0 packets dropped by kernel

2、抓取IP包長度大於500的資料包

[root@localhost ~]# tcpdump -i ens5f1 -c 5 `ip[2:2]>500`
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
15:35:27.613243 IP localhost.localdomain.5902 > 112.65.12.245.35445: Flags [.], seq 1864189501:1864198141, ack 2730205778, win 229, length 8640
15:35:27.613255 IP localhost.localdomain.5902 > 112.65.12.245.35445: Flags [P.], seq 8640:9410, ack 1, win 229, length 770
15:35:28.181058 IP localhost.localdomain.5902 > 112.65.12.245.35445: Flags [.], seq 9410:13730, ack 11, win 229, length 4320
15:35:28.181073 IP localhost.localdomain.5902 > 112.65.12.245.35445: Flags [.], seq 13730:18050, ack 11, win 229, length 4320
15:35:28.181090 IP localhost.localdomain.5902 > 112.65.12.245.35445: Flags [.], seq 18050:22370, ack 11, win 229, length 4320
5 packets captured
6 packets received by filter
0 packets dropped by kernel

proto[expr:size]詳細解釋如下所示:

proto:就是protocol的縮寫,表示協議的名稱。可以指定的協議型別大致如下:

- ether:鏈路層協議  - fddi:鏈路層協議
- wlan:鏈路層協議   - ppp:鏈路層協議
- link:鏈路層協議   - ip:網路層
- arp:網路層       - rarp:網路層
- icmp:網路層      - ip6:網路層
- tcp:傳輸層       - udp:傳輸層

expr:用來指定資料包偏移量,表示從某個協議資料包的第多個位開始提取內容,預設起始位置為0
size:表示從expr之後提取多少個位元組

- 如果只設定expr,而未設定size,則預設提取1個位元組
- ip[2:2]:則提取第3和第4個位元組
- ip[0]:則提取ip協議的第1個位元組

比較運算子:常用的比較運算子如下所示:

 <  >  <=  >=  =  !=

為了增加可讀性,TCP協議資料包頭中有6個tcpflags標誌欄位域,在tcpdump中可以用以下方式進行表達:

tcp-urg tcp-ack tcp-psuh tcp-rst tcp-syn tcp-fin

邏輯運算子:如果一個過濾表示式中包含多個過濾表示式,則可以使用邏輯運算子進行串聯

- ! / not:表示非關係
- && / and :表示與關係
- || / or:表示或關係

要詳細理解上面的這些示例,需要對常用協議的資料包格式有足夠的瞭解和掌握。

[參考資料]
1.計算機網路(第七版) 謝希仁

本文同步在微信訂閱號上釋出,如各位小夥伴們喜歡我的文章,也可以關注我的微信訂閱號:woaitest,或掃描下面的二維碼新增關注:
MyQRCode.jpg

相關文章