Linux抓包

首先查看当前设备上有几个网卡

#ifconfig
em1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.1.232  netmask 255.255.255.0  broadcast 172.20.1.255
        inet6 fe80::569f:35ff:fe1d:90ce  prefixlen 64  scopeid 0x20<link>
        ether 54:9f:35:1d:90:ce  txqueuelen 1000  (Ethernet)
        RX packets 41967785  bytes 12381472598 (11.5 GiB)
        RX errors 0  dropped 9565  overruns 0  frame 0
        TX packets 10415926  bytes 5771086680 (5.3 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 16  

em2: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 54:9f:35:1d:90:d0  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 17  

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 6733776  bytes 5017337440 (4.6 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6733776  bytes 5017337440 (4.6 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

使用tcpdump命令,如果我们想查看网卡em1上的通信包,就添加参数-i em1;tcp port 554指的是过滤条件,多个过滤条件之间用and连接。

#tcpdump -i em1 tcp port 554
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on em1, link-type EN10MB (Ethernet), capture size 65535 bytes
14:32:21.535221 IP 172.20.1.177.49444 > 172.20.1.232.rtsp: Flags [S], seq 773779494, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
14:32:21.535286 IP 172.20.1.232.rtsp > 172.20.1.177.49444: Flags [R.], seq 0, ack 773779495, win 0, length 0
14:32:22.034397 IP 172.20.1.177.49444 > 172.20.1.232.rtsp: Flags [S], seq 773779494, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
14:32:22.034456 IP 172.20.1.232.rtsp > 172.20.1.177.49444: Flags [R.], seq 0, ack 1, win 0, length 0
14:32:22.538449 IP 172.20.1.177.49444 > 172.20.1.232.rtsp: Flags [S], seq 773779494, win 8192, options [mss 1460,nop,nop,sackOK], length 0
14:32:22.538494 IP 172.20.1.232.rtsp > 172.20.1.177.49444: Flags [R.], seq 0, ack 1, win 0, length 0
14:32:22.538924 IP 172.20.1.177.49447 > 172.20.1.232.rtsp: Flags [S], seq 1476281833, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
14:32:22.538964 IP 172.20.1.232.rtsp > 172.20.1.177.49447: Flags [R.], seq 0, ack 1476281834, win 0, length 0
14:32:23.048457 IP 172.20.1.177.49447 > 172.20.1.232.rtsp: Flags [S], seq 1476281833, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
14:32:23.048529 IP 172.20.1.232.rtsp > 172.20.1.177.49447: Flags [R.], seq 0, ack 1, win 0, length 0
14:32:23.548504 IP 172.20.1.177.49447 > 172.20.1.232.rtsp: Flags [S], seq 1476281833, win 8192, options [mss 1460,nop,nop,sackOK], length 0
14:32:23.548560 IP 172.20.1.232.rtsp > 172.20.1.177.49447: Flags [R.], seq 0, ack 1, win 0, length 0
14:32:23.558961 IP 172.20.1.177.49452 > 172.20.1.232.rtsp: Flags [S], seq 1537317808, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
14:32:23.559010 IP 172.20.1.232.rtsp > 172.20.1.177.49452: Flags [R.], seq 0, ack 1537317809, win 0, length 0
14:32:24.068521 IP 172.20.1.177.49452 > 172.20.1.232.rtsp: Flags [S], seq 1537317808, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
14:32:24.068578 IP 172.20.1.232.rtsp > 172.20.1.177.49452: Flags [R.], seq 0, ack 1, win 0, length 0
14:32:24.568551 IP 172.20.1.177.49452 > 172.20.1.232.rtsp: Flags [S], seq 1537317808, win 8192, options [mss 1460,nop,nop,sackOK], length 0
14:32:24.568606 IP 172.20.1.232.rtsp > 172.20.1.177.49452: Flags [R.], seq 0, ack 1, win 0, length 0

tcpdump命令需要在root用户下执行。如果想要保存通信报文,可将输出结果重定向到一个snoop文件。snoop文件可以用wireshark等工具软件打开。

Linux 中的抓包工具主要是 tcpdump 和 Wireshark 的命令行版本 tshark。这里主要介绍 tcpdump 的使用方法。 tcpdump 是一个命令行网络分析工具,它允许用户截取经过网络接口的数据包,并能够将网络层、协议、端口等信息进行过滤,方便用户分析网络上的问题或数据流动情况。使用 tcpdump 的基本命令格式如下: ``` tcpdump [选项] [过滤表达式] ``` - 选项部分可以指定抓包的接口、抓包数量、抓包超时时间等。 - 过滤表达式用于指定需要抓取哪些数据包,比如根据 IP 地址、端口号、协议类型等进行过滤。 基本示例命令: 1. 抓取指定网络接口 eth0 的所有数据包: ``` tcpdump -i eth0 ``` 2. 保存抓取的数据包到文件中: ``` tcpdump -w capture.pcap -i eth0 ``` 3. 读取之前保存的抓包文件: ``` tcpdump -r capture.pcap ``` 4. 过滤只抓取包含特定 IP 地址的数据包: ``` tcpdump -i eth0 src 192.168.1.100 ``` 或 ``` tcpdump -i eth0 dst 192.168.1.100 ``` (src 表示源地址,dst 表示目的地址) 5. 过滤特定端口的数据包: ``` tcpdump -i eth0 port 80 ``` 6. 过滤特定协议的数据包,比如只捕获 TCP 或者 UDP 协议的数据包: ``` tcpdump -i eth0 tcp ``` 或 ``` tcpdump -i eth0 udp ``` 这些是使用 tcpdump 命令进行基本抓包操作的一些示例,tcpdump 还有许多高级的用法和参数,可以通过阅读它的手册(man tcpdump)来获取更多信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皓月如我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值