tcpdump tutorial

*/-->

UP
|
HOME

tcpdump tutorial

1 Options

It's also important to note that tcpdump only takes the first [S:68:S] 96 bytes of data from a packet by default. If you
would like to look at more, add the -s number option to the mix, where number is the number of bytes you want to capture. I
recommend using 0 (zero) for a snaplength, which gets everything. Here's a short list of the options I use most:

  • -i any : Listen on all interfaces just to see if you're seeing any traffic.
  • -n : Don't resolve hostnames.
  • -nn : Don't resolve hostnames or port names.
  • -X : Show the packet's contents in both hex and ASCII.
  • -XX : Same as -X, but also shows the ethernet header.
  • -v, -vv, -vvv : Increase the amount of packet information you get back.
  • -c : Only get x number of packets and then stop.
  • -s : Define the snaplength (size) of the capture in bytes. Use -s0 to get everything, unless you are intentionally
    capturing less.
  • -S : Print absolute sequence numbers.
  • -e : Get the ethernet header as well.
  • -q : Show less protocol information.
  • -E : Decrypt IPSEC traffic by providing an encryption key.

2 Basic Usage

So, based on the kind of traffic I'm looking for, I use a different combination of options to tcpdump, as can be seen
below:

  1. Basic communication // see the basics without many options

    
        #!/bin/bash
    tcpdump -nS
  1. Basic communication (very verbose) // see a good amount of traffic, with verbosity and no name help

    
        #!/bin/bash
    tcpdump -nnvvS
  2. A deeper look at the traffic // adds -X for payload but doesn't grab any more of the packet
    
        #!/bin/bash
    tcpdump -nnvvXS
  3. Heavy packet viewing // the final "s" increases the snaplength, grabbing the whole packet
    
        #!/bin/bash
    tcpdump -nnvvXSs 1514

Here's a capture of exactly two (-c2) ICMP packets (a ping and pong) using some of the options described above. Notice how
much we see about each packet.


    #!/bin/bash
tcpdump -nnvXSs 0 -c2 icmp
tcpdump: listening on eth0, link-type EN10MB (Ethernet), 23:11:10.370321 IP
(tos 0x20, ttl 48, id 34859, offset 0, flags [none], length: 84)
69.254.213.43 > 72.21.34.42: icmp 64: echo request seq 0 0x0000: 4520 0054 882b 0000 3001 7cf5 45fe d52b E..T.+..0.|.E..+
0x0010: 4815 222a 0800 3530 272a 0000 25ff d744 H."*..50'*..%..D
0x0020: ae5e 0500 0809 0a0b 0c0d 0e0f 1011 1213 .^..............
0x0030: 1415 1617 1819 1a1b 1c1d 1e1f 2021 2223 .............!"#
0x0040: 2425 2627 2829 2a2b 2c2d 2e2f 3031 3233 $%&'()*+,-./0123
0x0050: 3435 3637 4567
23:11:10.370344 IP (tos 0x20, ttl 64, id 35612, offset 0, flags [none],
length: 84) 72.21.34.42 > 69.254.213.43: icmp 64: echo reply seq 0
0x0000: 4520 0054 8b1c 0000 4001 6a04 4815 222a E..T....@.j.H."*
0x0010: 45fe d52b 0000 3d30 272a 0000 25ff d744 E..+..=0'*..%..D
0x0020: ae5e 0500 0809 0a0b 0c0d 0e0f 1011 1213 .^..............
0x0030: 1415 1617 1819 1a1b 1c1d 1e1f 2021 2223 .............!"#
0x0040: 2425 2627 2829 2a2b 2c2d 2e2f 3031 3233 $%&'()*+,-./0123
0x0050: 3435 3637 4567
2 packets captured
2 packets received by filter
0 packets dropped by kernel

3 Common Syntax

Type options are host, net, and port. Direction is indicated by dir, and there you can have src, dst, src or dst, and src
and dst. Here are a few that you should definitely be comfortable with:

  • host // look for traffic based on IP address (also works with hostname if you're not using -n)

    
        #!/bin/bash
    
        tcpdump host 1.2.3.4
    
  • src, dst // find traffic from only a source or destination (eliminates one side of a host conversation)

    
        #!/bin/bash
    
        tcpdump src 2.3.4.5
    tcpdump dst 3.4.5.6
  • net // capture an entire network using CIDR notation

    
        #!/bin/bash
    tcpdump net 1.2.3.0/24
  • proto // works for tcp, udp, and icmp. Note that you don't have to type proto

    
        #!/bin/bash
    
        tcpdump icmp
    
  • port // see only traffic to or from a certain port

    
        #!/bin/bash
    
        tcpdump port 3389
    
  • src, dst port // filter based on the source or destination port

    
        #!/bin/bash
    
        tcpdump src port 1025
    tcpdump dst port 389
  • src/dst, port, protocol // combine all three

    
        #!/bin/bash
    tcpdump src port 1025 and tcp
    tcpdump udp and src port 53

You also have the option to filter by a range of ports instead of declaring them individually, and to only see packets that
are above or below a certain size.

  • Port Ranges // see traffic to any port in a range

    
        #!/bin/bash
    tcpdump portrange 21-23
  • Packet Size Filter // only see packets below or above a certain size (in bytes)

    
        #!/bin/bash
    tcpdump less 32
    tcpdump greater 128

[ You can use the symbols for less than, greater than, and less than or equal / greater than or equal signs as well. ]

// filtering for size using symbols


    #!/bin/bash
tcpdump > 32
tcpdump

4 Writing to a File

tcpdump allows you to send what you're capturing to a file for later use using the -w option, and then to read it back
using the -r option. This is an excellent way to capture raw traffic and then run it through various tools later. The traffic captured in this way is stored in tcpdump format, which is pretty much universal in the network analysis space.
This means it can be read in by all sorts of tools, including Wireshark, Snort, etc. Capture all Port 80 Traffic to a File

    #!/bin/bash
tcpdump -s 1514 port 80 -w capture_file

Then, at some point in the future, you can then read the traffic back in like so:

Read Captured Traffic back into tcpdump


    #!/bin/bash
tcpdump -r capture_file

Getting Creative

Expressions are nice, but the real magic of tcpdump comes from the ability to combine them in creative ways in order to
isolate exactly what you're looking for. There are three ways to do combinations, and if you've studied computers at all
they'll be pretty familar to you:

  1. AND
    and or &&
  2. OR
    or or ||
  3. EXCEPT
    not or !

More Examples


    #!/bin/bash

    # TCP traffic from 10.5.2.3 destined for port 3389

    tcpdump -nnvvS and src 10.5.2.3 and dst port 3389

    #!/bin/bash

    # Traffic originating from the 192.168 network headed for the 10 or 172.16 networks

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

    #!/bin/bash

    # Non-ICMP traffic destined for 192.168.0.2 from the 172.16 network

    tcpdump -nvvXSs 1514 dst 192.168.0.2 and src net and not icmp

    #!/bin/bash

    # Traffic originating from Mars or Pluto that isn't to the SSH port

    tcpdump -vv src mars and not dst port 22

As you can see, you can build queries to find just about anything you need. The key is to first figure out precisely what
you're looking for and then to build the syntax to isolate that specific type of traffic.

5 Grouping

Also keep in mind that when you're building complex queries you might have to group your options using single quotes.
Single quotes are used in order to tell tcpdump to ignore certain special characters – in this case the "( )" brackets.
This same technique can be used to group using other expressions such as host, port, net, etc. Take a look at the command
below:


    #!/bin/bash

    # Traffic that's from 10.0.2.4 AND destined for ports 3389 or 22 (incorrect)

    tcpdump src 10.0.2.4 and (dst port 3389 or 22)

If you tried to run this otherwise very useful command, you'd get an error because of the parenthesis. You can either fix
this by escaping the parenthesis (putting a \ before each one), or by putting the entire command within single quotes:


    #!/bin/bash

    # Traffic that's from 10.0.2.4 AND destined for ports 3389 or 22 (correct)

    tcpdump 'src 10.0.2.4 and (dst port 3389 or 22)'

Advanced

You can also filter based on specific portions of a packet, as well as combine multiple conditions into groups. The former
is useful when looking for only SYNs or RSTs, for example, and the latter for even more advanced traffic isolation.

[ Hint: An anagram for the TCP flags: Unskilled Attackers Pester Real Security Folk ]

Show me all URGENT (URG) packets…


    #!/bin/bash
tcpdump 'tcp[13] & 32!=0'

Show me all ACKNOWLEDGE (ACK) packets…


    #!/bin/bash
tcpdump 'tcp[13] & 16!=0'

Show me all PUSH (PSH) packets…


    #!/bin/bash
tcpdump 'tcp[13] & 8!=0'

Show me all RESET (RST) packets…


    #!/bin/bash
tcpdump 'tcp[13] & 4!=0'

Show me all SYNCHRONIZE (SYN) packets…


    #!/bin/bash
tcpdump 'tcp[13] & 2!=0'

Show me all FINISH (FIN) packets…


    #!/bin/bash
tcpdump 'tcp[13] & 1!=0'

Show me all SYNCHRONIZE/ACKNOWLEDGE (SYNACK) packets…


    #!/bin/bash
tcpdump 'tcp[13]=18'

[ Note: Only the PSH, RST, SYN, and FIN flags are displayed in tcpdump's flag field output. URGs and ACKs are displayed,
but they are shown elsewhere in the output rather than in the flags field ]

Keep in mind the reasons these filters work. The filters above find these various packets because tcp1 looks at offset
13 in the TCP header, the number represents the location within the byte, and the !=0 means that the flag in question is
set to 1, i.e. it's on.

As with most powerful tools, however, there are multiple ways to do things. The example below shows another way to capture
packets with specific TCP flags set.

Capture TCP Flags Using the tcpflags Option…


    #!/bin/bash
tcpdump 'tcp[tcpflags] & & tcp-syn != 0'

Specialized Traffic

Finally, there are a few quick recipes you'll want to remember for catching specific and specialized traffic, such as IPv6
and malformed/likely-malicious packets.

IPv6 traffic


    #!/bin/bash
tcpdump ip6

Packets with both the RST and SYN flags set (why?)


    #!/bin/bash
tcpdump 'tcp[13] = 6'

Traffic with the 'Evil Bit' Set


    #!/bin/bash
tcpdump 'ip[6] & 128 != 0'

Conclusion

Well, this primer should get you going strong, but the man page should always be handy for the most advanced and one-off
usage scenarios. I truly hope this has been useful to you, and feel free to contact me if you have any questions. ::

Footnotes:

1 DEFINITION NOT FOUND: 13

Date: 2013-07-31 Wed

Author: liweilijie

Org version 7.9.2 with Emacs version 23

Validate XHTML 1.0

tcpdump tutorial的更多相关文章

  1. TCPDUMP Command Examples

    tcpdump command is also called as packet analyzer. tcpdump command will work on most flavors of unix ...

  2. 抓包神器 tcpdump 使用介绍

    tcpdump 命令使用简介 简单介绍 tcpdump 是一款强大的网络抓包工具,运行在 linux 平台上.熟悉 tcpdump 的使用能够帮助你分析.调试网络数据. 要想使用很好地掌握 tcpdu ...

  3. 抓包神器 tcpdump 使用介绍 (转)

    tcpdump 命令使用简介 简单介绍 tcpdump 是一款强大的网络抓包工具,运行在 linux 平台上.熟悉 tcpdump 的使用能够帮助你分析.调试网络数据. 要想使用很好地掌握 tcpdu ...

  4. kubernetes网络排错思想

    Overview 本文将引入一个思路:"在Kubernetes集群发生网络异常时如何排查".文章将引入Kubernetes 集群中网络排查的思路,包含网络异常模型,常用工具,并且提 ...

  5. OpenNF tutorial复现

    这篇博客记录了自己实现OpenNF官网上tutorial的过程和遇见的问题,如果有不对的地方还请批评指正! tutorial链接 实验内容 这个实验展示了如何迅速且安全地把一个TCP流从一个NF实例迁 ...

  6. 如何利用tcpdump对mysql进行抓包操作

    命令如下: tcpdump -s -l -w - dst -i eno16777736 |strings 其中-i指定监听的网络接口,在RHEL 7下,网络接口名不再是之前的eth0,而是 eno16 ...

  7. [翻译+山寨]Hangfire Highlighter Tutorial

    前言 Hangfire是一个开源且商业免费使用的工具函数库.可以让你非常容易地在ASP.NET应用(也可以不在ASP.NET应用)中执行多种类型的后台任务,而无需自行定制开发和管理基于Windows ...

  8. 运维之网络安全抓包—— WireShark 和 tcpdump

    ------------------------------------------------本文章只解释抓包工具的捕获器和过滤器的说明,以及简单使用,应付日常而已----------------- ...

  9. tcpdump、nc网络工具使用

    tcpdump: 网络嗅探器 nc: nmap: 端口扫描 混杂模式(promisc) C设置为监控,当A和B通信,C是无法探测到数据的,除非有交换机的权限,将全网端口的数据通信都发送副本到C的端口上 ...

随机推荐

  1. 创建MySQL存储过程示例

    创建MySQL存储过程是学习MySQL数据库必须要掌握的知识,下文对创建MySQL存储过程作了详细的介绍,供您参考学习. AD:2013大数据全球技术峰会课程PPT下载 下文将教您如何创建MySQL存 ...

  2. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  3. 一个tomcat上放多个webapp问题,那这多个webapp会不会竞争端口呢?不会!安全两码事

    1.一个tomcat上放多个webapp问题,那这多个webapp会不会竞争端口呢?不会!安全两码事

  4. JTAG ARM-OB 被识别为盗版修复的方法

    今天下了一个 Keil 的最新版 V4.70,打开工程,弹出个升级Jlink固件的对话框,也没仔细看,直接点了yes .这下爽了,升级之后弹出个对话框说我的Jlink是盗版的,然后工程自动关闭,很是无 ...

  5. Java:编码的详解

    ASCII:美国信息标准信息码,用一个字节的7为表示. ISO8859-1:拉丁码表 欧洲码表 ,用一个字节的8位表示. GB2312:中国的中文编码表. GBK:中国的中文编码表升级,融合了更多的中 ...

  6. android 使用静态变量传递数据

    使用静态变量传递数据之通用方式. 测试应用:当前页面点击button传递数据到一个新的页面显示在textview中. 首先在,mainActivity.xml文件中加入一个button按钮 <B ...

  7. Jquery瀑布流布局

    瀑布流布局最近真的很流行,很多人都跟我一样想知道是怎么做出来的吧,经过网上搜索大量的参考结合N边的实验今天终于被我写出来了,为了便于大家理解我使用了jQuery(当然用源生js代码执行的效率会高一些, ...

  8. wrong number of arguments,java方法反射时数组参数的坑

    java方法中只有一个参数是数组,反射的时候我们不能想当然的传歌数组进去,传数组进去的时候表示多个参数. 两个数组不是一个意思啊. 我们应该把数组转为objet,这样才表示一个参数. import j ...

  9. eclipse 中使用tomcat

    最近写了个商品搜索模块,要做成tomcat服务,以前只关注算法,从来没有使用过tomcat,这次上网上查了些资料还搞定(小公司真是锻炼人啊,以前我从来不考虑这些服务问题). 1.tomcat 环境的搭 ...

  10. android 从服务器上获取APK下载安装

    简单的为新手做个分享.  网上有些资料,不过都是很零散,或是很乱的,有的人说看不懂. 一直有新手说 做到服务器更新APK时没有思路,这里做个简单的分享,希望有不同思路的可以讨论.  下面做个很简单的读 ...