-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathauto.py
318 lines (262 loc) · 9.22 KB
/
auto.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
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
import argparse
import zcu
from zcu.known_keys import KNOWN_KEYS, KNOWN_SIGNATURES
from zcu.xcryptors import Xcryptor, CBCXcryptor
from zcu.known_keys import mac_to_str
KNOWN_KEYPAIR_SUFFIXES = [
("", ""), # e.g. type 3
("key", "IV"),
("Key02660004", "Iv02660004"),
("Key02710001", "Iv02710001"),
("Key02710010", "Iv02710010"),
("Key02721401", "Iv02721401"),
("8cc72b05705d5c46f412af8cbed55aa", "667b02a85c61c786def4521b060265e"),
]
KNOWN_KEYPAIR_PREFIXES = [
("", ""),
("8cc72b05705d5c46", "667b02a85c61c786"),
("8dc79b15726d5c46", "678b02a85c63c786"),
]
KNOWN_KEYPAIRS = [
("H267AV1_CZkey", "H267AV1_CZIV"),
("8cc72b05705d5c46f412af8cbed55aad", "667b02a85c61c786def4521b060265e8"),
("8dc79b15726d5c46d412af8cbed65aad", "678b02a85c63c786def4523b061265e8"),
]
KNOWN_PASSWORD_KEYPAIR_SUFFIXES = [
("", ""),
("Mcd5c46e", "G21b667b"),
]
KNOWN_MAC_SERIAL_IVS = [
"ZTE%FN$GponNJ025",
]
def hardcoded_keypairs(args):
return [(k, None) for k in KNOWN_KEYS]
def signature_keypairs(args):
keypairs = []
if args.key and args.iv:
keypairs += [(args.key, args.iv)]
signatures = []
if args.signature:
signatures += [
args.signature,
args.signature.replace(" ", ""),
]
signatures += KNOWN_SIGNATURES
for signature in signatures:
if args.key_suffix and args.iv_suffix:
keypairs += [
(f"{signature}{args.key_suffix}", f"{signature}{args.iv_suffix}")
]
keypairs += [
(f"{signature}{key}", f"{signature}{iv}")
for (key, iv) in KNOWN_KEYPAIR_SUFFIXES
]
keypairs += [(key, iv) for (key, iv) in KNOWN_KEYPAIRS]
return keypairs
def serial_keypairs(args):
keypairs = []
if args.serial_number is None:
print("To decode any 'serial' payloads, please specify Serial Number, e.g.")
print(" --serial 'SERIALNUMBER'")
return keypairs
serial = args.serial_number
keypair_prefixes = []
if args.key_prefix and args.iv_prefix:
keypair_prefixes += [
(args.key_prefix, args.iv_prefix),
]
keypair_prefixes += KNOWN_KEYPAIR_PREFIXES
keypairs += [(f"{key}{serial}", f"{iv}{serial}") for (key, iv) in keypair_prefixes]
return keypairs
def mac_serial_keypairs(args):
keypairs = []
if args.mac_address is None or args.serial_number is None:
print(
"To decode any 'mac+serial' payloads, please specify MAC Address and Serial Number, e.g."
)
print(" --mac 'AA:BB:CC:DD:EE:FF' --serial 'SERIALNUMBER'")
return keypairs
serial = args.serial_number
mac = args.mac_address
for iv in KNOWN_MAC_SERIAL_IVS:
keypairs += [
# raw serial
(serial + mac_to_str(mac, reverse=False, separator=""), iv),
(serial + mac_to_str(mac, reverse=True, separator=""), iv),
(serial + mac_to_str(mac, reverse=False, separator=":"), iv),
(serial + mac_to_str(mac, reverse=True, separator=":"), iv),
# skip first 4 chars, e.g. ZTEGXXXXXXXX
(serial[4:] + mac_to_str(mac, reverse=False, separator=""), iv),
(serial[4:] + mac_to_str(mac, reverse=True, separator=""), iv),
(serial[4:] + mac_to_str(mac, reverse=False, separator=":"), iv),
(serial[4:] + mac_to_str(mac, reverse=True, separator=":"), iv),
# take last 8 chars, e.g. ZTEGXXXXXXXX
(serial[-8:] + mac_to_str(mac, reverse=False, separator=""), iv),
(serial[-8:] + mac_to_str(mac, reverse=True, separator=""), iv),
(serial[-8:] + mac_to_str(mac, reverse=False, separator=":"), iv),
(serial[-8:] + mac_to_str(mac, reverse=True, separator=":"), iv),
]
# # convert first 8 hex chars to ascii
# if all([x in "0123456789abcdef" for x in serial[:8].lower()]):
# ascii_serial = bytearray.fromhex(serial[:8]).decode()
# keypairs += [
# (ascii_serial + mac_to_str(mac, reverse=False, separator=""), iv),
# (ascii_serial + mac_to_str(mac, reverse=True, separator=""), iv),
# (ascii_serial + mac_to_str(mac, reverse=False, separator=":"), iv),
# (ascii_serial + mac_to_str(mac, reverse=True, separator=":"), iv),
# ]
return keypairs
def mac_serial_password_keypairs(args):
# NOTE: "suffix" is a misnomer here:
# key is PASSWORD|SERIAL|SUFFIX
# iv is PREFIX|MAC|PASSWORD
keypairs = []
if args.mac_address is None or args.serial_number is None or args.password is None:
print(
"To decode any 'mac+serial+password' payloads, please specify MAC Address, Serial Number and Password parameters, e.g."
)
print(
" --mac 'AA:BB:CC:DD:EE:FF' --serial 'SERIALNUMBER' --password 'password'"
)
return keypairs
mac = mac_to_str(args.mac_address, reverse=False, separator=":")
serial = args.serial_number
password = args.password
if args.key_suffix and args.iv_suffix:
keypairs += [
(f"{password}{serial}{args.key_suffix}", f"{args.iv_suffix}{mac}{password}")
]
keypairs += [
(f"{password}{serial}{key}", f"{iv}{mac}{password}")
for (key, iv) in KNOWN_PASSWORD_KEYPAIR_SUFFIXES
]
return keypairs
def decrypt(infile, decryptor, keypair):
start_pos = infile.tell()
decryptor.set_key(*keypair)
try:
decrypted = decryptor.decrypt(infile)
except ValueError:
infile.seek(start_pos)
return None
infile.seek(start_pos)
if decrypted is not None:
if zcu.zte.read_payload_type(decrypted, raise_on_error=False) is not None:
return decrypted
return None
HANDLERS = [
lambda a: (hardcoded_keypairs(a), Xcryptor()),
lambda a: (signature_keypairs(a), CBCXcryptor()), # requires signature
lambda a: (serial_keypairs(a), CBCXcryptor()), # requires serial
lambda a: (mac_serial_keypairs(a), CBCXcryptor()), # requires mac, serial
lambda a: (
mac_serial_password_keypairs(a),
CBCXcryptor(),
), # requires mac, serial, password
]
def main():
parser = argparse.ArgumentParser()
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=str,
help="Supply a Key to try",
)
parser.add_argument(
"--iv",
type=str,
help="Supply a IV to try",
)
parser.add_argument(
"--signature",
type=str,
help="Supply/override Signature for Type-4 key generation",
)
parser.add_argument(
"--serial-number",
type=str,
help="Supply Serial Number of device",
)
parser.add_argument(
"--mac-address",
type=str,
help="Supply MAC Address of device, e.g. AA:BB:CC:DD:EE:FF",
)
parser.add_argument(
"--password",
type=str,
help="Supply Long password from TagParams (entry 4100) for key generation",
)
parser.add_argument(
"--key-prefix",
type=str,
help="Supply Key Prefix",
)
parser.add_argument(
"--key-suffix",
type=str,
help="Supply Key Suffix",
)
parser.add_argument(
"--iv-prefix",
type=str,
help="Supply IV Prefix",
)
parser.add_argument(
"--iv-suffix",
type=str,
help="Supply IV Suffix",
)
args = parser.parse_args()
infile = args.infile
# check magic
header = infile.read(4)
if header == b"BAMC":
print(f"ERROR: {infile.name} is base64 encoded, please decode and try again.")
return 1
infile.seek(0)
zcu.zte.read_header(infile, little_endian=args.little_endian)
signature = zcu.zte.read_signature(infile).decode()
if args.signature is None:
args.signature = signature
payload_type = zcu.zte.read_payload_type(infile)
if payload_type != 0:
for handler in HANDLERS:
success = False
keypairs, decryptor = handler(args)
for keypair in keypairs:
# print(f"Trying (key, iv): {keypair}")
decrypted = decrypt(infile, decryptor, keypair)
if decrypted is not None:
success = True
break
if success:
break
else:
print("Unable to find valid key for payload.")
return 1
else:
decompressed, _ = zcu.compression.decompress(infile)
args.outfile.write(decompressed.read())
print(f"Successfully decompressed {infile.name}")
return 0
decompressed, _ = zcu.compression.decompress(decrypted)
args.outfile.write(decompressed.read())
print(
f"Successfully decrypted and decompressed {infile.name} using (key, iv): {keypair}"
)
return 0
if __name__ == "__main__":
main()