diff --git a/tests/test_import_call_sites.py b/tests/test_import_call_sites.py index 8bc667cb2..de2d20a95 100644 --- a/tests/test_import_call_sites.py +++ b/tests/test_import_call_sites.py @@ -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() diff --git a/tools/import_call_sites.py b/tools/import_call_sites.py index 0471438cc..047bbd433 100644 --- a/tools/import_call_sites.py +++ b/tools/import_call_sites.py @@ -4,11 +4,13 @@ Usage: python -I tools/import_call_sites.py [--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(" len(blob): + continue + target = rva + s + 6 + struct.unpack_from("