-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicFileInfo_Decode_V4.py
More file actions
144 lines (118 loc) · 3.59 KB
/
Copy pathBasicFileInfo_Decode_V4.py
File metadata and controls
144 lines (118 loc) · 3.59 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
import argparse
import pathlib
import re
import sys
STDOUT_ENCODING = sys.stdout.encoding or 'utf-8'
def safe_print(text: str = ""):
try:
print(text)
except UnicodeEncodeError:
safe = text.encode(STDOUT_ENCODING, errors="backslashreplace").decode(
STDOUT_ENCODING, errors="backslashreplace"
)
print(safe)
def extract_utf16le_strings(blob: bytes, min_len: int = 4):
results = []
current = []
def flush():
if len(current) >= min_len:
results.append("".join(current))
current.clear()
i = 0
while i + 1 < len(blob):
ch = blob[i]
nul = blob[i + 1]
if 32 <= ch <= 126 and nul == 0:
current.append(chr(ch))
else:
flush()
i += 2
flush()
return results
def decode_length_prefixed_utf16le(blob: bytes, max_len: int = 4096):
results = []
i = 0
while i + 4 <= len(blob):
length = int.from_bytes(blob[i : i + 4], "little")
i += 4
if length == 0:
results.append("")
continue
if length > max_len:
break
byte_len = length * 2
if i + byte_len > len(blob):
break
chunk = blob[i : i + byte_len]
i += byte_len
try:
s = chunk.decode("utf-16-le", errors="strict")
except UnicodeDecodeError:
s = chunk.decode("utf-16-le", errors="ignore")
printable = sum(1 for c in s if 32 <= ord(c) <= 126)
ratio = printable / max(1, len(s))
if ratio >= 0.6:
results.append(s)
else:
results.append(s)
return results
def decode_full_utf16le(blob: bytes):
text = blob.decode("utf-16-le", errors="ignore")
text = text.replace("\x00", "")
lines = [line.strip() for line in text.replace("\r", "").split("\n")]
return [line for line in lines if line]
def extract_guids(text: str):
pattern = re.compile(
r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
)
return sorted(set(pattern.findall(text)))
def main():
parser = argparse.ArgumentParser(
description="Decode BasicFileInfo.bin from an RFA unpack."
)
parser.add_argument(
"path",
nargs="?",
default=r"racbasicsamplefamily\BasicFileInfo.bin",
help="Path to BasicFileInfo.bin",
)
args = parser.parse_args()
path = pathlib.Path(args.path)
if not path.exists():
safe_print(f"File not found: {path}")
return 1
blob = path.read_bytes()
safe_print(f"File: {path}")
safe_print(f"Size: {len(blob)} bytes")
safe_print()
safe_print("Length-prefixed UTF-16LE strings (heuristic):")
for s in decode_length_prefixed_utf16le(blob):
if s:
safe_print(f" {s}")
safe_print()
safe_print("UTF-16LE lines (full decode):")
lines = decode_full_utf16le(blob)
for line in lines:
safe_print(f" {line}")
safe_print()
safe_print("Key/Value lines:")
for line in lines:
if ":" in line:
key, value = line.split(":", 1)
key = key.strip()
value = value.strip()
safe_print(f" {key}: {value}")
safe_print()
joined = "\n".join(lines)
guids = extract_guids(joined)
if guids:
safe_print("GUIDs:")
for g in guids:
safe_print(f" {g}")
safe_print()
safe_print("Extracted UTF-16LE substrings (scan):")
for s in extract_utf16le_strings(blob):
safe_print(f" {s}")
return 0
if __name__ == "__main__":
raise SystemExit(main())