Photo by Gerald Schömbs on Unsplash
Wireshark is the world’s most widely used network protocol analyzer. It lets you dive into captured traffic and analyze what is going on within a network. Today, let’s talk about how you can use Wireshark’s command-line interface, TShark, to accomplish similar results.
We will go through some example commands, so feel free to use a PCAP file to follow along! You can find some sample capture files here.
Without an input file, TShark simply acts like tcpdump. It will capture traffic from the first available network and display its packets to standard output. Alternatively, you can use the
-r flag to specify the network capture file.
tshark -r network.pcap
This way, TShark will display the packets of the capture file in standard output. Let’s take a look at a line of the output!
35 29.947879 192.168.0.55 → 192.168.0.91 HTTP 423 HTTP/1.1 200 OK
This may seem complicated, but remember that the command line output of TShark mirrors the Wireshark interface! The fields from left to right in the command line output are:
Packet number, Time, Source, Destination, Protocol, Length, Info
35, 29.947879, 192.168.0.55, 192.168.0.91, HTTP, 423, HTTP/1.1 200 OK
The “Time” field shows when the packet was captured. The “Source” and “Destination” fields show the source IP and destination IP of that packet. The “Protocol” field displays the protocol used. The “Length” field shows the length of the packet. And finally, the “Info” field displays any additional info about the packet.
You can filter these packet summaries by piping TShark’s output into grep. For example, this command will output the packets with a “200 OK” HTTP status code.
tshark -r network.pcap | grep "200 OK"
Exporting interesting packets
You can examine packet contents by exporting its objects. Object exporting in TShark enables you to extract different types of packet data, such as HTTP and SMB objects. The syntax for exporting objects is as follows.
tshark -r network.pcap --export-objects PROTOCOL,DESTINATION_DIR
The PROTOCOL specifies the export object type, while the DESTINATION_DIR is the directory TShark will use to store the exported files. For example, this command will export the files that have been transported through the network and store them in the “exported_files_dir” directory.
tshark -r network.pcap --export-objects http,exported_files_dir
Using packet filters
Just like in Wireshark, you can also filter packets based on certain criteria. You can simply put your filters in quotes at the end of the command.
tshark -r network.pcap "http.request.method == POST and http.file_data contains password"
The format of the filters that can be applied is identical to that in Wireshark. You can find a list of available filters here.
You can also specify the output format for the decoded packet data using the
-T flag. For example, this command will display all HTTP GET requests in the JSON format.
tshark -r network.pcap -T json "http.request.method==GET"
Finally, you can process the output from TShark by piping it into other command-line tools such as grep.
This post only introduces a small fraction of what Wireshark and TShark can do. They are versatile tools that are capable of performing many different types of analysis.
If you are interested in learning more about these tools, visit their documentation here.