tcpdump命令详解

tcpdump将网络接口设置成混杂模式以便捕获到达的每一个数据包.下面给出TCPdump的部分常用选项:

-i <interface> 指定监听的网络接口

-t  不显示时间戳

-c 限制抓取数据包个数

-w 保存文件

-v 指定详细模式输出详细的报文信息

-vv 指定更详细模式输出更详细的报文信息

-x 指定以16进制数格式显示数据包( 解析和打印时,作为打印每一包的头信息的补充(默认情况下 TCPDump 只打印每一包的头信息),将每一包的数据内容也打印出来。)

-X 规定以ASCII码格式显示输出( 在解析和打印时,作为打印每一包的头信息的补充,将每一包的内容信息也以十六进制和 ASCII 码的方式打印出来。这样对于解析新协议是很方便的。)

-n 规定在捕获过程中不需向DNS查询IP地址

-F <file> 从指定文件中读取表达式

-D 显示可用网络接口

-s <length> 设置捕获数据包的长度

-r 从本地读取已经保存的文件

tcpdump的表达式:

默认情况下TCPDump将捕获所有到达网络的数据包。

1.类型限定词

类型限定词有: host, port和net. 

  • host用来指定主机或目的地址

  • port指定端口

  • net可以用来指定某一子网

例:

tcpdump host 192.168.1.100 #监听指定IP地址

tcpdump 'port 80' #监听80端口

tcpdump 'net 192.168.1' #监听子网192.168.1.0

tcpdump 'net 192.168.1.0/24' #监听子网网段

2.逻辑运算符

逻辑运算符有and or not. ( )可将多个表达式组合起来.

  • and与

  • or或

  • not非

例:

tcpdump port 80 and host 192.168.1.10 #过滤端口是80与IP为192.168.1.10的数据

tcpdump port 80 or host 192.168.1.10 #过滤端口为80或IP为192.168.1.10的数据

tcpdump not port 80 #过滤端口非80的数据

tcpdump port 80 and (host 192.168.1.10 or host 192.168.1.11) #监听主机192.168.1.10 或192.168.1.11的80端口.

3.传输方向限定词

传输的关键词 src dst.

  • src源地址

  • dst目的地址

  • src port 源端口

  • dst port目的端口

例:

tcpdump src 192.168.1.10 #过滤源地址是192.168.1.10的数据

tcpdump dst 192.168.1.10 #过滤目的地址是192.168.1.10的数据

tcpdump dst port 80 #过滤目的端口为80的数据

tcpdump src port 22 #过滤源端口为22的数据

4.协议限定词

用来捕获特定协议的数据包有: ether(Ethernet), TCP,UDP,ICMP,IP,ip6(IPv6),ARP,rarp(reverse ARP)等.

例:

tcpdump icmp #过滤所有ICMP协议的数据包

tcpdump tcp #过滤所有tcp协议的数据包

tcpdump udp #过滤所有udp协议的数据包

5. 监视指定网络接口的数据包

网络接口 eth0 eth1 ,路由器可以划分lan口和wan口。

例:

tcpdump -i eth0 #抓eth0接口的所有数据包

tcpdump -i lan1 #抓取lan1口的所有数据包

tcpdump -i wan1 #抓取wan1口的所有数据包

6.tcpdump参数

例:

  • tcpdump --h (查看tcpdump的所有参数及tcpdump的版本和分组捕获函数库的版本)

root@iKuai:/root$tcpdump --h

tcpdump: invalid option -- -

tcpdump version 4.5.1 #tcpdump版本

libpcap version 1.5.3 #分组捕获函数库版本

Usage: tcpdump [-aAbdDefhHIJKlLnNOpqRStuUvxX] [ -B size ] [ -c count ]

        [ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]

        [ -i interface ] [ -j tstamptype ] [ -M secret ]

        [ -P in|out|inout ]

        [ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]

        [ -W filecount ] [ -y datalinktype ] [ -z command ]

        [ -Z user ] [ expression ]
  • tcpdump -D (显示可用的所有网络接口 )

root@iKuai:/root$tcpdump -D

1.eth0

2.imq0

3.ra0

4.eth0.1

5.eth0.2

6.eth0.3

7.eth0.4

8.eth0.5

9.imq1

10.lan1

11.wan1

12.ra1

13.lan2

14.wan2

15.lan3

16.wan3

17.lan4

18.wan4

19.any (Pseudo-device that captures on all interfaces)

20.lo
  • tcpdump -i lan1 -c 3 -v (-i lan1 .指定tcpdump监听接口为lan1;-c 3 .限制抓包的数量为3个;-v . 详细输出报文的信息)

root@iKuai:/root$tcpdump -i lan1 -c 3 -v

tcpdump: listening on lan1, link-type EN10MB (Ethernet), capture size 65535 bytes

11:39:32.143485 IP (tos 0x10, ttl 64, id 61174, offset 0, flags [DF], proto TCP (6), length 92)

    localhost.ssh > localhost.16414: Flags [P.], cksum 0xa804 (incorrect -> 0x9a4b), seq 644042004:644042056, ack 1286375511, win 1246, length 52

11:39:32.143753 IP (tos 0x0, ttl 64, id 23249, offset 0, flags [DF], proto TCP (6), length 40)

    localhost.16414 > localhost.ssh: Flags [.], cksum 0x3fe2 (correct), ack 52, win 16217, length 0

11:39:32.144106 IP (tos 0x10, ttl 64, id 61175, offset 0, flags [DF], proto TCP (6), length 92)

    localhost.ssh > localhost.16414: Flags [P.], cksum 0xa804 (incorrect -> 0xdc83), seq 52:104, ack 1, win 1246, length 52

3 packets captured

10 packets received by filter

0 packets dropped by kernel
  • tcpdump -i lan1 -x (指定以16进制数格式显示数据包 )

root@iKuai:/root$tcpdump -i lan1 -x

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on lan1, link-type EN10MB (Ethernet), capture size 65535 bytes

11:42:41.641921 IP localhost.ssh > localhost.16414: Flags [P.], seq 644278276:644278328, ack 1286376763, win 1246, length 52

    0x0000:  4510 005c f254 4000 4006 a081 c0a8 1801

    0x0010:  c0a8 0e64 0016 401e 2666 e804 4cac 8d3b

    0x0020:  5018 04de a804 0000 dd05 55ca 7f8e b435

    0x0030:  87f1 7403 1f76 8ba9 40d2 f62b ff52 6e7f

    0x0040:  027d ec0e 5b41 2df5 d8e0 c837 906e dd26

    0x0050:  b9a8 7838 5f95 cfcc 8539 7951

1 packets captured

5 packets received by filter

0 packets dropped by kernel
  • tcpdump -i lan1 -X (规定以ASCII码格式显示输出 )

root@iKuai:/root$tcpdump -i lan1 -X

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on lan1, link-type EN10MB (Ethernet), capture size 65535 bytes

11:46:20.626955 IP localhost.ssh > localhost.16414: Flags [P.], seq 644284348:644284400, ack 1286377235, win 1246, length 52

    0x0000:  4510 005c f27c 4000 4006 a059 c0a8 1801  E...|@.@..Y....

    0x0010:  c0a8 0e64 0016 401e 2666 ffbc 4cac 8f13  ...d..@.&f..L...

    0x0020:  5018 04de a804 0000 5a39 92e4 d64d aa44  P.......Z9...M.D

    0x0030:  8841 7651 ec9d 92e5 90f0 72e4 b561 5360  .AvQ......r..aS`

    0x0040:  4967 66cc 425a 3ccc aa0f ad48 0181 de41  Igf.BZ<....H...A

    0x0050:  ee87 c71d 89e5 7fa0 050b 14cb            ............

11:46:20.627301 IP localhost.16414 > localhost.ssh: Flags [.], ack 52, win 16083, length 0

    0x0000:  4500 0028 6011 4000 4006 3309 c0a8 0e64  E..(`.@.@.3....d

    0x0010:  c0a8 1801 401e 0016 4cac 8f13 2666 fff0  ....@...L...&f..

    0x0020:  5010 3ed3 8700 0000 0000 0000 0000       P.>...........
  • tcpdump -i lan1 -s 0 -w /root/test.pcap (-s 0 .设置抓取数据包的长度,0代表所有长度的数据包;-w .保存抓取的数据包,/root/保存的文件路径,pcap保存的文件格式,pcap格式能被wireshark读取)

root@iKuai:/root$tcpdump -i lan1 -s 0 -w /root/test.pcap

tcpdump: listening on lan1, link-type EN10MB (Ethernet), capture size 65535 bytes

^C29 packets captured

30 packets received by filter

0 packets dropped by kernel

tcpdump -r /root/test/pcap host 192.168.14.100 (-r .从本地读取以保存的文件; /root/test.pcap .读取文件的路径;host 192.168.14.100 .读取文件时可以直接过滤,如读取时过滤IP地址为192.168.14.100的数据)

root@iKuai:/root$tcpdump -r /root/test.pcap host 192.168.14.100

reading from file /root/test.pcap, link-type EN10MB (Ethernet)

11:48:53.054469 IP localhost.ssh > localhost.16414: Flags [P.], seq 644299104:644299156, ack 1286379891, win 1246, length 52

11:48:53.054746 IP localhost.16414 > localhost.ssh: Flags [.], ack 52, win 16100, length 0

11:48:53.055079 IP localhost.ssh > localhost.16414: Flags [P.], seq 52:104, ack 1, win 1246, length 52
原文地址:https://www.cnblogs.com/APeng2019/p/10719310.html