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
port : 指定抓取某個X埠的網路封包。
and or not邏輯運運算元 :
tcpdump -i eth0 「host redhat.com and (port 80 or port 443)」
tcp/udp: 過濾tcp/udp流量: tcpdump -i eth0 host janus.t.17usoft.com and tcp
本文來自部落格園,作者:{有態度的馬甲},轉載請註明原文連結:https://www.cnblogs.com/JulianHuang/p/17823599.html
歡迎關注我的原創技術、職場公眾號, 加好友談天說地,一起進化