Tcpdump Primer With Examples
Tcpdump Primer With Examples
Tcpdump Primer With Examples
Blog
Study
Podcast
Essays
Projects
About
Options
Basic Usage
Examples
Writing to a File
Getting Creative
Advanced
is the premier network analysis tool for information security
professionals. Having a solid grasp of this ber-powerful application is
mandatory for anyone desiring a thorough understanding of TCP/IP. Many
prefer to use higher level analysis tools such as Ethereal Wireshark, but I
believe this to usually be a mistake.
tcpdump
Options
Below are a few options (with examples) that will help you greatly when
working with the tool. Theyre easy to forget and/or confuse with other types
of lters, i.e. ethereal, so hopefully this page can serve as a reference for
you, as it does me.
First o, I like to add a few options to the tcpdump command itself, depending
on what Im looking at. The rst of these is -n, which requests that names
are not resolved, resulting in the IPs themselves always being displayed. The
second is -X, which displays both hex and ascii content within the packet.
The nal one is -S, which changes the display of sequence numbers to
absolute rather than relative. The idea there is that you cant see weirdness
in the sequence numbers if theyre being hidden from you. Remember, the
advantage of using tcpdump vs. another tool is getting manual interaction with
the packets.
Its also important to note that tcpdump only takes the rst 68 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.
Heres a short list of the options I use most:
[ NOTE: All of the following come after
tcpdump,
for example:
tcpdump -i any
-i eth0
Basic Usage
So, based on the kind of trac Im looking for, I use a dierent combination
of options to tcpdump, as can be seen below:
1. Basic communication // see the basics without many options
# tcpdump -nS
2. Basic communication (very verbose) // see a good amount of trac, with
verbosity and no name help
# tcpdump -nnvvS
3. A deeper look at the trac // adds -X for payload but doesnt grab any more of
the packet
# tcpdump -nnvvXS
4. Heavy packet viewing // the nal s increases the snaplength, grabbing the
whole packet
7cf5
0000
0e0f
1e1f
2e2f
45fe
25ff
1011
2021
3031
d52b
d744
1213
2223
3233
E..T.+..0.|.E..+
H."..50'..%..D
.^..............
.............!"#
$%&'()+,-./0123
4567
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 [email protected]."
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
hermes root #
Examples
Expressions allow you to trim out various types of trac and nd exactly
what youre looking for. Mastering the expressions and learning to combine
them creatively is what makes one truly powerful with tcpdump. There are
three main types of expression: type, dir, and proto.
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 denitely be comfortable with:
host // look for trac based on IP address (also works with hostname if youre not
using -n)
conversation)
# tcpdump icmp
port // see only trac to or from a certain port
You also have the option to lter 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 trac to any port in a range
tcpdump portrange 21-23
Packet Size Filter // only see packets below or above a certain size (in bytes)
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. ]
// ltering for size using symbols
tcpdump > 32
tcpdump <= 128
Writing to a File
allows you to send what youre capturing to a le 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 trac and then run it through various tools later.
tcpdump
The trac 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 Trac to a File
# tcpdump -s 1514 port 80 -w capture_le
Then, at some point in the future, you can then read the trac back in like
so:
Read Captured Trac back into
tcpdump
# tcpdump -r capture_le
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 youre looking
for. There are three ways to do combinations, and if youve studied
computers at all theyll be pretty familar to you:
1. AND
and or
2. OR
&&
or or ||
3. EXCEPT
not or !
More Examples
# TCP trac from 10.5.2.3 destined for port 3389
tcpdump -nnvvS src 10.5.2.3 and dst port 3389
# Trac 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
# Non-ICMP trac 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
# Trac originating from Mars or Pluto that isnt to the SSH port
tcpdump -vv src mars and not dst port 22
As you can see, you can build queries to nd just about anything you need.
The key is to rst gure out precisely what youre looking for and then to
build the syntax to isolate that specic type of trac.
Grouping
Also keep in mind that when youre 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:
# Trac thats 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, youd get an error
because of the parenthesis. You can either x this by escaping the
parenthesis (putting a \ before each one), or by putting the entire command
within single quotes:
# Trac thats 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 lter based on specic 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 trac
isolation.
[ Hint: An anagram for the TCP ags: Unskilled Attackers Pester Real
Security Folk ]
Show me all URGENT (URG) packets
# tcpdump tcp[13] & 32!=0
Show me all ACKNOWLEDGE (ACK) packets
# tcpdump tcp[13] & 16!=0
Show me all PUSH (PSH) packets
# tcpdump tcp[13] & 8!=0
Show me all RESET (RST) packets
# tcpdump tcp[13] & 4!=0
Show me all SYNCHRONIZE (SYN) packets
# tcpdump tcp[13] & 2!=0
Show me all FINISH (FIN) packets
# tcpdump tcp[13] & 1!=0
Show me all SYNCHRONIZE/ACKNOWLEDGE (SYNACK) packets
# tcpdump tcp[13]=18
[ Note: Only the PSH, RST, SYN, and FIN ags are displayed in tcpdumps ag
eld output. URGs and ACKs are displayed, but they are shown elsewhere in
the output rather than in the ags eld ]
Keep in mind the reasons these lters work. The lters above nd these
various packets because tcp[13] looks at oset 13 in the TCP header, the
number represents the location within the byte, and the !=0 means that the
ag in question is set to 1, i.e. its on.
As with most powerful tools, however, there are multiple ways to do things.
The example below shows another way to capture packets with specic TCP
ags set.
Capture TCP Flags Using the
tcpflags
Option
Specialized Trac
Finally, there are a few quick recipes youll want to remember for catching
specic and specialized trac, such as IPv6 and malformed/likely-malicious
packets.
IPv6 trac
# tcpdump ip6
Packets with both the RST and SYN ags set (why?)
# tcpdump tcp[13] = 6
Trac with the Evil Bit Set
# 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-o usage scenarios. I truly
hope this has been useful to you, and feel free to contact me if you have any
questions.
[ If you liked this, check out my other technical primers here. ]
Notes
1. Im currently writing a book on tcpdump for No Starch Press.
2. The leading image is from SecurityWizardry.com.
An ICMP Reference
Entering Promiscuous
Mode on OS X
How to Remember
Your TCP Flags
An IPTABLES Primer
21 Comments
danielmiessler.com
Share
Recommend 6
Login
Sort by Best
2 years ago
4
Russell
2 years ago
Great write up. Just FYI, it does not look like this is correct.
# Traffic originating from Mars or Pluto that isn't to the SSH port
tcpdump -vv src mars and not dst port 22
Reply Share
2
Nope
2 years ago
2
uday3110
a year ago
1
Dan
a year ago
Reply Share
Vincent Bernat
2 years ago
The default value for -s is 65535 since 4.1.0. However, I keep using -s 0 to not care of
the version. Also, using more than 1514 is useful even without jumbo frames, for
example when you are using TSO/GSO/GRO/LRO.
I also like to use a command like this:
ssh somehost tcpdump -s0 -w - -U some filter | wireshark -k -i -
Reply Share
Search
:: RSS
:: Twitter
:: Github
Related
An ICMP Reference
Entering Promiscuous Mode on OS X
Not All SYNs Are Created Equal
How to Remember Your TCP Flags
Benign Indian DDoS Attempt
Explore
My Tutorial Series
Recommended
Sitemap
Categories
Categories Select Category
Subscribe
Subscribe
Daniel Miessler 1999-2015 | Stack | Share | Syndication | Privacy