UNPKG

4.71 kBMarkdownView Raw
1ngrep
2===
3
4方便的数据包匹配和显示工具
5
6## 补充说明
7
8**ngrep命令** 是grep命令的网络版,他力求更多的grep特征,用于搜寻指定的数据包。正由于安装ngrep需用到libpcap库, 所以支持大量的操作系统和网络协议。能识别TCP、UDP和ICMP包,理解bpf的过滤机制。
9
10### 安装
11
12ngrep命令的下载地址:http://ngrep.sourceforge.net/,libpcap下载地址:http://www.tcpdump.org/。先用`yum install libpcap`完全安装libpcap,注意有时候用libpcap安装包安装的不完整会影响ngrep的使用。
13
14如果yum无法安装就用以下步骤安装libpcap:
15
16```shell
17wget http://www.tcpdump.org/release/libpcap-1.3.0.tar.gz
18tar -zxf libpcap-1.3.0.tar.gz
19cd libpcap-1.3.0
20./configure
21make && make install
22```
23
24ngrep的安装就是 configure/make/make install 三部曲。
25
26注:configure时是遇到 please wipe out all unused pcap installations,添加以下选项:
27
28```shell
29./configure --with-pcap-includes=/usr/local/include/pcap
30```
31
32在安装后输入ngrep来验证下安装是否成功。
33
34### 语法
35
36```shell
37ngrep <-LhNXViwqpevxlDtTRM> <-IO pcap_dump> <-n num> <-d dev> <-A num>
38<-s snaplen> <-S limitlen> <-w normal|byline|single|none> <-c cols>
39<-P char> <-F file> <match expression> <bpf filter>
40```
41
42### 选项
43
44```shell
45-e # 显示空数据包
46-i # 忽略大小写
47-v # 反转匹配
48-R # don't do privilege revocation logic
49-x # 以16进制格式显示
50-X # 以16进制格式匹配
51-w # 整字匹配
52-p # 不使用混杂模式
53-l # make stdout line buffered
54-D # replay pcap_dumps with their recorded time intervals
55-t # 在每个匹配的包之前显示时间戳
56-T # 显示上一个匹配的数据包之间的时间间隔
57-M # 仅进行单行匹配
58-I # 从文件中读取数据进行匹配
59-O # 将匹配的数据保存到文件
60-n # 仅捕获指定数目的数据包进行查看
61-A # 匹配到数据包后dump随后的指定数目的数据包
62-s # set the bpf caplen
63-S # set the limitlen on matched packets
64-W # 设置显示格式byline将解析包中的换行符
65-c # 强制显示列的宽度
66-P # set the non-printable display char to what is specified
67-F # 使用文件中定义的bpf(Berkeley Packet Filter)
68-N # 显示由IANA定义的子协议号
69-d # 使用哪个网卡,可以用-L选项查询
70-L # 查询网卡接口
71```
72
73### 实例
74
75捕捉cloudian:18080端口的request和response,`-W byline`用来解析包中的换行符,否则包里的所有数据都是连续的,可读性差。`-d lo`是监听本地网卡:
76
77```shell
78ngrep -W byline -d lo port 18080
79```
80
81捕捉amazon:80端口的request和response。`-d eth0是`用来监听对外的网卡:
82
83```shell
84ngrep -W byline -d eth0 port 80
85```
86
87可以用`-d any`来捕捉所有的包:
88
89```shell
90ngrep '[a-zA-Z]' -t -W byline -d any tcp port 18080
91```
92
93捕获字符串`.flv`,比如要查看在Web Flash 视频中的.flv文件的下载地址:
94
95```shell
96ngrep -d3 -N -q \.flv
97interface: \Device\TNT_40_1_{670F6B50-0A13-4BAB-9D9E-994A833F5BA9} (10.132.0.0/2
9855.255.192.0)
99match: \.flv
100```
101
102打开一个视频页面:
103
104```shell
105T(6) 10.132.34.23:24860 -> 61.142.208.154:80 [AP]
106GET /f59.c31.56.com/flvdownload/12/19/ggyg7741@56.com_56flv_zhajm_119556973
10797.flv HTTP/1.1..accept: */*..Referer: http://www.56.com/flashApp/v_player_
108site.swf..x-flash-version: 9,0,45,0..UA-CPU: x86..Accept-Encoding: gzip, de
109flate..User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET
110CLR 2.0.50727; .NET CLR 3.0.04506.30)..host: f59.r.56.com..Connection: Keep
111-Alive..Cookie: whistoryview=23423759-23635627-23423344-23171935-23058374-2
1123081156-23207350-22395727-; geoip=............; wl_all_s=y....
113```
114
115OK。地址已经找到了,就是http://f59.c31.56.com/flvdownload/12/19/ggyg7741@56.com_56flv_zhajm_11955697397.flv
116
117加个`-W byline`参数后,将解析包中的换行符:
118
119```shell
120T(6) 2007/11/25 15:56:12.192619 10.132.34.23:26365 -> 59.151.21.101:80 [AP]
121GET /aa.flv HTTP/1.1.
122Accept: */*.
123Accept-Language: zh-cn.
124UA-CPU: x86.
125Accept-Encoding: gzip, deflate.
126User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.5072
1277; .NET CLR 3.0.04506.30).
128Host: www.google.cn.
129Connection: Keep-Alive.
130Cookie: PREF=id=a0b2932c336477e9:TB=4:NW=1:TM=1187877372:LM=1187956074:S=Y1Fzndp
131rT3vFo7ac; SID=DQAAAHcAAABJCEXeOVLHu2rIfb5BfKP3GG9PbhJDEkXsLTV8y0f_lvSd2Y46Q0FPt
13283CnEs9rxA1xBDM9mLR8-ckWeScyOQA8PyYnX5u5OjFvjfRbDg_FDZfwxhRzqS9KPZv26pjnsUxs0FDM
1331xpJ5AgDn38pXtlCdkksJ0-cbiIWoA61oHWMg; NID=7=AvJxn5B6YOLLxoYz4LLzhIbNsQUQiulRS6U
134JGxdBniQBmXm99y7L-NBNORN82N3unmZSGHFPfePVHnLK2MjYjglyXZhU9x7ETXNBnY3NurNijHDhJ7K
135yi7E53UBOcv4V.
136```
137
138
139<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ -->
\No newline at end of file