diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:34:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:34:10 +0000 |
commit | e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc (patch) | |
tree | 68cb5ef9081156392f1dd62a00c6ccc1451b93df /tools/generate_cbor_pcap.py | |
parent | Initial commit. (diff) | |
download | wireshark-e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc.tar.xz wireshark-e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc.zip |
Adding upstream version 4.2.2.upstream/4.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/generate_cbor_pcap.py')
-rwxr-xr-x | tools/generate_cbor_pcap.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/tools/generate_cbor_pcap.py b/tools/generate_cbor_pcap.py new file mode 100755 index 00000000..545b985e --- /dev/null +++ b/tools/generate_cbor_pcap.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +''' +Convert a CBOR diagnostic notation file into an HTTP request +for the encoded cbor. +This allows straightforward test and debugging of simple pcap files. + + Copyright 2021 Brian Sipos <brian.sipos@gmail.com> + +SPDX-License-Identifier: LGPL-2.1-or-later +''' + +from argparse import ArgumentParser +from io import BytesIO +import scapy +from scapy.layers.l2 import Ether +from scapy.layers.inet import IP, TCP +from scapy.layers.http import HTTP, HTTPRequest +from scapy.packet import Raw +from scapy.utils import wrpcap +from subprocess import check_output +import sys + + +def main(): + parser = ArgumentParser() + parser.add_argument('--content-type', default='application/cbor', + help='The request content-type header') + parser.add_argument('--infile', default='-', + help='The diagnostic text input file, or "-" for stdin') + parser.add_argument('--outfile', default='-', + help='The PCAP output file, or "-" for stdout') + parser.add_argument('--intype', default='cbordiag', + choices=['cbordiag', 'raw'], + help='The input data type.') + args = parser.parse_args() + + # First get the CBOR data itself + infile_name = args.infile.strip() + if infile_name != '-': + infile = open(infile_name, 'rb') + else: + infile = sys.stdin.buffer + + if args.intype == 'raw': + cbordata = infile.read() + elif args.intype == 'cbordiag': + cbordata = check_output('diag2cbor.rb', stdin=infile) + + # Now synthesize an HTTP request with that body + req = HTTPRequest( + Method='POST', + Host='example.com', + User_Agent='scapy', + Content_Type=args.content_type, + Content_Length=str(len(cbordata)), + ) / Raw(cbordata) + + # Write the request directly into pcap + outfile_name = args.outfile.strip() + if outfile_name != '-': + outfile = open(outfile_name, 'wb') + else: + outfile = sys.stdout.buffer + + pkt = Ether()/IP()/TCP()/HTTP()/req + wrpcap(outfile, pkt) + +if __name__ == '__main__': + sys.exit(main()) |