How do I use tshark to print request-response pairs from a pcap file?

19,949

Solution 1

If you are willing to switch to another tool, tcptrace can do this with the -e option. It also has an HTTP analysis extension (xHTTP option) that generates the HTTP request/repsonse pairs for each TCP stream.

Here is a usage example:

tcptrace --csv -xHTTP -f'port=80' -lten capturefile.pcap
  • --csv to format output as comma sperated variable
  • -xHTTP for HTTP request/response written to 'http.times' this also switches on -e to dump the TCP stream payloads, so you really don't need -e as well
  • -f'port=80' to filter out non-web traffic
  • -l for long output form
  • -t to give me progress indication
  • -n to turn off hostname resolution (much faster without this)

Solution 2

This probably wasn't an option when the question was asked but newer versions of tshark can "follow" conversations.

tshark -nr dump.pcap -qz follow,tcp,ascii,123

I know this is a super old question. I'm just adding this for anyone that ends up here looking for a current solution.

Solution 3

If you captured a pcap file, you can do the following to show all requests+responses.

filename="capture_file.pcap"
for stream in `tshark -r "$filename" -2 -R "tcp and (http.request or http.response)" -T fields -e tcp.stream | sort -n | uniq`; do
    echo "==========BEGIN REQUEST=========="
    tshark -q -r "$filename" -z follow,tcp,ascii,$stream;
    echo "==========END REQUEST=========="
done;

I just made diyism answer a bit easier to understand (you don't need sudo, and multiline script is imo simple to look at)

Share:
19,949
Steven
Author by

Steven

Updated on July 29, 2022

Comments

  • Steven
    Steven almost 2 years

    Given a pcap file, I'm able to extract a lot of information from the reconstructed HTTP request and responses using the neat filters provided by Wireshark. I've also been able to split the pcap file into each TCP stream.

    Trouble I'm running into now is that of all the cool filters I'm able to use with tshark, I can't find one that will let me print out full request/response bodies. I'm calling something like this:

     tshark -r dump.pcap -R "tcp.stream==123 and http.request" -T fields -e http.request.uri
    

    Is there some filter name I can pass to -e to get the request/response body? The closest I've come is to use the -V flag, but it also prints out a bunch of information I don't necessary want and want to avoid having to kludge out with a "dumb" filter.

  • Steven
    Steven over 12 years
    I used tcptrace. It's pretty promising. Thanks! For some strange reason, just using tcptrace -e my.dump didn't separate out requests correctly. I suspect this is just a case of me doing something wrong since Wireshark does the same splitting just fine, so I'll poke into it a little bit more. If you had a one-liner at the top of your head to extract request-response pairs from a standard pcap file (unfortunately with a handful of cut off packets), I'm all ears :).
  • rupello
    rupello over 12 years
    added an example - this works for me, but I'm sure you will run into issues if yu have truncated packets
  • SimonSimCity
    SimonSimCity over 5 years
    This is perfect for me excepted that I also need the exact timestamp the request got sent. I'm trying to reproduce a scenario where I need to send the stuff at the same time as it happened previously.