tcpdump能幫助我們捕捉並保存網絡包,保存下來的網絡包可用於分析網絡負載情況,包可通過tcpdump命令解析,也可以保存成后綴為pcap的文件,使用wireshark等軟件進行查看。
以下將給出9個使用tcpdump的例子,以說明tcpdump的具體使用方法。
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
Reference: Packet Analyzer: 15 TCPDUMP Command Examples