Wireshark alternative ➡️ tcpdump (Linux)

Infographic showing wiretapping.


In this blog:

  • What is tcpdump?
  • Installation.
  • Usage.
  • Saving captures to a file.
  • Filtering output.
  • Expressions.
  • Understanding the output.
  • Packet content. 
  • The end?
See my blog on uncovering network attacks using Wireshark here.

                                 

What is tcpdump?

Tcpdump is a command line alternative to Wireshark and runs natively on Linux based operating systems. 

The tool is not as feature rich as Wireshark but can be faster and more efficient in capturing and displaying packets, hence why many network admins and security professionals like using tcpdump for quick analysis. 

Being a command line tool allows it to be run on remote servers to troubleshoot networks where a gui may not be available. The .PCAP files can then be analysed with Wireshark later.

Most Linux distributions come with tcpdump installed, so your distro might already have it downloaded. To check if you have it, type which tcpdump and the path to the software will be displayed if it is on the system.


Image showing Linux command.
Checking if we have tcpdump installed.



Installation


To download, open the Linux terminal and type Sudo apt-get install tcpdump, then type your admin password. Sudo stands for ‘superuser do’. 

The library libcap (used for packet captures must be on the system too). This will be downloaded as a dependency during install.

To get more information on the tool type man tcpdump to display the manual.


Image showing manual page for tcpdump.
Manual page for tcpdump.


Usage

To see what interfaces are available for us to capture packets on, run the command        tcpdump --list-interfaces.


Image showing interfaces on a computer system.
Seeing what interfaces are available.

The interface any allows us to capture packets on any interface so we can use this.

Run the command sudo tcpdump --interface any.



Image showing network traffic being intercepted.
Traffic captured.


In this instance, I interrupted the process after capturing almost 3000 packets.

 


You can disable name resolution using -n and port resolution by using -nn

Tcpdump can also be limited to the number of packets it captures by using the               -c<number of packets> flag.

This makes it easier to quickly troubleshoot networks. 

Since the domain name servers don't need to be connected to during this kind of scan, the load on the network is less and reduces network traffic.


Image showing Linux command.
Preventing host name and port resolution and limiting packets captured.


Saving captures to a file


Saving captures to a file is great if you have too many packets to analyse as you can let the program run and once it terminates, you can analyse the results from the file whenever you want.


To save captures to a file, use the -w flag (for writing out).


Image showing Linux command.
Writing our packet capture to a file named packetfile.pcap.


The file extension .pcap stands for packet capture and allows us to open the file using any packet capture software we want. By default in Kali Linux, the file is opened in Wireshark.


Verbose output

The -v flag stands for verbose output and lets us know what is happening throughout the capture. 

For example, in the image above the Got 14 feedback is part of the verbose output.


Image showing Linux command.
Verbose output.



Reading file in terminal

The -r flag lets us read the capture file in the terminal itself.


Image showing Linux commands.
Reading the file in terminal.


Opening the .pcap file starts up Wireshark so we can analyse the results.


Image showing Linux command.
Opening the file in Wireshark.


Filtering output

Filtering by destination IP

You can filter the output in terminal. 

For example, if you want to filter by destination IP, type ➡️

tcpdump -r <file_name> dst <IP address>.

I removed the sudo prefix as we are not capturing packets from the network anymore.


Image showing Linux command.
Filtering output by destination IP.


You can Also filter by source IP by using the src flag. Type ➡️ 

tcpdump -r <file_name> src <IP address>.



Image showing Linux command.
Filtering output by destination IP.




Filtering by protocol

You can filter the output for certain protocols that you want to look out for.

Just type the tcpdump command but add the protocol that you want at the end. 

For example, if you want to look out for ICMP packets, type ➡️

sudo tcpdump -i any -c5 icmp.

The interface flag can be shortened to -i


Image showing Linux command.
Start the command with icmp filter.



Start another terminal and ping whatever website you want. I pinged 8.8.8.8, or Google.

ICMP (internet control message protocol) is the protocol used to communicate error or update messages to routers, hosts or intermediary messages.



Image showing Linux command.
Pinging Google for tcpdump to pick up icmp packets.



Image showing Linux command.
tcpdump has captured icmp messages from our connection to Google.



Filtering by host

You can filter captures by host by using the host flag. Type ➡️

Sudo tcpdump -i any -c5 host <host_name>.



Image showing Linux command.
Start the command with host filter.



Again, I started another terminal and pinged bbc.com to get the connection working.



Image showing Linux command.
Pinging bbc.com for tcpdump to pick up packets directed at bbc.com.



Image showing Linux command.
tcpdump has captured only messages from our connection to bbc.com.







Filtering by port

You can filter the output by port by using the port flag and providing the port number you want to filter for. Type ➡️ 

Sudo tcpdump -i any -c5 port 443.

This command filters for port 443 which is used by web servers to redirect traffic to its destination over HTTPS via port 443.



Image showing Linux command.
tcpdump has captured only packets sent over port 443.




Expressions

Expressions can be used to combine multiple filters to fine comb our results even more. They can be simple or complex. 

To filter for packets from source IP 143.244.38.136  and port 443, Type ➡️ 

Sudo tcpdump -i any -c5 src 143.244.38.136 and port 443.


Image showing Linux command.

tcpdump has captured packets sent over port 443 from our specified source IP.


To filter for packets from destination host wireshark  and port 443 OR port 1023,      Type ➡️ 

Sudo tcpdump -i any -c5 "dst wireshark and (port 443 and port 1023)".


Image showing Linux command.
tcpdump has captured packets sent over tcp port 443 or port 1023 and from our specified destination IP.


Understanding the output


I will go through the average TCP packet that tcpdump captures.

Typical flags in a tcpdump output ➡️



Image showing flags used by tcpdump.
Flags in tcpdump and their meanings.


Typical tcp packet ➡️ 


Image showing typical tcp packet.
Typical tcp packet.



The first part is the timestamp of the packet according to the local time ➡️


Image showing timestamp of received packet.
Timestamp of the received packet.



The next part eth0 shows what interface the packet was captured from ➡️ 


Image showing interface.
Interface where packets are being captured from.



Source and destination IP addresses are shown next with the port number shown after the first IP address. IP is the network layer protocol and represents IPv4. In the case of IPv6, IP6 would be shown ➡️ 



Image showing IP adresses.
Source and destination IP addresses and port number.



The flags represent what stage the TCP connection is at ➡️ 

This one below stands for push-ack. The other flags and their meanings can be found above in the table.


Image showing flags.
Flag showing push-ack.




The sequence number is used to identify the packets in a capture and every packet after the first uses the first packet's sequence number to make is easier to follow.

In this example, the sequence is seq 619:795, which means this packet contains bytes 618 to 795 in this capture flow.



Image showing sequence number.
Sequence ID.


The next part is the Ack Number which is sent from the TCP server to acknowledge that the packet has been received and that it is ready for the next bit of data. 

In this case, the Ack Number is 1 as this is the side sending the data. 

If this was captured on the receiving side (webserver), the Ack Number would signify the next byte of data being sent in this flow (795 in our case).


Image showing Ack Number.
Ack Number shown.

                             

The window size comes next and refers to the buffer size (bytes) available or remaining in this particular TCP connection. 

The buffer available on the receiving side tells the webserver how much data is able to be sent over the wire before the client sends the acknowledgement flag and the connection can continue. 

If the buffer size is too low, this indicates that the client is unable to process all the data being sent in time and the server must slow down. 

When a buffer size of 0 is advertised by the receiver, the sender stops sending data and starts a persist timer which is used to prevent a deadlock situation wherein the next window size update is lost and the sender has to wait for an update. 

When this timer expires, the sender tries to get an ack message and a window size update by sending small packets. 

If this continues, the receiver will repeatedly advertise small buffer values which in turn results in a smaller set of data being sent and results in the silly window syndrome which is incredibly inefficient as it sends just a few bytes of data in a TCP segment.

       

Image showing window buffer size.
Window size remaining in this connection.


The final part of the TCP packet is the packet length, in bytes. 

This is the length of the payload data, in bytes. (The difference between the last and first bytes in the sequence number).


Image showing packet length.
Packet length.


Packet content

Sometimes checking just the packet headers for source/ destination IPs isn't enough to troubleshoot the network and we must check the packet contents in more detail to find what we need. 

To view the contents in hex and ASCII, use the flag -X. 

To view the contents in just ASCII, use the flag -A. Type ➡️ 

Sudo tcpdump -i any -c10 -A port 80.

This command provides us with the packet contents of a http transmission. 



Image showing packet content.
Checking packet content.


The end?

This blog only goes through the basics of tcpdump and barely touches on its vast capabilities. 

If you want to learn more about this command line alternative to Wireshark, visit the tcpdump website. 


Comments

Post a Comment

Popular posts from this blog

Connecting Metasploitable to Kali Linux

Fixing kernel timer error in Metasploitable

Setting up Metasploitable lab