歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux服務器 >> Linux下抓包工具tcpdump使用介紹

Linux下抓包工具tcpdump使用介紹

日期:2017/3/2 16:29:09   编辑:Linux服務器

  在傳統的網絡分析和測試技術中,嗅探器(sniffer)是最常見,也是最重要的技術之一。sniffer工具首先是為網絡管理員和網絡程序員進行網絡分析而設計的。

  匹配ether廣播包。ether廣播包的特征是mac全1.故如下即可匹配:

  tcpdump 'ether dst ff:ff:ff:ff:ff:ff'

  ylin@ylin:~$ sudo tcpdump -c 1 'ether dst ff:ff:ff:ff:ff:ff'

  tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

  listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

  10:47:57.784099 arp who-has 192.168.240.77 tell 192.168.240.189

  在此,只匹配1個包就退出了。第一個是arp請求包,arp請求包的是采用廣播的方式發送的,被匹配那是當之無愧的。

  匹配ether組播包,ether的組播包的特征是mac的最高位為1,其它位用來表示組播組編號,如果你想匹配其的多播組,知道它的組MAC地址即可。如

  tcpdump 'ether dst ' Mac_Address表示地址,填上適當的即可。如果想匹配所有的ether多播數據包,那麼暫時請放下,下面會繼續為你講解更高級的應用。

  (2)匹配arp包

  arp包用於IP到Mac址轉換的一種協議,包括arp請求和arp答應兩種報文,arp請求報文是ether廣播方式發送出去的,也即 arp請求報文的mac地址是全1,因此用ether dst FF;FF;FF;FF;FF;FF可以匹配arp請求報文,但不能匹配答應報文。因此要匹配arp的通信過程,則只有使用arp來指定協議。

  tcpdump 'arp' 即可匹配網絡上arp報文。

  ylin@ylin:~$ arping -c 4 192.168.240.1>/dev/null& sudo tcpdump -p 'arp'

  [1] 9293

  WARNING: interface is ignored: Operation not permitted

  tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

  listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

  11:09:25.042479 arp who-has 192.168.240.1 (00:03:d2:20:04:28 (oui Unknown)) tell ylin.local

  11:09:25.042702 arp reply 192.168.240.1 is-at 00:03:d2:20:04:28 (oui Unknown)

  11:09:26.050452 arp who-has 192.168.240.1 (00:03:d2:20:04:28 (oui Unknown)) tell ylin.local

  11:09:26.050765 arp reply 192.168.240.1 is-at 00:03:d2:20:04:28 (oui Unknown)

  11:09:27.058459 arp who-has 192.168.240.1 (00:03:d2:20:04:28 (oui Unknown)) tell ylin.local

  11:09:27.058701 arp reply 192.168.240.1 is-at 00:03:d2:20:04:28 (oui Unknown)

  11:09:33.646514 arp who-has ylin.local tell 192.168.240.1

  11:09:33.646532 arp reply ylin.local is-at 00:19:21:1d:75:e6 (oui Unknown)

  本例中使用arping -c 4 192.168.240.1產生arp請求和接收答應報文,而tcpdump -p 'arp'匹配出來了。此處-p選項是使網絡工作於正常模式(非混雜模式),這樣是方便查看匹配結果。

  (3)匹配IP包

  眾所周知,IP協議是TCP/IP協議中最重要的協議之一,正是因為它才能把Internet互聯起來,它可謂功不可沒,下面分析匹配IP包的表達式。

  對IP進行匹配

  tcpdump 'ip src 192.168.240.69'

  ylin@ylin:~$ sudo tcpdump -c 3 'ip src 192.168.240.69'

  tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

  listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

  11:20:00.973605 IP ylin.local.51486 > walnut.crossbeamsys.com.ssh: S 2706301341:2706301341(0) win 5840

  11:20:00.974328 IP ylin.local.32849 > 192.168.200.150.domain: 5858+ PTR? 20.200.168.192.in-addr.arpa. (45)

  11:20:01.243490 IP ylin.local.51486 > walnut.crossbeamsys.com.ssh: . ack 2762262674 win 183

  IP廣播組播數據包匹配:只需指明廣播或組播地址即可

  tcpdump 'ip dst 240.168.240.255'

  ylin@ylin:~$ sudo tcpdump 'ip dst 192.168.240.255'

  tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

  listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

  11:25:29.690658 IP dd.local > 192.168.240.255: ICMP echo request, id 10022, seq 1, length 64

  11:25:30.694989 IP dd.local > 192.168.240.255: ICMP echo request, id 10022, seq 2, length 64

  11:25:31.697954 IP dd.local > 192.168.240.255: ICMP echo request, id 10022, seq 3, length 64

  11:25:32.697970 IP dd.local > 192.168.240.255: ICMP echo request, id 10022, seq 4, length 64

  11:25:33.697970 IP dd.local > 192.168.240.255: ICMP echo request, id 10022, seq 5, length 64

  11:25:34.697982 IP dd.local > 192.168.240.255: ICMP echo request, id 10022, seq 6, length 64

  此處匹配的是ICMP的廣播包,要產生此包,只需要同一個局域網的另一台主機運行ping -b 192.168.240.255即可,當然還可產生組播包,由於沒有適合的軟件進行模擬產生,在此不舉例子。

  (4)匹配TCP數據包

  TCP同樣是TCP/IP協議棧裡面最為重要的協議之一,它提供了端到端的可靠數據流,同時很多應用層協議都是把TCP作為底層的通信協議,因為TCP的匹配是非常重要的。

  如果想匹配HTTP的通信數據,那只需指定匹配端口為80的條件即可

  tcpdump 'tcp dst port 80'

  ylin@ylin:~$ wget http://www.baidu.com 2>1 1 >/dev/null & sudo tcpdump -c 5 'tcp port 80'

  [1] 10762

  tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

  listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

  12:02:47.549056 IP xd-22-43-a8.bta.net.cn.www > ylin.local.47945: S 1202130469:1202130469(0) ack 1132882351 win 2896

  12:02:47.549085 IP ylin.local.47945 > xd-22-43-a8.bta.net.cn.www: . ack 1 win 183

  12:02:47.549226 IP ylin.local.47945 > xd-22-43-a8.bta.net.cn.www: P 1:102(101) ack 1 win 183

  12:02:47.688978 IP xd-22-43-a8.bta.net.cn.www > ylin.local.47945: . ack 102 win 698

  12:02:47.693897 IP xd-22-43-a8.bta.net.cn.www > ylin.local.47945: . 1:1409(1408) ack 102 win 724

  (5)匹配udp數據包

  udp是一種無連接的非可靠的用戶數據報,因此udp的主要特征同樣是端口,用如下方法可以匹配某一端口

  tcpdump 'upd port 53' 查看DNS的數據包

  ylin@ylin:~$ ping -c 1 www.baidu.com > /dev/null& sudo tcpdump -p udp port 53

  [1] 11424

  tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

  listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

  12:28:09.221950 IP ylin.local.32853 > 192.168.200.150.domain: 63228+ PTR? 43.22.108.202.in-addr.arpa. (44)

  12:28:09.222607 IP ylin.local.32854 > 192.168.200.150.domain: 5114+ PTR? 150.200.168.192.in-addr.arpa. (46)

  12:28:09.487017 IP 192.168.200.150.domain > ylin.local.32853: 63228 1/0/0 (80)

  12:28:09.487232 IP 192.168.200.150.domain > ylin.local.32854: 5114 NXDomain* 0/1/0 (140)

  12:28:14.488054 IP ylin.local.32854 > 192.168.200.150.domain: 60693+ PTR? 69.240.168.192.in-addr.arpa. (45)

  12:28:14.755072 IP 192.168.200.150.domain > ylin.local.32854: 60693 NXDomain 0/1/0 (122)

  使用ping www.baidu.com目標是產生DNS請求和答應,53是DNS的端口號。

  此外還有很多qualitifer是還沒有提及的,下面是其它合法的primitive,在tcpdump中是可以直接使用的。

  gateway host

  匹配使用host作為網關的數據包,即數據報中mac地址(源或目的)為host,但IP報的源和目的地址不是host的數據包。

  dst net net

  src net net

  net net

  net net mask netmask

  net net/len

  匹配IPv4/v6地址為net網絡的數據報。

  其中net可以為192.168.0.0或192.168這兩種形式。如net 192.168 或net 192.168.0.0

  net net mask netmask僅對IPv4數據包有效,如net 192.168.0.0 mask 255.255.0.0

  net net/len同樣只對IPv4數據包有效,如net 192.168.0.0/16

  dst portrange port1-port2

  src portrange port1-port2

  portrange port1-port2

  匹配端口在port1-port2范圍內的ip/tcp,ip/upd,ip6/tcp和ip6/udp數據包。dst, src分別指明源或目的。沒有則表示src or dst

  less length 匹配長度少於等於length的報文。

  greater length 匹配長度大於等於length的報文。

  ip protochain protocol 匹配ip報文中protocol字段值為protocol的報文

  ip6 protochain protocol 匹配ipv6報文中protocol字段值為protocol的報文

  如tcpdump 'ip protochain 6 匹配ipv4網絡中的TCP報文,與tcpdump 'ip && tcp'用法一樣,這裡的&&連接兩個primitive。6是TCP協議在IP報文中的編號。

  ether broadcast

  匹配以太網廣播報文

  ether multicast

  匹配以太網多播報文

  ip broadcast

  匹配IPv4的廣播報文。也即IP地址中主機號為全0或全1的IPv4報文。

  ip multicast

  匹配IPv4多播報文,也就是IP地址為多播地址的報文。

  ip6 multicast

  匹配IPv6多播報文,即IP地址為多播地址的報文。

  vlan vlan_id

  匹配為vlan報文 ,且vlan號為vlan_id的報文

  到些為此,我們一直在介紹primitive是如何使用的,也即expression只有一個primitive。通過學會寫好每個primtive,我們就很容易把多個primitive組成一個expression,方法很簡單,通過邏輯運算符連接起來就可以了,邏輯運算符有以下三個:

  “&&” 或”and”

  “||” 或“or”

  “!” 或“not”

  並且可通過()進行復雜的連接運算。

  如tcpdump ‘ip && tcp’

  tcpdump ‘ host 192.168.240.3 &&( tcp port 80 || tcp port 443)’

  通過上面的各種primitive,我們可以寫出很豐富的條件,如ip, tcp, udp,vlan等等。如IP,可以按址址進行匹,tcp/udp可以按端口匹配。但是,如果我想匹配更細的條件呢?如tcp中只含syn標志,fin標志的報文呢?上面的primitive恐怕無能為力了。不用怕,tcpdump為你提供最後一個功能最強大的primitive,記住是primitive,而不是expression。你可以用多個這個的primitive組成更復雜的 expression.

Copyright © Linux教程網 All Rights Reserved