标题 简介 类型 公开时间
关联规则 关联知识 关联工具 关联文档 关联抓包
参考1(官网)
参考2
参考3
详情
[SAFE-ID: JIWO-2025-1455]   作者: ecawen 发表于: [2018-04-14]

本文共 [307] 位读者顶过

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

        分析网络数据包,有Wireshark,它有着上千种设定、过滤器以及配置选项。它还有一个命令行版本Tshark。如果只是针对简单的任务,Wireshark就太重量级了,所以除非需要更强大的功能,一般情况下就用ngrep来处理了。Ngrep可以让你像类似grep处理文件的方式来处理网络封包。

伯克利包过滤(Berkeley Packet Filter,BPF)语言:http://www.cnblogs.com/zhongxinWang/p/4303153.html[出自:jiwo.org]



ngrep参数

[plain] view plain copy
  1. root@kali:~# man ngrep  
  2.   
  3. 用法: ngrep <-hNXViwqpevxlDtTRM> <-IO pcap_dump> <-n num> <-d dev> <-A num>  
  4.              <-s snaplen> <-S limitlen> <-W normal|byline|single|none> <-c cols>  
  5.              <-P char> <-F file> <match expression> <bpf filter>  
  6.    -h  帮助  
  7.    -V  版本信息  
  8.    -q  静默模式,如果没有此开关,未匹配的数据包都以“#”显示  
  9.    -e  显示空数据包  
  10.    -i  忽略大小写  
  11.    -v  反转匹配。    ngrep -v '' port 23   // 显示除telnet的数据包,-v意为反转。  
  12.    -R  is don't do privilege revocation logic  
  13.    -x  以16进制格式显示  
  14.    -X  以16进制格式匹配  
  15.    -w  整字匹配(is word-regex)  
  16.    -p  不使用混杂模式  
  17.    -l  is make stdout line buffered  
  18.    -D  is replay pcap_dumps with their recorded time intervals  
  19.    -t  is print timestamp every time a packet is matched  
  20.    -T  is print delta timestamp every time a packet is matched  
  21.    -M              仅进行单行匹配  
  22.    -I pcap_dump    从捕获的数据包文件pcap_dump中读取数据进行匹配  
  23.    -O pcap_dump    将匹配的数据保存到pcap格式的文件pcap_dump中  
  24.    -n num          仅捕获指定数目的数据包,然后退出。  
  25.    -A num          匹配到数据包后,Dump指定数目的数据包  
  26.    -s snaplen      设置 bpf caplen(default 65536)  
  27.    -S limitlen     在匹配的包上设置 上限长度  
  28.    -W normal | byline | single | none    设置dump格式。byline是解析包中的换行符 (normal, byline, single, none) 。加个-W byline参数后,将解析包中的换行符  
  29.    -c cols    强制显示列的宽度  
  30.    -P char    将不可打印的显示字符设置为指定的字符  
  31.    -F file    从文件中读取 bpf filter  
  32.    -N         显示由IANA定义的子协议号  
  33.    -d dev     ngrep会选择一个默认的网络接口进行监听,使用 -d 选项可以指定接口进行监听。 -d any 捕获所有的包  
  34.    -K num     杀死匹配的 TCP 连接(类似 tcpkill)。数值参数控制发送了多少个RST段。  

dst host host       // True if the IP destination field of the packet is host, which may be either an address or a name.

src host host        // True if the IP source field of the packet is host.

host host
True if either the IP source or destination of the packet is host. Any of the above host expressions can be prepended with the
keywords, ip, arp, or rarp as in:
ip host host
相当于:

ether dst ehost
True if the ethernet destination address is ehost. Ehost may be either a name from /etc/ethers or a number (see ethers(3N) for
numeric format).

ether src ehost
True if the ethernet source address is ehost.

ether host ehost
True if either the ethernet source or destination address is ehost.

gateway host
True if the packet used host as a gateway. I.e., the ethernet source or destination address was host but neither the IP source
nor the IP destination was host. Host must be a name and must be found in both /etc/hosts and /etc/ethers. (An equivalent
expression is ether host ehost and not host host which can be used with either names or numbers for host / ehost.)

dst net net
True if the IP destination address of the packet has a network number of net. Net may be either a name from /etc/networks or a
network number (see networks(4) for details).

src net net        // True if the IP source address of the packet has a network number of net.

net net              // True if either the IP source or destination address of the packet has a network number of net.

net net mask mask        // True if the IP address matches net with the specific netmask. May be qualified with src or dst.

net net/len                     // True if the IP address matches net a netmask len bits wide. May be qualified with src or dst.

dst port port
True if the packet is ip/tcp or ip/udp and has a destination port value of port. The port can be a number or a name used in
/etc/services (see tcp(4P) and udp(4P)). If a name is used, both the port number and protocol are checked. If a number or
ambiguous name is used, only the port number is checked (e.g., dst port 513 will print both tcp/login traffic and udp/who traf-
fic, and port domain will print both tcp/domain and udp/domain traffic).
src port port        // True if the packet has a source port value of port.

port port
True if either the source or destination port of the packet is port. Any of the above port expressions can be prepended with
the keywords, tcp or udp, as in:
tcp src port port        // which matches only tcp packets whose source port is port.

less length                  // True if the packet has a length less than or equal to length. This is equivalent to:
len <= length.

greater length            // True if the packet has a length greater than or equal to length. This is equivalent to:
len >= length.

ip proto protocol
True if the packet is an ip packet (see ip(4P)) of protocol type protocol. Protocol can be a number or one of the names tcp,
udp or icmp. Note that the identifiers tcp and udp are also keywords and must be escaped via backslash (\), which is \\ in the
C-shell.

ip broadcast
True if the packet is an IP broadcast packet. It checks for both the all-zeroes and all-ones broadcast conventions, and looks
up the local subnet mask.

ip multicast           // True if the packet is an IP multicast packet.

ip Abbreviation for:
ether proto ip

tcp, udp, icmp
Abbreviations for:
ip proto p
where p is one of the above protocols.


实例

[plain] view plain copy
  1. ngrep -d eth0 -W byline host 192.168.1.9    // 抓本机eth0 与192.168.1.9的通信信息,并且以行来打印出来  
  2.   
  3. ngrep -W byline host 192.168.1.8 and port 80    // 抓本机与192.168.1.8的通信端口为80(本机)的信息  
  4.   
  5. ngrep -W byline host 192.168.1.8 or host 192.168.1.9 port 80    // 抓本机与192.168.1.8和192.168.1.9的通信,并且本地端口为80  
  6.   
  7. ngrep host 192.168.1.8 udp       // 抓udp包  
  8.   
  9. ngrep -W byline 'GET /' 'tcp and dst port 80' -d eth1 | awk -v RS="#+" -v FS="\n" '{ print length() }'    // 统计请求头长度  
  10.   
  11. ngrep -W byline 'GET /' 'tcp and dst port 80' -d eth1 |  awk -v RS="#+" -v FS="\n" 'length() > 1000'      // 查询一下大于 1K 的请求头  
  12.   
  13. 捕获字符串.flv,比如要查看在Web Flash 视频中的.flv文件的下载地址:   
  14. ngrep -d3 -N -q \.flv  
  15. 然后打开一个视频页面  

针对Web流量,几乎总是想要加上-W byline选项,这会保留换行符,而-q选项可以抑制某些非匹配数据包而产生的输出。

抓取所有包含有GET或POST请求数据包的例子:ngrep –q –W byline “^(GET|POST) .*”

可以传入附加的报文过滤选项,比如限制匹配的报文只针对某个特定的主机,IP或端口。这里我们把所有流经Google的流量做一个过滤,只针对80端口且报文中包含“search”。例子:ngrep –q –W byline “search” host www.google.com and port 80

[plain] view plain copy
  1. ngrep '' udp            /*匹配udp包*/    
  2. ngrep '' icmp           /*匹配icmp包*/    
  3. ngrep '' port 53        /*显示所有的dns请求*/    
  4. ngrep '../'             /*监听远程主机的'../'请求*/    
  5. ngrep -d rl0 port 80    /*服务器端http数据*/    
  6. ngrep -d rl0 'error' port syslog        /**/    
  7. ngrep -wi -d rl0 'user|pass' port 21    /*关注端口21上的user和pass*/   
  8. ngrep -d eth0 ''        /* 显示所有的数据包,-d 指定硬件接口。 */

评论

暂无
发表评论
 返回顶部 
热度(307)
 关注微信