Ubuntu下tcpdump命令(二)expression
说明,tcpdump的包过滤表达是语法参见pcap-filter(7),本文翻译自man pcap-filter,若遇到不能理解的地方请参考man文档。
概述
pcap-filter - 包过滤语法
描述
pcap_compile()用于将字符串编译为过滤程序,该过滤程序可用于过滤出要提供给pcap_loop(), pcap_dispatch(), pcap_next()或者pcap_next_ex()的包。
过滤器表达式由一个或多个原语组成。原语通常由一个限定词加id(name或者number)组成。有三种不同的限定词:
type 指明id关联的是何种事物,可能的类型有host, net , port 和 portrange。如`host foo', `net 128.3', `port 20', `portrange 6000-6008'。如果没有type限定词,则假设为host
dir 指明一个特定的传输方向。可能的方向有src, dst, src or dst, src and dst, addr1, addr2, addr3, and addr4。如`src foo', `dst net 128.3', `src or dst port ftp-data'。如果没有dir限定词,则假设为src or dst。addr1, addr2, addr3, and addr4限定词仅对于IEEE 802.11无线局域网链路层有效
proto 指明协议。可能的协议有ether, fddi, tr, wlan, ip, ip6, arp, rarp, decnet, tcp and udp。如`ether src foo', `arp net 128.3', `tcp port 21', `udp portrange 7000-7009', `wlan addr2 0:2:3:4:5:6'。如果没有指定协议,则假设为所有和type一致的协议。
除了上面的,还有一些特殊的原语不遵从这些规则:gateway, broadcast, less, greater和arithmetic表达式
复杂的过滤表达是可通过使用and, or和not来组合原语。
允许的原语有:
dst host host
如果包的IPv4/v6的目的地址为host则为真
src host host
如果包的IPv4/v6的源地址为host则为真
host host
如果包的IPv4/v6的源或者目的地址为host则为真
ether dst ehost
如果以太网目的地址为ehost则为真
ether src ehost
如果以太网源地址为ehost则为真
ether host ehost
如果以太网源或目的地址为ehost则为真
gateway host
如果包使用host作为网关则为真
dst net net
如果包的IPv4/v6目的地址网络号为net
src net net
如果包的IPv4/v6源地址网络号为net
net net
如果包的IPv4/v6源或目的地址网络号为net
net net mask netmask
如果包的IPv4地址匹配指定的net和netmask则为真
net net/len
如果包的IPv4/v6地址匹配指定的net和netmask长度则为真
dst port port
如果包是ip/tcp, ip/udp, ip6/tcp或者ip6/udp并且目的端口为port则为真
src port port
如果包源端口值为port则为真
port port
如果包源或目的端口值为port则为真
dst portrange port1-port2
如果包是ip/tcp, ip/udp, ip6/tcp或者ip6/udp并且目的端口在port1和port2之间则为真
src portrange port1-port2
如果包源端口值在port1和port2之间则为真
portrange port1-port2
如果包源或者目的端口值在port1和port2之间则为真
less length
如果包长度小于等于length则为真
greater length
如果包长度大于等于length则为真
ip proto protocol
如果包是IPv4包并且协议类型为protocol则为真
ip6 proto protocol
如果包是IPv6包并且协议类型为protocol则为真
ip6 protochain protocol
True if the packet is IPv6 packet, and contains protocol header with type protocol in its protocol header chain. For example,
ip6 protochain 6
matches any IPv6 packet with TCP protocol header in the protocol header chain. The packet may contain, for example, authenti‐
cation header, routing header, or hop-by-hop option header, between IPv6 header and TCP header. The BPF code emitted by this
primitive is complex and cannot be optimized by the BPF optimizer code, so this can be somewhat slow.
ip protochain protocol
Equivalent to ip6 protochain protocol, but this is for IPv4.
ether broadcast
如果包是以太网广播包则为真
ip broadcast
如果包是IPv4广播包则为真
ether multicast
如果包是以太网广播包则为真
ip multicast
如果包是IPv4多播包则为真
ip6 multicast
如果包是IPv6多播包则为真
ether proto protocol
如果包是指定的以太网协议类型则为真。
decnet src host
True if the DECNET source address is host, which may be an address of the form ``10.123'', or a DECNET host name. [DECNET
host name support is only available on ULTRIX systems that are configured to run DECNET.]
decnet dst host
True if the DECNET destination address is host.
decnet host host
True if either the DECNET source or destination address is host.
ifname interface
True if the packet was logged as coming from the specified interface (applies only to packets logged by OpenBSD's or FreeBSD's
pf(4)).
on interface
Synonymous with the ifname modifier.
rnr num
True if the packet was logged as matching the specified PF rule number (applies only to packets logged by OpenBSD's or Free‐
BSD's pf(4)).
rulenum num
Synonymous with the rnr modifier.
reason code
True if the packet was logged with the specified PF reason code. The known codes are: match, bad-offset, fragment, short,
normalize, and memory (applies only to packets logged by OpenBSD's or FreeBSD's pf(4)).
rset name
True if the packet was logged as matching the specified PF ruleset name of an anchored ruleset (applies only to packets logged
by OpenBSD's or FreeBSD's pf(4)).
ruleset name
Synonomous with the rset modifier.
srnr num
True if the packet was logged as matching the specified PF rule number of an anchored ruleset (applies only to packets logged
by OpenBSD's or FreeBSD's pf(4)).
subrulenum num
Synonomous with the srnr modifier.
action act
True if PF took the specified action when the packet was logged. Known actions are: pass and block and, with later versions
of pf(4)), nat, rdr, binat and scrub (applies only to packets logged by OpenBSD's or FreeBSD's pf(4)).
wlan addr1 ehost
True if the first IEEE 802.11 address is ehost.
wlan addr2 ehost
True if the second IEEE 802.11 address, if present, is ehost. The second address field is used in all frames except for CTS
(Clear To Send) and ACK (Acknowledgment) control frames.
wlan addr3 ehost
True if the third IEEE 802.11 address, if present, is ehost. The third address field is used in management and data frames,
but not in control frames.
wlan addr4 ehost
True if the fourth IEEE 802.11 address, if present, is ehost. The fourth address field is only used for WDS (Wireless Distri‐
bution System) frames.
ip, ip6, arp, rarp, atalk, aarp, decnet, iso, stp, ipx, netbeui
Abbreviations for:
ether proto p
where p is one of the above protocols.
lat, moprc, mopdl
Abbreviations for:
ether proto p
where p is one of the above protocols. Note that not all applications using pcap(3) currently know how to parse these proto‐
cols.
type wlan_type
True if the IEEE 802.11 frame type matches the specified wlan_type. Valid wlan_types are: mgt, ctl and data.
type wlan_type subtype wlan_subtype
True if the IEEE 802.11 frame type matches the specified wlan_type and frame subtype matches the specified wlan_subtype.
If the specified wlan_type is mgt, then valid wlan_subtypes are: assoc-req, assoc-resp, reassoc-req, reassoc-resp, probe-req,
probe-resp, beacon, atim, disassoc, auth and deauth.
If the specified wlan_type is ctl, then valid wlan_subtypes are: ps-poll, rts, cts, ack, cf-end and cf-end-ack.
If the specified wlan_type is data, then valid wlan_subtypes are: data, data-cf-ack, data-cf-poll, data-cf-ack-poll, null, cf-
ack, cf-poll, cf-ack-poll, qos-data, qos-data-cf-ack, qos-data-cf-poll, qos-data-cf-ack-poll, qos, qos-cf-poll and qos-cf-ack-
poll.
subtype wlan_subtype
True if the IEEE 802.11 frame subtype matches the specified wlan_subtype and frame has the type to which the specified
wlan_subtype belongs.
dir dir
True if the IEEE 802.11 frame direction matches the specified dir. Valid directions are: nods, tods, fromds, dstods, or a
numeric value.
vlan [vlan_id]
True if the packet is an IEEE 802.1Q VLAN packet. If [vlan_id] is specified, only true if the packet has the specified
vlan_id. Note that the first vlan keyword encountered in expression changes the decoding offsets for the remainder of expres‐
sion on the assumption that the packet is a VLAN packet. The vlan [vlan_id] expression may be used more than once, to filter
on VLAN hierarchies. Each use of that expression increments the filter offsets by 4.
For example:
vlan 100 && vlan 200
filters on VLAN 200 encapsulated within VLAN 100, and
vlan && vlan 300 && ip
filters IPv4 protocols encapsulated in VLAN 300 encapsulated within any higher order VLAN.
mpls [label_num]
True if the packet is an MPLS packet. If [label_num] is specified, only true is the packet has the specified label_num. Note
that the first mpls keyword encountered in expression changes the decoding offsets for the remainder of expression on the
assumption that the packet is a MPLS-encapsulated IP packet. The mpls [label_num] expression may be used more than once, to
filter on MPLS hierarchies. Each use of that expression increments the filter offsets by 4.
For example:
mpls 100000 && mpls 1024
filters packets with an outer label of 100000 and an inner label of 1024, and
mpls && mpls 1024 && host 192.9.200.1
filters packets to or from 192.9.200.1 with an inner label of 1024 and any outer label.
pppoed True if the packet is a PPP-over-Ethernet Discovery packet (Ethernet type 0x8863).
pppoes True if the packet is a PPP-over-Ethernet Session packet (Ethernet type 0x8864). Note that the first pppoes keyword encoun‐
tered in expression changes the decoding offsets for the remainder of expression on the assumption that the packet is a PPPoE
session packet.
For example:
pppoes && ip
filters IPv4 protocols encapsulated in PPPoE.
tcp, udp, icmp
Abbreviations for:
ip proto p or ip6 proto p
where p is one of the above protocols.
iso proto protocol
True if the packet is an OSI packet of protocol type protocol. Protocol can be a number or one of the names clnp, esis, or
isis.
clnp, esis, isis
Abbreviations for:
iso proto p
where p is one of the above protocols.
l1, l2, iih, lsp, snp, csnp, psnp
Abbreviations for IS-IS PDU types.
vpi n True if the packet is an ATM packet, for SunATM on Solaris, with a virtual path identifier of n.
vci n True if the packet is an ATM packet, for SunATM on Solaris, with a virtual channel identifier of n.
lane True if the packet is an ATM packet, for SunATM on Solaris, and is an ATM LANE packet. Note that the first lane keyword
encountered in expression changes the tests done in the remainder of expression on the assumption that the packet is either a
LANE emulated Ethernet packet or a LANE LE Control packet. If lane isn't specified, the tests are done under the assumption
that the packet is an LLC-encapsulated packet.
llc True if the packet is an ATM packet, for SunATM on Solaris, and is an LLC-encapsulated packet.
oamf4s True if the packet is an ATM packet, for SunATM on Solaris, and is a segment OAM F4 flow cell (VPI=0 & VCI=3).
oamf4e True if the packet is an ATM packet, for SunATM on Solaris, and is an end-to-end OAM F4 flow cell (VPI=0 & VCI=4).
oamf4 True if the packet is an ATM packet, for SunATM on Solaris, and is a segment or end-to-end OAM F4 flow cell (VPI=0 & (VCI=3 |
VCI=4)).
oam True if the packet is an ATM packet, for SunATM on Solaris, and is a segment or end-to-end OAM F4 flow cell (VPI=0 & (VCI=3 |
VCI=4)).
metac True if the packet is an ATM packet, for SunATM on Solaris, and is on a meta signaling circuit (VPI=0 & VCI=1).
bcc True if the packet is an ATM packet, for SunATM on Solaris, and is on a broadcast signaling circuit (VPI=0 & VCI=2).
sc True if the packet is an ATM packet, for SunATM on Solaris, and is on a signaling circuit (VPI=0 & VCI=5).
ilmic True if the packet is an ATM packet, for SunATM on Solaris, and is on an ILMI circuit (VPI=0 & VCI=16).
connectmsg
True if the packet is an ATM packet, for SunATM on Solaris, and is on a signaling circuit and is a Q.2931 Setup, Call Proceed‐
ing, Connect, Connect Ack, Release, or Release Done message.
metaconnect
True if the packet is an ATM packet, for SunATM on Solaris, and is on a meta signaling circuit and is a Q.2931 Setup, Call
Proceeding, Connect, Release, or Release Done message.
expr relop expr
True if the relation holds, where relop is one of >, <, >=, <=, =, !=, and expr is an arithmetic expression composed of inte‐
ger constants (expressed in standard C syntax), the normal binary operators [+, -, *, /, &, |, <<, >>], a length operator, and
special packet data accessors. Note that all comparisons are unsigned, so that, for example, 0x80000000 and 0xffffffff are >
0. To access data inside the packet, use the following syntax:
proto [ expr : size ]
Proto is one of ether, fddi, tr, wlan, ppp, slip, link, ip, arp, rarp, tcp, udp, icmp, ip6 or radio, and indicates the proto‐
col layer for the index operation. (ether, fddi, wlan, tr, ppp, slip and link all refer to the link layer. radio refers to
the "radio header" added to some 802.11 captures.) Note that tcp, udp and other upper-layer protocol types only apply to
IPv4, not IPv6 (this will be fixed in the future). The byte offset, relative to the indicated protocol layer, is given by
expr. Size is optional and indicates the number of bytes in the field of interest; it can be either one, two, or four, and
defaults to one. The length operator, indicated by the keyword len, gives the length of the packet.
For example, `ether[0] & 1 != 0' catches all multicast traffic. The expression `ip[0] & 0xf != 5' catches all IPv4 packets
with options. The expression `ip[6:2] & 0x1fff = 0' catches only unfragmented IPv4 datagrams and frag zero of fragmented IPv4
datagrams. This check is implicitly applied to the tcp and udp index operations. For instance, tcp[0] always means the first
byte of the TCP header, and never means the first byte of an intervening fragment.
Some offsets and field values may be expressed as names rather than as numeric values. The following protocol header field
offsets are available: icmptype (ICMP type field), icmpcode (ICMP code field), and tcpflags (TCP flags field).
The following ICMP type field values are available: icmp-echoreply, icmp-unreach, icmp-sourcequench, icmp-redirect, icmp-echo,
icmp-routeradvert, icmp-routersolicit, icmp-timxceed, icmp-paramprob, icmp-tstamp, icmp-tstampreply, icmp-ireq, icmp-ireqre‐
ply, icmp-maskreq, icmp-maskreply.
The following TCP flags field values are available: tcp-fin, tcp-syn, tcp-rst, tcp-push, tcp-ack, tcp-urg.
Primitives may be combined using:
A parenthesized group of primitives and operators (parentheses are special to the Shell and must be escaped).
Negation (`!' or `not').
Concatenation (`&&' or `and').
Alternation (`||' or `or').
Negation has highest precedence. Alternation and concatenation have equal precedence and associate left to right. Note that
explicit and tokens, not juxtaposition, are now required for concatenation.
If an identifier is given without a keyword, the most recent keyword is assumed. For example,
not host vs and ace
is short for
not host vs and host ace
which should not be confused with
not ( host vs or ace )