Jans的BLOG
Jans的BLOG

ngrep 是grep(在文本中搜索字符串的工具)的网络版,他力求更多的grep特征,
用于搜寻指定的数据包。正由于安装ngrep需用到libpcap库, 所以支持大量的
操作系统和网络协议。能识别TCP、UDP和ICMP包,理解bpf的过滤机制。

ngrep搜寻数据包
ngrep 是grep(在文本中搜索字符串的工具)的网络版,他力求更多的grep特征,
用于搜寻指定的数据包。正由于安装ngrep需用到libpcap库, 所以支持大量的
操作系统和网络协议。能识别TCP、UDP和ICMP包,理解bpf的过滤机制。
ngrep下载地址:http://ngrep.sourceforge.net/;
libpcap下载地址:http://www.tcpdump.org/

使用方法:()

usage: 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>
   -h  is help/usage
   -V  is version information
   -q  is be quiet (don't print packet reception hash marks)静默模式,如果没有此开关,未匹配的数据包都以“#”显示

   -e  is show empty packets 显示空数据包
   -i  is ignore case    忽略大小写
   -v  is invert match 反转匹配
   -R  is don't do privilege revocation logic
   -x  is print in alternate hexdump format 以16进制格式显示
   -X  is interpret match expression as hexadecimal  以16进制格式匹配
   -w  is word-regex (expression must match as a word) 整字匹配
   -p  is don't go into promiscuous mode 不使用混杂模式
   -l  is make stdout line buffered
   -D  is replay pcap_dumps with their recorded time intervals
   -t  is print timestamp every time a packet is matched在每个匹配的包之前显示时间戳
   -T  is print delta timestamp every time a packet is matched显示上一个匹配的数据包之间的时间间隔
   -M  is don't do multi-line match (do single-line match instead)仅进行单行匹配
   -I  is read packet stream from pcap format file pcap_dump 从文件中读取数据进行匹配
   -O  is dump matched packets in pcap format to pcap_dump 将匹配的数据保存到文件
   -n  is look at only num packets 仅捕获指定数目的数据包进行查看
   -A  is dump num packets after a match匹配到数据包后Dump随后的指定数目的数据包
   -s  is set the bpf caplen
   -S  is set the limitlen on matched packets
   -W  is set the dump format (normal, byline, single, none) 设置显示格式byline将解析包中的换行符
   -c  is force the column width to the specified size 强制显示列的宽度
   -P  is set the non-printable display char to what is specified
   -F  is read the bpf filter from the specified file 使用文件中定义的bpf(Berkeley Packet Filter)
   -N  is show sub protocol number 显示由IANA定义的子协议号
   -d  is use specified device (index) instead of the pcap default 使用哪个网卡,可以用-L选项查询
   -L  is show the winpcap device list index 查询网卡接口


应用如下(以下的例子中本机地址是"10.1.41.31"):

1.捕获字符串".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

打开一个视频页面

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。地址已经找到了,就是http://f59.c31.56.com/flvdownload/12/19/ggyg7741@56.com_56flv_zhajm_11955697397.flv

加个-Wbyline 参数后,将解析包中的换行符

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.
.

posted on 2007-11-25 16:10  Jans  阅读(9057)  评论(0编辑  收藏  举报