Wireshark 在 macOS 上使用及问题解决
wireshark概述
Wireshark 是被广泛使用的免费开源网络协议分析软件(network protocol analyzer)或网络数据包分析工具,它可以让你在微观层面上查看网络上发生的事情。它的主要功能是截取网络数据包,并尽可能详细地展示网络数据包信息内容。
项目地址:https://github.com/wireshark/wireshark
本篇文章主要讲述在 Mac 使用 Wireshark 时可能遇到的一些问题,并通过一个实例介绍详细的抓包手法。
常见问题
抓包报错
报错信息
You do not have permission to capture on device "bridge100". ((cannot open BPF device) /dev/bpf0: Permission denied)
解决办法
查看 /dev/bpf* 文件的权限
ls -l /dev/bpf*
更新 /dev/bpf* 文件权限和组权限
sudo chmod g+r /dev/bpf* && sudo chgrp admin /dev/bpf*
网卡太多
使用 ifconfig
命令可以查看到 Mac 中的所有网络接口,可能会出现数量繁多,不清楚选哪个的问题。
先来看看我的网卡信息。
$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>inet 127.0.0.1 netmask 0xff000000inet6 ::1 prefixlen 128 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
anpi1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500options=400<CHANNEL_IO>ether e2:5d:00:2d:ee:ecmedia: nonestatus: inactive
anpi0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500options=400<CHANNEL_IO>ether e2:5d:00:2d:ee:ebmedia: nonestatus: inactive
en3: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500options=400<CHANNEL_IO>ether e2:5d:00:2d:ee:cbnd6 options=201<PERFORMNUD,DAD>media: nonestatus: inactive
en4: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500options=400<CHANNEL_IO>ether e2:5d:00:2d:ee:ccnd6 options=201<PERFORMNUD,DAD>media: nonestatus: inactive
en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500options=460<TSO4,TSO6,CHANNEL_IO>ether 36:b2:2d:e4:95:80media: autoselect <full-duplex>status: inactive
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500options=460<TSO4,TSO6,CHANNEL_IO>ether 36:b2:2d:e4:95:84media: autoselect <full-duplex>status: inactive
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500options=63<RXCSUM,TXCSUM,TSO4,TSO6>ether 36:b2:2d:e4:95:80Configuration:id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0ipfilter disabled flags 0x0member: en1 flags=3<LEARNING,DISCOVER>ifmaxaddr 0 port 8 priority 0 path cost 0member: en2 flags=3<LEARNING,DISCOVER>ifmaxaddr 0 port 9 priority 0 path cost 0nd6 options=201<PERFORMNUD,DAD>media: <unknown type>status: inactive
ap1: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 1500options=400<CHANNEL_IO>ether 3e:57:dc:50:21:95media: autoselect
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500options=6460<TSO4,TSO6,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>ether 1c:57:dc:50:21:95inet6 fe80::493:9294:92cb:962d%en0 prefixlen 64 secured scopeid 0xc inet 10.201.102.126 netmask 0xffffe000 broadcast 10.201.127.255nd6 options=201<PERFORMNUD,DAD>media: autoselectstatus: active
awdl0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500options=6460<TSO4,TSO6,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>ether ce:41:0d:0a:06:8binet6 fe80::cc41:dff:fe0a:68b%awdl0 prefixlen 64 scopeid 0xd nd6 options=201<PERFORMNUD,DAD>media: autoselectstatus: active
llw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500options=400<CHANNEL_IO>ether ce:41:0d:0a:06:8binet6 fe80::cc41:dff:fe0a:68b%llw0 prefixlen 64 scopeid 0xe nd6 options=201<PERFORMNUD,DAD>media: autoselectstatus: inactive
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380inet6 fe80::bbb3:6daa:deb9:6132%utun0 prefixlen 64 scopeid 0xf nd6 options=201<PERFORMNUD,DAD>
utun1: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000inet6 fe80::a590:7109:be1b:f900%utun1 prefixlen 64 scopeid 0x10 nd6 options=201<PERFORMNUD,DAD>
utun2: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1000inet6 fe80::ce81:b1c:bd2c:69e%utun2 prefixlen 64 scopeid 0x11 nd6 options=201<PERFORMNUD,DAD>
utun3: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1500inet6 fe80::4562:973c:823c:162d%utun3 prefixlen 64 scopeid 0x12 nd6 options=201<PERFORMNUD,DAD>
utun4: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 9000inet 198.18.0.1 --> 198.18.0.1 netmask 0xfffe0000
vmenet0: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500ether 3e:64:95:b7:62:33media: autoselectstatus: active
bridge100: flags=8a63<UP,BROADCAST,SMART,RUNNING,ALLMULTI,SIMPLEX,MULTICAST> mtu 1500options=3<RXCSUM,TXCSUM>ether 1e:57:dc:05:87:64inet 192.168.64.1 netmask 0xffffff00 broadcast 192.168.64.255inet6 fe80::1c57:dcff:fe05:8764%bridge100 prefixlen 64 scopeid 0x15 inet6 fd93:45e9:560c:404:1020:f068:469e:d583 prefixlen 64 autoconf secured Configuration:id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0ipfilter disabled flags 0x0member: vmenet0 flags=3<LEARNING,DISCOVER>ifmaxaddr 0 port 20 priority 0 path cost 0nd6 options=201<PERFORMNUD,DAD>media: autoselectstatus: active
(base)
如何选网卡
在众多网卡中,重点关注 en0
和 bridge100
这两个接口,其他多数可以忽略。
接口名 | 含义 / 作用 | IP 地址 | 状态 |
---|---|---|---|
lo0 | 本地回环接口(localhost) | 127.0.0.1 | active |
en0 | 无线网卡或主要物理网卡 | 10.201.102.126 | active |
bridge100 | Docker 创建的虚拟桥接网卡 | 192.168.64.1 | active |
awdl0 | Apple Wireless Direct Link,隔空投送用 | 有 IPv6 | active |
utun* | VPN 隧道接口(一般是 VPN 连接创建的) | 有 IPv6 | active |
vmenet0 | 虚拟机网络桥接接口(如 VMware 或 UTM) | 无 IPv4 | active |
en1/en2/en3... | 其他物理或虚拟网卡 | 无 IPv4 | ❌ 多数 inactive |
anpi0/anpi1 | Apple Silicon 特有接口 | 无 IP | ❌ inactive |
抓包实例
本例用于分析在漏洞复现或攻击检测中捕获的数据流量,步骤如下:
确定靶机 IP
可以通过查看靶机(如虚拟机)中的 IP 地址获取。
判断哪个网卡可以访问靶机
在本机 ifconfig
中找到能访问靶机 IP 的网卡,一般为 bridge100
。
在 Wireshark 中选择该网卡进行抓包
启动 Wireshark,选择对应的 bridge100
网卡,点击开始抓包即可。
如图这是我抓的工具攻击的流量数据,这样就可以单独拿出来分析了。
wireshrk命令速查
IP 地址过滤
- ip.src == 192.168.1.107:来源 IP 是 192.168.1.107
- ip.dst == 192.168.1.107:目标 IP 是 192.168.1.107
- ip.addr == 192.168.1.107:来源或目标 IP 是该地址
端口过滤
- tcp.port == 80:来源或目标 TCP 端口为 80
- tcp.srcport == 80:来源端口为 80
- tcp.dstport == 80:目标端口为 80
- udp.port == 15000:UDP 任意端口为 15000
- tcp.port >= 1 and tcp.port <= 80:TCP 端口范围过滤
协议过滤
- tcp/ udp/ icmp/ http/ dns/ ftp/ ssl
- not arp/!arp:排除 ARP 包
MAC 地址过滤
- eth.src == A0:00:00:04:C5:84:来源 MAC
- eth.dst == A0:00:00:04:C5:84:目标 MAC
- eth.addr == A0:00:00:04:C5:84:任意 MAC(源或目的)
包长度过滤
- udp.length == 26:UDP 数据长度
- tcp.len >= 7:TCP 负载长度
- ip.len == 94:IP 层长度
- frame.len == 119:整个数据帧长度
HTTP 过滤
按方法
- http.request.method == “GET”
- http.request.method == “POST”
按内容
- http contains “GET”:包含 GET 字符串
- http contains “HTTP/1.1 200 OK”:HTTP 成功响应
TCP 参数过滤
- tcp.flags.syn == 1:TCP SYN 包
- tcp.window_size == 0 and tcp.flags.reset != 1
数据内容过滤(进阶)
UDP:
- udp[8:3] == 20:21:22:UDP payload 前3字节为指定值
- udp contains 7c:7c:7d:7d:包含任意位置匹配内容
TCP:
- tcp[20:3] == 47:45:54:等价于 tcp[20:3] == “GET”(ASCII)
- tcp[20:] matches “^GET.*HTTP/1.1\x0d\x0a”:用正则匹配完整 GET 请求
正则匹配语法速查
表达式 | 说明 |
---|---|
\d | 数字字符 |
\D | 非数字 |
\w | 单词字符 (字母、数字、下划线) |
\s | 空白字符 |
. | 任意字符 |
.* | 任意数量任意字符(除换行) |
^ | 字符串开头 |
$ | 字符串结尾 |
例子:
tcp[20:] matches "^GET.*"
:匹配以 GET 开头的 TCP 数据udp[8:] matches "\\x14\\x05"
:匹配特定字节序列