赞
踩
目录
tcpdump是Linux里的字符界面的数据抓包分析软件。tcpdump是一个用于截取网络分组,并输出分组内容的工具。
tcpdump:其中tcp:传输控制协议,位于传输层transmission control protocol。dump:导出。
Windows 抓包软件:wireshark、科来网络分析系统:抓网卡数据包。
使用tcpdump来抓包可以知道有哪些客户端的人访问到自己的服务器上了。
只使用tcpdump命令进行抓包:截获主机收到和发出的所有数据包。
tcpdump截取的包默认数据包的头部,默认情况下,直接启动tcpdump将监听的是第一个网络接口上所有流过的数据包。
- [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 kernel
tcpdump里面的参数,这些参数是用来过滤的。
-i | interface指定抓哪个接口的数据包。 |
-vv | 显示更加详细的信息。 |
-e | 显示mac地址。 |
-w | write 写入保存到文件中。导入的文件可以使用wireshark打开。 |
-r | read 读取文件中的数据。 |
-c | 在收到指定包数目之后,tcpdump就会停止。 |
文件的后缀名可以自定义.pcap packet capture
例如:
1、监听指定网卡ens33的所有传输数据包
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^C
-
- 21:48:51.610340 ARP, Request who-has 192.168.2.104 tell gateway, length 46
-
- 1032 packets captured
- 1086 packets received by filter
-
2、创建文件,文件名后缀可以随便取,然后使用tcp协议抓网卡ens33的所有传输数据包并显示mac地址并写入feng.sc文件中。
- [root@nginx-kafka01 shell]# touch feng.sc
- # -i ens33:指定抓ens33接口中的数据包
- [root@nginx-kafka01 shell]# tcpdump -i ens33 -e -vv tcp -w feng.sc
-
- # -r :读取文件数据
- [root@nginx-kafka01 shell]# tcpdump5 -r feng.sc
3、-i :抓取网卡ens33的所有传输数据包 ,-vv:显示更加详细的信息。
- [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 :抓取网卡ens33的所有传输数据包 ,-vv:显示更加详细的信息。-e:显示mac地址。
- tcpdump -i ens33 -vv -e
- [root@nginx-kafka01 ~]# tcpdump -i ens33 -vv -e
type类过滤器后面需要接相应的参数。
host | 根据主机ip来抓包 |
port | 根据端口进行抓包 |
net | 根据网段进行抓包 |
protocol | 根据协议进行抓包 |
protocol协议 | tcp、udp、icmp、ip、arp、rarp、ether协议等 |
icmp协议 | ping命令 |
mac地址 | 指定mac地址进行抓包 src mac:源mac ,dst mac:目的mac |
多条件组合的时候:需要运算符进行连接:and 、 or 、not
host:src host、 dst host
1、指定主机ip进行抓包分析。抓取ip为 192.168.2.152的数据包
- [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 kernel
src host:根据源ip地址来抓包
dst host:根据目的ip地址来抓包
根据源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)根据源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号端口的数据包。
[root@nginx-kafka01 /]# tcpdump -i ens33 port 80
3、net:网段
抓取源IP从192.168.0.0/24网段来的,访问本机80端口的数据包。
- [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:根据mac地址进行抓包
src 00:0c:29:ae:89:5d :是指源mac地址为:00:0c:29:ae:89:5d
ether:是值icmp协议中没有源ip和目的ip,所以需要依赖于ether 协议。
[root@nginx-kafka01 ~]# tcpdump -i ens33 ether src 00:0c:29:ae:89:5d
5、抓取源ip地址为192.168.2.137访问本机的tcp协议的80端口的数据包。
- [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接口的数据包,源ip是192.168.0.1 目的端口是80 目的mac是00:0c:29:ea:ac:65的数据包,是tcp协议,请实现这个需求的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过来的数据包。
- [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
对输出结果进行分析:
第一列:时分秒毫秒
第二列:网络协议 IP
第三列:发送方的ip地址
第四列:箭头 >, 表示数据流向
第五列:接收方的ip地址
第六列:冒号
第七列:数据包内容,包括Flags 标识符,seq 号,ack 号,win 窗口,数据长度 length,其中 [P.] 表示 PUSH 标志位为 1。
更多标识符:使用 tcpdump 抓包后,会遇到的 TCP 报文 Flags,有以下几种:
[S]
: SYN(开始连接)[P]
: PSH(推送数据)[F]
: FIN (结束连接)[R]
: RST(重置连接)[.]
: 没有 Flag (意思是除上面四种类型外的其他情况,有可能是 ACK 也有可能是 URG)。 Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。