Network Traffic Analysis with tcpdump
30 Aug 2022Introduction
Sometimes it can be of value to be able to isolate and analyse specific network traffic that is flowing through your network interface. tcpdump offers you this capability in a command line application.
There are many tutorials already that take you through tcpdump comprehensively, so this article will just be constrained to usages that have benefited me.
Reading the output
In order for this tool to be of any use, it pays to know how to read the output. In this example I’m capturing all of the port 80 traffic flowing through my network interface.
The stream of output that you see after this (once you have some port 80 traffic going) is the output that you’ll use for analysis. Here’s an excerpt after hitting the first page on the internet.
There’s lots here.
We’re given the time of the packet being observed 21:49:54.056071
.
We’re given the network layer protocol IP
, source address (my machine) 192.168.20.35
and port 60584
; along
with the destination 188.184.21.108
(on port 80
).
The next field Flags [P.]
is an encoded representation of the TCP flags. The following table gives a
breakdown of these flag values.
Value | Flag | Description |
---|---|---|
S | SYN | Connection start |
F | FIN | Connection finish |
P | PUSH | Data push |
R | RST | Connection reset |
. | ACK | Acknowledgement |
The combination of values tells you the flags that are up. In this case P.
tells us this is a PUSH-ACK
packet.
The sequence number seq 3043086668:3043087150
tells us the run of bytes contained within this sample. The ack
value ack 3119373143
is the next byte expected. The win
value tells us the number of bytes available in the
buffer followed by the TCP options.
The packet length is given at the end of the line.
The data frame is now split into a hexadecimal representation in the middle (given by -X
); and the ASCII representation to the
right.
With the basic output view out of the way, we get move onto some useful invocations.
Invocations
Filter by Port
As per the above example, we can filter traffic by any port that we give to port
switch. Here
we can see any SMTP traffic.
Everything
Sometimes it can be useful to just receive everything flowing through a network interface.
Filter by Host
You can use the host
keyword to see traffic going to or coming from an IP address. You can constrain this
even further using src
(coming from) or dest
(going to).
Filter by Network
Using broader strokes, you can use net
to specify a full network to filter packets on. This will allow you
to filter a whole network or subnet.
Filter by Protocol
Just seeing ping (ICMP) traffic can be filtered like so:
Conclusion
tcpdump is a very useful network analysis tool do perform discoveries on what’s actually happening. There’s a lot more power that can be unlocked by combining some of these basic filters together using logical concatenators.