wireshark
安装
wireshark是一个网络封包分析软件,在linux上可以解析tcpdump文件
wireshark下载地址www.wireshark.org/
安装时会同步安装Npcap,否则无法正常抓包
官方文档:www.wireshark.org/docs/wsug_h…
捕获过滤器表达式
语法格式:
<Protocol:网络层、传输层协议>
<Direction:方向> <Host:主机地址> <Value:值>
<Logical Operation>
<other expression>
说明:
Protocol:常用的有tcp
udp
ip
ip6
Host:包含:
-
net网段,用法
- net ip切片,如
net 10.200
捕获掩码为255.255.0.0的10.200网段 - net 完整ip msk,如
net 10.200.1.0 255.255.255.0
捕获掩码为255.255.255.0的10.200.1网段 - net 完整ip/掩码位数,CIDR格式的掩码,如
net 10.200.1.0/24
同上
- net ip切片,如
-
host主机 ,域名或IP,如
host 10.200.193.45
-
port端口,端口,如
port 80
-
portrange端口范围,用法为
portrange startP-endP
,如portrange 8080-8089
Direction:常用的有src
dst
,如dst port 80表示仅捕获目标端口为80的包(通常即发送给标http服务器的包,也就是回来的不捕获)
Logical Operation:支持和显示表达式一样的逻辑运算符,如tcp and ip
:表示捕获tcp协议且ip协议的包
常用语法:
捕获tcp/ip协议和特定host交互的包: tcp and ip and host www.baidu.com
查看捕获过滤器
显示过滤器表达式
官方文档:www.wireshark.org/docs/wsug_h…
表达式格式:[not] primitive [and|or [not] primitive ...]
原语格式:Protocol[.string1.string2...stringN] [Comparison operator] [Value]
受支持的协议列表:
-
与:
&&
and
示例:tcp and udp
捕获tcp且udp协议包(什么都没有) -
或:
||
or
示例:tcp or udp
捕获tcp或udp协议包 -
非:
!
not
示例:not udp
捕获非udp包 -
等于:
==
eq
示例:ip.addr==127.0.0.1
-
不等于:
!=
ne
示例:ip.addr ne 127.0.0.1
地址中某一个不是127.0.0.1的包 -
全部不等于:
!==any_ne
和不等于不同的时如果匹配的列表大于1的时候要求所有的都不等于,-. 示例:
ip.addr !== 127.0.0.1
源地址和目标地址都不是127.0.0.1的包===
表示全等于,同理
-
大于:
>
gt
-
小于:
<
lt
-
大于等于:
>=
ge
-
小于等于:
<=
le
-
组合:
()
示例:http || tls && (ip.dst==10.200.193.45 || ip.dst==10.200.193.46)
in
示例:json.key in {"id","name"}
json中的key包含id和name
-
包含:
contains
类似于于lua的string.match(str,regExp,1)函数,可以用于字符串字段等大部分场景。示例:http.content_type contains "^app"
捕获http协议的content_type以字符串“app”开头的包,ip contains 10.5
捕获ip包含“10.5”的包 -
匹配:
~
matches
类似于于lua的string.match(str,regExp,1)函数,只能用于协议或其他字符串,支持完整的Perl语言正则表达式。示例:ip matches "10.200.193."
匹配包含“10.200.193.”的IP的包 -
截取:类似于lua的string.find(str,findStr,regExp,idx,true)函数,和golang的字符串切片不同
[idx]
从下标idx截取一个字符:示例http.host[0] == "i"
匹配host以“i”开头的包[idx:]
从下标idx截取一个及以上字符:示例http.host[3:] matches "sit"
匹配host下标3开始包含“sit”的包[idx:count]
从下标idx截取count个字符:示例http.host[3:3] == "sit"
匹配host下标3开始的三个字符等于“sit”的包
搜索显示过滤器表达式
常用显示过滤器表达式
-
特定协议:常用的有tcp、udp、http、json、json.array
-
域名IP端口:http.host、ip.dst、ip.addr、tcp.port
-
body长度大于某个bytes长度的http请求:
len(http.file_data) > 2514
-
body为空的http请求:
len(http.file_data) == 0
-
JSON字符串包含某个字符串:
json contains "字符串支持中文"
-
JSON含有某个key:
json.key == "name"
-
JSON包含某个键值对:
json.path_with_value == "/items/[]/name:北京市"
表示json path为:items[].name=北京市,即:{items:[name:"北京市"]}json.member_with_value == "name:北京市"
表示只要有{name:"北京市"}的键值对存在即可json.path_with_value === "/name:北京市"
表示仅显示{name:"北京市"}的JSON
-
JSON包含某个字符串的http请求:
json.value.string ~ "待办"
-
请求PATH包含某个字符串:
http.request.uri.path matches "^/amix"
-
请求query包含某个字符串:
http.request.uri.query matches "plainData="
-
500错误:
http.response.code == 500
快速过滤技巧
添加自定义过滤标签
tcpdump
## tcpdump抓包
## 抓8080端口的TCP链接所有报文
## -e:显示数据链路层信息(mac、vlan tag)
## -s0:显示全部报文
## -v:显示更多详情,vv再多,vvv再再多
## -i eth0 :监听指定网卡
## -w test.pcap:导出到指定文件
## tcp:过滤tcp
## prot 8080:过滤端口
## and:串联过滤
## net 10.52.35.194:过滤ip
tcpdump -e -s0 -v -i eth0 tcp port 8080 and net 10.52.35.194 -w test.pcap
## 模拟请求
curl -v 10.200.193.19:8080
##示例:
tcpdump -e -s0 -vvv -i eth0 tcp port 8080 and net 10.52.35.194
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
##同步阶段
##第一次握手,标志位Flags=S
##S=SYN(开始连接);P=PSH(推送数据);F=FIN (结束连接);R=RST(重置连接).:没有 Flag,一般为ACK
##时间 mac > mac 网络协议 长度
##host:port > host:port 标志 checksum 序列 窗口
23:10:38.768318 68:05:ca:21:d6:e5 (oui Unknown) > fa:16:3e:74:83:a4 (oui Unknown), ethertype IPv4 (0x0800), length 66: (tos 0x0, ttl 117, id 33823, offset 0, flags [DF], proto TCP (6), length 52)
10.52.35.194.61795 > rlkj-yk-nginx.webcache: Flags [S], cksum 0x8b9d (correct), seq 203346594, win 64240, options [mss 1200,nop,wscale 8,nop,nop,sackOK], length 0
##第二次握手,标志位Flags=[S.] SYN+ACK
23:10:38.768375 fa:16:3e:74:83:a4 (oui Unknown) > 68:05:ca:21:d6:e5 (oui Unknown), ethertype IPv4 (0x0800), length 66: (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52)
rlkj-yk-nginx.webcache > 10.52.35.194.61795: Flags [S.], cksum 0xf9f7 (incorrect -> 0x3dab), seq 1868195428, ack 203346595, win 29200, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
##第三次握手,标志位Flags=[.] ACK
23:10:38.807043 68:05:ca:21:d6:e5 (oui Unknown) > fa:16:3e:74:83:a4 (oui Unknown), ethertype IPv4 (0x0800), length 54: (tos 0x0, ttl 117, id 20255, offset 0, flags [DF], proto TCP (6), length 40)
10.52.35.194.61795 > rlkj-yk-nginx.webcache: Flags [.], cksum 0xee8a (correct), seq 1, ack 1, win 515, length 0
##报文读取阶段
##接收客户端报文 PSH+ACK
23:10:38.886951 68:05:ca:21:d6:e5 (oui Unknown) > fa:16:3e:74:83:a4 (oui Unknown), ethertype IPv4 (0x0800), length 135: (tos 0x0, ttl 117, id 6480, offset 0, flags [DF], proto TCP (6), length 121)
10.52.35.194.61795 > rlkj-yk-nginx.webcache: Flags [P.], cksum 0x2c7d (correct), seq 1:82, ack 1, win 515, length 81: HTTP, length: 81
GET / HTTP/1.1
# 其他http报文
##确认 ACK
23:10:38.886965 fa:16:3e:74:83:a4 (oui Unknown) > 68:05:ca:21:d6:e5 (oui Unknown), ethertype IPv4 (0x0800), length 54: (tos 0x0, ttl 64, id 23239, offset 0, flags [DF], proto TCP (6), length 40)
rlkj-yk-nginx.webcache > 10.52.35.194.61795: Flags [.], cksum 0xf9eb (incorrect -> 0xef57), seq 1, ack 82, win 229, length 0
##发送服务端报文 PSH+ACK
23:10:38.889932 fa:16:3e:74:83:a4 (oui Unknown) > 68:05:ca:21:d6:e5 (oui Unknown), ethertype IPv4 (0x0800), length 2439: (tos 0x0, ttl 64, id 23240, offset 0, flags [DF], proto TCP (6), length 2425)
rlkj-yk-nginx.webcache > 10.52.35.194.61795: Flags [P.], cksum 0x033d (incorrect -> 0x0e2a), seq 1:2386, ack 82, win 229, length 2385: HTTP, length: 2385
HTTP/1.1 200 OK
# 其他http报文
##确认 ack
23:10:38.926916 68:05:ca:21:d6:e5 (oui Unknown) > fa:16:3e:74:83:a4 (oui Unknown), ethertype IPv4 (0x0800), length 54: (tos 0x0, ttl 117, id 28835, offset 0, flags [DF], proto TCP (6), length 40)
10.52.35.194.61795 > rlkj-yk-nginx.webcache: Flags [.], cksum 0xe4e8 (correct), seq 82, ack 2386, win 515, length 0
##挥手FIN+ACK
23:10:38.990894 68:05:ca:21:d6:e5 (oui Unknown) > fa:16:3e:74:83:a4 (oui Unknown), ethertype IPv4 (0x0800), length 54: (tos 0x0, ttl 117, id 8643, offset 0, flags [DF], proto TCP (6), length 40)
10.52.35.194.61795 > rlkj-yk-nginx.webcache: Flags [F.], cksum 0xe4e7 (correct), seq 82, ack 2386, win 515, length 0
##FIN+ACK
23:10:38.990939 fa:16:3e:74:83:a4 (oui Unknown) > 68:05:ca:21:d6:e5 (oui Unknown), ethertype IPv4 (0x0800), length 54: (tos 0x0, ttl 64, id 23242, offset 0, flags [DF], proto TCP (6), length 40)
rlkj-yk-nginx.webcache > 10.52.35.194.61795: Flags [F.], cksum 0xf9eb (incorrect -> 0xe604), seq 2386, ack 83, win 229, length 0
##ACK
23:10:39.029077 68:05:ca:21:d6:e5 (oui Unknown) > fa:16:3e:74:83:a4 (oui Unknown), ethertype IPv4 (0x0800), length 54: (tos 0x0, ttl 117, id 30740, offset 0, flags [DF], proto TCP (6), length 40)
10.52.35.194.61795 > rlkj-yk-nginx.webcache: Flags [.], cksum 0xe4e6 (correct), seq 83, ack 2387, win 515, length 0
^C
10 packets captured
10 packets received by filter
0 packets dropped by kernel
tcpdump抓包导入wireshark
- 抓包dump文件:tcpdump -e -s0 -vvv -i eth0 tcp port 8080 -w test.pcap
- 导入wireshark查看日志
客户端抓包https(tls1.2)
- Chrome ssl日志参数 "C:\Program Files\Google\Chrome\Application\chrome.exe" --ssl-key-log-file=%USERPROFILE%\Desktop\sslkeys.pms
- 然后wireshark中首选项-protocols-TLS配置预主密钥日志%USERPROFILE%\Desktop\sslkeys.pms文件
- 不用再配置密钥xxx.key文件
- 测试查看筛选器:
http