linux中非常有用的网络工具,运行在用户态。
数据包到达网卡,经过数据包过滤器bpf筛选后,拷贝至用户态的tcpdump程序。
tcpdump抓包
“抓” 这个动作是由数据包过滤器bpf完成, bpf的主要作用就是根据用户输入的过滤规则,只将用户关心的数据包拷贝至tcpdump,注意是拷贝,不是剪切。
面试题:某些数据包被iptables封禁,是否可通过tcpdump抓包? linux中netfilter是工作在tcp协议栈阶段, tcpdump的过滤器bpf是工作在更底层,所以当然是可以抓到包。
抓包上帝视角:
注意:命令中的hostname会被解析成ip地址,如果解析出来的是vip(可能会作用到多个实际的业务Host),会抓取到非预期的数据包。
可以使用 src host 或者 dst host来抓取特定方向的流量。
tcpdump -n -i eth0 host janus.qa.xxxx.com -vvv -tttt
2023-11-08 14:37:48.478256 IP (tos 0x0, ttl 64, id 47134, offset 0, flags [DF], proto TCP (6), length 554)
10.178.75.56.44054 > 10.98.21.3.http: Flags [P.], cksum 0x776b (incorrect -> 0x1251), seq 514:1028, ack 186, win 589, length 514: HTTP, length: 514
POST /janus-api/api/agent/tasks HTTP/1.1
Host: janus.qa.****.com
User-Agent: Go-http-client/1.1
Content-Length: 366
Accept-Encoding: gzip
{"groupId":"63fefa20b1e3c135612005c9","ip":"10.178.75.56","env":"qa","idc":"officeidc_hd2","tags":["10.178.75.56","officeidc_hd2","machine"],"lastTaskId":"202303210000000017","versionInfo":{"staticConfigVersionId":"v2023.03.20.004","autoConfigVersionId":"v2023.03.20.004","configVersionId":"0","status":true,"ipTime":"2019-12-04 01:06:06"},"lastTasks":{"status":[]}}[!http]
2023-11-08 14:37:48.520706 IP (tos 0x0, ttl 52, id 64787, offset 0, flags [DF], proto TCP (6), length 40)
10.98.21.3.http > 10.178.75.56.44054: Flags [.], cksum 0xa1f6 (correct), seq 186, ack 1028, win 150, length 0
2023-11-08 14:37:48.523697 IP (tos 0x0, ttl 52, id 64788, offset 0, flags [DF], proto TCP (6), length 225)
10.98.21.3.http > 10.178.75.56.44054: Flags [P.], cksum 0xb2f6 (correct), seq 186:371, ack 1028, win 150, length 185: HTTP, length: 185
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 38
Connection: keep-alive
Date: Wed, 08 Nov 2023 06:37:48 GMT
2023-11-08 14:37:48.523718 IP (tos 0x0, ttl 64, id 47135, offset 0, flags [DF], proto TCP (6), length 40)
10.178.75.56.44054 > 10.98.21.3.http: Flags [.], cksum 0x7569 (incorrect -> 0x9f7e), seq 1028, ack 371, win 597, length 0
最后啰嗦一句:全文原创,希望得到各位反馈,欢迎斧正交流