feat(extractor): events per script type through the type's vftable

type_events reads getBehavioursList from each type's own vftable, so
unnamed classes (FILTER, FIFO/LIFO, MOVIE) get their events and
INTEGER/STRING/... resolve to CMC_Variable's list without guessing. The
slot is voted by same-class overrides because the folded base body
carries unrelated labels. Type vftables are recorded for every engine.
Schema 6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Patryk Gensch
2026-10-24 19:49:26 +02:00
co-authored by Claude Opus 5.5
parent f85e2a14ef
commit cb03de47ac
+65 -9
View File
@@ -692,7 +692,6 @@ def extract_events(program):
each class's list is self-contained (no inheritance chain, unlike methods). Each literal is each class's list is self-contained (no inheritance chain, unlike methods). Each literal is
loaded twice per entry (strlen + memcpy), so consecutive duplicates are collapsed.""" loaded twice per entry (strlen + memcpy), so consecutive duplicates are collapsed."""
fm = program.getFunctionManager() fm = program.getFunctionManager()
listing = program.getListing()
events = [] events = []
it = fm.getFunctions(True) it = fm.getFunctions(True)
while it.hasNext(): while it.hasNext():
@@ -701,16 +700,23 @@ def extract_events(program):
continue continue
ns = f.getParentNamespace() ns = f.getParentNamespace()
owner = ns.getName() if ns is not None else "?" owner = ns.getName() if ns is not None else "?"
order = 0 for order, name in enumerate(_event_literals(program, f)):
events.append({"owner": owner, "name": name, "order": order})
return events
def _event_literals(program, func):
"""Ordered event names a getBehavioursList body builds; each literal is loaded twice per
entry (strlen + memcpy), so consecutive duplicates collapse."""
names = []
last = None last = None
ins = listing.getInstructions(resolve_thunk(f).getBody(), True) ins = program.getListing().getInstructions(resolve_thunk(func).getBody(), True)
while ins.hasNext(): while ins.hasNext():
s = _imm_string(program, ins.next()) s = _imm_string(program, ins.next())
if s is not None and _is_method_name(s) and s != last: if s is not None and _is_method_name(s) and s != last:
events.append({"owner": owner, "name": s, "order": order}) names.append(s)
order += 1
last = s last = s
return events return names
def _is_cmc_ctor(func): def _is_cmc_ctor(func):
@@ -1768,6 +1774,54 @@ def annotate_type_vftables(program, types, method_dispatch):
row["per_type"][name] = "trivial_virtual" row["per_type"][name] = "trivial_virtual"
# --------------------------------------------------------------------------- type events
def _virtual_slot(program, member):
"""vtable offset of virtual `member`, voted by classes that override it in their own
namespace. A base body folded with unrelated trivial ones (ICF) carries many labels and
shows up in other slots too, so only same-class overrides vote."""
votes = {}
it = program.getSymbolTable().getAllSymbols(True)
while it.hasNext():
s = it.next()
ns = s.getParentNamespace()
if "vftable" not in s.getName() or ns is None or not ns.getName().startswith(CMC_PREFIX):
continue
for i in range(_MAX_VTABLE_SLOTS):
target = _read_ptr(program, s.getAddress().add(i * 4))
if target is None or not _executable(program, target):
break
if (ns.getName(), member) in _labels(program, target):
votes[i * 4] = votes.get(i * 4, 0) + 1
return max(votes, key=lambda k: votes[k]) if votes else None
def extract_type_events(program, types):
"""Per script type, the events its own vftable's getBehavioursList returns. Reaches the
unnamed classes (FILTER, FIFO/LIFO, MOVIE, ...) and resolves inheritance (INTEGER uses
CMC_Variable's list) without guessing. Needs `types[].vftable`; the lists are
self-contained (each repeats ONINIT/ONDONE/ONSIGNAL)."""
slot = _virtual_slot(program, "getBehavioursList")
if slot is None:
return []
out = []
seen = set()
for t in types:
if not t.get("vftable") or t["script_name"] in seen:
continue
seen.add(t["script_name"])
target = _read_ptr(program, _addr(program, int(t["vftable"], 16)).add(slot))
func = _ensure_function(program, target)
if func is None:
continue
owners = sorted(set(ns for ns, name in _labels(program, target) if name == "getBehavioursList"))
for order, name in enumerate(_event_literals(program, func)):
out.append({"type": t["script_name"], "name": name, "order": order,
"list_addr": _hex(resolve_thunk(func).getEntryPoint()),
"list_owner": owners[0] if len(owners) == 1 else None})
return out
# --------------------------------------------------------------------------- metadata # --------------------------------------------------------------------------- metadata
def detect_engine(factory): def detect_engine(factory):
@@ -1841,9 +1895,10 @@ def run():
if runner_map and "by_type_id" in runner_map: if runner_map and "by_type_id" in runner_map:
annotate_method_facts(program, method_dispatch, run_addrs, runner_map, types) annotate_method_facts(program, method_dispatch, run_addrs, runner_map, types)
annotate_type_vftables(program, types, method_dispatch) annotate_type_vftables(program, types, method_dispatch)
type_events = extract_type_events(program, types)
snapshot = { snapshot = {
"schema_version": 5, "schema_version": 6,
"binary": { "binary": {
"name": program.getName(), "name": program.getName(),
"sha256": sha256_of(program), "sha256": sha256_of(program),
@@ -1860,6 +1915,7 @@ def run():
"struct_layout": struct_layout, "struct_layout": struct_layout,
"method_dispatch": method_dispatch, "method_dispatch": method_dispatch,
"runner_map": runner_map, "runner_map": runner_map,
"type_events": type_events,
} }
args = getScriptArgs() args = getScriptArgs()
@@ -1870,9 +1926,9 @@ def run():
finally: finally:
fh.close() fh.close()
print("[+] %s [%s/%s]: %d types, %d methods, %d events, %d fields (%d layout, %d dispatch, %d runners) -> %s" % ( print("[+] %s [%s/%s]: %d types, %d methods, %d events (%d per type), %d fields (%d layout, %d dispatch, %d runners) -> %s" % (
program.getName(), engine, compiler, len(types), len(methods), program.getName(), engine, compiler, len(types), len(methods),
len(events), len(fields), len(struct_layout), len(method_dispatch), len(events), len(type_events), len(fields), len(struct_layout), len(method_dispatch),
len((runner_map or {}).get("runners", [])), out_path)) len((runner_map or {}).get("runners", [])), out_path))