Linux tcpdump命令实战
前言:
網絡這塊知識點一直都是筆者的薄弱之處。雖然也一直在學習理論知識,但是總覺得學完就忘,一直在重復學習。
究其原因,應該還是理論沒有與實踐結合在一起。而想學習具象的網絡知識,那么工具就是必不可少的。
目前的工具有兩類:wireshark(可視化工具)、tcpdump(命令)。本文我們就來學習下tcpdump命令的使用
1.tcpdump作用
tcpdump是一款強大的網絡抓包工具。通過tcpdump命令的使用可以幫助我們分析、調試網絡數據
2.tcpdump語法
同樣我們通過man命令來查看其基本語法
NAMEtcpdump - dump traffic on a networkSYNOPSIStcpdump [ -AbdDefhHIJKlLnNOpqStuUvxX# ] [ -B buffer_size ][ -c count ][ -C file_size ] [ -G rotate_seconds ] [ -F file ][ -i interface ] [ -j tstamp_type ] [ -m module ] [ -M secret ][ --number ] [ -Q in|out|inout ][ -r file ] [ -V file ] [ -s snaplen ] [ -T type ] [ -w file ][ -W filecount ][ -E spi@ipaddr algo:secret,... ][ -y datalinktype ] [ -z postrotate-command ] [ -Z user ][ --time-stamp-precision=tstamp_precision ][ --immediate-mode ] [ --version ][ expression ]具體參數細節可以通過man tcpdump查看,下面列舉一些常用的參數(來自:?tcpdump詳解_最強滴菜鳥的博客-CSDN博客_tcpdump? )
-A 以ASCII格式打印出所有分組,并將鏈路層的頭最小化。-c 在收到指定的數量的分組后,tcpdump就會停止。-C 在將一個原始分組寫入文件之前,檢查文件當前的大小是否超過了參數file_size 中指定的大小。如果超過了指定大小,則關閉當前文件,然后在打開一個新的文件。 參數 file_size 的單位是兆字節(是1,000,000字節,而不是1,048,576字節)。-d 將匹配信息包的代碼以人們能夠理解的匯編格式給出。-dd 將匹配信息包的代碼以c語言程序段的格式給出。-ddd 將匹配信息包的代碼以十進制的形式給出。-D 打印出系統中所有可以用tcpdump截包的網絡接口。-e 在輸出行打印出數據鏈路層的頭部信息。-E 用spi@ipaddr algo:secret解密那些以addr作為地址,并且包含了安全參數索引值spi的IPsec ESP分組。-f 將外部的Internet地址以數字的形式打印出來。-F 從指定的文件中讀取表達式,忽略命令行中給出的表達式。-i 指定監聽的網絡接口。-l 使標準輸出變為緩沖行形式,可以把數據導出到文件。-L 列出網絡接口的已知數據鏈路。-m 從文件module中導入SMI MIB模塊定義。該參數可以被使用多次,以導入多個MIB模塊。-M 如果tcp報文中存在TCP-MD5選項,則需要用secret作為共享的驗證碼用于驗證TCP-MD5選選項摘要(詳情可參考RFC 2385)。-b 在數據-鏈路層上選擇協議,包括ip、arp、rarp、ipx都是這一層的。-n 不把網絡地址轉換成名字。-nn 不進行端口名稱的轉換。-N 不輸出主機名中的域名部分。例如,‘nic.ddn.mil‘只輸出’nic‘。-t 在輸出的每一行不打印時間戳。-O 不運行分組分組匹配(packet-matching)代碼優化程序。-P 不將網絡接口設置成混雜模式。-q 快速輸出。只輸出較少的協議信息。-r 從指定的文件中讀取包(這些包一般通過-w選項產生)。-S 將tcp的序列號以絕對值形式輸出,而不是相對值。-s 從每個分組中讀取最開始的snaplen個字節,而不是默認的68個字節。-T 將監聽到的包直接解釋為指定的類型的報文,常見的類型有rpc遠程過程調用)和snmp(簡單網絡管理協議;)。-t 不在每一行中輸出時間戳。-tt 在每一行中輸出非格式化的時間戳。-ttt 輸出本行和前面一行之間的時間差。-tttt 在每一行中輸出由date處理的默認格式的時間戳。-u 輸出未解碼的NFS句柄。-v 輸出一個稍微詳細的信息,例如在ip包中可以包括ttl和服務類型的信息。-vv 輸出詳細的報文信息。-w 直接將分組寫入文件中,而不是不分析并打印出來。以上是輸入參,那么攔截到請求后,輸出是怎樣的呢,我們攔截一次請求來看下(nginx80端口攔截)
root@aa4e7f274852:/tmp# tcpdump port 80 tcpdump: verbose output suppressed, use -v[v]... for full protocol decode listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes 13:23:24.776819 IP 172.17.0.1.61002 > aa4e7f274852.80: Flags [S], seq 4130437317, win 65495, options [mss 65495,sackOK,TS val 4219824570 ecr 0,nop,wscale 7], length 0 13:23:24.776863 IP aa4e7f274852.80 > 172.17.0.1.61002: Flags [S.], seq 2368836658, ack 4130437318, win 65160, options [mss 1460,sackOK,TS val 1442703107 ecr 4219824570,nop,wscale 7], length 0 13:23:24.776942 IP 172.17.0.1.61002 > aa4e7f274852.80: Flags [.], ack 1, win 512, options [nop,nop,TS val 4219824570 ecr 1442703107], length 0 13:23:24.778381 IP 172.17.0.1.61002 > aa4e7f274852.80: Flags [P.], seq 1:74, ack 1, win 512, options [nop,nop,TS val 4219824572 ecr 1442703107], length 73: HTTP: GET / HTTP/1.1 13:23:24.778414 IP aa4e7f274852.80 > 172.17.0.1.61002: Flags [.], ack 74, win 509, options [nop,nop,TS val 1442703109 ecr 4219824572], length 0 13:23:24.778705 IP aa4e7f274852.80 > 172.17.0.1.61002: Flags [P.], seq 1:239, ack 74, win 509, options [nop,nop,TS val 1442703109 ecr 4219824572], length 238: HTTP: HTTP/1.1 200 OK 13:23:24.778751 IP 172.17.0.1.61002 > aa4e7f274852.80: Flags [.], ack 239, win 511, options [nop,nop,TS val 4219824572 ecr 1442703109], length 0 13:23:24.778793 IP aa4e7f274852.80 > 172.17.0.1.61002: Flags [P.], seq 239:854, ack 74, win 509, options [nop,nop,TS val 1442703109 ecr 4219824572], length 615: HTTP 13:23:24.778810 IP 172.17.0.1.61002 > aa4e7f274852.80: Flags [.], ack 854, win 507, options [nop,nop,TS val 4219824572 ecr 1442703109], length 0 13:23:24.788749 IP 172.17.0.1.61002 > aa4e7f274852.80: Flags [F.], seq 74, ack 854, win 512, options [nop,nop,TS val 4219824582 ecr 1442703109], length 0 13:23:24.788896 IP aa4e7f274852.80 > 172.17.0.1.61002: Flags [F.], seq 854, ack 75, win 509, options [nop,nop,TS val 1442703119 ecr 4219824582], length 0 13:23:24.789485 IP 172.17.0.1.61002 > aa4e7f274852.80: Flags [.], ack 855, win 512, options [nop,nop,TS val 4219824583 ecr 1442703119], length 0這里的輸出基本與wireshark輸出類似,需要讀者熟悉TCP基本知識。
在實際使用中,我們不會直接監聽很大范圍,那樣的話輸出就太多了,所以我們一般會使用命令參數來過濾。下面我們來看下那些比較常用的參數
3.tcpdump命令參數
3.1 tcpdump -i 指定網絡接口
通過ifconfig命令可以看到當前服務器的網絡接口,我們可以指定監聽某個網絡接口的信息,如下
# 查看網絡接口 root@7bc18553126f:/tmp# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)RX packets 5708257 bytes 34583301223 (34.5 GB)RX errors 0 dropped 3025 overruns 0 frame 0TX packets 6428544 bytes 352862121 (352.8 MB)TX errors 0 dropped 2759 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0loop txqueuelen 1000 (Local Loopback)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0# 抓包eth0接口網絡包 root@7bc18553126f:/tmp# tcpdump -i eth0 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes但是一般不會這么直接做,因為輸出實在太多
3.2 tcpdump -c count 指定打印條數
在接收到count條報文后,tcpdump命令就停止抓包,否則該命令會一直抓包下去
3.3 tcpdum -vv 展示詳細信息
默認是簡略版的,我們來看下詳細的有啥不同(同樣還是監聽nginx80端口報文)
root@aa4e7f274852:/tmp# tcpdump port 80 -vv tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes 13:36:45.949940 IP (tos 0x0, ttl 64, id 52376, offset 0, flags [DF], proto TCP (6), length 60)172.17.0.1.61004 > aa4e7f274852.80: Flags [S], cksum 0x5857 (incorrect -> 0x0962), seq 964764998, win 65495, options [mss 65495,sackOK,TS val 4220625795 ecr 0,nop,wscale 7], length 0 13:36:45.950123 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 60)aa4e7f274852.80 > 172.17.0.1.61004: Flags [S.], cksum 0x5857 (incorrect -> 0x307a), seq 1828122747, ack 964764999, win 65160, options [mss 1460,sackOK,TS val 1443504333 ecr 4220625795,nop,wscale 7], length 0 13:36:45.950248 IP (tos 0x0, ttl 64, id 52377, offset 0, flags [DF], proto TCP (6), length 52)172.17.0.1.61004 > aa4e7f274852.80: Flags [.], cksum 0x584f (incorrect -> 0x5bce), seq 1, ack 1, win 512, options [nop,nop,TS val 4220625796 ecr 1443504333], length 0 13:36:45.950735 IP (tos 0x0, ttl 64, id 52378, offset 0, flags [DF], proto TCP (6), length 125)172.17.0.1.61004 > aa4e7f274852.80: Flags [P.], cksum 0x5898 (incorrect -> 0x05e5), seq 1:74, ack 1, win 512, options [nop,nop,TS val 4220625796 ecr 1443504333], length 73: HTTP, length: 73GET / HTTP/1.1Host: localhostUser-Agent: curl/7.77.0Accept: */*...請求的明細信息都展示出來(這里HTTP頭信息也展示出來了)
3.4 過濾器
tcpdump最強大之處就是過濾器功能,通過過濾器可以快速找到自己需要的報文
過濾器語法可以參考命令 (man pcap-filter):
root@7bc18553126f:/tmp# man pcap-filter NAMEpcap-filter - packet filter syntaxDESCRIPTIONpcap_compile() is used to compile a string into a filter program. The resulting filter program can then be applied to some stream of packets to determinewhich packets will be supplied to pcap_loop(), pcap_dispatch(), pcap_next(), or pcap_next_ex().The filter expression consists of one or more primitives. Primitives usually consist of an id (name or number) preceded by one or more qualifiers. Thereare three different kinds of qualifier:type type qualifiers say what kind of thing the id name or number refers to. Possible types are host, net , port and portrange. E.g., `host foo', `net128.3', `port 20', `portrange 6000-6008'. If there is no type qualifier, host is assumed.dir dir qualifiers specify a particular transfer direction to and/or from id. Possible directions are src, dst, src or dst, src and dst, ra, ta,addr1, addr2, addr3, and addr4. E.g., `src foo', `dst net 128.3', `src or dst port ftp-data'. If there is no dir qualifier, src or dst isassumed. The ra, ta, addr1, addr2, addr3, and addr4 qualifiers are only valid for IEEE 802.11 Wireless LAN link layers. For some link layers,such as SLIP and the ``cooked'' Linux capture mode used for the ``any'' device and for some other device types, the inbound and outbound qualifierscan be used to specify a desired direction.proto proto qualifiers restrict the match to a particular protocol. Possible protos are: ether, fddi, tr, wlan, ip, ip6, arp, rarp, decnet, tcp and udp.E.g., `ether src foo', `arp net 128.3', `tcp port 21', `udp portrange 7000-7009', `wlan addr2 0:2:3:4:5:6'. If there is no proto qualifier, allprotocols consistent with the type are assumed. E.g., `src foo' means `(ip or arp or rarp) src foo' (except the latter is not legal syntax), `netbar' means `(ip or arp or rarp) net bar' and `port 53' means `(tcp or udp) port 53'.根據該內容可以看出,tcpdump的過濾器主要分為三類:type 、dir 、proto
3.4.1 type
type主要有四種類型:host 、port 、 net、portrange
root@aa4e7f274852:/tmp# tcpdump host 172.17.0.1 root@aa4e7f274852:/tmp# tcpdump port 80 root@aa4e7f274852:/tmp# tcpdump net 172.17.0.0/24 root@aa4e7f274852:/tmp# tcpdump host 172.17.0.1 and port 803.4.2 dir
dir主要有源地址(src)和目標地址(dst)
# 只攔截src為172.17.0.1的報文 root@aa4e7f274852:/tmp# tcpdump src 172.17.0.1 tcpdump: verbose output suppressed, use -v[v]... for full protocol decode listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes 14:13:36.699230 IP 172.17.0.1.61012 > aa4e7f274852.80: Flags [S], seq 4028567681, win 65495, options [mss 65495,sackOK,TS val 4222203183 ecr 0,nop,wscale 7], length 0 14:13:36.699476 IP 172.17.0.1.61012 > aa4e7f274852.80: Flags [.], ack 4235944116, win 512, options [nop,nop,TS val 4222203184 ecr 1445081721], length 0 14:13:36.700874 IP 172.17.0.1.61012 > aa4e7f274852.80: Flags [P.], seq 0:73, ack 1, win 512, options [nop,nop,TS val 4222203185 ecr 1445081721], length 73: HTTP: GET / HTTP/1.1 14:13:36.701364 IP 172.17.0.1.61012 > aa4e7f274852.80: Flags [.], ack 239, win 511, options [nop,nop,TS val 4222203186 ecr 1445081723], length 0 14:13:36.701471 IP 172.17.0.1.61012 > aa4e7f274852.80: Flags [.], ack 854, win 507, options [nop,nop,TS val 4222203186 ecr 1445081723], length 0 14:13:36.706212 IP 172.17.0.1.61012 > aa4e7f274852.80: Flags [F.], seq 73, ack 854, win 512, options [nop,nop,TS val 4222203191 ecr 1445081723], length 0 14:13:36.706519 IP 172.17.0.1.61012 > aa4e7f274852.80: Flags [.], ack 855, win 512, options [nop,nop,TS val 4222203191 ecr 1445081728], length 0 ...# 只攔截dst目標為172.17.0.1的報文 root@aa4e7f274852:/tmp# tcpdump dst 172.17.0.1 tcpdump: verbose output suppressed, use -v[v]... for full protocol decode listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes 14:13:56.729678 IP aa4e7f274852.80 > 172.17.0.1.61014: Flags [S.], seq 3849319136, ack 1537888095, win 65160, options [mss 1460,sackOK,TS val 1445101751 ecr 4222223214,nop,wscale 7], length 0 14:13:56.730927 IP aa4e7f274852.80 > 172.17.0.1.61014: Flags [.], ack 74, win 509, options [nop,nop,TS val 1445101752 ecr 4222223215], length 0 14:13:56.731152 IP aa4e7f274852.80 > 172.17.0.1.61014: Flags [P.], seq 1:239, ack 74, win 509, options [nop,nop,TS val 1445101752 ecr 4222223215], length 238: HTTP: HTTP/1.1 200 OK 14:13:56.731208 IP aa4e7f274852.80 > 172.17.0.1.61014: Flags [P.], seq 239:854, ack 74, win 509, options [nop,nop,TS val 1445101752 ecr 4222223215], length 615: HTTP 14:13:56.744499 IP aa4e7f274852.80 > 172.17.0.1.61014: Flags [F.], seq 854, ack 75, win 509, options [nop,nop,TS val 1445101766 ecr 4222223228], length 0 ...3.4.3 proto協議
協議主要有:ip、arp、rarp、tcp、udp、icmp等
同樣,這個單獨使用的場景還是比較少的
3.5 條件組合
tcpdump支持通過條件組合的方式來過濾網絡包。
主要包括:與 (and 或 &&)、或(or 或 ||)、非(not 或 !)
# 過濾host=172.17.0.1 并且 port=80 的包 root@aa4e7f274852:/tmp# tcpdump host 172.17.0.1 and port 80 # 過濾host=172.17.0.1 并且 port!=80 的包 root@aa4e7f274852:/tmp# tcpdump host 172.17.0.1 and port not 8080總結
以上是生活随笔為你收集整理的Linux tcpdump命令实战的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 51单片机c语言学习笔记,51单片机学习
- 下一篇: 王思聪吃热狗 - 飞机大战小游戏