mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:08:38 +00:00
tools/import_call_sites: scan direct call qword [rip+disp32] (FF 15) import call sites
Support direct import calls through call qword [rip+disp32] (\xff\x15) in tools/import_call_sites.py alongside thunks (jmp qword [rip+disp32] / \xff\x25). This enables discovering call sites in images where imports are called directly without an intermediate jmp thunk (e.g. XboxBvhBuilder.dll calls). Harden tools/import_fail_fast.py to identify instruction boundary sizes (5 bytes for E8 vs 6 bytes for FF 15) when classifying call sites. Add unit tests in tests/test_import_call_sites.py for direct slot call detection, combined thunk/direct calls, non-slot skips, and CLI reports. AI-assisted (Claude Code)
This commit is contained in:
@ -367,5 +367,67 @@ class DelayImportCallSitesTest(unittest.TestCase):
|
||||
thunks=[DELAY_THUNK], call=DELAY_CALLS, jmp=[DELAY_JMP])
|
||||
|
||||
|
||||
|
||||
class DirectSlotCallsTest(unittest.TestCase):
|
||||
def test_direct_call_slot_counted_without_thunk(self):
|
||||
# Image where XMemBar's thunk is wiped, but an instruction calls its IAT slot directly via FF 15.
|
||||
data = bytearray(build_image([]))
|
||||
data[THUNK_BAR:THUNK_BAR + 6] = b"\x90" * 6 # Nop out the thunk
|
||||
call_rva = TEXT_RVA + 0x150
|
||||
data[call_rva:call_rva + 6] = b"\xff\x15" + rel32(call_rva + 6, IAT_BAR)
|
||||
|
||||
out = import_call_sites.call_sites(bytes(data))["imports"]
|
||||
bar = out["xmem.dll!XMemBar"]
|
||||
self.assertEqual(bar["thunks"], [])
|
||||
self.assertEqual(bar["call"], [call_rva])
|
||||
self.assertEqual(bar["jmp"], [])
|
||||
|
||||
def test_direct_call_and_thunk_call_combined_and_sorted(self):
|
||||
# XMemFoo called both through its thunk (at 0x160) and directly via FF 15 (at 0x120).
|
||||
data = bytearray(build_image([(TEXT_RVA + 0x160, 0xE8, THUNK_FOO)]))
|
||||
direct_rva = TEXT_RVA + 0x120
|
||||
data[direct_rva:direct_rva + 6] = b"\xff\x15" + rel32(direct_rva + 6, IAT_FOO)
|
||||
|
||||
out = import_call_sites.call_sites(bytes(data))["imports"]
|
||||
foo = out["xmem.dll!XMemFoo"]
|
||||
self.assertEqual(foo["thunks"], [THUNK_FOO])
|
||||
self.assertEqual(foo["call"], [direct_rva, TEXT_RVA + 0x160])
|
||||
self.assertEqual(foo["jmp"], [])
|
||||
|
||||
def test_direct_call_to_non_slot_is_ignored(self):
|
||||
data = bytearray(build_image([]))
|
||||
call_rva = TEXT_RVA + 0x180
|
||||
data[call_rva:call_rva + 6] = b"\xff\x15" + rel32(call_rva + 6, IDATA_RVA + 0x500)
|
||||
|
||||
out = import_call_sites.call_sites(bytes(data))["imports"]
|
||||
for entry in out.values():
|
||||
self.assertNotIn(call_rva, entry["call"])
|
||||
|
||||
def test_direct_call_truncated_at_section_end(self):
|
||||
data = bytearray(build_image([]))
|
||||
# Place FF 15 in the last 4 bytes of .text so s + 6 > len(blob)
|
||||
cut = TEXT_RVA + 0x1000 - 4
|
||||
data[cut:cut + 2] = b"\xff\x15"
|
||||
|
||||
out = import_call_sites.call_sites(bytes(data))["imports"]
|
||||
self.assertIn("xmem.dll!XMemFoo", out)
|
||||
|
||||
def test_cli_output_includes_direct_slot_calls(self):
|
||||
data = bytearray(build_image([]))
|
||||
call_rva = TEXT_RVA + 0x150
|
||||
data[call_rva:call_rva + 6] = b"\xff\x15" + rel32(call_rva + 6, IAT_BAR)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = os.path.join(tmp, "image.exe")
|
||||
with open(path, "wb") as f:
|
||||
f.write(data)
|
||||
out = io.StringIO()
|
||||
with contextlib.redirect_stdout(out):
|
||||
rc = import_call_sites.main([path, "--dll", "xmem.dll", "--json"])
|
||||
self.assertEqual(rc, 0)
|
||||
parsed = json.loads(out.getvalue())
|
||||
self.assertEqual(parsed["imports"]["xmem.dll!XMemBar"]["call"], [hex(call_rva)])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -4,11 +4,13 @@ Usage:
|
||||
python -I tools/import_call_sites.py <image> [--import DLL!NAME ...] [--dll DLL ...] [--json]
|
||||
|
||||
An MSVC image calls an imported function through a thunk, `jmp qword [rip+disp32]` through the import's address-table
|
||||
slot, and reaches the thunk with `call rel32` (or `jmp rel32` as a tail call). This finds each wanted import's thunk,
|
||||
then every `E8`/`E9 rel32` in an executable section whose target is that thunk. Nothing is run and nothing is
|
||||
disassembled, so this is a byte pattern: a stray `E8` or `E9` inside another instruction that happens to land on a
|
||||
thunk would be counted too, which is rare, and a call that goes through a register (`call rax` after a load of the
|
||||
slot) is not seen. Read the counts as a lower bound for indirect uses and an upper bound for direct ones.
|
||||
slot, and reaches the thunk with `call rel32` (or `jmp rel32` as a tail call), or directly with `call qword [rip+disp32]`
|
||||
through the slot. This finds each wanted import's thunk and direct call sites, then every `E8`/`E9 rel32` in an
|
||||
executable section whose target is that thunk, plus every `FF 15 disp32` targeting the slot directly. Nothing is run
|
||||
and nothing is disassembled, so this is a byte pattern: a stray `E8`, `E9` or `FF 15` inside another instruction that
|
||||
happens to land on a thunk or slot would be counted too, which is rare, and a call that goes through a register
|
||||
(`call rax` after a load of the slot) is not seen. Read the counts as a lower bound for indirect uses and an upper bound
|
||||
for direct ones.
|
||||
|
||||
With no `--import` and no `--dll`, ordinary and delay-loaded imports are listed. An import named `#N` is an ordinal
|
||||
import. Slots with the same `DLL!NAME` spelling contribute to one row. If both tables claim the same slot RVA, the
|
||||
@ -29,6 +31,7 @@ if ROOT not in sys.path:
|
||||
import pe_inspect # noqa: E402
|
||||
|
||||
JMP_INDIRECT = re.compile(b"\xff\x25")
|
||||
CALL_INDIRECT = re.compile(b"\xff\x15")
|
||||
REL_BRANCH = re.compile(b"[\xe8\xe9]")
|
||||
|
||||
|
||||
@ -115,6 +118,13 @@ def call_sites(data, wanted=None, dlls=None):
|
||||
key = thunks.get(rva + s + 5 + struct.unpack_from("<i", blob, s + 1)[0])
|
||||
if key:
|
||||
out[key]["call" if blob[s] == 0xE8 else "jmp"].append(rva + s)
|
||||
for m in CALL_INDIRECT.finditer(blob):
|
||||
s = m.start()
|
||||
if s + 6 > len(blob):
|
||||
continue
|
||||
target = rva + s + 6 + struct.unpack_from("<i", blob, s + 2)[0]
|
||||
if target in slots:
|
||||
out[slots[target]]["call"].append(rva + s)
|
||||
for entry in out.values():
|
||||
for field in entry.values():
|
||||
field.sort()
|
||||
|
||||
@ -233,7 +233,13 @@ def fail_fast(data, wanted=None, dlls=None):
|
||||
direct = slot_calls(img, slots)
|
||||
out = {}
|
||||
for key, entry in thunk_calls.items():
|
||||
sites = sorted([(rva, rva + 5) for rva in entry["call"]] + [(rva, rva + 6) for rva in direct.get(key, [])])
|
||||
site_map = {}
|
||||
for rva in entry["call"]:
|
||||
at = img.offset(rva, 1)
|
||||
site_map[rva] = rva + (6 if at is not None and img.data[at] == 0xFF else 5)
|
||||
for rva in direct.get(key, []):
|
||||
site_map[rva] = rva + 6
|
||||
sites = sorted(site_map.items())
|
||||
fatal = []
|
||||
for rva, after in sites:
|
||||
cond = classify(img, after)
|
||||
|
||||
Reference in New Issue
Block a user