tcpdump抓包使用詳解

tcpdump能幫助我們捕捉並保存網絡包,保存下來的網絡包可用於分析網絡負載情況,包可通過tcpdump命令解析,也可以保存成後綴爲pcap的文件,使用wireshark等軟件進行查看。

1.針對特定網口抓包(-i選項)

當我們不加任何選項執行tcpdump時,tcpdump將抓取通過所有網口的包;使用-i選項,我們可以在某個指定的網口抓包:

linux:/tmp/lx # tcpdump -i eth0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
10:50:28.607429 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 116 win 64951
10:50:28.607436 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 116:232(116) ack 1 win 12864
10:50:30.384195 arp who-has 128.128.128.35 tell 128.128.128.35

以上例子中,tcpdump抓取所有通過eth0的包。

2.抓取指定數目的包(-c選項)

默認情況下tcpdump將一直抓包,直到按下”ctrl+c”中止,使用-c選項我們可以指定抓包的數量:

linux:/tmp/lx # tcpdump -c 2 -i eth0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
10:58:05.656104 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 1210443473:1210443589(116) ack 2583117929 win 12864
10:58:05.657074 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 116 win 65211

2 packets captured
6 packets received by filter
0 packets dropped by kernel

以上例子中,只針對eth0網口抓2個包。

3.將抓到包寫入文件中(-w選項)

使用-w選項,我們可將抓包記錄到一個指定文件中,以供後續分析

linux:/tmp/lx # tcpdump -w 20120606.pcap -i eth0
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

75 packets captured
150 packets received by filter
0 packets dropped by kernel

應當保存爲.pcap後綴的文件,方便我們使用wireshark等工具讀取分析。

4.讀取tcpdump保存文件(-r選項)

對於保存的抓包文件,我們可以使用-r選項進行讀取:

linux:/tmp/lx # tcpdump -r 20120606.pcap
reading from file 20120606.pcap, link-type EN10MB (Ethernet)
11:01:57.392907 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 1210446405:1210446457(52) ack 2583119957 win 12864
11:01:57.392917 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 52:168(116) ack 1 win 12864
11:01:57.393649 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 52 win 65327

5.抓包時不進行域名解析(-n選項)

默認情況下,tcpdump抓包結果中將進行域名解析,顯示的是域名地址而非ip地址,使用-n選項,可指定顯示ip地址。

6.增加抓包時間戳(-tttt選項)

使用-tttt選項,抓包結果中將包含抓包日期:

linux:/tmp/lx # tcpdump -n -tttt -i eth0
2012-06-06 11:14:59.539736 IP 10.71.171.140.22 > 10.70.121.95.1787: P 1:53(52) ack 100 win 7504
2012-06-06 11:14:59.539754 IP 10.71.171.140.22 > 10.70.121.95.1787: P 53:105(52) ack 100 win 7504
2012-06-06 11:14:59.539770 IP 10.71.171.140.22 > 10.70.121.95.1787: P 105:157(52) ack 100 win 7504

7.指定抓包的協議類型

我們可以只抓某種協議的包,tcpdump支持指定以下協議:ip,ip6,arp,tcp,udp,wlan等。以下例子只抓取arp協議的包

linux:/tmp/lx # tcpdump -i eth0 arp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
11:22:26.948656 arp who-has 10.10.1.30 tell 10.10.1.26
11:22:27.017406 arp who-has 10.10.1.30 tell 10.10.1.26
11:22:27.078803 arp who-has 10.10.1.30 tell 10.10.1.26

8.指定抓包端口

如果想要對某個特定的端口抓包,可以通過以下命令:

linux:/tmp/lx # tcpdump -i eth0 port 22
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
11:41:04.387547 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 1216136825 win 64751
11:41:04.387891 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 1:233(232) ack 0 win 16080
11:41:04.398973 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: P 0:52(52) ack 233 win 64519

9.抓取特定目標ip和端口的包

網絡包的內容中,包含了源ip地址、端口和目標ip、端口,我們可以根據目標ip和端口過濾tcpdump抓包結果,以下命令說明了此用法:

linux:/tmp/lx # tcpdump -i eth0 dst 10.70.121.92 and port 22

  1. tcpdump 與wireshark

Wireshark(以前是ethereal)是Windows下非常簡單易用的抓包工具(也有Linux版本)。但在Linux下很難找到一個好用的圖形化抓包工具。
還好有Tcpdump。我們可以用Tcpdump + Wireshark 的完美組合實現:在 Linux 裏抓包,然後在Windows 裏分析包。

總結:

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

(1)tcp: ip icmp arp rarp 和 tcp、udp、icmp這些選項等都要放到第一個參數的位置,用來過濾數據報的類型
(2)-i eth1 : 只抓經過接口eth1的包
(3)-t : 不顯示時間戳
(4)-s 0 : 抓取數據包時默認抓取長度爲68字節。加上-S 0 後可以抓到完整的數據包
(5)-c 100 : 只抓取100個數據包
(6)dst port ! 22 : 不抓取目標端口是22的數據包
(7)src net 192.168.1.0/24 : 數據包的源網絡地址爲192.168.1.0/24
(8)-w ./target.cap : 保存成cap文件,方便用ethereal(即wireshark)分析

tcpdump -i eth0 -nnX port 21
選項:
-i 指定網卡接口
-nn 將數據包中的域名與服務轉爲ip和端口
-X 以十六進制和ASCII碼的顯示數據包內容
port 指定監聽端口


[root@www ~]# tcpdump -i eth0 -nnX port 21
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
17:54:08.047349 IP 59.37.125.48.3128 > 172.16.0.11.21: Flags [S], seq 913042176, win 8192, options [mss 1412,nop,wscale 0,nop,nop,sackOK], length 0
    0x0000:  4500 0034 01f4 4000 3106 e35f 3b25 7d30  [email protected].._;%}0
    0x0010:  ac10 000b 0c38 0015 366b eb00 0000 0000  .....8..6k......
    0x0020:  8002 2000 bd1e 0000 0204 0584 0103 0300  ................
    0x0030:  0101 0402                                ....
17:54:08.047378 IP 172.16.0.11.21 > 59.37.125.48.3128: Flags [S.], seq 3851070068, ack 913042177, win 14600, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
    0x0000:  4500 0034 0000 4000 4006 d653 ac10 000b  E..4..@[email protected]....
    0x0010:  3b25 7d30 0015 0c38 e58a aa74 366b eb01  ;%}0...8...t6k..
    0x0020:  8012 3908 13cf 0000 0204 05b4 0101 0402  ..9.............
    0x0030:  0103 0307             
```                   ....
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章