mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:08:38 +00:00
Merge pull request #941 from TheGeogeo/fix/fail-fast-selectors
Reject unmatched --import/--dll selectors in import_fail_fast (FAIL-FAST-SELECTORS)
This commit is contained in:
@ -175,6 +175,58 @@ class ImportFailFastTest(unittest.TestCase):
|
||||
self.assertEqual(plain.splitlines()[-1], "1 of 3 import(s) called directly, 0 with a fatal result check, 0 fatal site(s)")
|
||||
self.assertEqual(len(every.splitlines()), 5)
|
||||
|
||||
def test_cli_refuses_a_malformed_selector(self):
|
||||
"""A bare DLL, a bare name or an empty half of --import, and a `DLL!NAME` given to --dll, select no import:
|
||||
the run printed "0 of 0 import(s) called directly" and exited 0, which reads as "nothing here stops the title"."""
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = os.path.join(tmp, "image.exe")
|
||||
with open(path, "wb") as f:
|
||||
f.write(self.data)
|
||||
for argv in (["--import", "xmem.dll"], ["--import", "XMemFoo"], ["--import", "xmem.dll!"],
|
||||
["--import", "!XMemFoo"], ["--dll", "xmem.dll!XMemFoo"], ["--dll", "!XMemFoo"]):
|
||||
err = io.StringIO()
|
||||
with self.assertRaises(SystemExit) as caught, contextlib.redirect_stderr(err):
|
||||
import_fail_fast.main([path, *argv])
|
||||
self.assertEqual(caught.exception.code, 2, argv)
|
||||
self.assertTrue(err.getvalue().strip(), argv)
|
||||
|
||||
def test_cli_refuses_a_well_formed_selector_this_image_does_not_have(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = os.path.join(tmp, "image.exe")
|
||||
with open(path, "wb") as f:
|
||||
f.write(self.data)
|
||||
for argv in (["--import", "xmem.dll!XMemFooo"], ["--import", "xmem!XMemFoo"],
|
||||
["--dll", "xmem"], ["--dll", "other.dll"]):
|
||||
for extra in ([], ["--json"], ["--all"]):
|
||||
code, out, err = run_cli(path, *argv, *extra)
|
||||
self.assertEqual((code, out), (2, ""), (argv, extra))
|
||||
self.assertIn(argv[0], err, argv)
|
||||
self.assertIn(argv[1].lower(), err.lower(), argv)
|
||||
|
||||
def test_cli_refuses_a_typo_beside_a_good_selector_without_output(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = os.path.join(tmp, "image.exe")
|
||||
with open(path, "wb") as f:
|
||||
f.write(self.data)
|
||||
code, out, err = run_cli(path, "--import", FOO, "--import", "xmem.dll!XMemFooo")
|
||||
self.assertEqual((code, out), (2, ""))
|
||||
self.assertIn("xmem.dll!XMemFooo", err)
|
||||
self.assertNotIn(FOO + ",", err)
|
||||
code, out, err = run_cli(path, "--dll", "xmem.dll", "--dll", "other.dll")
|
||||
self.assertEqual((code, out), (2, ""))
|
||||
self.assertIn("other.dll", err)
|
||||
|
||||
def test_cli_still_runs_valid_selectors(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = os.path.join(tmp, "image.exe")
|
||||
with open(path, "wb") as f:
|
||||
f.write(self.data)
|
||||
for argv in (["--import", FOO], ["--import", "XMEM.DLL!XMEMBAR"], ["--import", ORD],
|
||||
["--dll", "xmem.dll"], ["--dll", "XMEM.DLL", "--import", BAR]):
|
||||
code, out, err = run_cli(path, *argv)
|
||||
self.assertEqual((code, err), (0, ""), argv)
|
||||
self.assertIn("import(s) called directly", out, argv)
|
||||
|
||||
def test_cli_exits_2_for_a_missing_or_non_pe_file(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
code, out, err = run_cli(os.path.join(tmp, "missing.exe"))
|
||||
@ -208,8 +260,8 @@ class ImportFailFastTest(unittest.TestCase):
|
||||
},
|
||||
},
|
||||
)
|
||||
self.assertEqual((other_code, other_err), (0, ""))
|
||||
self.assertEqual(json.loads(other_out), {"image_base": "0x140000000", "imports": {}})
|
||||
self.assertEqual((other_code, other_out), (2, ""))
|
||||
self.assertIn("no import matches --dll other.dll", other_err)
|
||||
|
||||
def test_additional_result_copy_width_and_mask_encodings(self):
|
||||
"""Exercise 0x89 reg copies, 0xF7 masks, 16-bit 0x66 ax tests, and 0x3D imm32 compares."""
|
||||
|
||||
@ -46,7 +46,8 @@ if ROOT not in sys.path:
|
||||
sys.path.insert(0, ROOT)
|
||||
import pe_inspect # noqa: E402
|
||||
from gdk_runtime_calls import RAX, decode, writes # noqa: E402
|
||||
from import_call_sites import call_sites, import_slots # noqa: E402
|
||||
from import_call_sites import (call_sites, dll_selector, import_selector, import_slots, # noqa: E402
|
||||
unmatched_selectors)
|
||||
|
||||
CALL_SLOT = re.compile(b"\xff\x15")
|
||||
PREFIXES = frozenset((0x26, 0x2E, 0x36, 0x3E, 0x64, 0x65, 0x66, 0x67, 0xF0, 0xF2, 0xF3))
|
||||
@ -260,9 +261,9 @@ def main(argv=None):
|
||||
ap = argparse.ArgumentParser(description="List the call sites where an x86-64 PE image stops itself when an "
|
||||
"import returns a failure.")
|
||||
ap.add_argument("image")
|
||||
ap.add_argument("--import", dest="imports", action="append", default=[], metavar="DLL!NAME",
|
||||
ap.add_argument("--import", dest="imports", action="append", default=[], metavar="DLL!NAME", type=import_selector,
|
||||
help="an import to look at (repeatable); '#N' names an ordinal import")
|
||||
ap.add_argument("--dll", dest="dlls", action="append", default=[], metavar="DLL",
|
||||
ap.add_argument("--dll", dest="dlls", action="append", default=[], metavar="DLL", type=dll_selector,
|
||||
help="every import of this DLL (repeatable)")
|
||||
ap.add_argument("--all", action="store_true", help="list the imports with no fatal site too")
|
||||
ap.add_argument("--json", action="store_true", help="print JSON (RVAs in hex) instead of the table")
|
||||
@ -274,6 +275,11 @@ def main(argv=None):
|
||||
except (OSError, pe_inspect.PeFormatError) as e:
|
||||
print(f"{os.path.basename(sys.argv[0])}: {e}", file=sys.stderr)
|
||||
return 2
|
||||
missing = unmatched_selectors(data, args.imports, args.dlls)
|
||||
if missing:
|
||||
print(f"{os.path.basename(sys.argv[0])}: {os.path.basename(args.image)}: "
|
||||
f"no import matches {', '.join(missing)}", file=sys.stderr)
|
||||
return 2
|
||||
imports = result["imports"]
|
||||
if args.json:
|
||||
printable = {k: {"calls": e["calls"], "fatal": [{"rva": hex(f["rva"]), "condition": f["condition"]}
|
||||
|
||||
Reference in New Issue
Block a user