ngrep.8 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. .\" $Id$
  2. .\"
  3. .\" All content, except portions of the bpf filter explanation, are:
  4. .\"
  5. .\" Copyright (c) 2001 Jordan Ritter <[email protected]>
  6. .\"
  7. .\" Please refer to the COPYRIGHT file for more information.
  8. .TH NGREP 8 "December 2001" Linux "User Manuals"
  9. .SH NAME
  10. ngrep \- network grep
  11. .SH SYNOPSIS
  12. .B ngrep <-hXViwqpevxlDtT> <-IO
  13. .I pcap_dump
  14. .B > < -n
  15. .I num
  16. .B > < -d
  17. .I dev
  18. .B > < -A
  19. .I num
  20. .B > < -s
  21. .I snaplen
  22. .B > <
  23. .I match expression
  24. .B > <
  25. .I bpf filter
  26. .B >
  27. .SH DESCRIPTION
  28. ngrep strives to provide most of GNU grep's common features, applying
  29. them to the network layer. ngrep is a pcap-aware tool that will allow
  30. you to specify extended regular expressions to match against data
  31. payloads of packets. It currently recognizes TCP, UDP and ICMP across
  32. Ethernet, PPP, SLIP, FDDI and null interfaces, and understands bpf
  33. filter logic in the same fashion as more common packet sniffing tools,
  34. such as
  35. .BR tcpdump (8)
  36. and
  37. .BR snoop (1).
  38. .SH OPTIONS
  39. .IP -h
  40. Display help/usage information.
  41. .IP -L
  42. Display captured packets in line-by-line manner. Good for post-processing
  43. of textual protocols.
  44. .IP -X
  45. Treat the match expression as a hexadecimal string. See the
  46. explanation of \fImatch expression\fP below.
  47. .IP -V
  48. Display version information.
  49. .IP -i
  50. Ignore case for the regex expression.
  51. .IP -w
  52. Match the regex expression as a word.
  53. .IP -q
  54. Be quiet; don't output any information other than packet headers and
  55. their payloads (if relevant).
  56. .IP -p
  57. Don't put the interface into promiscuous mode.
  58. .IP -e
  59. Show empty packets. Normally empty packets are discarded because they
  60. have no payload to search. If specified, empty packets will be shown,
  61. regardless of the specified regex expression.
  62. .IP -v
  63. Invert the match; only display packets that don't match.
  64. .IP -x
  65. Dump packet contents as hexadecimal as well as ASCII.
  66. .IP -l
  67. Make stdout line buffered.
  68. .IP -D
  69. When reading pcap_dump files, replay them at their recorded time
  70. intervals (mimic realtime).
  71. .IP -t
  72. Print a timestamp in the form of YYYY/MM/DD HH:MM:SS.UUUUUU everytime
  73. a packet is matched.
  74. .IP -T
  75. Print a timestamp in the form of +S.UUUUUU, indicating the delta
  76. between packet matches.
  77. .IP "-s snaplen"
  78. Set the bpf caplen to snaplen (default 65536).
  79. .IP "-I pcap_dump"
  80. Input file pcap_dump into ngrep. Works with any pcap-compatible dump
  81. file format. This option is useful for searching for a wide range of
  82. different patterns over the same packet stream.
  83. .IP "-O pcap_dump"
  84. Output matched packets to a pcap-compatible dump file. This feature
  85. does not interfere with normal output to stdout.
  86. .IP "-n num"
  87. Match only
  88. .I \fInum\fP
  89. packets total, then exit.
  90. .IP "-d dev"
  91. By default ngrep will select a default interface to listen on. Use
  92. this option to force ngrep to listen on interface \fIdev\fP.
  93. .IP "-A num"
  94. Dump \fInum\fP packets of trailing context after matching a packet.
  95. .IP "\fI match expression\fP"
  96. A match expression is either an extended regular expression, or if the
  97. \fI-X\fP option is specified, a string signifying a hexadecimal value.
  98. An extended regular expression follows the rules as implemented by the
  99. .B GNU regex
  100. .BR library .
  101. Hexadecimal expressions can optionally be preceded by `0x'. E.g.,
  102. `DEADBEEF', `0xDEADBEEF'.
  103. .IP "\fI bpf filter\fP"
  104. Selects a filter that specifies what packets will be dumped. If no
  105. \fIbpf filter\fP is given, all IP packets seen on the selected
  106. interface will be dumped. Otherwise, only packets for which \fIbpf
  107. filter\fP is `true' will be dumped.
  108. .LP
  109. The \fIbpf filter\fP consists of one or more
  110. .I primitives.
  111. Primitives usually consist of an
  112. .I id
  113. (name or number) preceded by one or more qualifiers. There are three
  114. different kinds of qualifier:
  115. .IP \fItype\fP
  116. qualifiers say what kind of thing the id name or number refers to.
  117. Possible types are
  118. .BR host ,
  119. .B net
  120. and
  121. .BR port .
  122. E.g., `host blort', `net 1.2.3', `port 80'. If there is no type
  123. qualifier,
  124. .B host
  125. is assumed.
  126. .IP \fIdir\fP
  127. qualifiers specify a particular transfer direction to and/or from
  128. .I id.
  129. Possible directions are
  130. .BR src ,
  131. .BR dst ,
  132. .B "src or dst"
  133. and
  134. .B "src and"
  135. .BR dst .
  136. E.g., `src foo', `dst net 1.2.3', `src or dst port ftp-data'. If
  137. there is no dir qualifier,
  138. .B "src or dst"
  139. is assumed.
  140. For `null' link layers (i.e. point to point protocols such as slip) the
  141. .B inbound
  142. and
  143. .B outbound
  144. qualifiers can be used to specify a desired direction.
  145. .IP \fIproto\fP
  146. qualifiers are restricted to ip-only protocols. Possible protos are:
  147. .B tcp ,
  148. .B udp
  149. and
  150. .BR icmp .
  151. e.g., `udp src foo' or `tcp port 21'. If there is no proto qualifier,
  152. all protocols consistent with the type are assumed. E.g., `src foo'
  153. means `ip and ((tcp or udp) src foo)', `net bar' means `ip and (net
  154. bar)', and `port 53' means `ip and ((tcp or udp) port 53)'.
  155. .LP
  156. In addition to the above, there are some special `primitive' keywords
  157. that don't follow the pattern:
  158. .BR gateway ,
  159. .BR broadcast ,
  160. .BR less ,
  161. .B greater
  162. and arithmetic expressions. All of these are described below.
  163. .LP
  164. More complex filter expressions are built up by using the words
  165. .BR and ,
  166. .B or
  167. and
  168. .B not
  169. to combine primitives. E.g., `host blort and not port ftp and not
  170. port ftp-data'. To save typing, identical qualifier lists can be
  171. omitted. E.g., `tcp dst port ftp or ftp-data or domain' is exactly
  172. the same as `tcp dst port ftp or tcp dst port ftp-data or tcp dst port
  173. domain'.
  174. .LP
  175. Allowable primitives are:
  176. .IP "\fBdst host \fIhost\fR"
  177. True if the IP destination field of the packet is \fIhost\fP,
  178. which may be either an address or a name.
  179. .IP "\fBsrc host \fIhost\fR"
  180. True if the IP source field of the packet is \fIhost\fP.
  181. .IP "\fBhost \fIhost\fP"
  182. True if either the IP source or destination of the packet is \fIhost\fP.
  183. Any of the above host expressions can be prepended with the keywords,
  184. \fBip\fP, \fBarp\fP, or \fBrarp\fP as in:
  185. .in +.5i
  186. .nf
  187. \fBip host \fIhost\fR
  188. .fi
  189. .in -.5i
  190. which is equivalent to:
  191. .in +.5i
  192. .IP "\fBether dst \fIehost\fP"
  193. True if the ethernet destination address is \fIehost\fP. \fIEhost\fP
  194. may be either a name from /etc/ethers or a number (see
  195. .IR ethers (3N)
  196. for numeric format).
  197. .IP "\fBether src \fIehost\fP"
  198. True if the ethernet source address is \fIehost\fP.
  199. .IP "\fBether host \fIehost\fP"
  200. True if either the ethernet source or destination address is \fIehost\fP.
  201. .IP "\fBgateway\fP \fIhost\fP"
  202. True if the packet used \fIhost\fP as a gateway. I.e., the ethernet
  203. source or destination address was \fIhost\fP but neither the IP source
  204. nor the IP destination was \fIhost\fP. \fIHost\fP must be a name and
  205. must be found in both /etc/hosts and /etc/ethers. (An equivalent
  206. expression is
  207. .in +.5i
  208. .nf
  209. \fBether host \fIehost \fBand not host \fIhost\fR
  210. .fi
  211. .in -.5i
  212. which can be used with either names or numbers for \fIhost / ehost\fP.)
  213. .IP "\fBdst net \fInet\fR"
  214. True if the IP destination address of the packet has a network
  215. number of \fInet\fP. \fINet\fP may be either a name from /etc/networks
  216. or a network number (see \fInetworks(4)\fP for details).
  217. .IP "\fBsrc net \fInet\fR"
  218. True if the IP source address of the packet has a network
  219. number of \fInet\fP.
  220. .IP "\fBnet \fInet\fR"
  221. True if either the IP source or destination address of the packet has a network
  222. number of \fInet\fP.
  223. .IP "\fBnet \fInet\fR \fBmask \fImask\fR"
  224. True if the IP address matches \fInet\fR with the specific netmask.
  225. May be qualified with \fBsrc\fR or \fBdst\fR.
  226. .IP "\fBnet \fInet\fR/\fIlen\fR"
  227. True if the IP address matches \fInet\fR a netmask \fIlen\fR bits wide.
  228. May be qualified with \fBsrc\fR or \fBdst\fR.
  229. .IP "\fBdst port \fIport\fR"
  230. True if the packet is ip/tcp or ip/udp and has a
  231. destination port value of \fIport\fP.
  232. The \fIport\fP can be a number or a name used in /etc/services (see
  233. .IR tcp (4P)
  234. and
  235. .IR udp (4P)).
  236. If a name is used, both the port
  237. number and protocol are checked. If a number or ambiguous name is used,
  238. only the port number is checked (e.g., \fBdst port 513\fR will print both
  239. tcp/login traffic and udp/who traffic, and \fBport domain\fR will print
  240. both tcp/domain and udp/domain traffic).
  241. .IP "\fBsrc port \fIport\fR"
  242. True if the packet has a source port value of \fIport\fP.
  243. .IP "\fBport \fIport\fR"
  244. True if either the source or destination port of the packet is \fIport\fP.
  245. Any of the above port expressions can be prepended with the keywords,
  246. \fBtcp\fP or \fBudp\fP, as in:
  247. .in +.5i
  248. .nf
  249. \fBtcp src port \fIport\fR
  250. .fi
  251. .in -.5i
  252. which matches only tcp packets whose source port is \fIport\fP.
  253. .IP "\fBless \fIlength\fR"
  254. True if the packet has a length less than or equal to \fIlength\fP.
  255. This is equivalent to:
  256. .in +.5i
  257. .nf
  258. \fBlen <= \fIlength\fP.
  259. .fi
  260. .in -.5i
  261. .IP "\fBgreater \fIlength\fR"
  262. True if the packet has a length greater than or equal to \fIlength\fP.
  263. This is equivalent to:
  264. .in +.5i
  265. .nf
  266. \fBlen >= \fIlength\fP.
  267. .fi
  268. .in -.5i
  269. .IP "\fBip proto \fIprotocol\fR"
  270. True if the packet is an ip packet (see
  271. .IR ip (4P))
  272. of protocol type \fIprotocol\fP. \fIProtocol\fP can be a number or
  273. one of the names \fItcp\fP, \fIudp\fP or \fIicmp\fP. Note that the
  274. identifiers \fItcp\fP and \fIudp\fP are also keywords and must be
  275. escaped via backslash (\\), which is \\\\ in the C-shell.
  276. .IP "\fBip broadcast\fR"
  277. True if the packet is an IP broadcast packet. It checks for both
  278. the all-zeroes and all-ones broadcast conventions, and looks up
  279. the local subnet mask.
  280. .IP "\fBip multicast\fR"
  281. True if the packet is an IP multicast packet.
  282. .IP "\fBip\fR"
  283. Abbreviation for:
  284. .in +.5i
  285. .nf
  286. \fBether proto ip\fR
  287. .fi
  288. .IP "\fBtcp\fR, \fBudp\fR, \fBicmp\fR"
  289. Abbreviations for:
  290. .in +.5i
  291. .nf
  292. \fBip proto \fIp\fR
  293. .fi
  294. .in -.5i
  295. where \fIp\fR is one of the above protocols.
  296. .IP "\fIexpr relop expr\fR"
  297. True if the relation holds, where \fIrelop\fR is one of >, <, >=, <=, =, !=,
  298. and \fIexpr\fR is an arithmetic expression composed of integer constants
  299. (expressed in standard C syntax), the normal binary operators
  300. [+, -, *, /, &, |], a length operator, and special packet data accessors.
  301. To access
  302. data inside the packet, use the following syntax:
  303. .in +.5i
  304. .nf
  305. \fIproto\fB [ \fIexpr\fB : \fIsize\fB ]\fR
  306. .fi
  307. .in -.5i
  308. \fIProto\fR is one of \fBip, tcp, udp \fRor \fBicmp\fR, and
  309. indicates the protocol layer for the index operation. The byte
  310. offset, relative to the indicated protocol layer, is given by
  311. \fIexpr\fR. \fISize\fR is optional and indicates the number of bytes
  312. in the field of interest; it can be either one, two, or four, and
  313. defaults to one. The length operator, indicated by the keyword
  314. \fBlen\fP, gives the length of the packet.
  315. For example, `\fBether[0] & 1 != 0\fP' catches all multicast traffic.
  316. The expression `\fBip[0] & 0xf != 5\fP'
  317. catches all IP packets with options. The expression
  318. `\fBip[6:2] & 0x1fff = 0\fP'
  319. catches only unfragmented datagrams and frag zero of fragmented datagrams.
  320. This check is implicitly applied to the \fBtcp\fP and \fBudp\fP
  321. index operations.
  322. For instance, \fBtcp[0]\fP always means the first
  323. byte of the TCP \fIheader\fP, and never means the first byte of an
  324. intervening fragment.
  325. .LP
  326. Primitives may be combined using:
  327. .IP
  328. A parenthesized group of primitives and operators
  329. (parentheses are special to the Shell and must be escaped).
  330. .IP
  331. Negation (`\fB!\fP' or `\fBnot\fP').
  332. .IP
  333. Concatenation (`\fB&&\fP' or `\fBand\fP').
  334. .IP
  335. Alternation (`\fB||\fP' or `\fBor\fP').
  336. .LP
  337. Negation has highest precedence.
  338. Alternation and concatenation have equal precedence and associate
  339. left to right. Note that explicit \fBand\fR tokens, not juxtaposition,
  340. are now required for concatenation.
  341. .LP
  342. If an identifier is given without a keyword, the most recent keyword
  343. is assumed.
  344. For example,
  345. .in +.5i
  346. .nf
  347. \fBnot host vs and ace\fR
  348. .fi
  349. .in -.5i
  350. is short for
  351. .in +.5i
  352. .nf
  353. \fBnot host vs and host ace\fR
  354. .fi
  355. .in -.5i
  356. which should not be confused with
  357. .in +.5i
  358. .nf
  359. \fBnot ( host vs or ace )\fR
  360. .fi
  361. .in -.5i
  362. .LP
  363. Expression arguments can be passed to ngrep as either a single
  364. argument or as multiple arguments, whichever is more convenient.
  365. Generally, if the expression contains Shell metacharacters, it is
  366. easier to pass it as a single, quoted argument. Multiple arguments
  367. are concatenated with spaces before being parsed.
  368. .SH DIAGNOSTICS
  369. Errors from
  370. .B ngrep, libpcap,
  371. and the
  372. .B GNU regex library
  373. are all output to stderr.
  374. .SH AUTHOR
  375. Written by Jordan Ritter <[email protected]>.
  376. .SH REPORTING BUGS
  377. Send bug reports to the author.
  378. .SH NOTES
  379. ALL YOUR BASE ARE BELONG TO US.