A tcpdump Tutorial with Examples — 50 Ways to Isolate Traffic - Daniel Miessler

Ask questions Research chat →

https://danielmiessler.com/study/tcpdump/ · scraped

terminal

Attachments

Scraped Content

— 1670 words · 2026-02-14 17:41:15 UTC ·

Excerpt

![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/f096c75b-be1c-4c8c-887b-3ba146a9adb0/ip-header-2021.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB466ZN7PAWPP%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T174113Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJIMEYCIQDC9rAKuJYOWpVWLxssXrrqszOo2MA4%2FuyiMjcfMV7xUgIhANDFazehNLPVlmUh9LPz3aApcWdsL98ZVGR5xL%2BZgtI%2BKv8DCAoQABoMNjM3NDIzMTgzODA1IgyjG1zg%2FkgIRCjeYMwq3APCJwUP3OfN3cuDZJDwo0cpTpkkYeb%2B%2B4IvevKuwniSqQRZLPf6gThZK1afvuM7Tk%2BSRMtc%2BHHXOf803iNk7J1Hz7GR2%2FCJ%2FMP8YBG1ZbYp9aWQfCIPX1FkekeMqomwRHc8Q6GQe6tEyb24aZEnFFCbQAThffnjJ1dPXSwPLkyqocwq56t%2BwcumkG9vgvWQMS7jw7Q9%2BAnkNGKh1fHgJxBr42JkMvuQ9GwhsU7aF7d%2FHNsYb13SLd8I2qNmNjAaOW5FeKFSAbt%2FvPmBYCadmJBI5I3GcOULD21gjAek9K9XVS817dJsPXg7eVG4okXbOm4%2Fg0qweKUdYFbYFpZRdvkh18wthRqsieTG1MlYf3a3qjVwf6QT6CnjvCq%2Fd2HPmbj28AcHZMzS5yH5qq6NWrzTGtFqwU2LiT
![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/f096c75b-be1c-4c8c-887b-3ba146a9adb0/ip-header-2021.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB466ZN7PAWPP%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T174113Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJIMEYCIQDC9rAKuJYOWpVWLxssXrrqszOo2MA4%2FuyiMjcfMV7xUgIhANDFazehNLPVlmUh9LPz3aApcWdsL98ZVGR5xL%2BZgtI%2BKv8DCAoQABoMNjM3NDIzMTgzODA1IgyjG1zg%2FkgIRCjeYMwq3APCJwUP3OfN3cuDZJDwo0cpTpkkYeb%2B%2B4IvevKuwniSqQRZLPf6gThZK1afvuM7Tk%2BSRMtc%2BHHXOf803iNk7J1Hz7GR2%2FCJ%2FMP8YBG1ZbYp9aWQfCIPX1FkekeMqomwRHc8Q6GQe6tEyb24aZEnFFCbQAThffnjJ1dPXSwPLkyqocwq56t%2BwcumkG9vgvWQMS7jw7Q9%2BAnkNGKh1fHgJxBr42JkMvuQ9GwhsU7aF7d%2FHNsYb13SLd8I2qNmNjAaOW5FeKFSAbt%2FvPmBYCadmJBI5I3GcOULD21gjAek9K9XVS817dJsPXg7eVG4okXbOm4%2Fg0qweKUdYFbYFpZRdvkh18wthRqsieTG1MlYf3a3qjVwf6QT6CnjvCq%2Fd2HPmbj28AcHZMzS5yH5qq6NWrzTGtFqwU2LiT9AVgshfb3KFY2lhHCEkfp8S6TmDd0QjEowQWEjZROaN8YGOjK8uupshZ2cEUI7o0f6yLxhjFKNtN7C%2B7C4pQRtKToZkjKoHPc3imAfQsNMETEAhrEyT2L4aPxTp6qFUSkGeK3u72zxQt8rJw0aYJJJZR0TKK2To6zM3GjjezTF13diO7gvHmQ7rrCgyKKGweIWG4oA5MhYVktJNbXrrCToz8aYqDD%2B0cLMBjqkATCpfR%2F6SU9xdQ5CwvryKbCdqjMEq6K2mN3oN0OBJd%2FeKOIEHGAvmoxIdGsbUj4xz%2BoSC8TLLSCZqzizAXlngoMtjADlsYRFvIQqY8eyNQadjFaUp6G3QVwObMfmzpIo3lEGHT38glg0bURJ2XTgvwYIEiBPkFAWtmHA9Yz4kbddJAwHONX6G3cr0kvr4RYvJfQLcaNgK%2BzevY029f%2Fmn8iyk4P0&X-Amz-Signature=ff35cd3c4bb00bab1e20b022ce69352e56c28ce5029a2b612263d070cb99ffec&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject) tcpdump is the world’s premier network analysis tool—combining both power and simplicity into a single command-line interface. This guide will show you how to isolate traffic in multiple ways—including by IP, port, protocol, or application to help you find what you’re looking for. ### Traffic isolation examples Install: apt install tcpdump (Ubuntu) yum install tcpdump (Redhat/Centos) Let’s start with a basic command that will get us HTTPS traffic: tcpdump -nnSX port 443 ```plain text 04:45:40.573686 IP 78.149.209.110.27782 > 172.30.0.144.443: Flags [.], ack 278239097, win 28, options [nop,nop,TS val 939752277 ecr 1208058112], length 0 0x0000: 4500 0034 0014 0000 2e06 c005 4e8e d16e E..4........N..n 0x0010: ac1e 0090 6c86 01bb 8e0a b73e 1095 9779 ....l......>...y 0x0020: 8010 001c d202 0000 0101 080a 3803 7b55 ............8.{U 0x0030: 4801 8100 ``` You can get a single packet with -c 1, or n number with -c n. This showed some HTTPS traffic, with a hex display visible on the right portion of the output (alas, it’s encrypted). Just remember—when in doubt, run the command above with the port you’re interested in, and you should be on your way. ## Examples PacketWizard™ isn’t really trademarked, but it should be. Now that you are able to get basic traffic, let’s step through numerous examples that you are likely to need during your job in networking, security, or as any type of PacketWizard™. Just see what’s going on, by looking at what’s hitting your interface. Or get all interfaces with -i any. tcpdump -i eth0 One of the most common queries, using host, you can see traffic that’s going to or from 1.1.1.1. Expression Types: host, net, and port. Directions: src and dst. Types: host, net, and port. Protocols: tcp, udp, icmp, and many more. tcpdump host 1.1.1.1 ```plain text 06:20:25.593207 IP 172.30.0.144.39270 > one.one.one.one.domain: 12790+ A? google.com. (28) 06:20:25.594510 IP one.one.one.one.domain > 172.30.0.144.39270: 12790 1/0/0 A 172.217.15.78 (44) ``` If you only want to see traffic in one direction or the other, you can use src and dst. tcpdump src 1.1.1.1 tcpdump dst 1.0.0.1 To find packets going to or from a particular network or subnet, use the net option. You can combine this with the src and dst options as well. tcpdump net 1.2.3.0/24 Hex output is useful when you want to see the content of the packets in question, and it’s often best used when you’re isolating a few candidates for closer scrutiny. tcpdump -c 1 -X icmp ![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/fdc2f3d5-4d5e-4558-9b08-71576f5fa2a4/single-icmp.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB466ZN7PAWPP%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T174113Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJIMEYCIQDC9rAKuJYOWpVWLxssXrrqszOo2MA4%2FuyiMjcfMV7xUgIhANDFazehNLPVlmUh9LPz3aApcWdsL98ZVGR5xL%2BZgtI%2BKv8DCAoQABoMNjM3NDIzMTgzODA1IgyjG1zg%2FkgIRCjeYMwq3APCJwUP3OfN3cuDZJDwo0cpTpkkYeb%2B%2B4IvevKuwniSqQRZLPf6gThZK1afvuM7Tk%2BSRMtc%2BHHXOf803iNk7J1Hz7GR2%2FCJ%2FMP8YBG1ZbYp9aWQfCIPX1FkekeMqomwRHc8Q6GQe6tEyb24aZEnFFCbQAThffnjJ1dPXSwPLkyqocwq56t%2BwcumkG9vgvWQMS7jw7Q9%2BAnkNGKh1fHgJxBr42JkMvuQ9GwhsU7aF7d%2FHNsYb13SLd8I2qNmNjAaOW5FeKFSAbt%2FvPmBYCadmJBI5I3GcOULD21gjAek9K9XVS817dJsPXg7eVG4okXbOm4%2Fg0qweKUdYFbYFpZRdvkh18wthRqsieTG1MlYf3a3qjVwf6QT6CnjvCq%2Fd2HPmbj28AcHZMzS5yH5qq6NWrzTGtFqwU2LiT9AVgshfb3KFY2lhHCEkfp8S6TmDd0QjEowQWEjZROaN8YGOjK8uupshZ2cEUI7o0f6yLxhjFKNtN7C%2B7C4pQRtKToZkjKoHPc3imAfQsNMETEAhrEyT2L4aPxTp6qFUSkGeK3u72zxQt8rJw0aYJJJZR0TKK2To6zM3GjjezTF13diO7gvHmQ7rrCgyKKGweIWG4oA5MhYVktJNbXrrCToz8aYqDD%2B0cLMBjqkATCpfR%2F6SU9xdQ5CwvryKbCdqjMEq6K2mN3oN0OBJd%2FeKOIEHGAvmoxIdGsbUj4xz%2BoSC8TLLSCZqzizAXlngoMtjADlsYRFvIQqY8eyNQadjFaUp6G3QVwObMfmzpIo3lEGHT38glg0bURJ2XTgvwYIEiBPkFAWtmHA9Yz4kbddJAwHONX6G3cr0kvr4RYvJfQLcaNgK%2BzevY029f%2Fmn8iyk4P0&X-Amz-Signature=0927cdf6683b286100ac590815bb5f299eb59715cb64d659e77ed0d7e71fe607&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject) tcpdump is the tool everyone should learn as their base for packet analysis. You can find specific port traffic by using the port option followed by the port number. Common Options: - nn : Don’t resolve hostnames or port names. - S : Get the entire packet. - X : Get hex output. If you’re looking for one particular kind of traffic, you can use tcp, udp, icmp, and many others as well. tcpdump icmp You can also find all IP6 traffic using the protocol option. You can also use a range of ports to find traffic. If you’re looking for packets of a particular size you can use these options. You can use less, greater, or their associated symbols that you would expect from mathematics. tcpdump less 32 tcpdump greater 64 tcpdump <= 128 It’s often useful to save packet captures into a file for analysis in the future. These files are known as PCAP (PEE-cap) files, and they can be processed by hundreds of different applications, including network analyzers, intrusion detection systems, and of course by tcpdump itself. Here we’re writing to a file called capture_file using the -w switch. tcpdump port 80 -w capture_file You can read PCAP files by using the -r switch. Note that you can use all the regular commands within tcpdump while reading in a file; you’re only limited by the fact that you can’t capture and process what doesn’t exist in the file already. tcpdump -r capture_file ## Advanced Now that we’ve seen what we can do with the basics through some examples, let’s look at some more advanced stuff. ### More options Here are some additional ways to tweak how you call tcpdump. - XX : Same as X, but also shows the ethernet header. - D : Show the list of available interfaces - l : Line-readable output (for viewing as you save, or sending to other commands) - q : Be less verbose (more quiet) with your output. - t : Give human-readable timestamp output. - tttt : Give maximally human-readable timestamp output. - i eth0 : Listen on the eth0 interface. - vv : Verbose output (more v’s gives more output). - 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. ## It’s All About the Combinations Being able to do these various things individually is powerful, but the real magic of tcpdump comes from the ability to combine options 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 programming at all they’ll be pretty familiar to you. 1. AND and or && 2. OR or or || 3. EXCEPT not or ! Use this combination to see verbose output, with no resolution of hostnames or port numbers, using absolute sequence numbers, and showing human-readable timestamps. tcpdump -ttnnvvS Here are some examples of combined commands. Let’s find all traffic from 10.5.2.3 going to any host on port 3389. tcpdump -nnvvS src 10.5.2.3 and dst port 3389 Let’s look for all traffic coming from 192.168.x.x and going to the 10.x or 172.16.x.x networks, and we’re showing hex output with no hostname resolution and one level of extra verbosity. tcpdump -nvX src net 192.168.0.0/16 and dst net 10.0.0.0/8 or 172.16.0.0/16 This will show us all traffic going to 192.168.0.2 that is not ICMP. tcpdump dst 192.168.0.2 and src net and not icmp This will show us all traffic from a host that isn’t SSH traffic (assuming default port usage). 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. 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 below the “( )” brackets. This same technique can be used to group using other expressions such as host, port, net, etc. You can also use filters to isolate packets with specific TCP flags set. ### Isolate TCP RST flags. The filters below find these various packets because tcp[13] 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. tcpdump 'tcp[13] & 4!=0' tcpdump 'tcp[tcpflags] == tcp-rst' ### Isolate TCP SYN flags. tcpdump 'tcp[13] & 2!=0' tcpdump 'tcp[tcpflags] == tcp-syn' ### Isolate packets that have both the SYN and ACK flags set. tcpdump 'tcp[13]=18' 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. ### Isolate TCP URG flags. tcpdump 'tcp[13] & 32!=0' tcpdump 'tcp[tcpflags] == tcp-urg' ### Isolate TCP ACK flags. tcpdump 'tcp[13] & 16!=0' tcpdump 'tcp[tcpflags] == tcp-ack' ### Isolate TCP PSH flags. tcpdump 'tcp[13] & 8!=0' tcpdump 'tcp[tcpflags] == tcp-push' ### Isolate TCP FIN flags. tcpdump 'tcp[13] & 1!=0' tcpdump 'tcp[tcpflags] == tcp-fin' Because tcpdump can output content in ASCII, you can use it to search for cleartext content using other command-line tools like grep. Finally, now that we the theory out of the way, here are a number of quick recipes you can use for catching various kinds of traffic. The -l switch lets you see the traffic as you’re capturing it, and helps when sending to commands like grep. tcpdump -vvAls0 | grep 'User-Agent:' tcpdump -vvAls0 | grep 'Host:' tcpdump -vvAls0 | grep 'Set-Cookie|Host:|Cookie:' This one works regardless of what port the connection comes in on, because it’s getting the banner response. tcpdump -vvAs0 port ftp or ftp-data tcpdump port http or port ftp or port smtp or port imap or port pop3 or port telnet -lA | egrep -i -B5 'pass=|pwd=|log=|login=|user=|username=|pw=|passw=|passwd= |password=|pass:|user:|username:|password:|login:|pass |user ' There’s a bit in the IP header that never gets set by legitimate applications, which we call the “Evil Bit”. Here’s a fun filter to find packets where it’s been toggled. Here are the takeaways. 1. tcpdump is a valuable tool for anyone looking to get into networking or information security. 2. The raw way it interfaces with traffic, combined with the precision it offers in inspecting packets make it the best possible tool for learning TCP/IP. 3. Protocol Analyzers like Wireshark are great, but if you want to truly master packet-fu, you must become one with tcpdump first. 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. ### Notes 1. I’m currently (sort of) writing a book on tcpdump for No Starch Press. 2. The leading image is from SecurityWizardry.com. 3. Some of the isolation filters borrowed from Sébastien Wains. 4. Thanks to Peter at hackertarget.com for inspiration on the new table of contents (simplified), and also for some additional higher-level protocol filters added in July 2018.

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation