運維命令:tcpdump

 tcpdump命令html

 tcpdump 命令是一款sniffer工具,它能夠打印全部通過網絡接口的數據包的頭信息,也可使用 -w 選項將數據包保存到文件中,方便之後分析。linux

 

 經常使用參數:ios

-a:嘗試將網絡和廣播地址轉換成名稱;
-c<數據包數目>:收到指定的數據包數目後,就中止進行傾倒操做;
-d:把編譯過的數據包編碼轉換成可閱讀的格式,並傾倒到標準輸出;
-dd:把編譯過的數據包編碼轉換成C語言的格式,並傾倒到標準輸出;
-ddd:把編譯過的數據包編碼轉換成十進制數字的格式,並傾倒到標準輸出;
-e:在每列傾倒資料上顯示鏈接層級的文件頭;(顯示數據鏈路層信息)
-f:用數字顯示網際網絡地址;
-F<表達文件>:指定內含表達方式的文件;
-i<網絡界面>:使用指定的網絡截面送出數據包;
-l:使用標準輸出列的緩衝區;
-n:不把主機的網絡地址轉換成名字;
-N:不列出域名;
-O:不將數據包編碼最佳化;
-p:不讓網絡界面進入混雜模式;
-q :快速輸出,僅列出少數的傳輸協議信息;
-r<數據包文件>:從指定的文件讀取數據包數據;
-s<數據包大小>:設置每一個數據包的大小; -s : 抓取數據包時默認抓取長度爲68字節。加上-S 0 後能夠抓到完整的數據包
-S:用絕對而非相對數值列出TCP關聯數;
-t:在每列傾倒資料上不顯示時間戳記;
-tt: 在每列傾倒資料上顯示未經格式化的時間戳記;
-T<數據包類型>:強制將表達方式所指定的數據包轉譯成設置的數據包類型;
-v:詳細顯示指令執行過程;
-vv:更詳細顯示指令執行過程;
-x:用十六進制字碼列出數據包資料;(查看包內容)
-w<數據包文件>:把數據包數據寫入指定的文件。  

 

 實例:bash

直接啓動tcpdump將監視第一個網絡接口上全部流過的數據包
# tcpdump
監視指定網絡接口的數據包
# tcpdump -i eth1  

 若是不指定網卡,默認tcpdump只會監視第一個網絡接口,通常是eth0,下面的例子都沒有指定網絡接口。 服務器

  • 監視指定主機的數據包
打印全部進入或離開sundown的數據包。
# tcpdump host sundown
也能夠指定ip,例如截獲全部210.27.48.1 的主機收到的和發出的全部的數據包
# tcpdump host 210.27.48.1
打印helios 與 hot 或者與 ace 之間通訊的數據包
# tcpdump host helios and \( hot or ace \)
截獲主機210.27.48.1 和主機210.27.48.2 或210.27.48.3的通訊
# tcpdump host 210.27.48.1 and \ (210.27.48.2 or 210.27.48.3 \)
打印ace與任何其餘主機之間通訊的IP 數據包, 但不包括與helios之間的數據包.
# tcpdump ip host ace and not helios
若是想要獲取主機210.27.48.1除了和主機210.27.48.2以外全部主機通訊的ip包,使用命令:
# tcpdump ip host 210.27.48.1 and ! 210.27.48.2
截獲主機hostname發送的全部數據
# tcpdump -i eth0 src host hostname
監視全部送到主機hostname的數據包
# tcpdump -i eth0 dst host hostname  
  • 監視指定主機和端口的數據包
若是想要獲取主機210.27.48.1接收或發出的telnet包,使用以下命令
# tcpdump tcp port 23 host 210.27.48.1
對本機的udp 123 端口進行監視 123 爲ntp的服務端口
# tcpdump udp port 123
獲取本機arp包輸出信息
# tcpdump arp  

 

 基本語法: 網絡

  • 過濾主機
- 抓取全部通過 eth1,目的或源地址是 192.168.1.1 的網絡數據
# tcpdump -i eth1 host 192.168.1.1

- 源地址
# tcpdump -i eth1 src host 192.168.1.1

- 目的地址
# tcpdump -i eth1 dst host 192.168.1.1
  • 過濾端口
- 抓取全部通過 eth1,目的或源端口是 25 的網絡數據
# tcpdump -i eth1 port 25

- 源端口
# tcpdump -i eth1 src port 25

- 目的端口
# tcpdump -i eth1 dst port 25網絡過濾

--------

# tcpdump -i eth1 net 192.168
# tcpdump -i eth1 src net 192.168
# tcpdump -i eth1 dst net 192.168
  • 協議過濾
# tcpdump -i eth1 arp
# tcpdump -i eth1 ip
# tcpdump -i eth1 tcp
# tcpdump -i eth1 udp
# tcpdump -i eth1 icmp  
  • 經常使用表達式
非 : ! or "not" (去掉雙引號)
且 : && or "and"
或 : || or "or"

  

 實例: 運維

  • - 抓取全部通過 eth1,目的地址是 192.168.1.254 或 192.168.1.200 端口是 80 的 TCP 數據
# tcpdump -i eth1 '((tcp) and (port 80) and ((dst host 192.168.1.254) or (dst host 192.168.1.200)))'
  • - 抓取全部通過 eth1,目標 MAC 地址是 00:01:02:03:04:05 的 ICMP 數據
# tcpdump -i eth1 '((icmp) and ((ether dst host 00:01:02:03:04:05)))'
  • - 抓取全部通過 eth1,目的網絡是 192.168,但目的主機不是 192.168.1.200 的 TCP 數據
# tcpdump -i eth1 '((tcp) and ((dst net 192.168) and (not dst host 192.168.1.200)))'  
  • - 只抓 SYN 包
# tcpdump -i eth1 'tcp[tcpflags] = tcp-syn'
  • - 抓 SYN, ACK
# tcpdump -i eth1 'tcp[tcpflags] & tcp-syn != 0 and tcp[tcpflags] & tcp-ack != 0'
  • - 抓 SMTP 數據
# tcpdump -i eth1 '((port 25) and (tcp[(tcp[12]>>2):4] = 0x4d41494c))'
抓取數據區開始爲"MAIL"的包,"MAIL"的十六進制爲 0x4d41494c。  
  • - 抓 HTTP GET 數據
# tcpdump -i eth1 'tcp[(tcp[12]>>2):4] = 0x47455420'
"GET "的十六進制是 47455420
  • - 抓 SSH 返回
# tcpdump -i eth1 'tcp[(tcp[12]>>2):4] = 0x5353482D'
"SSH-"的十六進制是 0x5353482D

# tcpdump -i eth1 '(tcp[(tcp[12]>>2):4] = 0x5353482D) and (tcp[((tcp[12]>>2)+4):2]= 0x312E)'
抓老版本的 SSH 返回信息,如"SSH-1.99.."  
  • - 抓 DNS 請求數據
# tcpdump -i eth1 udp dst port 53  
  • - 其餘
-c 參數對於運維人員來講也比較經常使用,由於流量比較大的服務器,能夠用-c 參數指定抓多少個包。
# time tcpdump -nn -i eth0 'tcp[tcpflags] = tcp-syn' -c 10000 > /dev/null
上面的命令計算抓 10000 個 SYN 包花費多少時間,能夠判斷訪問量大概是多少。  
  • - 實時抓取端口號8000的GET包,而後寫入GET.log
# tcpdump -i eth0 '((port 8000) and (tcp[(tcp[12]>>2):4]=0x47455420))' -nnAl -w /tmp/GET.log  

 

 參考網站:tcp

http://man.linuxde.net/tcpdump
https://www.cnblogs.com/yc_sunniwell/archive/2010/07/05/1771563.html
https://www.cnblogs.com/chenpingzhao/p/9108570.html
https://www.cnblogs.com/qiumingcheng/p/8075283.html  
相關文章
相關標籤/搜索