最近家里添置了不少物联网设备。现在不少的物联网设备为了方便,都采用配网协议,让用户将Wi-Fi的SSID和密码直接配置上去。这样设备就可以自主连接其服务器,进行数据上报和控制流转发。

这样一来方便的确是是方便了。不过作为一个互联网行业的从业者,深知这种情况下的数据隐私泄露的风险极大。虽然我们可以君子协定似的相信各厂商,但是翻翻各大安卓市场应用的应用,就知道这种相信是多么的天真。

所以,为了保险,最好的方法就是看看他们到底联网干什么。不入虎穴焉得虎子。要想搞清楚这个,最好的方法莫过于网络抓包。不过由于物联网设备本身就是嵌入式的,想要直接在设备上抓包几乎是不可能的。那么怎么办呢?最简单最全面的办法,就是在接入端,也就是路由器侧抓取和分析网络流量。

一般的路由器,想要抓包还是挺麻烦的。不过所幸我是自己刷的 OpenWrt ,作为一款支持 Linux 的嵌入式系统,可以直接安装 Tcpdump ,抓包的问题就迎刃而解。想要在 OpenWrt 安装 Tcpdump ,可以登陆 OpenWrt 终端,执行下面的命令:

opkg update
opkg install tcpdump

安装好 Tcpdump 后,就可以直接运行起来开始抓包了。不过这里又有一个问题了,路由器本身也是一个嵌入式设备,本身内存优先就限制了抓包的量不能太大。另外抓下来的包,在路由器上也不好分析。说到分析网络流量包,我想大多数人都听说过甚至喜欢 Wireshark 。那么,有没有办法使用 Wireshark 来分析 OpenWrt 抓取的流量呢?

还真有!得益于 Wireshark 的强大功能,从 Wireshark--interface 参数文档 中我们可以看到。除了支持从指定网络接口抓包,还支持从管道获取数据,当管道名为-时,可以直接从标准输出获取数据流。原始文档摘录如下:

-i <capture interface>, --interface <capture interface>
Set the name of the network interface or pipe to use for live packet capture.

Network interface names should match one of the names listed in wireshark -D (described above). A number, as reported by wireshark -D, can also be used. If you’re using UNIX, netstat -i, ifconfig -a or ip link might also work to list interface names, although not all versions of UNIX support the -a flag to ifconfig.

If no interface is specified, Wireshark searches the list of interfaces, choosing the first non-loopback interface if there are any non-loopback interfaces, and choosing the first loopback interface if there are no non-loopback interfaces; if there are no interfaces, Wireshark reports an error and doesn’t start the capture.

Pipe names should be either the name of a FIFO (named pipe) or “-” to read data from the standard input. Data read from pipes must be in standard libpcap format.

这样一来,问题就简单了。我们可以通过SSH登陆到路由器,然后在终端上运行 Tcpdump 进行抓包,并将抓取的数据直接输出到标准输出,并通过管道传递给 Wireshark 。参考命令如下:

ssh 用户名@主机地址 'tcpdump  -s 0 -U -n -w - -i br-lan [过滤表达式]' | wireshark -k -i -

其中, Tcpdump 的几个参数说明如下:

  • -s 0 : 表示抓包大小,设置为 0 表示不限制。
  • -n : 不解析IP地址为主机名(以及不解析端口为端口名)
  • -w - : 表示抓取到的包输出到标准输出
  • -i br-lan : 表示抓取 br-lan 接口上的包。路由器上通常都是这个网络接口。

关于 Wireshark 的参数说明如下:

  • -k : 表示启动wireshark后立即开始抓包
  • -i - : 表述从标准输入获取数据

例如,路由器用户名是 root 、路由器地址是 192.168.0.1 ,想要抓取路由器上流过的所有 192.168.0.100 的数据,则可以用如下命令:

ssh root@192.168.0.1 'tcpdump  -s 0 -U -n -w - -i br-lan host 192.168.0.100' | wireshark -k -i -