-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_retina.py
More file actions
executable file
·274 lines (239 loc) · 9.06 KB
/
Copy pathbuild_retina.py
File metadata and controls
executable file
·274 lines (239 loc) · 9.06 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
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
#!/usr/bin/env python3
import argparse
import subprocess
import sys
import re
import os
import requests
import struct
from pathlib import Path
import random
# Custom section
hvsc_download_url = "https://hvsc.brona.dk/HVSC/HVSC_82-all-of-them.rar"
capa_exe = "capa"
java_cmd = "java"
kickassembler_cmd = "/opt/KickAss/KickAss.jar"
retina_src = "src"
CLOCK_MAP = {
0: "Unknown",
1: "PAL",
2: "NTSC",
3: "PAL&NTSC",
}
def get_valid_sid():
local_filename = hvsc_download_url.split('/')[-1]
extract_path = local_filename.replace('.rar', '')
download_and_uncompress(hvsc_download_url, local_filename, extract_path)
valid_sid = []
for sid_path in collect_sid_files(extract_path):
info = read_sid_header(sid_path)
if (info['init_address'] == 0x1000 and
info['play_address'] == 0x1003 and
info['speed_mask'] == '$00000000' and
info['clock'] == 'PAL' and
info['file_size'] <= 0x1000):
valid_sid.append(info)
return valid_sid
def read_sid_header(path):
file_size = path.stat().st_size
with path.open("rb") as f:
header = f.read(0x7C) # grab enough to cover v4 fields
if len(header) < 0x16:
raise ValueError("File too small to be a valid SID")
# Offsets are big-endian
# ----- mandatory part -----
magic = header[0:4].decode("ascii", "replace")
if magic not in ("PSID", "RSID"):
raise ValueError(f"Bad magic: {magic!r}")
version = struct.unpack(">H", header[4:6])[0]
load_addr = struct.unpack(">H", header[8:10])[0]
init_addr = struct.unpack(">H", header[10:12])[0]
play_addr = struct.unpack(">H", header[12:14])[0]
speed_mask = struct.unpack(">I", header[0x12:0x16])[0] # 0x12 == 18
# ----- optional flags (v ≥ 2) -----
if version >= 2 and len(header) >= 0x78:
raw_flag = struct.unpack(">H", header[0x76:0x78])[0]
clock_bits = (raw_flag >> 2) & 0b11
clock = CLOCK_MAP.get(clock_bits, "Unknown")
else:
clock = "n/a (version 1 header)"
return {
"file_name" : str(path),
"file_size" : file_size,
"magic" : magic,
"version" : version,
"load_address" : load_addr,
"init_address" : init_addr,
"play_address" : play_addr,
"speed_mask" : f"$%08X" % speed_mask,
"clock" : clock,
}
def collect_sid_files(root):
root_path = Path(root)
if root_path.is_file():
if root_path.suffix.lower() == ".sid":
yield root_path
return
yield from root_path.rglob("*.sid")
def patch_sid_reference(
asm_file,
sid_filename,
create_backup=True,
):
IMPORT_RE = re.compile(
r'(\.import\s+binary\s+")([^"]*)(",\s*\$7E)', # 3 capture groups
flags=re.IGNORECASE,
)
asm_path = Path(asm_file)
text = asm_path.read_text(encoding="utf-8")
# Build the replacement string on-the-fly: keep groups 1 & 3, swap group 2
repl = r'\1' + sid_filename + r'\3'
new_text, n_subs = IMPORT_RE.subn(repl, text, count=1)
if n_subs == 0:
return False # no `.import binary` line with , $7E found
if create_backup:
asm_path.with_suffix(asm_path.suffix + ".bak").write_text(text, encoding="utf-8")
asm_path.write_text(new_text, encoding="utf-8")
return True
def download_and_uncompress(url, local_path, extract_to):
# Check if file is already downloaded
if not os.path.exists(local_path):
print("Downloading...", local_path)
response = requests.get(url, stream=True)
response.raise_for_status()
with open(local_path, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
# Check if extraction is already done
if not os.path.exists(extract_to):
print("Extracting...", extract_to)
os.makedirs(extract_to, exist_ok=True)
subprocess.run(['unrar', 'x', '-y', local_path, extract_to], check=True)
# Function to parse capa output
def parse_capa_output(output):
result = {}
# Extract md5
md5_match = re.search(r'md5\s+([a-fA-F0-9]{32})', output)
result['md5'] = md5_match.group(1) if md5_match else None
# Extract rules
rules_pattern = re.compile(r'([^\n]+)\nnamespace\s+[^\n]+\nscope\s+([^\n]+)\nmatches\s+(.*(?:0x[0-9A-Fa-f]+\s*)+)', re.MULTILINE)
rules = []
for match in rules_pattern.finditer(output):
title = match.group(1).strip().lower()
scope = match.group(2).strip().lower()
matches = re.findall(r'0x[0-9A-Fa-f]+', match.group(3))
matches = [ x.lower() for x in matches ]
rules.append({
'title': title[:35],
'scope': scope,
'matches': matches
})
result['rules'] = rules
return result
def generate_asm(result):
comment_separator = "//==============================================================================\n"
# TODO: customize depending on malware
mdBckgrndColor1 = "DARK_GRAY"
mdBckgrndColor2 = "YELLOW"
featuresNum = len(result['rules'])
with open(retina_src + "/malwareData.asm", "w") as asm:
asm.write(comment_separator)
asm.write("// Malware Data File\n")
asm.write(comment_separator)
asm.write("\n#importonce\n")
asm.write("\n// Includes\n//\n\n")
asm.write(comment_separator)
asm.write("// BackGround Colors, depending on Malware Type\n//\n\n")
asm.write(f".const mdBckgrndColor1 = {mdBckgrndColor1}\n")
asm.write(f".const mdBckgrndColor2 = {mdBckgrndColor2}\n\n")
asm.write(comment_separator)
asm.write("// File MD5\n//\n\n")
asm.write(f"mdMD5: .text \"{result['md5']}\"\n")
asm.write(" .byte 0\n\n")
asm.write(comment_separator)
asm.write("// Features\n//\n\n")
asm.write(f".const mdFeatureNum = {featuresNum}\n\n")
asm.write("mdCurrentFeature: .byte 0\n\n// Pointers to Rules strings\n")
# Rules
asm.write("mdRulesPtrs: .word ")
for i in range(1, featuresNum + 1):
asm.write(f"mdRule{i}")
if i < featuresNum:
asm.write(", ")
asm.write("\n\n")
for i in range(1, featuresNum + 1):
asm.write(f"mdRule{i}: .text \"{result['rules'][i-1]['title']}\"\n")
asm.write(f" .byte 0\n")
# Scopes
asm.write(f"\n// Pointers to Scopes\n")
asm.write("mdScopePtrs: .word ")
for i in range(1, featuresNum + 1):
asm.write(f"mdScope{i}")
if i < featuresNum:
asm.write(", ")
asm.write("\n\n")
for i in range(1, featuresNum + 1):
asm.write(f"mdScope{i}: .text \"{result['rules'][i-1]['scope']}\"\n")
asm.write(f" .byte 0\n")
# Matches
asm.write(f"\n// Pointers to Matches\n")
asm.write("mdMatchesPtrs: .word ")
for i in range(1, featuresNum + 1):
asm.write(f"mdMatch{i}")
if i < featuresNum:
asm.write(", ")
asm.write("\n\n")
for i in range(1, featuresNum + 1):
m = result['rules'][i-1]
asm.write(f"mdMatch{i}: .text \"{', '.join(m['matches'][:3])}\"\n")
asm.write(f" .byte 0\n")
return
def main():
parser = argparse.ArgumentParser(
description="R.E.T.I.N.A. Builder"
)
parser.add_argument("filename", help="Malware file to be analyzed")
parser.add_argument("--sid", help="SID file to use (default is random)")
args = parser.parse_args()
filename = args.filename
print("[+] Executing file analysis, may take a while...")
try:
# Run capa
result = subprocess.run(
[capa_exe, "-v", filename ],
capture_output=True,
text=True,
check=True
)
except subprocess.CalledProcessError as e:
print("Error running capa:", e, file=sys.stderr)
sys.exit(1)
print("[+] Parsing result")
result = parse_capa_output(result.stdout)
print("[+] Generating ASM file")
generate_asm(result)
print("[+] Processing music")
if args.sid == None:
valid_sid = get_valid_sid()
sid = random.choice(valid_sid)
sid_file_name = sid['file_name']
print(f" Randomly choosing from {len(valid_sid)} SID files")
else:
sid_file_name = args.sid
print(f" Using SID file {sid_file_name}")
patch_sid_reference(retina_src + "/gameData.asm", sid_file_name)
print("[+] Compiling R.E.T.I.N.A.")
try:
# Compile program
result = subprocess.run(
[java_cmd, "-jar", kickassembler_cmd, retina_src + "/main.asm" ],
capture_output=True,
text=True,
check=True
)
except subprocess.CalledProcessError as e:
print("Error compiling:", e, file=sys.stderr)
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()