-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent_parser.py
More file actions
361 lines (304 loc) · 11.4 KB
/
content_parser.py
File metadata and controls
361 lines (304 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
"""
This program is free software: you can redistribute it under the terms
of the GNU General Public License, v. 3.0. If a copy of the GNU General
Public License was not distributed with this file, see <https://www.gnu.org/licenses/>.
"""
import base64
import struct
from collections import namedtuple
from logutils import get_logger
logger = get_logger(__name__)
FormatSpec = namedtuple("FormatSpec", ["key", "fmt", "decoding"])
def parse_payload(payload: bytes, format_spec: list) -> dict:
"""
Parses a binary payload based on the provided format specification.
Args:
payload (bytes): The binary data to parse.
format_spec (list[FormatSpec]): List of FormatSpec named tuples defining parsing rules.
Returns:
dict: Parsed key-value pairs from the payload.
"""
result, offset = {}, 0
total_len = len(payload)
for spec in format_spec:
fmt = spec.fmt(result) if callable(spec.fmt) else spec.fmt
if isinstance(fmt, int):
fmt = f"{fmt}s"
size = struct.calcsize(fmt)
if offset + size > total_len:
break
(value,) = struct.unpack_from(fmt, payload, offset)
offset += size
if spec.decoding and isinstance(value, (bytes, bytearray)):
value = value.decode(spec.decoding)
result[spec.key] = value
for spec in format_spec:
if spec.key not in result:
default = "" if spec.decoding else b""
result[spec.key] = default
logger.debug("Parsed payload fields: %s", list(result.keys()))
return result
def decode_v0(payload: bytes) -> tuple:
"""Decodes version 0 content.
Args:
payload (bytes): The binary data to decode.
Returns:
tuple: A dictionary of parsed values and an optional error.
"""
parsers = {
0: [
FormatSpec(key="len_public_key", fmt="<i", decoding=None),
FormatSpec(
key="public_key", fmt=lambda d: d["len_public_key"], decoding=None
),
],
1: [
FormatSpec(key="len_auth_code", fmt="<B", decoding=None),
FormatSpec(
key="auth_code", fmt=lambda d: d["len_auth_code"], decoding="utf-8"
),
],
2: [
FormatSpec(
key="len_auth_code",
fmt=1,
decoding=None,
),
FormatSpec(key="len_ciphertext", fmt="<i", decoding=None),
FormatSpec(key="bridge_letter", fmt=1, decoding=None),
FormatSpec(
key="auth_code", fmt=lambda d: d["len_auth_code"], decoding="utf-8"
),
FormatSpec(
key="content_ciphertext",
fmt=lambda d: d["len_ciphertext"],
decoding=None,
),
],
3: [
FormatSpec(key="len_ciphertext", fmt="<i", decoding=None),
FormatSpec(key="bridge_letter", fmt=1, decoding=None),
FormatSpec(
key="content_ciphertext",
fmt=lambda d: d["len_ciphertext"],
decoding=None,
),
],
}
switch_value = payload[0]
logger.debug("Decoding v0 payload with switch value: %s", switch_value)
if switch_value not in parsers:
return None, ValueError(f"Invalid content switch: {switch_value}")
result = parse_payload(payload[1:], parsers[switch_value])
return result, None
def decode_v1(payload: bytes) -> tuple:
"""Decodes version 1 content.
Args:
payload (bytes): The binary data to decode.
Returns:
tuple: A dictionary of parsed values and an optional error.
"""
version = f"v{payload[0] - 9}" if payload[0] == 10 else f"v{payload[0]}"
switch_value = payload[1]
logger.debug(
"Decoding v1 payload - version: %s, switch value: %s", version, switch_value
)
parsers = {
0: [
FormatSpec(key="len_public_key", fmt="<B", decoding=None),
FormatSpec(
key="len_ciphertext",
fmt="<H",
decoding=None,
),
FormatSpec(key="bridge_letter", fmt=1, decoding="ascii"),
FormatSpec(key="skid", fmt="<B", decoding=None),
FormatSpec(
key="public_key",
fmt=lambda d: d["len_public_key"],
decoding=None,
),
FormatSpec(
key="content_ciphertext",
fmt=lambda d: d["len_ciphertext"],
decoding=None,
),
FormatSpec(key="language", fmt=2, decoding="ascii"),
],
1: [
FormatSpec(key="len_ciphertext", fmt="<H", decoding=None),
FormatSpec(key="bridge_letter", fmt=1, decoding="ascii"),
FormatSpec(
key="content_ciphertext",
fmt=lambda d: d["len_ciphertext"],
decoding=None,
),
FormatSpec(key="language", fmt=2, decoding="ascii"),
],
}
if switch_value not in parsers:
return None, ValueError(f"Invalid switch value: {switch_value}")
result = parse_payload(payload[2:], parsers[switch_value])
result["version"] = version
return result, None
def decode_content(content: str) -> tuple:
"""Decodes a base64-encoded content payload.
Args:
content (str): The base64-encoded string to decode.
Returns:
tuple: A dictionary of parsed values and an optional error.
"""
try:
payload = base64.b64decode(content)
version_marker = payload[0]
logger.debug(
"Decoded base64 content - version marker: %s, payload length: %s",
version_marker,
len(payload),
)
if version_marker not in [2, 3, 10]:
return None, f"Unsupported version marker: {version_marker}"
return decode_v1(payload)
except Exception as e:
return None, e
def extract_content(bridge_name: str, content: str) -> tuple:
"""Extracts components based on the specified bridge name.
Args:
bridge_name (str): The bridge identifier.
content (str): The content string to extract from.
Returns:
tuple: A tuple of extracted values or an error message.
"""
if bridge_name == "email_bridge":
parts = content.split(":", 4)
if len(parts) != 5:
return None, "Email content must have exactly 5 parts."
return tuple(parts), None
return None, "Invalid bridge name."
def extract_content_v2(bridge_name: str, content: bytes, image_length: int) -> tuple:
"""Extracts components based on the specified bridge name for v2.
Args:
bridge_name (str): The bridge identifier.
content (bytes): The binary content to extract from.
image_length (int): The length of the image data at the start of the content.
Returns:
tuple: A tuple of extracted values or an error message.
"""
if bridge_name != "email_bridge":
return None, "Invalid bridge name."
image_data = content[:image_length]
text_content = content[image_length:]
logger.debug("Extracting v2 email content from %s bytes", len(content))
format_spec = [
FormatSpec(key="length_to", fmt="<H", decoding=None),
FormatSpec(key="length_cc", fmt="<H", decoding=None),
FormatSpec(key="length_bcc", fmt="<H", decoding=None),
FormatSpec(key="length_subject", fmt="<B", decoding=None),
FormatSpec(key="length_body", fmt="<H", decoding=None),
FormatSpec(key="to", fmt=lambda d: d["length_to"], decoding="utf-8"),
FormatSpec(key="cc", fmt=lambda d: d["length_cc"], decoding="utf-8"),
FormatSpec(key="bcc", fmt=lambda d: d["length_bcc"], decoding="utf-8"),
FormatSpec(key="subject", fmt=lambda d: d["length_subject"], decoding="utf-8"),
FormatSpec(key="body", fmt=lambda d: d["length_body"], decoding="utf-8"),
]
try:
result = parse_payload(text_content, format_spec)
extracted = {
"to": result.get("to", ""),
"cc": result.get("cc", ""),
"bcc": result.get("bcc", ""),
"subject": result.get("subject", ""),
"body": result.get("body", ""),
"image": image_data,
}
logger.debug(
"Extracted v2 email fields - \nto: %s\ncc: %s\nbcc: %s\nsubject: %s"
"\nbody length: %s\nimage length: %s\n body: %s\nimage: %s",
extracted["to"],
extracted["cc"],
extracted["bcc"],
extracted["subject"],
len(extracted["body"]),
len(extracted["image"]),
extracted["body"],
extracted["image"],
)
return extracted, None
except Exception as e:
return None, e
def extract_content_v3(bridge_name: str, content: bytes) -> tuple:
"""Extracts components based on the specified bridge name for v3.
Args:
bridge_name (str): The bridge identifier.
content (bytes): The binary content to extract from.
Returns:
tuple: A tuple of extracted values or an error message.
"""
if bridge_name != "email_bridge":
return None, "Invalid bridge name."
logger.debug("Extracting v3 email content from %s bytes", len(content))
bitmap = content[0]
logger.debug("v3 bitmap value: %s (binary: %s)", bitmap, format(bitmap, "08b"))
fields_present = {
"cc": bool(bitmap & 0b00000001),
"bcc": bool(bitmap & 0b00000010),
}
logger.debug(
"v3 optional fields - cc present: %s, bcc present: %s",
fields_present["cc"],
fields_present["bcc"],
)
format_spec = [
FormatSpec(key="length_to", fmt="<H", decoding=None),
]
if fields_present["cc"]:
format_spec.append(FormatSpec(key="length_cc", fmt="<H", decoding=None))
if fields_present["bcc"]:
format_spec.append(FormatSpec(key="length_bcc", fmt="<H", decoding=None))
format_spec.extend(
[
FormatSpec(key="length_subject", fmt="<B", decoding=None),
FormatSpec(key="length_body", fmt="<H", decoding=None),
]
)
format_spec.append(
FormatSpec(key="to", fmt=lambda d: d["length_to"], decoding="utf-8")
)
if fields_present["cc"]:
format_spec.append(
FormatSpec(key="cc", fmt=lambda d: d["length_cc"], decoding="utf-8")
)
if fields_present["bcc"]:
format_spec.append(
FormatSpec(key="bcc", fmt=lambda d: d["length_bcc"], decoding="utf-8")
)
format_spec.extend(
[
FormatSpec(
key="subject", fmt=lambda d: d["length_subject"], decoding="utf-8"
),
FormatSpec(key="body", fmt=lambda d: d["length_body"], decoding="utf-8"),
]
)
try:
result = parse_payload(content[1:], format_spec)
extracted = {
"to": result.get("to", ""),
"cc": result.get("cc", ""),
"bcc": result.get("bcc", ""),
"subject": result.get("subject", ""),
"body": result.get("body", ""),
}
logger.debug(
"Extracted v3 email fields - \nto: %s\ncc: %s\nbcc: %s"
"\nsubject: %s\nbody length: %s\nbody: %s",
extracted["to"],
extracted["cc"],
extracted["bcc"],
extracted["subject"],
len(extracted["body"]),
extracted["body"],
)
return extracted, None
except Exception as e:
return None, e