linux系統(tǒng)通常已預(yù)裝tcpdump工具,若未安裝,可使用以下命令安裝:
复制代码
- yum install -y tcpdump
查看tcpdump版本信息:
复制代码
- tcpdump --help
確定網(wǎng)卡名稱:
掌握網(wǎng)卡信息后,即可利用tcpdump監(jiān)控和過濾服務(wù)器網(wǎng)絡(luò)數(shù)據(jù)。
1. 捕獲指定IP地址的網(wǎng)絡(luò)數(shù)據(jù):
捕獲所有經(jīng)過eth0網(wǎng)卡,目的或源IP地址為192.168.29.162的網(wǎng)絡(luò)數(shù)據(jù):
复制代码
- tcpdump -n -i eth0 host 192.168.29.162
捕獲源IP地址為192.168.29.162的網(wǎng)絡(luò)數(shù)據(jù) (eth1網(wǎng)卡):
复制代码
- tcpdump -i eth1 src host 192.168.29.162
捕獲目的IP地址為192.168.29.162的網(wǎng)絡(luò)數(shù)據(jù) (eth1網(wǎng)卡):
复制代码
- tcpdump -i eth1 dst host 192.168.29.162
2. 捕獲指定端口的網(wǎng)絡(luò)數(shù)據(jù):
捕獲eth0網(wǎng)卡8080端口的網(wǎng)絡(luò)數(shù)據(jù):
复制代码
- tcpdump -n -i eth0 port 8080
3. 捕獲mysql數(shù)據(jù)庫相關(guān)數(shù)據(jù):
捕獲MySQL執(zhí)行的SQL語句:
复制代码
- tcpdump -i eth1 -s 0 -l -w - dst port 3306 | strings
捕獲MySQL通訊數(shù)據(jù)包 (生成的cap文件需使用wireshark等工具打開):
复制代码
- tcpdump -n -nn -tttt -i eth0 -s 65535 'port 3306' -w 20160505mysql.cap
4. 捕獲其他協(xié)議數(shù)據(jù):
捕獲SMTP數(shù)據(jù):
复制代码
- tcpdump -i eth1 'tcp[tcpflags] & tcp-syn != 0 and tcp[tcpflags] & tcp-ack != 0'
捕獲http GET請(qǐng)求:
复制代码
- tcpdump -i eth1 'tcp[(tcp[12]>>2):4] = 0x47455420'
捕獲ssh響應(yīng):
复制代码
- tcpdump -i eth1 'tcp[(tcp[12]>>2):4] = 0x5353482D'
5. 高級(jí)過濾和保存:
實(shí)時(shí)捕獲eth0網(wǎng)卡8080端口的GET請(qǐng)求,并將數(shù)據(jù)保存到GET.log文件:
复制代码
- tcpdump -i eth0 '((port 8080) and (tcp[(tcp[12]>>2):4]=0x47455420))' -nnAl -w /tmp/GET.log
捕獲指定數(shù)量的SYN包 (-c參數(shù)指定捕獲包數(shù)量):
复制代码
- time tcpdump -nn -i eth0 'tcp[tcpflags] = tcp-syn' -c 10
這些命令示例提供了tcpdump的基本用法,您可以根據(jù)實(shí)際需求調(diào)整參數(shù)進(jìn)行更精細(xì)的網(wǎng)絡(luò)數(shù)據(jù)捕獲和分析。 請(qǐng)注意替換 eth0 和 eth1 為您的實(shí)際網(wǎng)卡名稱。