-
Notifications
You must be signed in to change notification settings - Fork 86
/
extractodx.py
225 lines (192 loc) · 6.15 KB
/
extractodx.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
import argparse
import binascii
from lib import legacysimos
from pathlib import Path
import os
import xml.etree.ElementTree as ET
from lib import constants
from lib.modules import (
simos8,
simos10,
simos18,
simos1810,
simos184,
dq250mqb,
simos16,
simos122,
dq381,
)
def bits(byte):
return (
(byte >> 7) & 1,
(byte >> 6) & 1,
(byte >> 5) & 1,
(byte >> 4) & 1,
(byte >> 3) & 1,
(byte >> 2) & 1,
(byte >> 1) & 1,
(byte) & 1,
)
def decompress_raw_lzss10(indata, decompressed_size):
"""Decompress LZSS-compressed bytes. Returns a bytearray."""
data = bytearray()
it = iter(indata)
def writebyte(b):
data.append(b)
def readbyte():
return next(it)
def readshort():
# big-endian
a = next(it)
b = next(it)
return (a << 8) | b
def copybyte():
data.append(next(it))
while len(data) < decompressed_size:
b = readbyte()
flags = bits(b)
for flag in flags:
if flag == 0:
copybyte()
elif flag == 1:
sh = readshort()
count = sh >> 10
disp = sh & 0x3FF
for _ in range(count):
writebyte(data[-disp])
else:
raise ValueError(flag)
if decompressed_size <= len(data):
break
return data
def extract_odx(odx_string, flash_info: constants.FlashInfo, is_dsg=False):
root = ET.fromstring(odx_string)
flashdata = root.findall("./FLASH/ECU-MEMS/ECU-MEM/MEM/FLASHDATAS/FLASHDATA")
boxcodes = root.findall(
"./FLASH/ECU-MEMS/ECU-MEM/MEM/SESSIONS/SESSION/EXPECTED-IDENTS/EXPECTED-IDENT/IDENT-VALUES/IDENT-VALUE"
)
all_data = {}
allowed_boxcodes = []
for boxcode in boxcodes:
allowed_boxcodes.append(boxcode.text.rstrip())
for data in flashdata:
dataContent = data.findall("./DATA")[0].text
encryptionCompressionType = data.findall("./ENCRYPT-COMPRESS-METHOD")[0].text
compressionType = encryptionCompressionType[0]
encryptionType = encryptionCompressionType[1]
dataId = data.get("ID")
length = int(
root.findall(
"./FLASH/ECU-MEMS/ECU-MEM/MEM/DATABLOCKS/DATABLOCK/FLASHDATA-REF[@ID-REF='{}']/../SEGMENTS/SEGMENT/UNCOMPRESSED-SIZE".format(
dataId
)
)[0].text
)
if len(dataContent) == 2:
# These are ERASE blocks with no data so skip them
continue
dataBinary = binascii.unhexlify(dataContent)
if encryptionType == "0":
decryptedContent = dataBinary
else:
decryptedContent = flash_info.crypto.decrypt(dataBinary)
if compressionType == "A" or compressionType == "a" or is_dsg:
decompressedContent = decompress_raw_lzss10(decryptedContent, length)
elif compressionType == "1":
decompressedContent = legacysimos.decompress(decryptedContent)
else:
decompressedContent = decryptedContent
all_data[data[0].text] = decompressedContent
return (all_data, allowed_boxcodes)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Decrypt and decompress flash blocks from an ODX file using Simos18.1 or Simos12 AES keys.",
epilog="For example, --file test.odx --outdir test",
)
parser.add_argument("--file", type=str, help="ODX file input", required=True)
parser.add_argument(
"--simos8",
dest="simos8",
action="store_true",
default=False,
help="(optional) use Simos8 compression",
)
parser.add_argument(
"--simos10",
dest="simos10",
action="store_true",
default=False,
help="(optional) use Simos10 XOR encryption and compression",
)
parser.add_argument(
"--simos1810",
dest="simos1810",
action="store_true",
default=False,
help="(optional) use known Simos18.10 AES keys instead of Simos18.1/18.6",
)
parser.add_argument(
"--simos1841",
dest="simos1841",
action="store_true",
default=False,
help="(optional) use known Simos18.41 AES keys instead of Simos18.1/18.6",
)
parser.add_argument(
"--simos122",
dest="simos122",
action="store_true",
default=False,
help="(optional) use known Simos12.2 AES keys instead of Simos18.1/18.6",
)
parser.add_argument(
"--simos16",
dest="simos16",
action="store_true",
default=False,
help="(optional) use known Simos16 AES keys instead of Simos18.1/18.6",
)
parser.add_argument(
"--dsg",
dest="dsg",
action="store_true",
default=False,
help="(optional) use DSG decryption algorithm",
)
parser.add_argument(
"--dq381",
dest="dq381",
action="store_true",
default=False,
help="(optional) use DSG decryption algorithm",
)
parser.add_argument(
"--outdir",
type=str,
default="",
help="(optional) output directory, otherwise files will be output to current directory",
)
args = parser.parse_args()
flash_info = simos18.s18_flash_info
if args.simos122:
flash_info = simos122.s122_flash_info
if args.simos1810:
flash_info = simos1810.s1810_flash_info
if args.simos1841:
flash_info = simos184.s1841_flash_info
if args.simos16:
flash_info = simos16.s16_flash_info
if args.simos10:
flash_info = simos10.s10_flash_info
if args.simos8:
flash_info = simos8.s8_flash_info
if args.dq381:
flash_info = dq381.dsg_flash_info
if args.dsg:
flash_info = dq250mqb.dsg_flash_info
file_data = Path(args.file).read_text()
(data_blocks, allowed_boxcodes) = extract_odx(file_data, flash_info, args.dsg)
for data_block in data_blocks:
print(data_block)
with open(os.path.join(args.outdir, data_block), "wb") as dataFile:
dataFile.write(data_blocks[data_block])