wireshark-notes

捕获过滤器

Wireshark 中,捕获过滤器使用了伯克利包过滤(Berkeley Packet Filter,BPF),它采用了一种与自然语言很接近的语法,利用这种语法构成的字符串可以确定保留哪些数据包以及忽略掉哪些数据包,限定符有 3 种:

  • type:host(默认)、net、port
  • dir:src、dst
  • proto:ether、ip、tcp、arp 等

举例:

  • src host 192.168.1.100 and dst net 192.168.1.0/24
  • ether dst 11:22:33:44:55:66
  • tcp port 8080

高级用法:可以进行精确到位的操作,针对数据报文中的每个字段进行过滤控制,语法为 proto[expr:size] ,其中 expr 为偏移量,size 为要操作的字节数,默认为 1。

例如:一个 IP 数据包头部长度为 20 字节,其中 12~15 这四个字节表示的是源地址,如果要过滤源地址为 192.168.1.1 的数据包(需要把 IP 地址转换为 16 进制),表达式可以写为:ip[12:4] = 0xC0A80101

显示过滤器

显示过滤器是在捕获数据之后再使用,语法与捕获过滤器不同。

一般由三部分组成:字段名称、关系、值,例如 ip.src == 192.168.1.1,Wireshark 过滤器输入框具有联想功能,可以显示出协议的可选字段(如下图),方便进行过滤器的编写。

过滤器

一个过滤表达式编写完成且格式正确时,输入框会显示为绿色;反之,则为红色,需要表达式进行修改。

Wireshark 还可以使用数据包中的指定字段来创建过滤表达式。首先在数据包列表处选中一个数据包,然后在数据包详细信息栏处查看这个数据包的详细内容,这里会以行的形式展示数据包的信息,当选中其中一行时(见下图),例如 IP 地址,那么在状态栏处就会显示出该数据包该行对应的过滤器表达式。

状态栏显示过滤器

还有更简单的办法,就是在选中的字段单击右键,在弹出菜单中选择“作为过滤器应用”,并根据二级菜单进行选择“选中”,就可以在输入栏中看到生成的表达式了。

远程捕获数据包

在实际工作中,经常会碰到捕捉其他设备通信数据包的情况,例如在机房的服务器,可以登录但却不能直接触碰。多数情况下,会采用在远程设备上安装 Wireshark,再通过远程桌面的方式进行操作,这样不但会加大工作量,也产生了不必要的数据包。

Wireshark 提供了远程捕获数据包的功能,可以很方便的监控远程服务器上的流量,只需要在服务器上安装 rpcap 即可,Windows 操作系统安装了 WinPcap 后,就自带了该工具。

Linux 下需要编译安装,参见 rpcapd

使用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# rpcapd -h

USAGE:
rpcapd [-b <address>] [-p <port>] [-6] [-l <host_list>] [-a <host,port>]
[-n] [-v] [-d] [-s <file>] [-f <file>]
-b <address>: the address to bind to (either numeric or literal).
Default: it binds to all local IPv4 addresses
-p <port>: the port to bind to. Default: it binds to port 2002
-4: use only IPv4 (default both IPv4 and IPv6 waiting sockets are used)
-l <host_list>: a file that keeps the list of the hosts which are allowed
to connect to this server (if more than one, list them one per line).
We suggest to use literal names (instead of numeric ones) in order to
avoid problems with different address families
-n: permit NULL authentication (usually used with '-l')
-a <host,port>: run in active mode when connecting to 'host' on port 'port'
In case 'port' is omitted, the default port (2003) is used
-v: run in active mode only (default: if '-a' is specified, it accepts
passive connections as well
-d: run in daemon mode (UNIX only) or as a service (Win32 only)
Warning (Win32): this switch is provided automatically when the service
is started from the control panel
-s <file>: save the current configuration to file
-f <file>: load the current configuration from file; all the switches
specified from the command line are ignored
-h: print this help screen

例如:在 远程服务器中运行了 rpcapd -4 -n -p 8888,那么可以在本地 Wireshark 中,添加远程抓包,如下图:

开启远程抓包

wireshark 配置连接远程抓包

之后,就可以在本地的 Wireshark 抓到远程服务器上的数据包了。

解密数据包

Chrome 浏览器添加运行参数 :--ssl-key-log-file="C:\Users\xdai\Desktop\sslkeylog.log",可以将访问网站的 SSL Key 信息保存下来,或者为系统添加环境变量 SSHKEYLOGFILE = "/Path/to/log" 来保存 log 文件,对于 Windows 系统,环境变量重启系统会才会生效/取消生效,Linux 系统使用 export 立即生效。

然后打开 Wireshark 开启抓包,打开 Chrome 访问百度;抓取到数据包后,导入已经出现的 SSL Key 信息:“编辑”→“首选项”,然后在打开的“首选项”窗口中选中“Protocols”,找到 TLS 并导入文件。

ssl key 设置

可以看到之前为加密的数据包,导入 Key 之后,以明文的方式显示出来了(例如,403 和 408)。

解密前:

解密前报文

解密后:

解密后报文