计算机网络抓包工具——tcpdump详解
目錄
一、tcpdump是什么?
二、tcpdump里常用參數(shù)
1、可接的選項
?2、type類過濾器【參數(shù)】
一、tcpdump是什么?
tcpdump是Linux里的字符界面的數(shù)據(jù)抓包分析軟件。tcpdump是一個用于截取網(wǎng)絡(luò)分組,并輸出分組內(nèi)容的工具。
tcpdump:其中tcp:傳輸控制協(xié)議,位于傳輸層transmission?control?protocol。dump:導(dǎo)出。
Windows 抓包軟件:wireshark、科來網(wǎng)絡(luò)分析系統(tǒng):抓網(wǎng)卡數(shù)據(jù)包。
使用tcpdump來抓包可以知道有哪些客戶端的人訪問到自己的服務(wù)器上了。
二、tcpdump里常用參數(shù)
只使用tcpdump命令進行抓包:截獲主機收到和發(fā)出的所有數(shù)據(jù)包。
tcpdump截取的包默認(rèn)數(shù)據(jù)包的頭部,默認(rèn)情況下,直接啟動tcpdump將監(jiān)聽的是第一個網(wǎng)絡(luò)接口上所有流過的數(shù)據(jù)包。
[root@nginx-kafka01 ~]# tcpdump tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on docker0, link-type EN10MB (Ethernet), capture size 262144 bytes ^C 0 packets captured 0 packets received by filter 0 packets dropped by kerneltcpdump里面的參數(shù),這些參數(shù)是用來過濾的。
1、可接的選項
| -i | interface指定抓哪個接口的數(shù)據(jù)包。 |
| -vv | 顯示更加詳細(xì)的信息。 |
| -e | 顯示mac地址。 |
| -w | write 寫入保存到文件中。導(dǎo)入的文件可以使用wireshark打開。 |
| -r | read 讀取文件中的數(shù)據(jù)。 |
| -c | 在收到指定包數(shù)目之后,tcpdump就會停止。 |
文件的后綴名可以自定義.pcap packet capture
例如:
1、監(jiān)聽指定網(wǎng)卡ens33的所有傳輸數(shù)據(jù)包?
tcpdump -i ens33
[root@nginx-kafka01 ~]# tcpdump -i ens33 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on docker0, link-type EN10MB (Ethernet), capture size 262144 bytes 21:48:51.599093 IP 192.168.2.130.55734 > nginx-kafka01.ssh: Flags [.], ack 141668, win 4103, length 0 21:48:51.599156 IP nginx-kafka01.ssh > 192.168.2.130.55734: Flags [P.], seq 141668:141928, ack 37, win 261, length 260 21:48:51.599274 IP nginx-kafka01.ssh > 192.168.2.130.55734: Flags [P.], seq 141928:142084, ack 37, win 261, length 156 21:48:51.605319 IP 192.168.2.130.55734 > nginx-kafka01.ssh: Flags [.], ack 142084, win 4101, length 0 21:48:51.605373 IP nginx-kafka01.ssh > 192.168.2.130.55734: Flags [P.], seq 142084:142240, ack 37, win 261, length 156 21:48:51.608121 IP nginx-kafka01.ssh > 192.168.2.130.55734: Flags [P.], seq 142240:142500, ack 37, win 261, length 260 ^C^C21:48:51.610340 ARP, Request who-has 192.168.2.104 tell gateway, length 461032 packets captured 1086 packets received by filter?2、創(chuàng)建文件,文件名后綴可以隨便取,然后使用tcp協(xié)議抓網(wǎng)卡ens33的所有傳輸數(shù)據(jù)包并顯示mac地址并寫入feng.sc文件中。
[root@nginx-kafka01 shell]# touch feng.sc # -i ens33:指定抓ens33接口中的數(shù)據(jù)包 [root@nginx-kafka01 shell]# tcpdump -i ens33 -e -vv tcp -w feng.sc# -r :讀取文件數(shù)據(jù) [root@nginx-kafka01 shell]# tcpdump5 -r feng.sc3、-i :抓取網(wǎng)卡ens33的所有傳輸數(shù)據(jù)包 ,-vv:顯示更加詳細(xì)的信息。
[root@nginx-kafka01 /]# tcpdump -vv -i ens33 tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes 22:26:53.664261 IP (tos 0x10, ttl 64, id 32581, offset 0, flags [DF], proto TCP (6), length 164)nginx-kafka01.ssh > 192.168.2.130.60716: Flags [P.], cksum 0x8701 (incorrect -> 0xc90f), seq 1310010851:1310010975, ack 1763474695, win 284, length 124 22:26:53.664807 IP (tos 0x0, ttl 64, id 22115, offset 0, flags [DF], proto UDP (17), length 72)nginx-kafka01.53273 > public1.114dns.com.domain: [bad udp cksum 0xa86a -> 0xc47a!] 235+ PTR? 130.2.168.192.in-addr.arpa. (44) 22:26:53.664962 IP (tos 0x10, ttl 64, id 32582, offset 0, flags [DF], proto TCP (6), length 172)nginx-kafka01.ssh > 192.168.2.130.60716: Flags [P.], cksum 0x8709 (incorrect -> 0xbf23), seq 124:256, ack 1, win 284, length 132 22:26:53.665071 IP (tos 0x0, ttl 128, id 62460, offset 0, flags [DF], proto TCP (6), length 40)192.168.2.130.60716 > nginx-kafka01.ssh: Flags [.], cksum 0xc900 (correct), seq 1, ack 256, win 4106, length 0 22:26:53.689765 IP (tos 0x0, ttl 152, id 0, offset 0, flags [none], proto UDP (17), length 149)public1.114dns.com.domain > nginx-kafka01.53273: [udp sum ok] 235 NXDomain q: PTR? 130.2.168.192.in-addr.arpa. 0/1/0 ns: 168.192.in-addr.arpa. SOA prisoner.iana.org. hostmaster.root-servers.org. 1 604800 60 604800 604800 (121) 22:26:53.691101 IP (tos 0x0, ttl 64, id 22121, offset 0, flags [DF], proto UDP (17), length 74)4、-i :抓取網(wǎng)卡ens33的所有傳輸數(shù)據(jù)包 ,-vv:顯示更加詳細(xì)的信息。-e:顯示mac地址。
tcpdump -i ens33 -vv -e [root@nginx-kafka01 ~]# tcpdump -i ens33 -vv -e?2、type類過濾器【參數(shù)】
type類過濾器后面需要接相應(yīng)的參數(shù)。
| host | 根據(jù)主機ip來抓包 |
| port | 根據(jù)端口進行抓包 |
| net | 根據(jù)網(wǎng)段進行抓包 |
| protocol | 根據(jù)協(xié)議進行抓包 |
| protocol協(xié)議 | tcp、udp、icmp、ip、arp、rarp、ether協(xié)議等 |
| icmp協(xié)議 | ping命令 |
| mac地址 | 指定mac地址進行抓包?src?mac:源mac ,dst?mac:目的mac |
多條件組合的時候:需要運算符進行連接:and? ?、 or 、not
host:src?host、 dst?host
1、指定主機ip進行抓包分析。抓取ip為?192.168.2.152的數(shù)據(jù)包
[root@nginx-kafka01 /]# tcpdump -i ens33 host 192.168.2.152 tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes 2:09:09.020981 IP nginx-kafka01.ssh > 192.168.2.130.60716: Flags [P.], seq 1528452:1528616, ack 181, win 284, length 164 22:09:09.021904 IP nginx-kafka01.ssh > 192.168.2.130.60716: Flags [P.], seq 1528616:1528884, ack 181, win 284, length 268 22:09:09.023833 IP 192.168.2.130.60716 > nginx-kafka01.ssh: Flags [.], ack 1528884, win 4102, length 0 22:09:09.023872 IP nginx-kafka01.ssh > 192.168.2.130.60716: Flags [P.], seq 1528884:1529048, ack 181, win 284, length 164 22:09:09.023879 IP 192.168.2.130.60716 > nginx-kafka01.ssh: Flags [P.], seq 181:217, ack 1528884, win 4102, length 36 ^C 10889 packets captured 10889 packets received by filter 0 packets dropped by kernelsrc?host:根據(jù)源ip地址來抓包?
dst?host:根據(jù)目的ip地址來抓包
根據(jù)源ip抓包:需要兩臺虛擬機配合,虛擬機A去ping,虛擬機B去抓包。
虛擬機A的ip為192.168.2.43,虛擬機B的ip為192.168.2.152。
先在A機器進行ping?192.168.2.152。
然后在B機器上(192.168.2.152)根據(jù)源ip:192.168.2.43進行抓包。
[root@nginx-kafka01 shell]# tcpdump -i ens33 icmp and src host 192.168.2.43 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes 12:03:18.050672 IP 192.168.2.43 > nginx-kafka01: ICMP echo request, id 13063, seq 22, length 64 12:03:19.029060 IP 192.168.2.43 > nginx-kafka01: ICMP echo request, id 13063, seq 23, length 64 12:03:20.026979 IP 192.168.2.43 > nginx-kafka01: ICMP echo request, id 13063, seq 24, length 64 12:03:21.033853 IP 192.168.2.43 > nginx-kafka01: ICMP echo request, id 13063, seq 25, length 64 12:03:22.028979 IP 192.168.2.43 > nginx-kafka01: ICMP echo request, id 13063, seq 26, length 64 12:03:23.044427 IP 192.168.2.43 > nginx-kafka01: ICMP echo request, id 13063, seq 27, length 64 ^C 6 packets captured 7 packets received by filter 0 packets dropped by kernel?2、port?端口:src port、dst port
src port:源端口
dst port:目的端口
舉例:抓取訪問本機的80號端口的數(shù)據(jù)包。
[root@nginx-kafka01 /]# tcpdump -i ens33 port 80?3、net:網(wǎng)段
抓取源IP從192.168.0.0/24網(wǎng)段來的,訪問本機80端口的數(shù)據(jù)包。
[root@nginx-kafka01 /]# tcpdump -i ens33 port 80 and src net 192.168.0.0/24 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes?4、mac:根據(jù)mac地址進行抓包
src 00:0c:29:ae:89:5d :是指源mac地址為:00:0c:29:ae:89:5d?
ether:是值icmp協(xié)議中沒有源ip和目的ip,所以需要依賴于ether 協(xié)議。
[root@nginx-kafka01 ~]# tcpdump -i ens33 ether src 00:0c:29:ae:89:5d?5、抓取源ip地址為192.168.2.137訪問本機的tcp協(xié)議的80端口的數(shù)據(jù)包。
[root@nginx-kafka01 /]# tcpdump -i ens33 src host 192.168.2.137 and dst port 80 and tcp tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes??6、抓ens33接口的數(shù)據(jù)包,源ip是192.168.0.1 目的端口是80 目的mac是00:0c:29:ea:ac:65的數(shù)據(jù)包,是tcp協(xié)議,請實現(xiàn)這個需求的tcpdump命令
[root@nginx-kafka01 shell]# tcpdump -i ens33 -e -vv tcp and src host 192.168.0.1 and dst port 80 and ether src 00:0c:29:ea:ac:65 [root@nginx-kafka01 ~]# tcpdump -i ens33 tcp and src host 192.168.0.1 and dst port 80 and ether src 02:42:50:15:dc:29 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes ^C 0 packets captured 1 packet received by filter 0 packets dropped by kernel?7、抓取192.168.2.137這臺機器ping過來的數(shù)據(jù)包。
[root@nginx-kafka01 ~]# tcpdump -i ens33 icmp and src host 192.168.2.137 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes 22:55:58.510368 IP nginx-kafka03 > nginx-kafka01: ICMP echo request, id 7518, seq 225, length 64 22:55:59.513581 IP nginx-kafka03 > nginx-kafka01: ICMP echo request, id 7518, seq 226, length 64 22:56:00.515075 IP nginx-kafka03 > nginx-kafka01: ICMP echo request, id 7518, seq 227, length 64 22:56:01.518040 IP nginx-kafka03 > nginx-kafka01: ICMP echo request, id 7518, seq 228, length 64 ^C 4 packets captured 4 packets received by filter 0 packets dropped by kernel?8、抓取訪問本機的icmp報文或者arp報文。
[root@nginx-kafka01 /]# tcpdump -nn -i ens33 icmp or arp tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes 22:59:25.984345 ARP, Request who-has 192.168.2.129 tell 192.168.2.1, length 46 22:59:25.993326 IP 192.168.2.137 > 192.168.2.152: ICMP echo request, id 7518, seq 432, length 64 22:59:25.993363 IP 192.168.2.152 > 192.168.2.137: ICMP echo reply, id 7518, seq 432, length 64 22:59:26.290334 ARP, Request who-has 192.168.2.126 tell 192.168.2.1, length 46 22:59:26.995193 IP 192.168.2.137 > 192.168.2.152: ICMP echo request, id 7518, seq 433, length 64 22:59:26.995234 IP 192.168.2.152 > 192.168.2.137: ICMP echo reply, id 7518, seq 433, length 64 ^C 6 packets captured 6 packets received by filter 0 packets dropped by kernel對輸出結(jié)果進行分析:?
第一列:時分秒毫秒?
第二列:網(wǎng)絡(luò)協(xié)議 IP
第三列:發(fā)送方的ip地址
第四列:箭頭 >, 表示數(shù)據(jù)流向
第五列:接收方的ip地址
第六列:冒號
第七列:數(shù)據(jù)包內(nèi)容,包括Flags 標(biāo)識符,seq 號,ack 號,win 窗口,數(shù)據(jù)長度 length,其中 [P.] 表示 PUSH 標(biāo)志位為 1。
更多標(biāo)識符:使用 tcpdump 抓包后,會遇到的 TCP 報文 Flags,有以下幾種:
- [S]?: SYN(開始連接)
- [P]?: PSH(推送數(shù)據(jù))
- [F]?: FIN (結(jié)束連接)
- [R]?: RST(重置連接)
- [.]?: 沒有 Flag (意思是除上面四種類型外的其他情況,有可能是 ACK 也有可能是 URG)。??
總結(jié)
以上是生活随笔為你收集整理的计算机网络抓包工具——tcpdump详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软抄袭 AppGet 始末,开源普法任
- 下一篇: 光电玻璃LED透明屏是黑科技?揭秘玻璃L