Linux命令 ngrep - 方便的數(shù)據(jù)包匹配和顯示工具

2022-03-16 11:32 更新

ngrep

方便的數(shù)據(jù)包匹配和顯示工具

補(bǔ)充說(shuō)明

ngrep命令 是grep命令的網(wǎng)絡(luò)版,他力求更多的grep特征,用于搜尋指定的數(shù)據(jù)包。正由于安裝ngrep需用到libpcap庫(kù), 所以支持大量的操作系統(tǒng)和網(wǎng)絡(luò)協(xié)議。能識(shí)別TCP、UDP和ICMP包,理解bpf的過(guò)濾機(jī)制。

安裝

ngrep命令的下載地址:http://ngrep.sourceforge.net/,libpcap下載地址:http://www.tcpdump.org/。先用?yum install libpcap?完全安裝libpcap,注意有時(shí)候用libpcap安裝包安裝的不完整會(huì)影響ngrep的使用。

如果yum無(wú)法安裝就用以下步驟安裝libpcap:

wget http://www.tcpdump.org/release/libpcap-1.3.0.tar.gz
tar -zxf libpcap-1.3.0.tar.gz
cd libpcap-1.3.0
./configure
make && make install

ngrep的安裝就是 configure/make/make install 三部曲。

注:configure時(shí)是遇到 please wipe out all unused pcap installations,添加以下選項(xiàng):

./configure --with-pcap-includes=/usr/local/include/pcap

在安裝后輸入ngrep來(lái)驗(yàn)證下安裝是否成功。

語(yǔ)法

ngrep <-LhNXViwqpevxlDtTRM> <-IO pcap_dump> <-n num> <-d dev> <-A num>
<-s snaplen> <-S limitlen> <-w normal|byline|single|none> <-c cols>
<-P char> <-F file> <match expression> <bpf filter>

選項(xiàng)

-e # 顯示空數(shù)據(jù)包
-i # 忽略大小寫(xiě)
-v # 反轉(zhuǎn)匹配
-R # don't do privilege revocation logic
-x # 以16進(jìn)制格式顯示
-X # 以16進(jìn)制格式匹配
-w # 整字匹配
-p # 不使用混雜模式
-l # make stdout line buffered
-D # replay pcap_dumps with their recorded time intervals
-t # 在每個(gè)匹配的包之前顯示時(shí)間戳
-T # 顯示上一個(gè)匹配的數(shù)據(jù)包之間的時(shí)間間隔
-M # 僅進(jìn)行單行匹配
-I # 從文件中讀取數(shù)據(jù)進(jìn)行匹配
-O # 將匹配的數(shù)據(jù)保存到文件
-n # 僅捕獲指定數(shù)目的數(shù)據(jù)包進(jìn)行查看
-A # 匹配到數(shù)據(jù)包后dump隨后的指定數(shù)目的數(shù)據(jù)包
-s # set the bpf caplen
-S # set the limitlen on matched packets
-W # 設(shè)置顯示格式byline將解析包中的換行符
-c # 強(qiáng)制顯示列的寬度
-P # set the non-printable display char to what is specified
-F # 使用文件中定義的bpf(Berkeley Packet Filter)
-N # 顯示由IANA定義的子協(xié)議號(hào)
-d # 使用哪個(gè)網(wǎng)卡,可以用-L選項(xiàng)查詢(xún)
-L # 查詢(xún)網(wǎng)卡接口

實(shí)例

捕捉cloudian:18080端口的request和response,-W byline用來(lái)解析包中的換行符,否則包里的所有數(shù)據(jù)都是連續(xù)的,可讀性差。-d lo是監(jiān)聽(tīng)本地網(wǎng)卡:

ngrep -W byline -d lo port 18080

捕捉amazon:80端口的request和response。-d eth0是用來(lái)監(jiān)聽(tīng)對(duì)外的網(wǎng)卡:

ngrep -W byline -d eth0 port 80

可以用-d any來(lái)捕捉所有的包:

ngrep '[a-zA-Z]' -t -W byline -d any tcp port 18080

捕獲字符串.flv,比如要查看在Web Flash 視頻中的.flv文件的下載地址:

ngrep -d3 -N -q \.flv
interface: \Device\TNT_40_1_{670F6B50-0A13-4BAB-9D9E-994A833F5BA9} (10.132.0.0/2
55.255.192.0)
match: \.flv

打開(kāi)一個(gè)視頻頁(yè)面:

T(6) 10.132.34.23:24860 -> 61.142.208.154:80 [AP]
GET /f59.c31.56.com/flvdownload/12/19/ggyg7741@56.com_56flv_zhajm_119556973
97.flv HTTP/1.1..accept: */*..Referer: http://www.56.com/flashApp/v_player_
site.swf..x-flash-version: 9,0,45,0..UA-CPU: x86..Accept-Encoding: gzip, de
flate..User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET
CLR 2.0.50727; .NET CLR 3.0.04506.30)..host: f59.r.56.com..Connection: Keep
-Alive..Cookie: whistoryview=23423759-23635627-23423344-23171935-23058374-2
3081156-23207350-22395727-; geoip=............; wl_all_s=y....

OK。地址已經(jīng)找到了,就是http://f59.c31.56.com/flvdownload/12/19/ggyg7741@56.com_56flv_zhajm_11955697397.flv

加個(gè)-W byline參數(shù)后,將解析包中的換行符:

T(6) 2007/11/25 15:56:12.192619 10.132.34.23:26365 -> 59.151.21.101:80 [AP]
GET /aa.flv HTTP/1.1.
Accept: */*.
Accept-Language: zh-cn.
UA-CPU: x86.
Accept-Encoding: gzip, deflate.
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.5072
7; .NET CLR 3.0.04506.30).
Host: www.google.cn.
Connection: Keep-Alive.
Cookie: PREF=id=a0b2932c336477e9:TB=4:NW=1:TM=1187877372:LM=1187956074:S=Y1Fzndp
rT3vFo7ac; SID=DQAAAHcAAABJCEXeOVLHu2rIfb5BfKP3GG9PbhJDEkXsLTV8y0f_lvSd2Y46Q0FPt
83CnEs9rxA1xBDM9mLR8-ckWeScyOQA8PyYnX5u5OjFvjfRbDg_FDZfwxhRzqS9KPZv26pjnsUxs0FDM
1xpJ5AgDn38pXtlCdkksJ0-cbiIWoA61oHWMg; NID=7=AvJxn5B6YOLLxoYz4LLzhIbNsQUQiulRS6U
JGxdBniQBmXm99y7L-NBNORN82N3unmZSGHFPfePVHnLK2MjYjglyXZhU9x7ETXNBnY3NurNijHDhJ7K
yi7E53UBOcv4V.


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)