Use Tshark to view json data

10,345

Solution 1

tshark -r test.cap  -Y 'http>0' -T json

tshark -r test.cap  -Y 'http>0' -T json -x # to also include the raw packet data

Solution 2

Had similar problem. Failed to solved it with wireshark/tshark options only. Below is my workaround for extracting raw json and xml from cap files.

# 1. convert to pdml with DISABLED json and xml dissectors
tshark -r "wireshark.cap" -2 -R "http" --disable-protocol json --disable-protocol xml -V -T pdml > "wireshark.cap.pdml.xml" 

# 2. get hex encoded raw data from media.type pdml element
# 3. perform hex decode

I used groovy script for steps 2 and 3

import groovy.xml.*

...

def String hexDecode(String s) {
    if ( null == s || 0 == s.length() ) { 
            return null
    }   
    def res = ""
    for (int i = 0; i < s?.length(); i += 2) {
            res += (Character)((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16))
    }   
    return res 
}

...

def xmlFile = new File("wireshark.cap.pdml.xml")
def pdml  = new XmlParser().parseText( xmlFile.text )
pdml.packet.each{ packet->
    def media = packet.proto.find{ "media"==it.@name }
    def hex  = media?.field.find{"media.type"==it.@name }?.@value
    def raw = hexDecode(hex)
}
Share:
10,345
Tony
Author by

Tony

Updated on July 26, 2022

Comments

  • Tony
    Tony almost 2 years

    When I use tshark to decode capfile like this

     tshark -V -r test.cap  -Y 'http>0'
    

    I got

    ...
    JavaScript Object Notation: application/json
        Object
            Member Key: "ret"
                Number value: 99
            Member Key: "message"
                String value:test
    

    Question is how I can get json data like that use tshark

    ...
    {"ret":99,"message":"test"}