网络抓包工具tcpdump使用教程

网络抓包工具tcpdump使用教程

(2012-07-10 19:46:35)
转载
标签:

杂谈

分类: 网络安全

一、选项

-A:以ASCII形式打印每个包,对抓取web page很方便(最小化链路层报头)。

-B buffer_size:设置操作系统中的捕获缓冲区大小为buffer_size

-c count:抓包个数为count

-C file_size:控制保存文件大小,以MB为单位。

-d:

-dd:

-ddd:

-D:打印系统中可用的网卡。

-e:打印链路层报头。

-E spi@ipaddr algo:secret,…:解码IPsec ESP包

-f:用数字形式显示 '外部的' 互联网地址, 而不是字符形式。

-F file:使用file作为filer expression 的输入。

-G rotate_seconds:每rotate_seconds循环一次-w保存的数据文件。

-I interface:指定网卡。

-I:使网卡工作在monitor mode,只支持wifi网卡。

-K:不试图检验ip,tcp,udp的checksums。

-l:使stdout为行缓冲。

-L:列出已知的data link type。

-m module:从文件module中载入SMI MIB module definitions。

-M secret:使用secret作为在tcp segments with TCP-MD5中的摘要中的digest的验证的共享secret.

-n:不要把地址转换为名字。

-N:不要打印域名的host限定名。

-O:不要运行包匹配code优化器。

-p:不要将网卡设置为混杂模式。

-qquiet 输出,输出较小的信息。

-R:

-r file:从file中读文件(二进制)

-S:打印绝对TCP序列号

-s snaplen:从每个packet中snarf snaplen个字节而不是默认的65535。

-T type:强制被”expression”选择的packets以type方式解释,type有:aodv,cnfp,rpc,rtp,rtcp,snmp,tftp,vat,wb。

-t:不显示时间戳

-tt:非格式化的时间戳

-ttt:时间增量(微秒)

-tttt:加上日期

-ttttt:以第一行为基的增量。

u:打印非解码的NFS handles。

-U:用-w保存的包用“packet-buffered”方式。

-v:以冗长的方式解析和打印包

-vv:

-vvv:

-w file:将原始包写入file。而非解析和打印。

-W filecount:file的数量限制为filecount,和-C结合使用。

-x:除了报头,还以十六进制数形式显示每一个报文data (去掉链路层报头后)

-xx:除了报头,还以十六进制数形式显示每一个报文data (包括链路层报头后)

-X:除了报头,还以ASCII数形式显示每一个报文data (去掉链路层报头后)

-y datalinktype:设置date link type。

-z postrotate-command:在保存的file被关闭时运行command,和-C或-G结合使用。如:-z gzip

-Z user:改变user id为user。

二、表达式

表达式是一个正则表达式,tcpdump利用它作为过滤报文的条件,如果一个报文满足表达式的条件,则这个报文将会被捕获。如果没有给出任何条件,则网络上所有的信息包将会被截获。

---------------------------------------------------------------

[type] [dir] [proto] 标识

图:Expression的构造

---------------------------------------------------------------

expression由一个或多个原语 (primitive) 组成. 原语通常由一个标识(id,名称或数字)和 标识 前面的一个或多个修饰字(qualifier) 组成. 修饰字有三种不同的类型:
(1)type
类型修饰字指出标识名称或标识数字代表什么类型的东西. 可以使用的类型有host, net port. 例如, `host foo', `net 128.3', `port 20'. 如果不指定类型修饰字, 就使用 缺省的 host
(2)Direction
方向修饰字指出相对于标识的传输方向 (数据是传入还是传出标识). 可以使用的方向有 src, dst, src or dst src and dst. 例如, `src foo', `dst net 128.3', `src or dst port ftp-data'. 如果不指定方向修饰字, 就使用缺省的src or dst . 对于 `null' 链路层 (就是说 象 slip 之类的 点到点 协议), 用 inbound和outbound修饰字指定所需的传输方向.
(3)proto
协议修饰字要求匹配指定的协议. 可以使用的协议有: ether, fddi, ip, arp, rarp, decnet, lat, sca, moprc, mopdl, tcp udp. 例如,ether src foo', `arp net 128.3', `tcp port 21'. 如果不指定协议修饰字, 就使用所有符合类型的协议. 例如, `src foo' 指 `(ip or arp or rarp) src foo', `net bar' 指 `(ip or arp or rarp) net bar', `port 53' 指 `(tcp or udp) port 53'.
[`fddi' 实际上 是 `ether' 的 别名; 分析器把它们视为 ``用在指定网络接口上的数据链路层.'' FDDI 报头包含类似于以太协议的源目地址, 而且通常包含 类似于以太协议的报文类型, 因此你可以分析 FDDI 域, 就象分析以太协议一样. FDDI报头也包含其他域, 但是你不能在过滤器表达式里显式描述.]


作为上述的补充, 有一些特殊的 `原语' 关键字, 它们不同于上面的模式: gateway, broadcast, less, greater和数学表达式. 这些在后面有叙述.

更复杂的过滤器表达式 可以通过 and, or not 连接原语来组建. 例如, `host foo and not port ftp and not port ftp-data'. 为了少敲点键, 可以忽略 相同的修饰子. 例如, `tcp dst port ftp or ftp-data or domain' 实际上 就是 `tcp dst port ftp or tcp dst port ftp-data or tcp dst port domain'.

允许的 原语 有:
dst host host
如果报文中IP的目的地址域是 host, 则逻辑为真. host既可以是地址, 也可以是主机名.
src host host
如果报文中IP的源地址域 是 host, 则 逻辑 为 真.
host host
如果报文中 IP 的 源地址域或者目的地址域是host, 则逻辑为真. 上面所有的 host 表达式 都可以加上 ip, arp, 或 rarp 关键字做前缀, 就象:
ip host host

如果host是拥有多个IP地址的主机名, 它的每个地址都会被查验.

dst net net
如果报文的IP目的地址属于网络号net, 则逻辑为真.
src net net
如果 报文的 IP 源地址 属于 网络号 net, 则 逻辑为真.
net net
如果报文的IP源地址或目的地址属于网络号 net, 则逻辑为真.
net net mask netmask (如net 192.168.1.0 mask 255.255.255.128)
如果IP地址匹配指定网络掩码(netmask)的net, 则 逻辑为真. 本原语可以用src 或 dst 修饰.
net net/len
如果IP地址匹配指定网络掩码的net, 则逻辑为真, 掩码的有效位宽为len. 本原语可以用src或dst修饰.
dst port port
如果报文是ip/tcp或ip/udp, 并且目的端口是port, 则逻辑为真. port是一个数字, 也可以是 /etc/services 中 说明过的名字(参看tcp(4P)和udp(4P)). 如果使用名字, 则检查端口号和协议. 如果使用数字, 或者有二义的名字, 则只检查端口号 (例如, dst port 513 将显示 tcp/login 的数据 和 udp/who 的数据, 而 port domain 将显示 tcp/domain 和 udp/domain 的数据).
src port port
如果 报文 的 源端口号 是 port, 则 逻辑 为 真.
port port
如果报文的源端口或目的端口是port, 则逻辑为真. 上述的任意一个端口表达式都可以用关键字tcp或udp做前缀,就象:
tcp src port port
它只匹配源端口是port的 TCP 报文.


less length
如果 报文 的 长度 小于等于 length, 则 逻辑 为 真. 它等同于:
len <= length.

greater length
如果报文的长度大于等于length, 则逻辑为真. 它等同于:
len >= length.

原语可以用下述方法结合使用:
圆括弧括起来的原语和操作符(园括弧在Shell中有特定含义, 所以必须转义).
取反操作 (`!' or `not').
连结操作(`&&' or `and').
或操作 (`||' or `or').
取反操作有最高优先级. 或操作和连结操作有相同的优先级, 运算时从左到右结合. 注意连结操作需要显式的and算符, 而不是并列放置.

如果给出标识符, 但没给修饰符, 那么暗指最近使用的修饰符. 例如,
not host vs and ace作为 not host vs and host ace的简写形式, 不应该和 not ( host vs or ace )混淆.

expr relop expr

True if the relation holds, where relop is one of >, <, >=, <=,

=, !=,and expr is an arithmetic expression composed of integer

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 protocol

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, rela-

tive 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-echore-

ply, icmp-unreach, icmp-sourcequench, icmp-redirect, icmp-echo,

icmp-routeradvert, icmp-routersolicit, icmp-timxceed, icmp-

paramprob, icmp-tstamp, icmp-tstampreply, icmp-ireq, icmp-ire-

qreply, 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 )

三、输出格式

与协议相关,不同的协议输出的格式是不同的。

下面我们介绍几种典型的tcpdump命令的输出信息

(1) 数据链路层头信息

使用命令#tcpdump -e host ice

ice 是一台装有linux的主机,她的MAC地址是00:90:27:58:AF:1A

H219是一台装有SOLARIC的SUN工作站,它的MAC地址是08:00:20:79:5B:46;上一条

命令的输出结果如下所示:

21:50:12.847509 eth0 > 08:00:20:79:5b:46 00:90:27:58:af:1a ip 60: h219.33357 > ice.

telnet 0:0(0) ack 22535 win 8760 (DF)

分析:21:50:12是显示的时间, 847509是ID号, eth0 >表示从网络接口设备发送数据包(eth0 <表示从网络接口eth0 接受该数据包), 08:00:20:79:5b:46是主机H219的MAC地址,

它表明是从源地址H219发来的数据包. 00:90:27:58:af:1a是主机ICE的MAC地址,表示该数据包的目的地址是ICE . ip 是表明该数据包是IP数据包,60 是数据包的长度, h219.33357 > ice.

telnet 表明该数据包是从主机H219的33357端口发往主机ICE的TELNET(23)端口. ack 22535表明对序列号是222535的包进行响应. win 8760表明发送窗口的大小是8760.

(2) ARP包的TCPDUMP输出信息

使用命令#tcpdump arp

得到的输出结果是:

22:32:42.802509 eth0 > arp who-has h10 tell ice (0:90:27:58:af:1a)

22:32:42.802902 eth0 < arp reply h10 is-at 0:90:27:12:10:66 (0:90:27:58:af:1a)

  分析: 22:32:42是时间戳, 802509是ID号, eth0 >表明从主机发出该数据包, arp表明是

ARP请求包, who-has h10 tell ice表明是主机ICE请求主机h10的MAC地址。 0:90:27:5

8:af:1a是主机ICE的MAC地址。

(3) TCP包的输出信息

用TCPDUMP捕获的TCP包的一般输出信息是:

src > dst: flags data-seqno ack window urgent options

分析:

src > dst:表明从源地址到目的地址, flags是TCP包中的标志信息,如:S 是SYN标志, F (F

IN), P (PUSH) , R (RST) "." (没有标记); data-seqno是数据包中的数据的顺序号, ack是

下次期望的顺序号, window是接收缓存的窗口大小, urgent表明数据包中是否有紧急指针.

Options是选项.

(4) UDP包的输出信息

TCPDUMP捕获的UDP包的一般输出信息是:

H10.port1 > ice.port2: udp lenth

UDP十分简单,上面的输出行表明从主机h10的port1端口发出的一个UDP数据包到主机

ICE的port2端口,类型是UDP, 包的长度是lenth

example:

如果想要获取主机61.151.247.124接收或发出的telnet包,使用如下命令

tcpdump tcp port 23 and host 61.151.247.124

tcpdump -n tcp port 23 and src host 218.83.152.2

tcpdump -n -w /etc/tcpdump.log tcp port 23 and src host 218.83.152.2

tcpdump -r /etc/tcpdump.log

tcpdump -i eth0 -w tcpdump.log

tcpdump -r tcpdump.log

你可能感兴趣的:(linux)