tcpdump 透過 libpcap,使用 kernel 裡或 userspace 的 BPF 虛擬機器進行封包擷取工作,並將封包加以解釋後輸出。
指令格式tcpdump [參數] [過濾表示式]其中參數以「-」開頭,可能接著參數值,剩下的為過濾表示式。
執行 tcpdump,網卡原本只收 MAC 定址符合和廣播的封包,會進入 "promiscuous" 模式改接收所有的封包,處理額外的封包會用掉一些處理器效能。
參數
- -s0:擷取整個封包,而不是只有前 68 byte。
- -n:不解析主機名稱、MAC 位址,可加快處理,避免錯過封包。
- -i:指定要等待的網卡界面
- -w:結果存到 pcap 檔
- -d:
- -dd:
- -ddd:
- tcpdump # 全抓
- tcpdump -i eth0
- tcpdump -r in.pcap #讀取檔案
- tcpdump -i eth0 -G 3600 -w 'file.pcap' # 每小時 rotating
- tcpdump -i eth0 -C 100 -w capture # 每 100MB 換檔案
- tcpdump -s0 -n -i ethX -w /tmp/$(hostname)-$(date +"%Y-%m-%d-%H-%M-%S").pcap host <ip-address>
- tcpdump 'udp port 20000 or (ip[6:2] & 0x1fff) != 0' # 抓 udp port 20000 和所有 fragment offset 不為 0 的封包 (有些 Linux 版本反順序傳送 IP fragments,讓最後 fragment 先收到而能更正確預測 reassembled 後的封包大小。)
遠端 tcpdump
- How to process tcpdump live data stream from a remote machine on a local WireShark:建立 named pipe 給 wireshark 擷取,ssh 遠端執行 wireshark 輸出給 named pipe。
- Wireshark Debug 遠端的機器:和上述類似,但並不是每個遠端都可用 ssh 連,改用 nc
- wireshark -k -i <(ssh user@host sudo tcpdump -i em0 -U -w - not tcp port 22)
- ssh use@server sudo tcpdump -i pflog0 -U -w -| wireshark -k -i -
參考
- http://lirobo.blogspot.tw/2014/12/packet-socket.html
- Linux 的ngrep 網路封包分析工具使用教學 (類似 tcpdump,對 BPF 過濾結果再做正規表示式過濾,可擷取封包或過濾 pcap 檔案)
- 比較 tcpdump、dumpcap、wireshark
- pcapsipdump (one file per sip session)
- ping
- traceroute 或 mtr
- route -n 或 ip route
- ip addr list 或 ifconfig
- arp -an 或 cat /proc/net/arp
- nc (netcat)
- wget 或 curl
- nmap
- dmesg | egrep “eth|em” 或 cat /var/log/messages
- ip link show
- mii-tool eth0 (deprecated , doesn't work on Gigabit NICs)
- ethtool eth0 (net-tools 提供)
- cat /sys/class/net/eth0/operstate
- lsof (list open file)
- ifconfig
- ethtool -S
- 加大 buffer 大小:ethtool -g eth0 或 ethtool -G eth0 rx 4096
- netstat -s
- dropwatch
- sar -n NDEV (每秒有多少錯)
- net.ipv4.tcp_rmem = 4096 87380 4194304
- net.ipv4.tcp_wmem = 4096 87380 4194304
- ss -t -a:顯示所有 TCP socket
- ss -it:socket 內部資訊
offloading:ethtool -k eth0
沒有留言:
張貼留言