LATER... ei_kerberos_kdc_session_key ...
[wireshark-sm.git] / tools / generate_cbor_pcap.py
blob545b985eb18bcec78c8eee788b3a10effa2e82b1
1 #!/usr/bin/env python3
2 '''
3 Convert a CBOR diagnostic notation file into an HTTP request
4 for the encoded cbor.
5 This allows straightforward test and debugging of simple pcap files.
7 Copyright 2021 Brian Sipos <brian.sipos@gmail.com>
9 SPDX-License-Identifier: LGPL-2.1-or-later
10 '''
12 from argparse import ArgumentParser
13 from io import BytesIO
14 import scapy
15 from scapy.layers.l2 import Ether
16 from scapy.layers.inet import IP, TCP
17 from scapy.layers.http import HTTP, HTTPRequest
18 from scapy.packet import Raw
19 from scapy.utils import wrpcap
20 from subprocess import check_output
21 import sys
24 def main():
25 parser = ArgumentParser()
26 parser.add_argument('--content-type', default='application/cbor',
27 help='The request content-type header')
28 parser.add_argument('--infile', default='-',
29 help='The diagnostic text input file, or "-" for stdin')
30 parser.add_argument('--outfile', default='-',
31 help='The PCAP output file, or "-" for stdout')
32 parser.add_argument('--intype', default='cbordiag',
33 choices=['cbordiag', 'raw'],
34 help='The input data type.')
35 args = parser.parse_args()
37 # First get the CBOR data itself
38 infile_name = args.infile.strip()
39 if infile_name != '-':
40 infile = open(infile_name, 'rb')
41 else:
42 infile = sys.stdin.buffer
44 if args.intype == 'raw':
45 cbordata = infile.read()
46 elif args.intype == 'cbordiag':
47 cbordata = check_output('diag2cbor.rb', stdin=infile)
49 # Now synthesize an HTTP request with that body
50 req = HTTPRequest(
51 Method='POST',
52 Host='example.com',
53 User_Agent='scapy',
54 Content_Type=args.content_type,
55 Content_Length=str(len(cbordata)),
56 ) / Raw(cbordata)
58 # Write the request directly into pcap
59 outfile_name = args.outfile.strip()
60 if outfile_name != '-':
61 outfile = open(outfile_name, 'wb')
62 else:
63 outfile = sys.stdout.buffer
65 pkt = Ether()/IP()/TCP()/HTTP()/req
66 wrpcap(outfile, pkt)
68 if __name__ == '__main__':
69 sys.exit(main())