自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

抓包工具tcpdump用法說明

系統(tǒng) Linux
tcpdump采用命令行方式對接口的數(shù)據(jù)包進行篩選抓取,其豐富特性表現(xiàn)在靈活的表達式上。

[[353029]]

 tcpdump采用命令行方式對接口的數(shù)據(jù)包進行篩選抓取,其豐富特性表現(xiàn)在靈活的表達式上。

不帶任何選項的tcpdump,默認會抓取第一個網(wǎng)絡接口,且只有將tcpdump進程終止才會停止抓包。

例如: 

  1. shell> tcpdump -nn -i eth0 icmp 

下面是詳細的tcpdump用法。

1.1 tcpdump選項

它的命令格式為: 

  1. tcpdump [ -DenNqvX ] [ -c count ] [ -F file ] [ -i interface ] [ -r file ]  
  2.         [ -s snaplen ] [ -w file ] [ expression ]  
  3. 抓包選項:  
  4. -c:指定要抓取的包數(shù)量。注意,是最終要獲取這么多個包。例如,指定"-c 10"將獲取10個包,但可能已經(jīng)處理了100個包,只不過只有10個包是滿足條件的包。 
  5. -i interface:指定tcpdump需要監(jiān)聽的接口。若未指定該選項,將從系統(tǒng)接口列表中搜尋編號最小的已配置好的接口(不包括loopback接口,要抓取loopback接口使用tcpdump -i lo),  
  6.             :一旦找到第一個符合條件的接口,搜尋馬上結束??梢允褂?any'關鍵字表示所有網(wǎng)絡接口。  
  7. -n:對地址以數(shù)字方式顯式,否則顯式為主機名,也就是說-n選項不做主機名解析。  
  8. -nn:除了-n的作用外,還把端口顯示為數(shù)值,否則顯示端口服務名。  
  9. -N:不打印出host的域名部分。例如tcpdump將會打印'nic'而不是'nic.ddn.mil'。  
  10. -P:指定要抓取的包是流入還是流出的包。可以給定的值為"in"、"out"和"inout",默認為"inout"。  
  11. -s len:設置tcpdump的數(shù)據(jù)包抓取長度為len,如果不設置默認將會是65535字節(jié)。對于要抓取的數(shù)據(jù)包較大時,長度設置不夠可能會產(chǎn)生包截斷,若出現(xiàn)包截斷,  
  12.       :輸出行中會出現(xiàn)"[|proto]"的標志(proto實際會顯示為協(xié)議名)。但是抓取len越長,包的處理時間越長,并且會減少tcpdump可緩存的數(shù)據(jù)包的數(shù)量, 
  13.       :從而會導致數(shù)據(jù)包的丟失,所以在能抓取我們想要的包的前提下,抓取長度越小越好。  
  14. 輸出選項:  
  15. -e:輸出的每行中都將包括數(shù)據(jù)鏈路層頭部信息,例如源MAC和目標MAC。  
  16. -q:快速打印輸出。即打印很少的協(xié)議相關信息,從而輸出行都比較簡短。  
  17. -X:輸出包的頭部數(shù)據(jù),會以16進制和ASCII兩種方式同時輸出。  
  18. -XX:輸出包的頭部數(shù)據(jù),會以16進制和ASCII兩種方式同時輸出,更詳細。  
  19. -v:當分析和打印的時候,產(chǎn)生詳細的輸出。  
  20. -vv:產(chǎn)生比-v更詳細的輸出。  
  21. -vvv:產(chǎn)生比-vv更詳細的輸出。  
  22. 其他功能性選項:  
  23. -D:列出可用于抓包的接口。將會列出接口的數(shù)值編號和接口名,它們都可以用于"-i"后。  
  24. -F:從文件中讀取抓包的表達式。若使用該選項,則命令行中給定的其他表達式都將失效。  
  25. -w:將抓包數(shù)據(jù)輸出到文件中而不是標準輸出??梢酝瑫r配合"-G time"選項使得輸出文件每time秒就自動切換到另一個文件??赏ㄟ^"-r"選項載入這些文件以進行分析和打印。  
  26. -r:從給定的數(shù)據(jù)包文件中讀取數(shù)據(jù)。使用"-"表示從標準輸入中讀取。 

推薦給你:值得收藏!Linux系統(tǒng)常用命令速查手冊

所以常用的選項也就這幾個:

  •  tcpdump -D
  •  tcpdump -c num -i int -nn -XX -vvv

1.2 tcpdump表達式

表達式用于篩選輸出哪些類型的數(shù)據(jù)包,如果沒有給定表達式,所有的數(shù)據(jù)包都將輸出,否則只輸出表達式為true的包。在表達式中出現(xiàn)的shell元字符建議使用單引號包圍。

tcpdump的表達式由一個或多個"單元"組成,每個單元一般包含ID的修飾符和一個ID(數(shù)字或名稱)。有三種修飾符:

(1).type:指定ID的類型。

可以給定的值有host/net/port/portrange。例如"host foo","net 128.3","port 20","portrange 6000-6008"。默認的type為host。

(2).dir:指定ID的方向。

可以給定的值包括src/dst/src or dst/src and dst,默認為src or dst。例如,"src foo"表示源主機為foo的數(shù)據(jù)包,"dst net 128.3"表示目標網(wǎng)絡為128.3的數(shù)據(jù)包,"src or dst port 22"表示源或目的端口為22的數(shù)據(jù)包。

(3).proto:通過給定協(xié)議限定匹配的數(shù)據(jù)包類型。

常用的協(xié)議有tcp/udp/arp/ip/ether/icmp等,若未給定協(xié)議類型,則匹配所有可能的類型。例如"tcp port 21","udp portrange 7000-7009"。

所以,一個基本的表達式單元格式為"proto dir type ID"

除了使用修飾符和ID組成的表達式單元,還有關鍵字表達式單元:gateway,broadcast,less,greater以及算術表達式。

表達式單元之間可以使用操作符" and / && / or / || / not / ! "進行連接,從而組成復雜的條件表達式。如"host foo and not port ftp and not port ftp-data",這表示篩選的數(shù)據(jù)包要滿足"主機為foo且端口不是ftp(端口21)和ftp-data(端口20)的包",常用端口和名字的對應關系可在linux系統(tǒng)中的/etc/service文件中找到。

另外,同樣的修飾符可省略,如"tcp dst port ftp or ftp-data or domain"與"tcp dst port ftp or tcp dst port ftp-data or tcp dst port domain"意義相同,都表示包的協(xié)議為tcp且目的端口為ftp或ftp-data或domain(端口53)。

使用括號"()"可以改變表達式的優(yōu)先級,但需要注意的是括號會被shell解釋,所以應該使用反斜線"\"轉義為"\(\)",在需要的時候,還需要包圍在引號中。

1.3 tcpdump示例

注意,tcpdump只能抓取流經(jīng)本機的數(shù)據(jù)包。

(1).默認啟動 

  1. tcpdump 

默認情況下,直接啟動tcpdump將監(jiān)視第一個網(wǎng)絡接口(非lo口)上所有流通的數(shù)據(jù)包。這樣抓取的結果會非常多,滾動非???。

(2).監(jiān)視指定網(wǎng)絡接口的數(shù)據(jù)包 

  1. tcpdump -i eth1 

如果不指定網(wǎng)卡,默認tcpdump只會監(jiān)視第一個網(wǎng)絡接口,如eth0。

(3).監(jiān)視指定主機的數(shù)據(jù)包,例如所有進入或離開longshuai的數(shù)據(jù)包 

  1. tcpdump host longshuai 

(4).打印helios<-->hot或helios<-->ace之間通信的數(shù)據(jù)包 

  1. tcpdump host helios and \( hot or ace \) 

(5).打印ace與任何其他主機之間通信的IP數(shù)據(jù)包,但不包括與helios之間的數(shù)據(jù)包 

  1. tcpdump ip host ace and not helios 

(6).截獲主機hostname發(fā)送的所有數(shù)據(jù) 

  1. tcpdump src host hostname 

(7).監(jiān)視所有發(fā)送到主機hostname的數(shù)據(jù)包 

  1. tcpdump dst host hostname 

(8).監(jiān)視指定主機和端口的數(shù)據(jù)包 

  1. tcpdump tcp port 22 and host hostname 

(9).對本機的udp 123端口進行監(jiān)視(123為ntp的服務端口) 

  1. tcpdump udp port 123 

(10).監(jiān)視指定網(wǎng)絡的數(shù)據(jù)包,如本機與192.168網(wǎng)段通信的數(shù)據(jù)包,"-c 10"表示只抓取10個包 

  1. tcpdump -c 10 net 192.168 

(11).打印所有通過網(wǎng)關snup的ftp數(shù)據(jù)包(注意,表達式被單引號括起來了,這可以防止shell對其中的括號進行錯誤解析) 

  1. shell> tcpdump 'gateway snup and (port ftp or ftp-data)' 

(12).抓取ping包 

  1. [root@server2 ~]# tcpdump -c 5 -nn -i eth0 icmp   
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode  
  3. listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes  
  4. 12:11:23.273638 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16422, seq 10, length 64  
  5. 12:11:23.273666 IP 192.168.100.62 > 192.168.100.70: ICMP echo reply, id 16422, seq 10, length 64  
  6. 12:11:24.356915 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16422, seq 11, length 64  
  7. 12:11:24.356936 IP 192.168.100.62 > 192.168.100.70: ICMP echo reply, id 16422, seq 11, length 64  
  8. 12:11:25.440887 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16422, seq 12, length 64  
  9. packets captured  
  10. packets received by filter  
  11. packets dropped by kernel 

如果明確要抓取主機為192.168.100.70對本機的ping,則使用and操作符。 

  1. [root@server2 ~]# tcpdump -c 5 -nn -i eth0 icmp and src 192.168.100.62  
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode  
  3. listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes  
  4. 12:09:29.957132 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16166, seq 1, length 64  
  5. 12:09:31.041035 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16166, seq 2, length 64  
  6. 12:09:32.124562 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16166, seq 3, length 64  
  7. 12:09:33.208514 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16166, seq 4, length 64  
  8. 12:09:34.292222 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16166, seq 5, length 64  
  9. packets captured  
  10. packets received by filter  
  11. packets dropped by kernel 

注意不能直接寫icmp src 192.168.100.70,因為icmp協(xié)議不支持直接應用host這個type。

(13).抓取到本機22端口包 

  1. [root@server2 ~]# tcpdump -c 10 -nn -i eth0 tcp dst port 22    
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode  
  3. listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes  
  4. 12:06:57.574293 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 535528834, win 2053, length 0  
  5. 12:06:57.629125 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 193, win 2052, length 0  
  6. 12:06:57.684688 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 385, win 2051, length 0  
  7. 12:06:57.738977 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 577, win 2050, length 0  
  8. 12:06:57.794305 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 769, win 2050, length 0 
  9. 12:06:57.848720 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 961, win 2049, length 0  
  10. 12:06:57.904057 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 1153, win 2048, length 0  
  11. 12:06:57.958477 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 1345, win 2047, length 0  
  12. 12:06:58.014338 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 1537, win 2053, length 0  
  13. 12:06:58.069361 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 1729, win 2052, length 0  
  14. packets captured  
  15. packets received by filter  
  16. packets dropped by kernel 

(14).解析包數(shù)據(jù) 

  1. [root@server2 ~]# tcpdump -c 2 -q -XX -vvv -nn -i eth0 tcp dst port 22  
  2. tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes  
  3. 12:15:54.788812 IP (tos 0x0, ttl 64, id 19303, offset 0, flags [DF], proto TCP (6), length 40)  
  4.     192.168.100.1.5788 > 192.168.100.62.22: tcp 0  
  5.         0x0000:  000c 2908 9234 0050 56c0 0008 0800 4500  ..)..4.PV.....E.  
  6.         0x0010:  0028 4b67 4000 4006 a5d8 c0a8 6401 c0a8  .(Kg@.@.....d...  
  7.         0x0020:  643e 169c 0016 2426 5fd6 1fec 2b62 5010  d>....$&_...+bP.  
  8.         0x0030:  0803 7844 0000 0000 0000 0000            ..xD........  
  9. 12:15:54.842641 IP (tos 0x0, ttl 64, id 19304, offset 0, flags [DF], proto TCP (6), length 40)  
  10.     192.168.100.1.5788 > 192.168.100.62.22: tcp 0 
  11.          0x0000:  000c 2908 9234 0050 56c0 0008 0800 4500  ..)..4.PV.....E.  
  12.         0x0010:  0028 4b68 4000 4006 a5d7 c0a8 6401 c0a8  .(Kh@.@.....d...  
  13.         0x0020:  643e 169c 0016 2426 5fd6 1fec 2d62 5010  d>....$&_...-bP.  
  14.         0x0030:  0801 7646 0000 0000 0000 0000            ..vF........  
  15. packets captured  
  16. packets received by filter  
  17. packets dropped by kernel 

總的來說,tcpdump對基本的數(shù)據(jù)包抓取方法還是較簡單的。只要掌握有限的幾個選項(-nn -XX -vvv -i -c -q),再組合表達式即可。 

 

責任編輯:龐桂玉 來源: 民工哥技術之路
相關推薦

2021-01-05 06:12:38

Tcpdump工具網(wǎng)絡

2010-10-09 09:10:20

JavaScriptalert

2022-07-05 10:50:56

運維Linuxtcpdump

2010-08-04 09:52:49

ACL配置

2010-09-06 17:20:04

background-CSS

2010-08-19 11:32:10

CSSpaddingmargin

2022-02-16 22:57:57

Mitmproxy抓包工具

2010-09-16 14:59:27

Java虛擬機

2018-11-30 09:18:36

2024-08-22 14:47:50

開源Linux網(wǎng)絡抓包工具

2010-05-25 14:32:09

Linux tcpdu

2023-10-31 07:27:22

開源工具MySQL協(xié)議

2020-12-23 10:30:50

Web安全截包工具服務器

2018-11-05 13:50:44

Linux命令tcpdump

2022-08-09 10:05:57

Linux命令

2023-11-01 08:04:08

WiresharkTCP協(xié)議

2009-07-14 15:52:00

WebWork文件下載

2010-06-02 15:45:45

Linux 網(wǎng)絡性能

2010-06-24 14:12:20

Linux Cat命令

2024-09-02 00:00:01

點贊
收藏

51CTO技術棧公眾號