2 pcap_json_converter converts TiM781S pcap files to json 8 pip install python-pcapng 10 python pcap_json_converter.py --pcap_filename=<filepath.pcapng> 20 from pcapng
import FileScanner
21 from pcapng.blocks
import EnhancedPacket
25 from scapy.layers.l2
import Ether
28 return (b == 0x20)
or (b >= 48
and b <= 57)
or (b >= 65
and b <= 90)
or (b >= 97
and b <= 122)
31 if os.path.isfile(json_filename):
32 json_file = open(json_filename,
"a")
33 json_file.write(
",\n")
35 json_file = open(json_filename,
"a")
36 json_file.write(
"[\n")
37 json_file.write(
" {\n")
38 json_file.write(
" \"_source\": {\n")
39 json_file.write(
" \"layers\": {\n")
40 json_file.write(
" \"tcp\": {\n")
41 json_file.write(
" \"tcp.analysis\": {\n")
42 json_file.write(
" \"tcp.analysis.push_bytes_sent\": \"{}\"\n".format(len(payload)))
43 json_file.write(
" },\n")
44 json_file.write(
" \"Timestamps\": {\n")
45 json_file.write(
" \"tcp.time_relative\": \"{}\"\n".format(relative_timestamp))
46 json_file.write(
" },\n")
47 json_file.write(
" \"tcp.comment\": \"All entries created by pcap_json_converter, not by Wireshark.\",\n")
48 json_file.write(
" \"tcp.producer\": \"{} --pcap_filename={}\",\n".format(os.path.basename(__file__), os.path.basename(pcap_filename)))
49 readable_values = [chr(b)
if isJsonPrintable(b)
else '.' for b
in payload]
50 description =
"".join(readable_values)
51 hex_values = [format(b,
"02x")
for b
in payload]
52 hex_payload =
":".join(hex_values)
53 json_file.write(
" \"tcp.description\": \"{}\",\n".format(description))
54 json_file.write(
" \"tcp.payload\": \"{}\"\n".format(hex_payload))
55 json_file.write(
" }\n")
56 json_file.write(
" }\n")
57 json_file.write(
" }\n")
62 if os.path.isfile(json_filename):
63 json_file = open(json_filename,
"a")
64 json_file.write(
"\n]\n")
68 if os.path.isfile(cpp_filename):
69 cpp_file = open(cpp_filename,
"a")
71 cpp_file = open(cpp_filename,
"a")
72 cpp_file.write(
" std::map<std::string, sick_scan::SickLocColaTelegramMsg> emulator_responses = { // emulator responses to driver requests\n")
73 payload_all = [b
for b
in payload]
75 payload_unpacked = payload_all[1:-1]
77 payload_unpacked = payload_all[8:-1]
81 for n, b
in enumerate(payload_unpacked):
89 sep_idx2 = len(payload_unpacked)
90 cola_type =
"".join([chr(b)
for b
in payload_unpacked[0:sep_idx1]])
91 cola_name =
"".join([chr(b)
for b
in payload_unpacked[sep_idx1+1:sep_idx2]])
92 if sep_idx2+1 < len(payload_unpacked):
93 cola_args =
"".join([format(b,
"02x")
for b
in payload_unpacked[sep_idx2+1:]])
97 cpp_file.write(
" {{\"{}\", sick_scan::ColaParser::createColaTelegram(sick_scan::ColaParser::convertSopasCommand(\"{}\"), \"{}\", {{\"{}\"}})}},\n".format(cola_name, cola_type, cola_name, cola_args));
101 if os.path.isfile(cpp_filename):
102 cpp_file = open(cpp_filename,
"a")
103 cpp_file.write(
" };\n")
109 if len(payload) > 9
and payload.startswith(b
'\x02\x02\x02\x02'):
110 length = (payload[4] << 24) + (payload[5] << 16) + (payload[6] << 8) + (payload[7] << 0)
113 if __name__ ==
"__main__":
115 pcap_filename =
"example.pcapng" 116 arg_parser = argparse.ArgumentParser()
117 arg_parser.add_argument(
"--pcap_filename", help=
"pcapng filepath", default=pcap_filename, type=str)
118 cli_args = arg_parser.parse_args()
119 pcap_filename = cli_args.pcap_filename
120 print(
"pcap_json_converter {} started.".format(pcap_filename))
123 cpp_filename = pcap_filename +
".cpp" 124 json_filename = pcap_filename +
".json" 125 if os.path.isfile(cpp_filename):
126 os.remove(cpp_filename)
127 if os.path.isfile(json_filename):
128 os.remove(json_filename)
129 start_timestamp = -1.0
131 payload_completed =
False 133 with open(pcap_filename,
'rb')
as pcap_file:
134 pcap_scanner = FileScanner(pcap_file)
135 for block_cnt, block
in enumerate(pcap_scanner):
136 if isinstance(block, EnhancedPacket):
139 if block.captured_len != block.packet_len:
140 print(
"## pcap_json_converter block {}: {} byte block truncated to {} bytes".format(block_cnt, block.packet_len, block.captured_len))
141 block_data = Ether(block.packet_data)
142 block_decoded = block_data
143 for n
in range(0,10):
144 if isinstance(block_decoded.payload, scapy.packet.Raw):
146 elif isinstance(block_decoded.payload, scapy.packet.Packet):
147 block_decoded = block_decoded.payload
152 if start_timestamp < 0:
153 start_timestamp = block.timestamp
154 if isinstance(block_decoded.payload, scapy.packet.Raw)
and len(block_decoded.payload) > 0:
155 payload_chunk = bytes(block_decoded.payload)
157 if payload_chunk.startswith(b
'\x02\x02\x02\x02'):
158 relative_timestamp = block.timestamp - start_timestamp
159 payload = payload_chunk
161 elif payload_completed ==
False and payload.startswith(b
'\x02\x02\x02\x02'):
162 payload = payload + payload_chunk
165 elif payload_chunk.startswith(b
'\x02\x73'):
166 relative_timestamp = block.timestamp - start_timestamp
167 payload = payload_chunk
168 payload_completed = (payload.find(b
'\x03') > 0)
169 elif payload_completed ==
False and payload.startswith(b
'\x02\x73'):
170 payload = payload + payload_chunk
171 payload_completed = (payload.find(b
'\x03') > 0)
173 is_cola_ascii = payload.startswith(b
'\x02\x73')
174 is_cola_binary = payload.startswith(b
'\x02\x02\x02\x02')
175 if payload_completed ==
True and (is_cola_binary
or is_cola_ascii):
178 print(
"block {}: rel_timestamp = {}, payload = {}".format(block_cnt, relative_timestamp, payload))
180 payload_completed =
False 184 print(
"pcap_json_converter finished.")
def closeCppfile(cpp_filename)
def parseColaPayloadLength(payload)
def appendToJsonfile(json_filename, relative_timestamp, payload, pcap_filename)
def appendToCppfile(cpp_filename, payload, is_cola_ascii)
def closeJsonfile(json_filename)