-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathread_headers.py
230 lines (177 loc) · 6.75 KB
/
read_headers.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
import dataclasses
import enum
from typing import cast
import mmap
import os
import struct
import sys
import time
from pathlib import Path
class AAEntryTypes(enum.IntEnum):
REGULAR = ord("F")
DIRECTORY = ord("D")
SYMBOLIC_LINK = ord("L")
FIFO_SPECIAL = ord("P")
CHARACTER_SPECIAL = ord("C")
BLOCK_SPECIAL = ord("B")
SOCKET = ord("S")
WHITEOUT = ord("W")
DOOR = ord("R")
PORT = ord("T")
METADATA = ord("M")
def __str__(self):
return self.name
def __repr__(self):
return self.name
class AAYopTypes(enum.IntEnum):
COPY = ord("C")
EXTRACT = ord("E")
SRC_CHECK = ord("I")
MANIFEST = ord("M")
DST_FIXUP = ord("O")
PATCH = ord("P")
REMOVE = ord("R")
def __str__(self):
return self.name
def __repr__(self):
return self.name
class AAFieldTypes(enum.IntEnum):
FLAG = 0
UINT = 1
STRING = 2
HASH = 3
TIMESPEC = 4
BLOB = 5
@dataclasses.dataclass(frozen=True)
class AAFieldKey:
name: str
type: AAFieldTypes
def __str__(self):
return self.name
def __repr__(self):
return self.name
class AAFieldKeys(enum.Enum):
TYPE = AAFieldKey("TYP", AAFieldTypes.UINT)
YOP = AAFieldKey("YOP", AAFieldTypes.UINT)
LABEL = AAFieldKey("LBL", AAFieldTypes.STRING)
DATA = AAFieldKey("DAT", AAFieldTypes.BLOB)
SIZE = AAFieldKey("SIZ", AAFieldTypes.UINT)
ENTRY_OFFSET = AAFieldKey("IDX", AAFieldTypes.UINT)
ENTRY_SIZE = AAFieldKey("IDZ", AAFieldTypes.UINT)
PATH = AAFieldKey("PAT", AAFieldTypes.STRING)
LINK_PATH = AAFieldKey("LNK", AAFieldTypes.STRING)
FLAGS = AAFieldKey("FLG", AAFieldTypes.UINT)
UID = AAFieldKey("UID", AAFieldTypes.UINT)
GID = AAFieldKey("GID", AAFieldTypes.UINT)
MODE = AAFieldKey("MOD", AAFieldTypes.UINT)
MODIFICATION_TIME = AAFieldKey("MTM", AAFieldTypes.TIMESPEC)
CREATION_TIME = AAFieldKey("CTM", AAFieldTypes.TIMESPEC)
@classmethod
def _missing_(cls, value):
if isinstance(value, str):
for member in cls:
if member.value.name == value:
return member
return super()._missing_(value)
class MemoryviewStream:
def __init__(self, view: memoryview):
self.view = view
self.size = len(view)
self.idx = 0
def seek(self, offset: int, whence: int) -> int:
if whence == os.SEEK_SET:
self.idx = offset
elif whence == os.SEEK_CUR:
self.idx += offset
elif whence == os.SEEK_END:
self.idx = self.size + offset
else:
raise ValueError(f"Invalid whence: {whence}")
if self.idx < 0:
self.idx = 0
if self.idx > self.size:
self.idx = self.size
return self.idx
def read(self, n: int) -> bytes:
if self.idx + n > self.size:
raise ValueError(f"Attempted to read past end of stream: {self.idx + n} > {self.size}")
with self.view[self.idx : self.idx + n] as subview:
if len(subview) != n:
raise ValueError(f"Expected {n} bytes, got {len(subview)}")
result = subview.tobytes()
self.idx += n
return result
def readable(self) -> bool:
return self.idx < self.size
def close(self) -> None:
self.view.release()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
# path = Path("tests/ia/decrypt_attempt.aar")
path = Path(sys.argv[1])
MAGIC_AND_HEADER = "<4sH"
def scan_range(view: memoryview, indent="", offset=0):
with view:
idx = 0
while idx < len(view):
start = idx
magic, header_size = struct.unpack_from(MAGIC_AND_HEADER, view, idx)
if magic != b"AA01":
raise ValueError(f"Invalid magic at {idx}: {magic}")
assert header_size >= 6
header = view[start : start + header_size]
fields = MemoryviewStream(view[idx : idx + header_size])
idx += header_size
blob_size = 0
with header, fields:
parsed_fields = {}
fields.seek(struct.calcsize(MAGIC_AND_HEADER), os.SEEK_CUR)
while fields.readable():
KEY_AND_SUBTYPE = "<3sc"
key, subtype = struct.unpack_from(KEY_AND_SUBTYPE, fields.read(4))
key = AAFieldKeys(key.decode()).value
subtype = cast(bytes, subtype).decode()
# if key in [AAFieldKeys.TYPE, AAFieldKeys.YOP]:
if key.type == AAFieldTypes.UINT:
# Int types
size = int(subtype)
assert size in [1, 2, 4, 8]
val = int.from_bytes(fields.read(size), "little")
if key == AAFieldKeys.TYPE.value:
val = AAEntryTypes(val)
elif key == AAFieldKeys.YOP.value:
val = AAYopTypes(val)
elif key.type == AAFieldTypes.STRING:
assert subtype == "P"
size = int.from_bytes(fields.read(2), "little")
val = fields.read(size).decode()
elif key.type == AAFieldTypes.BLOB:
value_size = {"A": 2, "B": 4, "C": 8}[subtype]
size = int.from_bytes(fields.read(value_size), "little")
val = (idx, size)
idx += size
blob_size += size
elif key.type == AAFieldTypes.TIMESPEC:
# TODO: Figure out how to represent this
size = {"S": 8, "T": 12}[subtype]
val = int.from_bytes(fields.read(size), "little")
else:
raise ValueError(f"Unknown key type: {key.type}")
parsed_fields[key] = val
print(f"{indent}{start + offset:#010x}: {parsed_fields}")
if AAFieldKeys.DATA.value in parsed_fields:
blob_start, blob_size = parsed_fields[AAFieldKeys.DATA.value]
with view[blob_start : blob_start + 4] as inner_magic:
# TODO: This does not handle compressed blobs
if inner_magic == b"AA01":
scan_range(view[blob_start : blob_start + blob_size], indent + " ", offset + blob_start)
assert start + header_size + blob_size == idx
def main():
with path.open("rb") as f:
with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm:
scan_range(memoryview(mm))
time.sleep(1)
if __name__ == "__main__":
main()