【轉載】tcpdump簡明教程

tcpdump簡明教程

本文將會持續修正和更新,最新內容請參考個人 GITHUB 上的 程序猿成長計劃 項目,歡迎 Star,更多精彩內容請 follow mehtml

本文翻譯自 A tcpdump Tutorial and Primer with Examples 一文,在使用Linux系統進行網絡抓包分析的時候,一直沒有找到比較簡便的非圖形界面的方法,在Linux系統下tcpdump命令確實是一柄利器,可是一直苦於學習成本較高,遲遲沒有下手。看了 A tcpdump Tutorial and Primer with Examples 這篇文章以後,發現其實使用tcpdump也沒有那麼困難,特別是其導出的cap文件,再使用wireshark等圖形界面軟件打開分析很是方便。所以,將其翻譯出來,一方面方便本身學習,一方面也爲像我同樣對tcpdump感興趣的人提供一個學習途徑。linux

概述

對於專業的信息安全人員來講,tcpdump 是很是重要的網絡分析工具。對於任何想深刻理解TCP/IP的人來講,掌握該工具的使用時很是必要的。不少人更喜歡高級的分析工具,好比Wireshark,但我相信一般狀況下這是個錯誤的選擇。git

當使用工具對網絡進行分析的時候,更重要的是人對結果的分析,而不是應用的分析。這就促使了對TCP/IP協議棧的理解,所以,我強烈建議學會使用 tcpdump 代替其它工具。github

      
      
      
      
  1. 15:31:34.079416 IP (tos 0x0, ttl 64, id 20244, offset 0, flags [DF],
  2. proto: TCP (6), length: 60) source.35970 > dest.80: S, cksum 0x0ac1
  3. (correct), 2647022145:2647022145(0) win 5840 0x0000: 4500 003c 4f14
  4. 4006 7417 0afb 0257 E.. 0x0010: 4815 222a 8c82 0050 9dc6 5a41 0000
  5. 0000 H."*...P..ZA.... 0x0020: a002 16d0 0ac1 0000 0204 05b4
  6. 0402 080a ................ 0x0030: 14b4 1555 0000 0000 0103 0302
15:31:34.079416 IP (tos 0x0, ttl 64, id 20244, offset 0, flags [DF], 
proto: TCP (6), length: 60) source.35970 > dest.80: S, cksum 0x0ac1 
(correct), 2647022145:2647022145(0) win 5840 0x0000: 4500 003c 4f14 
4006 7417 0afb 0257  E..  0x0010: 4815 222a 8c82 0050 9dc6 5a41 0000 
0000  H."*...P..ZA.... 0x0020: a002 16d0 0ac1 0000 0204 05b4 
0402 080a  ................ 0x0030: 14b4 1555 0000 0000 0103 0302
15:31:34.079416 IP (tos 0x0, ttl 64, id 20244, offset 0, flags [DF], proto: TCP (6), length: 60) source.35970 > dest.80: S, cksum 0x0ac1 (correct), 2647022145:2647022145(0) win 5840 0x0000: 4500 003c 4f14 4006 7417 0afb 0257 E.. 0x0010: 4815 222a 8c82 0050 9dc6 5a41 0000 0000 H."*...P..ZA.... 0x0020: a002 16d0 0ac1 0000 0204 05b4 0402 080a ................ 0x0030: 14b4 1555 0000 0000 0103 0302

TABLE 1. 原生 TCP/IP 輸出shell

基礎

下面是一些用來配置 tcpdump 的選項,它們很是容易被遺忘,也容易和其它類型的過濾器好比Wireshark等混淆。安全

選項

  • -i any 監聽全部的網卡接口,用來查看是否有網絡流量
  • -i eth0 只監聽eth0網卡接口
  • -D 顯示可用的接口列表
  • -n 不要解析主機名
  • -nn 不要解析主機名或者端口名
  • -q 顯示更少的輸出(更加quiet)
  • -t 輸出可讀的時間戳
  • -tttt 輸出最大程度可讀的時間戳
  • -X 以hex和ASCII兩種形式顯示包的內容
  • -XX-X相似,增長以太網header的顯示
  • -v, -vv, -vvv 顯示更加多的包信息
  • -c 只讀取x個包,而後中止
  • -s 指定每個包捕獲的長度,單位是byte,使用-s0能夠捕獲整個包的內容
  • -S 輸出絕對的序列號
  • -e 獲取以太網header
  • -E 使用提供的祕鑰解密IPSEC流量

表達式

tcpdump中,可使用表達式過濾指定類型的流量。有三種主要的表達式類型:typedirproto網絡

  • 類型(type)選項包含:hostnetport
  • 方向(dir)選項包含:srcdst
  • 協議(proto)選項包含:tcpudpicmpah

示例

捕獲全部流量

查看全部網卡接口上發生了什麼app

      
      
      
      
  1. tcpdump -i any
tcpdump -i any
tcpdump -i any

指定網卡接口

查看指定網卡上發生了什麼less

      
      
      
      
  1. tcpdump -i eth0
tcpdump -i eth0
tcpdump -i eth0

原生輸出

查看更多的信息,不解析主機名和端口號,顯示絕對序列號,可讀的時間戳dom

      
      
      
      
  1. tcpdump -ttttnnvvS
tcpdump -ttttnnvvS
tcpdump -ttttnnvvS

查看指定IP的流量

這是最多見的方式,這裏只查看來自或者發送到IP地址1.2.3.4的流量。

      
      
      
      
  1. tcpdump host 1.2.3.4
tcpdump host 1.2.3.4
tcpdump host 1.2.3.4

查看更多的包信息,輸出HEX

當你須要查看包中的內容時,使用hex格式輸出是很是有用的。

      
      
      
      
  1. # tcpdump -nnvXSs 0 -c1 icmp
  2. tcpdump: data link type PKTAP
  3. tcpdump: listening on pktap, link-type PKTAP (Apple DLT_PKTAP), capture size 262144 bytes
  4. 16:08:16.791604 IP (tos 0x0, ttl 64, id 34318, offset 0, flags [none], proto ICMP (1), length 56)
  5. 192.168.102.35 > 114.114.114.114: ICMP 192.168.102.35 udp port 50694 unreachable, length 36
  6. IP (tos 0x0, ttl 152, id 0, offset 0, flags [none], proto UDP (17), length 112)
  7. 114.114.114.114.53 > 192.168.102.35.50694: [|domain]
  8. 0x0000: 5869 6c88 7f64 784f 4392 ed7e 0800 4500 Xil..dxOC..~..E.
  9. 0x0010: 0038 860e 0000 4001 e906 c0a8 6623 7272 .8....@.....f#rr
  10. 0x0020: 7272 0303 3665 0000 0000 4500 0070 0000 rr..6e....E..p..
  11. 0x0030: 0000 9811 16cd 7272 7272 c0a8 6623 0035 ......rrrr..f#.5
  12. 0x0040: c606 005c 0000 ...\..
  13. 1 packet captured
  14. 357 packets received by filter
  15. 0 packets dropped by kernel
# tcpdump -nnvXSs 0 -c1 icmp

tcpdump: data link type PKTAP
tcpdump: listening on pktap, link-type PKTAP (Apple DLT_PKTAP), capture size 262144 bytes
16:08:16.791604 IP (tos 0x0, ttl 64, id 34318, offset 0, flags [none], proto ICMP (1), length 56)
    192.168.102.35 > 114.114.114.114: ICMP 192.168.102.35 udp port 50694 unreachable, length 36
    IP (tos 0x0, ttl 152, id 0, offset 0, flags [none], proto UDP (17), length 112)
    114.114.114.114.53 > 192.168.102.35.50694: [|domain]
    0x0000:  5869 6c88 7f64 784f 4392 ed7e 0800 4500  Xil..dxOC..~..E.
    0x0010:  0038 860e 0000 4001 e906 c0a8 6623 7272  .8....@.....f#rr
    0x0020:  7272 0303 3665 0000 0000 4500 0070 0000  rr..6e....E..p..
    0x0030:  0000 9811 16cd 7272 7272 c0a8 6623 0035  ......rrrr..f#.5
    0x0040:  c606 005c 0000                           ...\..
1 packet captured
357 packets received by filter
0 packets dropped by kernel
# tcpdump -nnvXSs 0 -c1 icmp tcpdump: data link type PKTAP tcpdump: listening on pktap, link-type PKTAP (Apple DLT_PKTAP), capture size 262144 bytes 16:08:16.791604 IP (tos 0x0, ttl 64, id 34318, offset 0, flags [none], proto ICMP (1), length 56) 192.168.102.35 > 114.114.114.114: ICMP 192.168.102.35 udp port 50694 unreachable, length 36 IP (tos 0x0, ttl 152, id 0, offset 0, flags [none], proto UDP (17), length 112) 114.114.114.114.53 > 192.168.102.35.50694: [|domain] 0x0000: 5869 6c88 7f64 784f 4392 ed7e 0800 4500 Xil..dxOC..~..E. 0x0010: 0038 860e 0000 4001 e906 c0a8 6623 7272 .8....@.....f#rr 0x0020: 7272 0303 3665 0000 0000 4500 0070 0000 rr..6e....E..p.. 0x0030: 0000 9811 16cd 7272 7272 c0a8 6623 0035 ......rrrr..f#.5 0x0040: c606 005c 0000 ...\.. 1 packet captured 357 packets received by filter 0 packets dropped by kernel

使用源和目的地址過濾

      
      
      
      
  1. tcpdump src 2.3.4.6
  2. tcpdump dst 3.4.5.6
tcpdump src 2.3.4.6
tcpdump dst 3.4.5.6
tcpdump src 2.3.4.6 tcpdump dst 3.4.5.6

過濾某個子網的數據包

      
      
      
      
  1. tcpdump net 1.2.3.0/24
tcpdump net 1.2.3.0/24
tcpdump net 1.2.3.0/24

過濾指定端口相關的流量

      
      
      
      
  1. tcpdump port 3389
  2. tcpdump src port 1025
tcpdump port 3389
tcpdump src port 1025
tcpdump port 3389 tcpdump src port 1025

過濾指定協議的流量

      
      
      
      
  1. tcpdump icmp
tcpdump icmp
tcpdump icmp

只顯示IPV6流量

      
      
      
      
  1. tcpdump ip6
tcpdump ip6
tcpdump ip6

使用端口範圍過濾

      
      
      
      
  1. tcpdump portrange 21-23
tcpdump portrange 21-23
tcpdump portrange 21-23

基於包的大小過濾流量

      
      
      
      
  1. tcpdump less 32
  2. tcpdump greater 64
  3. tcpdump <=128
tcpdump less 32
tcpdump greater 64
tcpdump <=128
tcpdump less 32 tcpdump greater 64 tcpdump <=128

將捕獲的內容寫入文件

使用-w選項能夠將捕獲的數據包信息寫入文件以供之後分析,這些文件就是著名的PCAP(PEE-cap)文件,不少應用均可以處理它。

      
      
      
      
  1. tcpdump port 80 -w capture_file
tcpdump port 80 -w capture_file
tcpdump port 80 -w capture_file

使用tcpdump加載以前保存的文件進行分析

      
      
      
      
  1. tcpdump -r capture_file
tcpdump -r capture_file
tcpdump -r capture_file

高級

使用組合語句能夠完成更多高級的過濾。

  • AND: and or &&
  • OR: or or ||
  • EXCEPT: not or !

過濾指定源IP和目的端口

      
      
      
      
  1. tcpdump -nnvvS src 10.5.2.3 and dst port 3389
tcpdump -nnvvS src 10.5.2.3 and dst port 3389
tcpdump -nnvvS src 10.5.2.3 and dst port 3389

過濾指定網絡到另外一個網絡

好比下面這個,查看來自192.168.x.x的,而且目的爲10.x或者172.16.x.x的全部流量,這裏使用了hex輸出,同時不解析主機名

      
      
      
      
  1. tcpdump -nvX src net 192.168.0.0/16 and dst net 10.0.0.0/8 or 172.16.0.0/16
tcpdump -nvX src net 192.168.0.0/16 and dst net 10.0.0.0/8 or 172.16.0.0/16
tcpdump -nvX src net 192.168.0.0/16 and dst net 10.0.0.0/8 or 172.16.0.0/16

過濾到指定IP的非ICMP報文

      
      
      
      
  1. tcpdump dst 192.168.0.2 and src net and not icmp
tcpdump dst 192.168.0.2 and src net and not icmp
tcpdump dst 192.168.0.2 and src net and not icmp

過濾來自非指定端口的指定主機的流量

下面這個過濾出全部來自某個主機的非ssh流量

      
      
      
      
  1. tcpdump -vv src mars and not dst port 22
tcpdump -vv src mars and not dst port 22
tcpdump -vv src mars and not dst port 22

複雜分組和特殊字符

當構建複雜的過濾規則的時候,使用單引號將規則放到一塊兒是個很好的選擇。特別是在包含()的規則中。好比下面的規則就是錯誤的,由於括號在shell中會被錯誤的解析,能夠對括號使用\進行轉義或者使用單引號

      
      
      
      
  1. tcpdump src 10.0.2.3 and (dst port 3389 or 22)
tcpdump src 10.0.2.3 and (dst port 3389 or 22)
tcpdump src 10.0.2.3 and (dst port 3389 or 22)

應該修改成

      
      
      
      
  1. tcpdump 'src 10.0.2.3 and (dst port 3389 or 22)'
tcpdump 'src 10.0.2.3 and (dst port 3389 or 22)'
tcpdump 'src 10.0.2.3 and (dst port 3389 or 22)'

隔離指定的TCP標識

能夠基於指定的TCP標識(flag)來過濾流量。

下面的過濾規則中,tcp[13]表示在TCP header中的偏移位置13開始,後面的數字表明瞭匹配的byte數。

顯示全部的URGENT (URG)包

      
      
      
      
  1. tcpdump 'tcp[13] & 32!=0'
tcpdump 'tcp[13] & 32!=0'
tcpdump 'tcp[13] & 32!=0'

顯示全部的ACKNOWLEDGE (ACK)包

      
      
      
      
  1. tcpdump 'tcp[13] & 16!=0'
tcpdump 'tcp[13] & 16!=0'
tcpdump 'tcp[13] & 16!=0'

顯示全部的PUSH(PSH)包

      
      
      
      
  1. tcpdump 'tcp[13] & 8!=0'
tcpdump 'tcp[13] & 8!=0'
tcpdump 'tcp[13] & 8!=0'

顯示全部的RESET(RST)包

      
      
      
      
  1. tcpdump 'tcp[13] & 4!=0'
tcpdump 'tcp[13] & 4!=0'
tcpdump 'tcp[13] & 4!=0'

顯示全部的SYNCHRONIZE (SYN) 包

      
      
      
      
  1. tcpdump 'tcp[13] & 2!=0'
tcpdump 'tcp[13] & 2!=0'
tcpdump 'tcp[13] & 2!=0'

顯示全部的FINISH(FIN)包

      
      
      
      
  1. tcpdump 'tcp[13] & 1!=0'
tcpdump 'tcp[13] & 1!=0'
tcpdump 'tcp[13] & 1!=0'

顯示說有的SYNCHRONIZE/ACKNOWLEDGE (SYNACK)包

      
      
      
      
  1. tcpdump 'tcp[13]=18'
tcpdump 'tcp[13]=18'
tcpdump 'tcp[13]=18'

其它方式

與大多數工具同樣,也可使用下面這種方式來捕獲指定TCP標識的流量

      
      
      
      
  1. tcpdump 'tcp[tcpflags] == tcp-syn'
  2. tcpdump 'tcp[tcpflags] == tcp-rst'
  3. tcpdump 'tcp[tcpflags] == tcp-fin'
tcpdump 'tcp[tcpflags] == tcp-syn'
tcpdump 'tcp[tcpflags] == tcp-rst'
tcpdump 'tcp[tcpflags] == tcp-fin'
tcpdump 'tcp[tcpflags] == tcp-syn' tcpdump 'tcp[tcpflags] == tcp-rst' tcpdump 'tcp[tcpflags] == tcp-fin'

識別重要流量

最後,這裏有一些重要的代碼片斷你可能須要,它們用於過濾指定的流量,例如畸形的或者惡意的流量。

過濾同時設置SYN和RST標識的包(這在正常狀況下不該該發生)

      
      
      
      
  1. tcpdump 'tcp[13] = 6'
tcpdump 'tcp[13] = 6'
tcpdump 'tcp[13] = 6'

過濾明文的HTTP GET請求

      
      
      
      
  1. tcpdump 'tcp[32:4] = 0x47455420'
tcpdump 'tcp[32:4] = 0x47455420'
tcpdump 'tcp[32:4] = 0x47455420'

經過橫幅文本過濾任意端口的SSH鏈接

      
      
      
      
  1. tcpdump 'tcp[(tcp[12]>>2):4] = 0x5353482D'
tcpdump 'tcp[(tcp[12]>>2):4] = 0x5353482D'
tcpdump 'tcp[(tcp[12]>>2):4] = 0x5353482D'

過濾TTL小於10的包(一般狀況下是存在問題或者在使用traceroute)

      
      
      
      
  1. tcpdump 'ip[8] < 10'
tcpdump 'ip[8] < 10'
tcpdump 'ip[8] < 10'

過濾惡意的包

      
      
      
      
  1. tcpdump 'ip[6] & 128 != 0'
tcpdump 'ip[6] & 128 != 0'
tcpdump 'ip[6] & 128 != 0'

補充(非原文內容)

下面這個命令用於過濾全部與8080端口相關的tcp流量,將其輸出到capcha.cap文件中,咱們可使用wireshark打開這個文件,更加可視化的分析過濾其中包含的http流量。

      
      
      
      
  1. tcpdump -tttt -s0 -X -vv tcp port 8080 -w captcha.cap
tcpdump -tttt -s0 -X -vv tcp port 8080 -w captcha.cap
tcpdump -tttt -s0 -X -vv tcp port 8080 -w captcha.cap

本文將會持續修正和更新,最新內容請參考個人 GITHUB 上的 程序猿成長計劃 項目,歡迎 Star,更多精彩內容請 follow me

個人博客即將搬運同步至騰訊雲+社區,邀請你們一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=30w4cly1rgsgo

轉載自 https://aicode.cc/tcpdump-ru-men.html

相關文章
相關標籤/搜索