-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathdecode.py
282 lines (226 loc) · 7.96 KB
/
decode.py
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
"""Decode config.bin into config.xml"""
import argparse
import sys
from types import SimpleNamespace
import zcu
from zcu.xcryptors import Xcryptor, CBCXcryptor
from zcu.known_keys import TYPE_3_KNOWN_KEY_IVS
def error(msg):
print(msg, file=sys.stderr)
def try_decode_payload_type_0(infile, args, params):
print("Trying to decode Type 0 payload...")
# no decryption required
return (infile, None)
def try_decode_payload_type_2(infile, args, params):
print("Trying to decode Type 2 payload...")
keys = set()
if hasattr(params, "key"):
keys.add(params.key)
elif hasattr(params, "signature"):
key_for_signature = zcu.known_keys.find_key(params.signature)
if key_for_signature is not None:
keys.add(key_for_signature)
if args.try_all_known_keys:
for key in zcu.known_keys.get_all_keys():
keys.add(key)
if len(keys) == 0:
error(
"No --key specified or found via signature, try again --try-all-known-keys."
)
return None
decryptor = Xcryptor()
start_pos = infile.tell()
for key in keys:
infile.seek(start_pos)
if len(keys) > 1:
print(f"Trying key: '{key}'")
decryptor.set_key(key)
decrypted = decryptor.decrypt(infile)
if zcu.zte.read_payload_type(decrypted, raise_on_error=False) is not None:
return (decrypted, key)
error(f"Failed to decrypt payload. Tried {len(keys)} key(s)!")
return None
def try_decode_payload_type_3(infile, args, params):
print("Trying to decode Type 3 payload...")
models = []
if hasattr(params, "model"):
models.append(params.model)
if args.try_all_known_keys:
models.extend(zcu.known_keys.get_all_models())
key_ivs = [(m, m, f"Model {m}") for m in models]
if args.try_all_known_keys:
key_ivs.extend(TYPE_3_KNOWN_KEY_IVS)
if len(key_ivs) == 0:
error(
"Failed to decrypt payload. No keys found! Try specifying --model and/or --try-all-known-keys and try again."
)
return None
decryptor = CBCXcryptor()
start_pos = infile.tell()
for key, iv, name in key_ivs:
infile.seek(start_pos)
if len(models) > 1:
print(f"Trying key: {name}")
decryptor.set_key(key, iv)
decrypted = decryptor.decrypt(infile)
if zcu.zte.read_payload_type(decrypted, raise_on_error=False) is not None:
return (decrypted, key)
error(f"Failed to decrypt payload. Tried {len(models)} model name(s)!")
return None
def try_decode_payload_type_4(infile, args, params):
print("Trying to decode Type 4 payload...")
if args.try_all_known_keys:
key_ivs = zcu.known_keys.run_all_keygens(params)
else:
key_ivs = zcu.known_keys.run_keygens(params)
if len(key_ivs) == 0:
msg = "No keygens matched the supplied/detected signature and parameters! Try adding --try-all-known-keys"
if not hasattr(params, "serial"):
msg += " or --serial YOUR_SERIAL_NUMBER"
msg += " and try again."
error(msg)
return None
decryptor = CBCXcryptor()
start_pos = infile.tell()
for key, iv, source in key_ivs:
infile.seek(start_pos)
print(f"Trying key: '{key}' iv: '{iv}' generated from {source}")
decryptor.set_key(key, iv)
decrypted = decryptor.decrypt(infile)
if zcu.zte.read_payload_type(decrypted, raise_on_error=False) is not None:
return (decrypted, key)
error(f"Failed to decrypt payload. Tried {len(key_ivs)} generated key(s)!")
return None
def main():
"""the main function"""
parser = argparse.ArgumentParser(
description="Decode config.bin from ZTE Routers",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"infile",
type=argparse.FileType("rb"),
help="Encoded configuration file e.g. config.bin",
)
parser.add_argument(
"outfile", type=argparse.FileType("wb"), help="Output file e.g. config.xml"
)
parser.add_argument(
"--little-endian",
action="store_true",
help="Whether payload is little-endian (defaults to big-endian)",
)
parser.add_argument(
"--key", type=lambda x: x.encode(), default=b"", help="Key for AES decryption"
)
parser.add_argument(
"--model", type=str, default="", help="Device model for Type-3 key derivation"
)
parser.add_argument(
"--serial",
type=str,
default="",
help="Serial number for Type-4 key generation (digimobil routers/tagparams based)",
)
parser.add_argument(
"--mac",
type=str,
default="",
help="MAC address for TagParams-based key generation",
)
parser.add_argument(
"--longpass",
type=str,
default="",
help="Long password from TagParams (entry 4100) for key generation",
)
parser.add_argument(
"--signature",
type=str,
default="",
help="Supply/override signature for Type-4 key generation",
)
parser.add_argument(
"--try-all-known-keys",
action="store_true",
help="Try decrypting with all known keys and generators (default No)",
)
parser.add_argument(
"--key-prefix",
type=str,
default="",
help="Override Key prefix for Serial/TagParams based key generation",
)
parser.add_argument(
"--iv-prefix",
type=str,
default="",
help="Override IV prefix for Serial/TagParams based key generation",
)
parser.add_argument(
"--key-suffix",
type=str,
default="",
help="Override Key suffix for Signature based key generation",
)
parser.add_argument(
"--iv-suffix",
type=str,
default="",
help="Override IV suffix for Signature based key generation",
)
args = parser.parse_args()
infile = args.infile
outfile = args.outfile
zcu.zte.read_header(infile, little_endian=args.little_endian)
signature = zcu.zte.read_signature(infile).decode()
if signature:
print(f"Detected signature: {signature}")
payload_type = zcu.zte.read_payload_type(infile)
print(f"Detected payload type {payload_type}")
params = SimpleNamespace()
if args.signature:
params.signature = args.signature
else:
params.signature = signature
if args.key:
params.key = args.key
if args.model:
params.model = args.model
if args.serial:
params.serial = args.serial if (args.serial != "NONE") else ""
if args.mac:
params.mac = args.mac if (args.mac != "NONE") else ""
if args.longpass:
params.longPass = args.longpass if (args.longpass != "NONE") else ""
if args.key_prefix:
params.key_prefix = args.key_prefix if (args.key_prefix != "NONE") else ""
if args.key_suffix:
params.key_suffix = args.key_suffix if (args.key_suffix != "NONE") else ""
if args.iv_prefix:
params.iv_prefix = args.iv_prefix if (args.iv_prefix != "NONE") else ""
if args.iv_suffix:
params.iv_suffix = args.iv_suffix if (args.iv_suffix != "NONE") else ""
if payload_type == 0:
res = try_decode_payload_type_0(infile, args, params)
elif payload_type == 2:
res = try_decode_payload_type_2(infile, args, params)
elif payload_type == 3:
res = try_decode_payload_type_3(infile, args, params)
elif payload_type == 4:
res = try_decode_payload_type_4(infile, args, params)
else:
error(f"No support for payload type {payload_type}!")
return 1
if res is None:
return 1
decrypted, key = res
decompressed, _ = zcu.compression.decompress(decrypted)
outfile.write(decompressed.read())
if key is not None:
print(f"Successfully decoded using key: '{key}'")
else:
print("Successfully decoded")
return 0
if __name__ == "__main__":
main()