Analyzing and Exporting Network Traffic with Tshark
In this step, we'll focus on how to export network traffic in different formats and perform basic traffic analysis using tshark. These skills are crucial because they allow you to share the captured data with your colleagues or use it in other tools. By the end of this section, you'll be able to handle different file formats and extract valuable information from network traffic.
Wireshark, a well - known network protocol analyzer, supports several capture file formats. Each format has its own unique characteristics, which are important to understand as they determine how the data can be used later.
- pcapng: This is the default format used by Wireshark. It supports multiple interfaces and has advanced features. It's a great choice when you need to capture complex network scenarios.
- pcap: The classic format. It's compatible with older tools, but it has fewer features compared to pcapng. If you need to work with legacy systems, this format might be your go - to.
- csv: Comma - separated values. This format is very useful when you want to import the data into spreadsheets for further analysis.
- json: JavaScript Object Notation. It's ideal for programmatic analysis, as it can be easily parsed by programming languages.
- text: A plain text format that is human - readable. It's useful when you want to quickly view the data without any special tools.
To change the format of a capture file, you can use the -F
option in tshark. The general command structure is as follows:
tshark -r <input_file> -F <format> -w <output_file>
Here, -r
specifies the input file, -F
sets the output format, and -w
defines the output file.
Let's take an example and export our capture to the pcap format:
tshark -r /home/labex/project/capture.pcapng -F pcap -w /home/labex/project/export.pcap
When this command runs successfully, you won't see any output on the screen. To confirm that the export was successful, you can use the ls
command to list the details of the exported file:
ls -l /home/labex/project/export.pcap
You should see output similar to this:
-rw-r--r-- 1 labex labex 22468 Jan 27 12:45 /home/labex/project/export.pcap
Analyzing Protocol Statistics
Tshark is not only useful for exporting files but also for generating various statistics about the captured traffic. Let's explore some of these statistical analysis options.
Protocol Hierarchy Statistics
If you want to see how different protocols are distributed in your capture, you can use the following command:
tshark -r /home/labex/project/capture.pcapng -z io,phs
The -z
option is used to specify the statistics type. In this case, io,phs
stands for protocol hierarchy statistics. The output will show the hierarchy of protocols and the percentage of packets for each protocol.
Protocol Hierarchy Statistics
|
+ Ethernet
+ Internet Protocol Version 4
+ Transmission Control Protocol
+ Transport Layer Security
+ Hypertext Transfer Protocol Secure
+ User Datagram Protocol
+ Domain Name System
Conversation Statistics
To analyze the conversations between endpoints in the network, you can use the following command:
tshark -r /home/labex/project/capture.pcapng -z conv,tcp
This command focuses on TCP conversations. It shows statistics such as the endpoints involved, the number of packets exchanged, and the total bytes transferred.
TCP Conversations
| <- | | -> | | Total | Relative | Duration |
| Frames Bytes | | Frames Bytes | | Frames Bytes | Start | |
192.168.1.100:43210 <-> 93.184.216.34:443 24 18765 18 4532 42 23297 0.000000000 8.2345
HTTP Request Statistics
If your capture contains HTTP traffic, you can analyze the HTTP requests using the following command:
tshark -r /home/labex/project/capture.pcapng -z http,tree
This command organizes the HTTP requests by URI and shows the number of requests for each URI.
HTTP/Requests:
/index.html 1 requests
/images/logo.png 2 requests
Exporting to Different Text Formats
Apart from binary formats, tshark can also export data to text formats, which are often easier to analyze.
Exporting to CSV
To export specific fields from the capture to a CSV file, you can use the following command:
tshark -r /home/labex/project/capture.pcapng -T fields -e frame.number -e ip.src -e ip.dst -e tcp.srcport -e tcp.dstport -E header=y -E separator=, > /home/labex/project/tcp_summary.csv
Here, -T fields
specifies that we want to export specific fields. The -e
option is used to define the fields we want to export, such as frame number, source and destination IPs, and source and destination TCP ports. -E header=y
adds a header to the CSV file, and -E separator=,
sets the separator as a comma.
Examining the CSV Export
After exporting the data to a CSV file, you can quickly view the first few lines of the file using the head
command:
head -5 /home/labex/project/tcp_summary.csv
The output might look like this:
frame.number,ip.src,ip.dst,tcp.srcport,tcp.dstport
1,192.168.1.100,93.184.216.34,43210,443
2,93.184.216.34,192.168.1.100,443,43210
3,192.168.1.100,93.184.216.34,43210,443
...