tcpdump+wireshark抓包简介

257 阅读8分钟

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同上
  • 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]

受支持的协议列表:

  1. 与: && and 示例:tcp and udp捕获tcp且udp协议包(什么都没有)

  2. 或:|| or 示例:tcp or udp捕获tcp或udp协议包

  3. 非:! not示例:not udp捕获非udp包

  4. 等于:== eq 示例:ip.addr==127.0.0.1

  5. 不等于:!= ne 示例:ip.addr ne 127.0.0.1地址中某一个不是127.0.0.1的包

  6. 全部不等于:!==any_ne和不等于不同的时如果匹配的列表大于1的时候要求所有的都不等于,

    -. 示例:ip.addr !== 127.0.0.1源地址和目标地址都不是127.0.0.1的包

    • ===表示全等于,同理
  7. 大于:> gt

  8. 小于:< lt

  9. 大于等于:>= ge

  10. 小于等于:<= le

  11. 组合:

    1. () 示例:http || tls && (ip.dst==10.200.193.45 || ip.dst==10.200.193.46)
    2. in 示例:json.key in {"id","name"} json中的key包含id和name
  12. 包含:contains 类似于于lua的string.match(str,regExp,1)函数,可以用于字符串字段等大部分场景。示例:http.content_type contains "^app"捕获http协议的content_type以字符串“app”开头的包,ip contains 10.5捕获ip包含“10.5”的包

  13. 匹配:~ matches类似于于lua的string.match(str,regExp,1)函数,只能用于协议或其他字符串,支持完整的Perl语言正则表达式。示例:ip matches "10.200.193." 匹配包含“10.200.193.”的IP的包

  14. 截取:类似于lua的string.find(str,findStr,regExp,idx,true)函数,和golang的字符串切片不同

    1. [idx]从下标idx截取一个字符:示例http.host[0] == "i"匹配host以“i”开头的包
    2. [idx:]从下标idx截取一个及以上字符:示例http.host[3:] matches "sit"匹配host下标3开始包含“sit”的包
    3. [idx:count]从下标idx截取count个字符:示例http.host[3:3] == "sit"匹配host下标3开始的三个字符等于“sit”的包

搜索显示过滤器表达式

常用显示过滤器表达式

  1. 特定协议:常用的有tcp、udp、http、json、json.array

  2. 域名IP端口:http.host、ip.dst、ip.addr、tcp.port

  3. body长度大于某个bytes长度的http请求:len(http.file_data) > 2514

  4. body为空的http请求:len(http.file_data) == 0

  5. JSON字符串包含某个字符串:json contains "字符串支持中文"

  6. JSON含有某个key:json.key == "name"

  7. JSON包含某个键值对:

    1. json.path_with_value == "/items/[]/name:北京市"表示json path为:items[].name=北京市,即:{items:[name:"北京市"]}
    2. json.member_with_value == "name:北京市"表示只要有{name:"北京市"}的键值对存在即可
    3. json.path_with_value === "/name:北京市"表示仅显示{name:"北京市"}的JSON
  8. JSON包含某个字符串的http请求:json.value.string ~ "待办"

  9. 请求PATH包含某个字符串:http.request.uri.path matches "^/amix"

  10. 请求query包含某个字符串:http.request.uri.query matches "plainData="

  11. 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

  1. 抓包dump文件:tcpdump -e -s0 -vvv -i eth0 tcp port 8080 -w test.pcap
  2. 导入wireshark查看日志

image.png

客户端抓包https(tls1.2)

  1. Chrome ssl日志参数 "C:\Program Files\Google\Chrome\Application\chrome.exe" --ssl-key-log-file=%USERPROFILE%\Desktop\sslkeys.pms
  2. 然后wireshark中首选项-protocols-TLS配置预主密钥日志%USERPROFILE%\Desktop\sslkeys.pms文件

  1. 不用再配置密钥xxx.key文件
  2. 测试查看筛选器:http