【转载】tcpdump简明教程

tcpdump简明教程

本文将会持续修正和更新,最新内容请参考个人 GITHUB 上的 程序猿成长计划 项目,欢迎 Star,更多精彩内容请 follow mehtml

本文翻译自 A tcpdump Tutorial and Primer with Examples 一文,在使用Linux系统进行网络抓包分析的时候,一直没有找到比较简便的非图形界面的方法,在Linux系统下tcpdump命令确实是一柄利器,可是一直苦于学习成本较高,迟迟没有下手。看了 A tcpdump Tutorial and Primer with Examples 这篇文章以后,发现其实使用tcpdump也没有那么困难,特别是其导出的cap文件,再使用wireshark等图形界面软件打开分析很是方便。所以,将其翻译出来,一方面方便本身学习,一方面也为像我同样对tcpdump感兴趣的人提供一个学习途径。linux

概述

对于专业的信息安全人员来讲,tcpdump 是很是重要的网络分析工具。对于任何想深刻理解TCP/IP的人来讲,掌握该工具的使用时很是必要的。不少人更喜欢高级的分析工具,好比Wireshark,但我相信一般状况下这是个错误的选择。git

当使用工具对网络进行分析的时候,更重要的是人对结果的分析,而不是应用的分析。这就促使了对TCP/IP协议栈的理解,所以,我强烈建议学会使用 tcpdump 代替其它工具。github

      
      
      
      
  1. 15:31:34.079416 IP (tos 0x0, ttl 64, id 20244, offset 0, flags [DF],
  2. proto: TCP (6), length: 60) source.35970 > dest.80: S, cksum 0x0ac1
  3. (correct), 2647022145:2647022145(0) win 5840 0x0000: 4500 003c 4f14
  4. 4006 7417 0afb 0257 E.. 0x0010: 4815 222a 8c82 0050 9dc6 5a41 0000
  5. 0000 H."*...P..ZA.... 0x0020: a002 16d0 0ac1 0000 0204 05b4
  6. 0402 080a ................ 0x0030: 14b4 1555 0000 0000 0103 0302
15:31:34.079416 IP (tos 0x0, ttl 64, id 20244, offset 0, flags [DF], 
proto: TCP (6), length: 60) source.35970 > dest.80: S, cksum 0x0ac1 
(correct), 2647022145:2647022145(0) win 5840 0x0000: 4500 003c 4f14 
4006 7417 0afb 0257  E..  0x0010: 4815 222a 8c82 0050 9dc6 5a41 0000 
0000  H."*...P..ZA.... 0x0020: a002 16d0 0ac1 0000 0204 05b4 
0402 080a  ................ 0x0030: 14b4 1555 0000 0000 0103 0302
15:31:34.079416 IP (tos 0x0, ttl 64, id 20244, offset 0, flags [DF], proto: TCP (6), length: 60) source.35970 > dest.80: S, cksum 0x0ac1 (correct), 2647022145:2647022145(0) win 5840 0x0000: 4500 003c 4f14 4006 7417 0afb 0257 E.. 0x0010: 4815 222a 8c82 0050 9dc6 5a41 0000 0000 H."*...P..ZA.... 0x0020: a002 16d0 0ac1 0000 0204 05b4 0402 080a ................ 0x0030: 14b4 1555 0000 0000 0103 0302

TABLE 1. 原生 TCP/IP 输出shell

基础

下面是一些用来配置 tcpdump 的选项,它们很是容易被遗忘,也容易和其它类型的过滤器好比Wireshark等混淆。安全

选项

  • -i any 监听全部的网卡接口,用来查看是否有网络流量
  • -i eth0 只监听eth0网卡接口
  • -D 显示可用的接口列表
  • -n 不要解析主机名
  • -nn 不要解析主机名或者端口名
  • -q 显示更少的输出(更加quiet)
  • -t 输出可读的时间戳
  • -tttt 输出最大程度可读的时间戳
  • -X 以hex和ASCII两种形式显示包的内容
  • -XX-X相似,增长以太网header的显示
  • -v, -vv, -vvv 显示更加多的包信息
  • -c 只读取x个包,而后中止
  • -s 指定每个包捕获的长度,单位是byte,使用-s0能够捕获整个包的内容
  • -S 输出绝对的序列号
  • -e 获取以太网header
  • -E 使用提供的秘钥解密IPSEC流量

表达式

tcpdump中,可使用表达式过滤指定类型的流量。有三种主要的表达式类型:typedirproto网络

  • 类型(type)选项包含:hostnetport
  • 方向(dir)选项包含:srcdst
  • 协议(proto)选项包含:tcpudpicmpah

示例

捕获全部流量

查看全部网卡接口上发生了什么app

      
      
      
      
  1. tcpdump -i any
tcpdump -i any
tcpdump -i any

指定网卡接口

查看指定网卡上发生了什么less

      
      
      
      
  1. tcpdump -i eth0
tcpdump -i eth0
tcpdump -i eth0

原生输出

查看更多的信息,不解析主机名和端口号,显示绝对序列号,可读的时间戳dom

      
      
      
      
  1. tcpdump -ttttnnvvS
tcpdump -ttttnnvvS
tcpdump -ttttnnvvS

查看指定IP的流量

这是最多见的方式,这里只查看来自或者发送到IP地址1.2.3.4的流量。

      
      
      
      
  1. tcpdump host 1.2.3.4
tcpdump host 1.2.3.4
tcpdump host 1.2.3.4

查看更多的包信息,输出HEX

当你须要查看包中的内容时,使用hex格式输出是很是有用的。

      
      
      
      
  1. # tcpdump -nnvXSs 0 -c1 icmp
  2. tcpdump: data link type PKTAP
  3. tcpdump: listening on pktap, link-type PKTAP (Apple DLT_PKTAP), capture size 262144 bytes
  4. 16:08:16.791604 IP (tos 0x0, ttl 64, id 34318, offset 0, flags [none], proto ICMP (1), length 56)
  5. 192.168.102.35 > 114.114.114.114: ICMP 192.168.102.35 udp port 50694 unreachable, length 36
  6. IP (tos 0x0, ttl 152, id 0, offset 0, flags [none], proto UDP (17), length 112)
  7. 114.114.114.114.53 > 192.168.102.35.50694: [|domain]
  8. 0x0000: 5869 6c88 7f64 784f 4392 ed7e 0800 4500 Xil..dxOC..~..E.
  9. 0x0010: 0038 860e 0000 4001 e906 c0a8 6623 7272 .8....@.....f#rr
  10. 0x0020: 7272 0303 3665 0000 0000 4500 0070 0000 rr..6e....E..p..
  11. 0x0030: 0000 9811 16cd 7272 7272 c0a8 6623 0035 ......rrrr..f#.5
  12. 0x0040: c606 005c 0000 ...\..
  13. 1 packet captured
  14. 357 packets received by filter
  15. 0 packets dropped by kernel
# tcpdump -nnvXSs 0 -c1 icmp

tcpdump: data link type PKTAP
tcpdump: listening on pktap, link-type PKTAP (Apple DLT_PKTAP), capture size 262144 bytes
16:08:16.791604 IP (tos 0x0, ttl 64, id 34318, offset 0, flags [none], proto ICMP (1), length 56)
    192.168.102.35 > 114.114.114.114: ICMP 192.168.102.35 udp port 50694 unreachable, length 36
    IP (tos 0x0, ttl 152, id 0, offset 0, flags [none], proto UDP (17), length 112)
    114.114.114.114.53 > 192.168.102.35.50694: [|domain]
    0x0000:  5869 6c88 7f64 784f 4392 ed7e 0800 4500  Xil..dxOC..~..E.
    0x0010:  0038 860e 0000 4001 e906 c0a8 6623 7272  .8....@.....f#rr
    0x0020:  7272 0303 3665 0000 0000 4500 0070 0000  rr..6e....E..p..
    0x0030:  0000 9811 16cd 7272 7272 c0a8 6623 0035  ......rrrr..f#.5
    0x0040:  c606 005c 0000                           ...\..
1 packet captured
357 packets received by filter
0 packets dropped by kernel
# tcpdump -nnvXSs 0 -c1 icmp tcpdump: data link type PKTAP tcpdump: listening on pktap, link-type PKTAP (Apple DLT_PKTAP), capture size 262144 bytes 16:08:16.791604 IP (tos 0x0, ttl 64, id 34318, offset 0, flags [none], proto ICMP (1), length 56) 192.168.102.35 > 114.114.114.114: ICMP 192.168.102.35 udp port 50694 unreachable, length 36 IP (tos 0x0, ttl 152, id 0, offset 0, flags [none], proto UDP (17), length 112) 114.114.114.114.53 > 192.168.102.35.50694: [|domain] 0x0000: 5869 6c88 7f64 784f 4392 ed7e 0800 4500 Xil..dxOC..~..E. 0x0010: 0038 860e 0000 4001 e906 c0a8 6623 7272 .8....@.....f#rr 0x0020: 7272 0303 3665 0000 0000 4500 0070 0000 rr..6e....E..p.. 0x0030: 0000 9811 16cd 7272 7272 c0a8 6623 0035 ......rrrr..f#.5 0x0040: c606 005c 0000 ...\.. 1 packet captured 357 packets received by filter 0 packets dropped by kernel

使用源和目的地址过滤

      
      
      
      
  1. tcpdump src 2.3.4.6
  2. tcpdump dst 3.4.5.6
tcpdump src 2.3.4.6
tcpdump dst 3.4.5.6
tcpdump src 2.3.4.6 tcpdump dst 3.4.5.6

过滤某个子网的数据包

      
      
      
      
  1. tcpdump net 1.2.3.0/24
tcpdump net 1.2.3.0/24
tcpdump net 1.2.3.0/24

过滤指定端口相关的流量

      
      
      
      
  1. tcpdump port 3389
  2. tcpdump src port 1025
tcpdump port 3389
tcpdump src port 1025
tcpdump port 3389 tcpdump src port 1025

过滤指定协议的流量

      
      
      
      
  1. tcpdump icmp
tcpdump icmp
tcpdump icmp

只显示IPV6流量

      
      
      
      
  1. tcpdump ip6
tcpdump ip6
tcpdump ip6

使用端口范围过滤

      
      
      
      
  1. tcpdump portrange 21-23
tcpdump portrange 21-23
tcpdump portrange 21-23

基于包的大小过滤流量

      
      
      
      
  1. tcpdump less 32
  2. tcpdump greater 64
  3. tcpdump <=128
tcpdump less 32
tcpdump greater 64
tcpdump <=128
tcpdump less 32 tcpdump greater 64 tcpdump <=128

将捕获的内容写入文件

使用-w选项能够将捕获的数据包信息写入文件以供之后分析,这些文件就是著名的PCAP(PEE-cap)文件,不少应用均可以处理它。

      
      
      
      
  1. tcpdump port 80 -w capture_file
tcpdump port 80 -w capture_file
tcpdump port 80 -w capture_file

使用tcpdump加载以前保存的文件进行分析

      
      
      
      
  1. tcpdump -r capture_file
tcpdump -r capture_file
tcpdump -r capture_file

高级

使用组合语句能够完成更多高级的过滤。

  • AND: and or &&
  • OR: or or ||
  • EXCEPT: not or !

过滤指定源IP和目的端口

      
      
      
      
  1. tcpdump -nnvvS src 10.5.2.3 and dst port 3389
tcpdump -nnvvS src 10.5.2.3 and dst port 3389
tcpdump -nnvvS src 10.5.2.3 and dst port 3389

过滤指定网络到另外一个网络

好比下面这个,查看来自192.168.x.x的,而且目的为10.x或者172.16.x.x的全部流量,这里使用了hex输出,同时不解析主机名

      
      
      
      
  1. tcpdump -nvX src net 192.168.0.0/16 and dst net 10.0.0.0/8 or 172.16.0.0/16
tcpdump -nvX src net 192.168.0.0/16 and dst net 10.0.0.0/8 or 172.16.0.0/16
tcpdump -nvX src net 192.168.0.0/16 and dst net 10.0.0.0/8 or 172.16.0.0/16

过滤到指定IP的非ICMP报文

      
      
      
      
  1. tcpdump dst 192.168.0.2 and src net and not icmp
tcpdump dst 192.168.0.2 and src net and not icmp
tcpdump dst 192.168.0.2 and src net and not icmp

过滤来自非指定端口的指定主机的流量

下面这个过滤出全部来自某个主机的非ssh流量

      
      
      
      
  1. tcpdump -vv src mars and not dst port 22
tcpdump -vv src mars and not dst port 22
tcpdump -vv src mars and not dst port 22

复杂分组和特殊字符

当构建复杂的过滤规则的时候,使用单引号将规则放到一块儿是个很好的选择。特别是在包含()的规则中。好比下面的规则就是错误的,由于括号在shell中会被错误的解析,能够对括号使用\进行转义或者使用单引号

      
      
      
      
  1. tcpdump src 10.0.2.3 and (dst port 3389 or 22)
tcpdump src 10.0.2.3 and (dst port 3389 or 22)
tcpdump src 10.0.2.3 and (dst port 3389 or 22)

应该修改成

      
      
      
      
  1. tcpdump 'src 10.0.2.3 and (dst port 3389 or 22)'
tcpdump 'src 10.0.2.3 and (dst port 3389 or 22)'
tcpdump 'src 10.0.2.3 and (dst port 3389 or 22)'

隔离指定的TCP标识

能够基于指定的TCP标识(flag)来过滤流量。

下面的过滤规则中,tcp[13]表示在TCP header中的偏移位置13开始,后面的数字表明了匹配的byte数。

显示全部的URGENT (URG)包

      
      
      
      
  1. tcpdump 'tcp[13] & 32!=0'
tcpdump 'tcp[13] & 32!=0'
tcpdump 'tcp[13] & 32!=0'

显示全部的ACKNOWLEDGE (ACK)包

      
      
      
      
  1. tcpdump 'tcp[13] & 16!=0'
tcpdump 'tcp[13] & 16!=0'
tcpdump 'tcp[13] & 16!=0'

显示全部的PUSH(PSH)包

      
      
      
      
  1. tcpdump 'tcp[13] & 8!=0'
tcpdump 'tcp[13] & 8!=0'
tcpdump 'tcp[13] & 8!=0'

显示全部的RESET(RST)包

      
      
      
      
  1. tcpdump 'tcp[13] & 4!=0'
tcpdump 'tcp[13] & 4!=0'
tcpdump 'tcp[13] & 4!=0'

显示全部的SYNCHRONIZE (SYN) 包

      
      
      
      
  1. tcpdump 'tcp[13] & 2!=0'
tcpdump 'tcp[13] & 2!=0'
tcpdump 'tcp[13] & 2!=0'

显示全部的FINISH(FIN)包

      
      
      
      
  1. tcpdump 'tcp[13] & 1!=0'
tcpdump 'tcp[13] & 1!=0'
tcpdump 'tcp[13] & 1!=0'

显示说有的SYNCHRONIZE/ACKNOWLEDGE (SYNACK)包

      
      
      
      
  1. tcpdump 'tcp[13]=18'
tcpdump 'tcp[13]=18'
tcpdump 'tcp[13]=18'

其它方式

与大多数工具同样,也可使用下面这种方式来捕获指定TCP标识的流量

      
      
      
      
  1. tcpdump 'tcp[tcpflags] == tcp-syn'
  2. tcpdump 'tcp[tcpflags] == tcp-rst'
  3. tcpdump 'tcp[tcpflags] == tcp-fin'
tcpdump 'tcp[tcpflags] == tcp-syn'
tcpdump 'tcp[tcpflags] == tcp-rst'
tcpdump 'tcp[tcpflags] == tcp-fin'
tcpdump 'tcp[tcpflags] == tcp-syn' tcpdump 'tcp[tcpflags] == tcp-rst' tcpdump 'tcp[tcpflags] == tcp-fin'

识别重要流量

最后,这里有一些重要的代码片断你可能须要,它们用于过滤指定的流量,例如畸形的或者恶意的流量。

过滤同时设置SYN和RST标识的包(这在正常状况下不该该发生)

      
      
      
      
  1. tcpdump 'tcp[13] = 6'
tcpdump 'tcp[13] = 6'
tcpdump 'tcp[13] = 6'

过滤明文的HTTP GET请求

      
      
      
      
  1. tcpdump 'tcp[32:4] = 0x47455420'
tcpdump 'tcp[32:4] = 0x47455420'
tcpdump 'tcp[32:4] = 0x47455420'

经过横幅文本过滤任意端口的SSH链接

      
      
      
      
  1. tcpdump 'tcp[(tcp[12]>>2):4] = 0x5353482D'
tcpdump 'tcp[(tcp[12]>>2):4] = 0x5353482D'
tcpdump 'tcp[(tcp[12]>>2):4] = 0x5353482D'

过滤TTL小于10的包(一般状况下是存在问题或者在使用traceroute)

      
      
      
      
  1. tcpdump 'ip[8] < 10'
tcpdump 'ip[8] < 10'
tcpdump 'ip[8] < 10'

过滤恶意的包

      
      
      
      
  1. tcpdump 'ip[6] & 128 != 0'
tcpdump 'ip[6] & 128 != 0'
tcpdump 'ip[6] & 128 != 0'

补充(非原文内容)

下面这个命令用于过滤全部与8080端口相关的tcp流量,将其输出到capcha.cap文件中,咱们可使用wireshark打开这个文件,更加可视化的分析过滤其中包含的http流量。

      
      
      
      
  1. tcpdump -tttt -s0 -X -vv tcp port 8080 -w captcha.cap
tcpdump -tttt -s0 -X -vv tcp port 8080 -w captcha.cap
tcpdump -tttt -s0 -X -vv tcp port 8080 -w captcha.cap

本文将会持续修正和更新,最新内容请参考个人 GITHUB 上的 程序猿成长计划 项目,欢迎 Star,更多精彩内容请 follow me

个人博客即将搬运同步至腾讯云+社区,邀请你们一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=30w4cly1rgsgo

转载自 https://aicode.cc/tcpdump-ru-men.html

相关文章
相关标签/搜索