Display in JSON with -T json
In this step, we'll explore how to format captured HTTP traffic data as JSON using Wireshark's Tshark utility. JSON (JavaScript Object Notation) is a lightweight data format that's easy for both humans to read and machines to parse. This makes it ideal for analyzing network traffic programmatically.
Before we begin, let's understand why JSON output is valuable:
- Structured data organization
- Easy integration with other tools and scripts
- Standardized format for data exchange
- First, ensure you're in the default working directory where we'll run our commands:
cd ~/project
- Now let's run Tshark to capture HTTP requests and output them in JSON format. This command combines filtering with JSON formatting:
sudo tshark -Y "http.request" -T json -e http.request.method -e http.host -e http.request.uri
Let's break down what each part of this command does:
-Y "http.request"
: This filter tells Tshark to only show HTTP request packets
-T json
: Specifies that we want the output in JSON format
-e
fields: These extract specific pieces of information from each HTTP request:
http.request.method
: The HTTP method used (GET, POST, etc.)
http.host
: The website domain being accessed
http.request.uri
: The specific path or resource being requested
- To generate test traffic that we can capture, open a second terminal window and run these curl commands:
curl http://example.com
curl http://example.org/sample
When you run the Tshark command while this test traffic is generated, you'll see output structured like this:
[
{
"_index": "packets-1",
"_source": {
"layers": {
"http.request.method": ["GET"],
"http.host": ["example.com"],
"http.request.uri": ["/"]
}
}
},
{
"_index": "packets-2",
"_source": {
"layers": {
"http.request.method": ["GET"],
"http.host": ["example.org"],
"http.request.uri": ["/sample"]
}
}
}
]
Notice how each HTTP request becomes a separate JSON object with clearly labeled fields. This structure makes it simple to identify:
- Which website was accessed
- What type of request was made
- Which specific page or resource was requested
The JSON format is particularly useful when you want to save this data for later analysis or feed it into other tools that can process JSON data automatically.