Script which can be used to reconstruct pcap and perform packet modifications from tshark json output. Script is also allowing to perform pcap anonymization.
Source code is located on github https://github.com/H21lab/json2pcap
The above repository can contain more recent changes compared to Wireshark repository (https://gitlab.com/wireshark/wireshark/-/blob/master/tools/json2pcap/json2pcap.py).
Command tshark -T json -x or -T jsonraw output adds into hex-data output in JSON also the information on which position each field is dissected in the original frame, what is the field length, the bitmask (for not byte aligned fields) and the type. This information can be used for latter processing. One use-case is the json2pcap script included in wireshark, which assembles the protocol layers back together from upper to lowers layers. This allows revers json to pcap conversion and also the packet modification/editing/rewriting.
Pcap anonymization can be performed in the following way:
By -a switch should be specified all fields which require anonymization. These fields will be replaced by hex 0xFF in the output pcap. For identification of the names of the raw fields it is possible to open the json file which is generated by -T jsonraw option.
Anonymization example for SIP protocol
1. Download original SIP_CALL_RTP_G711 pcap from wireshark pcap samples
2. Run the following command
tshark -Y sip -r ./SIP_CALL_RTP_G711 -T json -x --no-duplicate-keys | python json2pcap.py -a "ip.src_raw" -a "ip.dst_raw" -a "sip.from.user_raw" -a "sip.from.user_raw" -a "sip.to.user_raw" -a "sip.contact.uri_raw" -a "sip.contact.user_raw" -a "sip.r-uri.user_raw" -a "sip.display.info_raw" --salt "iSaiU7Y6biYxAEeVbP77" -o ./SIP_CALL_RTP_G711_anonymized.pcap3. This will produce SIP_CALL_RTP_G711_anonymized.pcap as seen on the screenshots.
Original SIP_CALL_RTP_G711.pcap
SIP_CALL_RTP_G711_anonymized.pcap
1. Download original dns.cap from wireshark pcap samples
2. Create json from pcap
tshark -T jsonraw -J "dns" -r dns.cap > dns.cap.json3. Modify dns.cap.json
vi dns.cap.json{ "_index": "packets-2017-02-27", "_type": "pcap_file", "_score": null, "_source": { "layers": { "frame_raw": ["00c09f32418c00e018b10cad0800450000380000400040116547c0a8aa08c0a8aa14801b0035002485ed10320100000100000000000006676f6f676c6503636f6d0000100001", 0, 70, 0, 1], "frame": { "filtered": "frame" }, "eth_raw": ["00c09f32418c00e018b10cad0800", 0, 14, 0, 1], "eth": { "filtered": "eth" }, "ip_raw": ["450000380000400040116547c0a8aa08c0a8aa14", 14, 20, 0, 1], "ip": { "filtered": "ip" }, "udp_raw": ["801b0035002485ed", 34, 8, 0, 1], "udp": { "filtered": "udp" }, "dns_raw": ["10320100000100000000000006676f6f676c6503636f6d0000100001", 42, 28, 0, 1], "dns": { "dns.id_raw": ["1032", 42, 2, 0, 5], "dns.flags_raw": ["0100", 44, 2, 0, 5], "dns.flags_tree": { "dns.flags.response_raw": ["0", 44, 2, 32768, 2], "dns.flags.opcode_raw": ["0", 44, 2, 30720, 5], "dns.flags.truncated_raw": ["0", 44, 2, 512, 2], "dns.flags.recdesired_raw": ["1", 44, 2, 256, 2], "dns.flags.z_raw": ["0", 44, 2, 64, 2], "dns.flags.checkdisable_raw": ["0", 44, 2, 16, 2] }, "dns.count.queries_raw": ["0001", 46, 2, 0, 5], "dns.count.answers_raw": ["0000", 48, 2, 0, 5], "dns.count.auth_rr_raw": ["0000", 50, 2, 0, 5], "dns.count.add_rr_raw": ["0000", 52, 2, 0, 5], "Queries": { "google.com: type TXT, class IN": { "dns.qry.name_raw": ["0667676767676703636f6d00", 54, 12, 0, 26],4. json2pcap.py to generate pcap
The raw fields are flatten and by this the frame is created. The shortest raw fields overwrite the longer fields.
./wireshark/tools/json2pcap/json2pcap.py -p dns.cap.json5. (OPTIONAL) json2pcap.py to generate python payload instead of pcap.
This is with relative positions to the parents. And the pcap reconstruct algorithm is different by using the parents and child hierarchy. This can be useful for possible latter more complex encoding (e.g. using scapy, dpkt, pycrate/libmich or other libraries).
./wireshark/tools/json2pcap/json2pcap.py -p dns.cap.json6. New pcap dns.cap.json.pcap