scientific-method-engine 19.8.1__tar.gz → 19.9.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/PKG-INFO +1 -1
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/pyproject.toml +1 -1
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/cli.py +1 -1
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/reports.py +3 -0
- scientific_method_engine-19.9.0/src/scientific_method_engine/x86/scan_index.py +181 -0
- scientific_method_engine-19.9.0/tests/test_scan_index.py +163 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/.gitignore +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/LICENSE +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/README.md +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/__init__.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/__main__.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ClearNoReturnFunctions.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/CreateFunctions.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ExportBoundedFlow.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ExportCallEdges.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ExportFunctionFingerprints.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ExportFunctionInventory.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/MergeFallThroughFragment.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/RecoverCitedFunctions.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/RepairReturningCallers.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportCallArguments.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportCallPaths.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportCallSitesWithScalars.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportCallsToRange.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportConstantFirstArgumentCalls.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportDataBytes.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportDecompileMatches.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportDecompileWindow.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportFilePatternInMemory.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportFirstArgumentCallSummary.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportFunctionScalarConstants.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportFunctionSummary.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportInstructionContext.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportInstructionWindow.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportMemoryBlockForFileOffset.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportMemoryBlocks.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportRandomnessCandidates.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportRangeBoundaries.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportReferences.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportScalarConstants.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportStringReferences.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/ReportSymbolReferences.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/scientificmethod/FirstArgumentLookBack.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/scientificmethod/InstructionStart.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/scientificmethod/OperandConstants.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/ghidra/scientificmethod/Xxh3.java +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/__init__.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/argument_frames.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/derived_counts.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/dispatch.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/effect_order.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/image.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/inventory.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/loops.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/machine.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/memory_scopes.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/pcode.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/pcode_backend.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/pe.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/ranges.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/reach.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/relational.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/result_flow.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/table_bounds.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/trace.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/src/scientific_method_engine/x86/values.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/oracle.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_config_encoding.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_continuation_budget.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_derived_counts.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_dispatch.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_effect_order.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_enter_frames.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_entry_frame.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_indirect_far_transfers.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_interrupt_models.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_inventory_check.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_join_terms.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_loops.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_memory_scopes.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_nested_frame_request.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_oracle.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_pe.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_ranges.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_reach.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_relational_controls.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_return_checks.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_successor_rule.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_table_bounds.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_table_continuations.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_term_limit.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_upstream_gaps.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_volatile_memory.py +0 -0
- {scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_x86.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: scientific-method-engine
|
|
3
|
-
Version: 19.
|
|
3
|
+
Version: 19.9.0
|
|
4
4
|
Summary: Bounded instruction-derived x86 evidence reports for segmented MZ/FBOV and PE32/i386 code.
|
|
5
5
|
Project-URL: Source, https://github.com/kibertoad/refurbished-dinosaurs-toolkit/tree/main/packages/scientific-method-engine
|
|
6
6
|
Author: kibertoad
|
|
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "scientific-method-engine"
|
|
7
7
|
# The release workflow writes the published version from the package's release tag.
|
|
8
|
-
version = "19.
|
|
8
|
+
version = "19.9.0"
|
|
9
9
|
description = "Bounded instruction-derived x86 evidence reports for segmented MZ/FBOV and PE32/i386 code."
|
|
10
10
|
readme = "README.md"
|
|
11
11
|
requires-python = ">=3.12"
|
|
@@ -13,7 +13,7 @@ PREPARED_CONFIG_LIMIT = 16 * 1024 * 1024
|
|
|
13
13
|
# The header names the decoder and instruction semantics that actually ran, not the pins in pyproject.toml.
|
|
14
14
|
DECODER = f"capstone {CAPSTONE_VERSION or 'without distribution metadata'}"
|
|
15
15
|
INSTRUCTION_SEMANTICS = f"pypcode {pypcode.__version__} (Ghidra SLEIGH x86)"
|
|
16
|
-
USAGE = ("Usage: scientific-method-engine <operand|operand-candidates|target|bounds|owner|callees|reach|trace|uses|arguments|"
|
|
16
|
+
USAGE = ("Usage: scientific-method-engine <scan-index|operand|operand-candidates|target|bounds|owner|callees|reach|trace|uses|arguments|"
|
|
17
17
|
"effects|returns|memory|incoming|inventory-check|call-order|guards|allocation|dispatch> <config.json|->\n"
|
|
18
18
|
"effects includes ordered path writes/calls and local restoration witnesses; transactionality remains unestablished.\n"
|
|
19
19
|
"callees compares its edges with an ExportCallEdges.java export given as ghidraCallEdges;\n"
|
|
@@ -1884,6 +1884,9 @@ def _run_report(image, config, command):
|
|
|
1884
1884
|
raise ValueError("indirectCalls applies only to reach and inventory-check")
|
|
1885
1885
|
if command == "operand":
|
|
1886
1886
|
return operand_provenance(image, config)
|
|
1887
|
+
if command == "scan-index":
|
|
1888
|
+
from .scan_index import scan_index
|
|
1889
|
+
return scan_index(image, config)
|
|
1887
1890
|
if command == "target":
|
|
1888
1891
|
return call_target_report(image, config)
|
|
1889
1892
|
if command == "bounds":
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"""Reusable raw caller/operand candidates. No reachability or instruction-start claims."""
|
|
2
|
+
import hashlib
|
|
3
|
+
from contextlib import closing
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import sqlite3
|
|
8
|
+
import tempfile
|
|
9
|
+
|
|
10
|
+
from capstone import CS_GRP_CALL
|
|
11
|
+
from capstone.x86 import X86_OP_IMM, X86_OP_MEM
|
|
12
|
+
import xxhash
|
|
13
|
+
|
|
14
|
+
from .image import CAPSTONE_VERSION, integer, scan_limit
|
|
15
|
+
from .reports import _relative_transfer
|
|
16
|
+
from .trace import call_target, unsupported_transfer
|
|
17
|
+
|
|
18
|
+
SCHEMA = 2
|
|
19
|
+
MAX_INDEX_BYTES = 256 * 1024 * 1024
|
|
20
|
+
INSERT_BATCH = 10000
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _identity(image):
|
|
24
|
+
# Query, output location and source pathname do not change the decoded corpus.
|
|
25
|
+
config = {k: v for k, v in image.config.items() if k not in ("query", "scanIndex", "source")}
|
|
26
|
+
# The digest keeps the stored metadata and every report small however large the prepared config is.
|
|
27
|
+
config_digest = hashlib.sha256(json.dumps(config, sort_keys=True, separators=(",", ":")).encode()).hexdigest()
|
|
28
|
+
sources = hashlib.sha256()
|
|
29
|
+
root = Path(__file__).resolve().parent.parent
|
|
30
|
+
for path in sorted(root.rglob("*.py")):
|
|
31
|
+
sources.update(path.relative_to(root).as_posix().encode())
|
|
32
|
+
sources.update(path.read_bytes())
|
|
33
|
+
return {"schema": SCHEMA, "xxh3": xxhash.xxh3_128_hexdigest(image.data),
|
|
34
|
+
"size": len(image.data), "capstone": CAPSTONE_VERSION,
|
|
35
|
+
"engineSources": sources.hexdigest(), "configSha256": config_digest}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _build(image, destination, identity):
|
|
39
|
+
handle, temporary = tempfile.mkstemp(prefix="scan-index-", suffix=".partial", dir=destination.parent)
|
|
40
|
+
os.close(handle)
|
|
41
|
+
try:
|
|
42
|
+
with closing(sqlite3.connect(temporary)) as db, db:
|
|
43
|
+
# Bound disk use, including the final query index. No partial index is published.
|
|
44
|
+
db.execute("PRAGMA page_size=4096")
|
|
45
|
+
db.execute(f"PRAGMA max_page_count={MAX_INDEX_BYTES // 4096}")
|
|
46
|
+
db.execute("CREATE TABLE candidates (site INTEGER, size INTEGER, kind TEXT, value INTEGER)")
|
|
47
|
+
db.execute("CREATE TABLE metadata (value TEXT)")
|
|
48
|
+
db.execute("CREATE TABLE undecoded (start INTEGER, end INTEGER)")
|
|
49
|
+
db.execute("CREATE TABLE unresolved_calls (site INTEGER, size INTEGER)")
|
|
50
|
+
undecoded = unresolved = rows = 0
|
|
51
|
+
pending = []
|
|
52
|
+
for region in image.regions:
|
|
53
|
+
gap = None
|
|
54
|
+
for site in range(region["start"], region["end"]):
|
|
55
|
+
ins = image.decode(site)
|
|
56
|
+
# The one-shot decoder cache is unnecessary during a whole-corpus scan.
|
|
57
|
+
image.cache.pop(site, None)
|
|
58
|
+
if ins is None:
|
|
59
|
+
undecoded += 1
|
|
60
|
+
gap = site if gap is None else gap
|
|
61
|
+
continue
|
|
62
|
+
if gap is not None:
|
|
63
|
+
db.execute("INSERT INTO undecoded VALUES (?, ?)", (gap, site))
|
|
64
|
+
gap = None
|
|
65
|
+
found = set()
|
|
66
|
+
for operand in ins.operands:
|
|
67
|
+
# Only encoded literals, as operand-candidates reads them: implicit forms (SHL r,1;
|
|
68
|
+
# [BX]) and relative branch targets are not immediates or displacements.
|
|
69
|
+
if operand.type == X86_OP_IMM and ins.imm_size and not _relative_transfer(ins):
|
|
70
|
+
found.add(("immediate", operand.imm))
|
|
71
|
+
elif operand.type == X86_OP_MEM and ins.disp_size:
|
|
72
|
+
found.add(("displacement", operand.mem.disp))
|
|
73
|
+
if ins.group(CS_GRP_CALL):
|
|
74
|
+
# An operand-size-overridden call is outside the frame model, as direct_calls reports it.
|
|
75
|
+
target = None if unsupported_transfer(image, ins) else call_target(image, site, ins)[0]
|
|
76
|
+
if target is None:
|
|
77
|
+
unresolved += 1
|
|
78
|
+
db.execute("INSERT INTO unresolved_calls VALUES (?, ?)", (site, ins.size))
|
|
79
|
+
else:
|
|
80
|
+
found.add(("call", target))
|
|
81
|
+
pending.extend((site, ins.size, kind, value) for kind, value in sorted(found))
|
|
82
|
+
rows += len(found)
|
|
83
|
+
if len(pending) >= INSERT_BATCH:
|
|
84
|
+
db.executemany("INSERT INTO candidates VALUES (?, ?, ?, ?)", pending)
|
|
85
|
+
pending.clear()
|
|
86
|
+
if gap is not None:
|
|
87
|
+
db.execute("INSERT INTO undecoded VALUES (?, ?)", (gap, region["end"]))
|
|
88
|
+
db.executemany("INSERT INTO candidates VALUES (?, ?, ?, ?)", pending)
|
|
89
|
+
db.execute("CREATE INDEX lookup ON candidates (kind, value, site)")
|
|
90
|
+
metadata = {"identity": identity, "scannedBytes": image.code_bytes,
|
|
91
|
+
"undecodedOffsets": undecoded, "unresolvedCallCandidates": unresolved,
|
|
92
|
+
"candidateRows": rows}
|
|
93
|
+
db.execute("INSERT INTO metadata VALUES (?)", (json.dumps(metadata, sort_keys=True),))
|
|
94
|
+
# A concurrent builder of the same identity produces the same corpus. On Windows the replace
|
|
95
|
+
# fails while another run has that corpus open, which then already holds the same rows.
|
|
96
|
+
try:
|
|
97
|
+
os.replace(temporary, destination)
|
|
98
|
+
except PermissionError:
|
|
99
|
+
if not destination.exists():
|
|
100
|
+
raise
|
|
101
|
+
finally:
|
|
102
|
+
if os.path.exists(temporary):
|
|
103
|
+
os.unlink(temporary)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def scan_index(image, config):
|
|
107
|
+
"""Query a content-keyed local SQLite index of every-offset decode candidates.
|
|
108
|
+
|
|
109
|
+
``scanIndex.directory`` must be an absolute directory.
|
|
110
|
+
``query`` names ``kind`` (immediate, displacement, call), integer ``value``,
|
|
111
|
+
``limit`` (1..10000, default 1000) and optional matching-site ``controls``.
|
|
112
|
+
A call value is a mapped source-file offset, not a loaded address. Every
|
|
113
|
+
result remains a raw candidate, including prefix and interior decodes.
|
|
114
|
+
``undecodedRanges`` (half-open) and ``unresolvedCalls`` list where the scan
|
|
115
|
+
could not see, up to ``limit`` rows each.
|
|
116
|
+
Source verification and MZ/FBOV preparation stay with the normal reader.
|
|
117
|
+
"""
|
|
118
|
+
options, query = config.get("scanIndex"), config.get("query")
|
|
119
|
+
if not isinstance(options, dict) or set(options) != {"directory"}:
|
|
120
|
+
raise ValueError("scanIndex needs only directory")
|
|
121
|
+
directory = options["directory"]
|
|
122
|
+
if not isinstance(directory, str) or not Path(directory).is_absolute():
|
|
123
|
+
raise ValueError("scanIndex.directory must be an absolute path under GAME_DIR")
|
|
124
|
+
if not isinstance(query, dict) or set(query) - {"kind", "value", "limit", "controls"}:
|
|
125
|
+
raise ValueError("scan-index query needs kind, value, optional limit and controls")
|
|
126
|
+
kind = query.get("kind")
|
|
127
|
+
if kind not in ("immediate", "displacement", "call"):
|
|
128
|
+
raise ValueError("scan-index kind must be immediate, displacement or call")
|
|
129
|
+
value = integer(query.get("value"), -(1 << 63), (1 << 63) - 1, "query value")
|
|
130
|
+
limit = integer(query.get("limit", 1000), 1, 10000, "query limit")
|
|
131
|
+
controls = query.get("controls", [])
|
|
132
|
+
if not isinstance(controls, list) or len(controls) > 128:
|
|
133
|
+
raise ValueError("query controls must list at most 128 matching source offsets")
|
|
134
|
+
for site in controls:
|
|
135
|
+
integer(site, 0, len(image.data) - 1, "control site")
|
|
136
|
+
budget = scan_limit(image, config.get("scanLimit", 1048576))
|
|
137
|
+
if image.code_bytes > budget:
|
|
138
|
+
raise ValueError("scan-index scanLimit reached; no complete index produced")
|
|
139
|
+
identity = _identity(image)
|
|
140
|
+
key = hashlib.sha256(json.dumps(identity, sort_keys=True, separators=(",", ":")).encode()).hexdigest()
|
|
141
|
+
folder = Path(directory)
|
|
142
|
+
folder.mkdir(parents=True, exist_ok=True)
|
|
143
|
+
destination = folder / (key + ".sqlite")
|
|
144
|
+
hit = destination.exists()
|
|
145
|
+
if not hit:
|
|
146
|
+
try:
|
|
147
|
+
_build(image, destination, identity)
|
|
148
|
+
except sqlite3.Error as error:
|
|
149
|
+
raise ValueError(f"scan-index build failed and published nothing: {error}") from None
|
|
150
|
+
try:
|
|
151
|
+
if destination.stat().st_size > MAX_INDEX_BYTES:
|
|
152
|
+
raise ValueError("scan-index exceeds the 256 MiB cache limit")
|
|
153
|
+
with closing(sqlite3.connect(destination.as_uri() + "?mode=ro", uri=True)) as db:
|
|
154
|
+
raw = db.execute("SELECT value FROM metadata").fetchall()
|
|
155
|
+
if len(raw) != 1:
|
|
156
|
+
raise ValueError("scan-index metadata is invalid; remove this local cache and rebuild")
|
|
157
|
+
metadata = json.loads(raw[0][0])
|
|
158
|
+
if not isinstance(metadata, dict) or metadata.get("identity") != identity:
|
|
159
|
+
raise ValueError("scan-index identity differs; remove this local cache and rebuild")
|
|
160
|
+
missing = [site for site in controls if db.execute(
|
|
161
|
+
"SELECT 1 FROM candidates WHERE kind=? AND value=? AND site=? LIMIT 1",
|
|
162
|
+
(kind, value, site)).fetchone() is None]
|
|
163
|
+
if missing:
|
|
164
|
+
raise ValueError(f"scan-index matching-site controls missed: {missing}")
|
|
165
|
+
total = db.execute("SELECT count(*) FROM candidates WHERE kind=? AND value=?", (kind, value)).fetchone()[0]
|
|
166
|
+
rows = db.execute("SELECT site, size FROM candidates WHERE kind=? AND value=? ORDER BY site LIMIT ?",
|
|
167
|
+
(kind, value, limit)).fetchall()
|
|
168
|
+
# A negative result is only as complete as the offsets that decoded and the calls that resolved.
|
|
169
|
+
gaps = db.execute("SELECT start, end FROM undecoded ORDER BY start LIMIT ?", (limit + 1,)).fetchall()
|
|
170
|
+
calls = db.execute("SELECT site, size FROM unresolved_calls ORDER BY site LIMIT ?", (limit + 1,)).fetchall()
|
|
171
|
+
except (sqlite3.Error, json.JSONDecodeError) as error:
|
|
172
|
+
raise ValueError(f"scan-index cache failed; remove this local cache and rebuild: {error}") from None
|
|
173
|
+
return {"command": "scan-index", "cacheKey": key, "cacheHit": hit,
|
|
174
|
+
"scan": metadata, "query": query, "totalCandidates": total,
|
|
175
|
+
"resultLimitReached": total > limit,
|
|
176
|
+
"rawCandidates": [{"site": site, "size": size} for site, size in rows],
|
|
177
|
+
"undecodedRanges": [{"start": start, "end": end} for start, end in gaps[:limit]],
|
|
178
|
+
"undecodedRangesLimitReached": len(gaps) > limit,
|
|
179
|
+
"unresolvedCalls": [{"site": site, "size": size} for site, size in calls[:limit]],
|
|
180
|
+
"unresolvedCallsLimitReached": len(calls) > limit,
|
|
181
|
+
"meaning": "Every-offset decode candidates only; no entry-path qualification or absence proof."}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""Persistent scans use synthetic bytes only and never qualify raw candidates."""
|
|
2
|
+
import copy
|
|
3
|
+
from contextlib import closing
|
|
4
|
+
import os
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
import sqlite3
|
|
7
|
+
import sys
|
|
8
|
+
import tempfile
|
|
9
|
+
import unittest
|
|
10
|
+
from unittest.mock import patch
|
|
11
|
+
|
|
12
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
|
13
|
+
from scientific_method_engine.x86.reports import run_report
|
|
14
|
+
from scientific_method_engine.x86.image import Image
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ScanIndexTests(unittest.TestCase):
|
|
18
|
+
def setUp(self):
|
|
19
|
+
self.folder = tempfile.TemporaryDirectory()
|
|
20
|
+
self.addCleanup(self.folder.cleanup)
|
|
21
|
+
# MOV AX,1234; MOV AX,[BX+44]; CALL 000C; CALL AX; RET.
|
|
22
|
+
self.data = bytes.fromhex("b834128b4744e80300ffd0c3c3")
|
|
23
|
+
self.config = {"sourceKind": "synthetic-raw", "regions": [
|
|
24
|
+
{"name": "code", "start": 0, "end": len(self.data), "ip": 0,
|
|
25
|
+
"segment": 4096, "entries": [0], "evidence": "synthetic"}],
|
|
26
|
+
"scanIndex": {"directory": self.folder.name},
|
|
27
|
+
"query": {"kind": "immediate", "value": 0x1234, "controls": [0]}}
|
|
28
|
+
|
|
29
|
+
def report(self, config=None, data=None):
|
|
30
|
+
return run_report(self.data if data is None else data, config or self.config, "scan-index")
|
|
31
|
+
|
|
32
|
+
def test_reuses_corpus_for_different_operands_without_decoding(self):
|
|
33
|
+
first = self.report()
|
|
34
|
+
self.assertFalse(first["cacheHit"])
|
|
35
|
+
self.assertEqual(first["rawCandidates"], [{"site": 0, "size": 3}])
|
|
36
|
+
config = copy.deepcopy(self.config)
|
|
37
|
+
config["query"] = {"kind": "displacement", "value": 0x44, "controls": [3]}
|
|
38
|
+
with patch.object(Image, "decode", side_effect=AssertionError("cache hit decoded again")):
|
|
39
|
+
second = self.report(config)
|
|
40
|
+
self.assertTrue(second["cacheHit"])
|
|
41
|
+
self.assertEqual(first["cacheKey"], second["cacheKey"])
|
|
42
|
+
self.assertEqual(second["rawCandidates"], [{"site": 3, "size": 3}])
|
|
43
|
+
|
|
44
|
+
def test_calls_controls_and_result_limit(self):
|
|
45
|
+
config = copy.deepcopy(self.config)
|
|
46
|
+
config["query"] = {"kind": "call", "value": 12, "controls": [6]}
|
|
47
|
+
self.assertIn({"site": 6, "size": 3}, self.report(config)["rawCandidates"])
|
|
48
|
+
config["query"]["controls"] = [0]
|
|
49
|
+
with self.assertRaisesRegex(ValueError, "controls missed"):
|
|
50
|
+
self.report(config)
|
|
51
|
+
repeated = bytes.fromhex("b83412b83412c3")
|
|
52
|
+
config["regions"][0]["end"] = len(repeated)
|
|
53
|
+
config["query"] = {"kind": "immediate", "value": 0x1234, "limit": 1}
|
|
54
|
+
report = self.report(config, repeated)
|
|
55
|
+
self.assertTrue(report["resultLimitReached"])
|
|
56
|
+
self.assertEqual(report["totalCandidates"], 2)
|
|
57
|
+
self.assertEqual(len(report["rawCandidates"]), 1)
|
|
58
|
+
|
|
59
|
+
def test_reports_where_the_scan_could_not_see(self):
|
|
60
|
+
report = self.report()
|
|
61
|
+
self.assertEqual(report["unresolvedCalls"], [{"site": 9, "size": 2}])
|
|
62
|
+
self.assertFalse(report["unresolvedCallsLimitReached"])
|
|
63
|
+
self.assertEqual(report["undecodedRanges"], [])
|
|
64
|
+
# RET, then MOV AX,imm16 and XOR AL,imm8 cut off by each region's end.
|
|
65
|
+
data = bytes.fromhex("c3b834c3b834")
|
|
66
|
+
config = copy.deepcopy(self.config)
|
|
67
|
+
config["regions"] = [
|
|
68
|
+
{"name": "a", "start": 0, "end": 3, "ip": 0, "segment": 4096, "entries": [0], "evidence": "synthetic"},
|
|
69
|
+
{"name": "b", "start": 3, "end": 6, "ip": 0, "segment": 8192, "entries": [3], "evidence": "synthetic"}]
|
|
70
|
+
config["query"] = {"kind": "immediate", "value": 0x1234}
|
|
71
|
+
first = self.report(config, data)
|
|
72
|
+
self.assertEqual(first["undecodedRanges"], [{"start": 1, "end": 3}, {"start": 4, "end": 6}])
|
|
73
|
+
self.assertEqual(first["scan"]["undecodedOffsets"], 4)
|
|
74
|
+
config["query"]["limit"] = 1
|
|
75
|
+
with patch.object(Image, "decode", side_effect=AssertionError("cache hit decoded again")):
|
|
76
|
+
capped = self.report(config, data)
|
|
77
|
+
self.assertTrue(capped["cacheHit"])
|
|
78
|
+
self.assertEqual(capped["undecodedRanges"], [{"start": 1, "end": 3}])
|
|
79
|
+
self.assertTrue(capped["undecodedRangesLimitReached"])
|
|
80
|
+
|
|
81
|
+
def test_changed_source_mapping_entries_and_decoder_identity_invalidate(self):
|
|
82
|
+
first = self.report()["cacheKey"]
|
|
83
|
+
self.assertNotEqual(first, self.report(data=self.data[:-1] + b"\x90")["cacheKey"])
|
|
84
|
+
for field, value in (("ip", 32), ("entries", [3]), ("evidence", "changed mapping")):
|
|
85
|
+
config = copy.deepcopy(self.config)
|
|
86
|
+
config["regions"][0][field] = value
|
|
87
|
+
self.assertNotEqual(first, self.report(config)["cacheKey"])
|
|
88
|
+
with patch("scientific_method_engine.x86.scan_index.CAPSTONE_VERSION", "different"):
|
|
89
|
+
self.assertNotEqual(first, self.report()["cacheKey"])
|
|
90
|
+
|
|
91
|
+
def test_incomplete_scan_writes_nothing_and_invalid_options_fail(self):
|
|
92
|
+
config = copy.deepcopy(self.config)
|
|
93
|
+
config["scanLimit"] = 2
|
|
94
|
+
with self.assertRaisesRegex(ValueError, "scanLimit reached"):
|
|
95
|
+
self.report(config)
|
|
96
|
+
self.assertEqual(list(Path(self.folder.name).iterdir()), [])
|
|
97
|
+
config = copy.deepcopy(self.config)
|
|
98
|
+
config["scanIndex"]["directory"] = "relative"
|
|
99
|
+
with self.assertRaisesRegex(ValueError, "absolute"):
|
|
100
|
+
self.report(config)
|
|
101
|
+
for query in ({"kind": "unknown", "value": 0}, {"kind": "call", "value": True},
|
|
102
|
+
{"kind": "call", "value": 0, "limit": 0},
|
|
103
|
+
{"kind": "call", "value": 0, "limit": 10001},
|
|
104
|
+
{"kind": "call", "value": 0, "controls": [0] * 129},
|
|
105
|
+
{"kind": "call", "value": 0, "controls": [999]}):
|
|
106
|
+
config = copy.deepcopy(self.config)
|
|
107
|
+
config["query"] = query
|
|
108
|
+
with self.assertRaises(ValueError):
|
|
109
|
+
self.report(config)
|
|
110
|
+
|
|
111
|
+
def test_corrupt_or_mismatched_cache_is_refused(self):
|
|
112
|
+
report = self.report()
|
|
113
|
+
path = Path(self.folder.name) / (report["cacheKey"] + ".sqlite")
|
|
114
|
+
with closing(sqlite3.connect(path)) as db, db:
|
|
115
|
+
db.execute("UPDATE metadata SET value='{}'")
|
|
116
|
+
with self.assertRaisesRegex(ValueError, "identity differs"):
|
|
117
|
+
self.report()
|
|
118
|
+
path.write_bytes(b"not a database")
|
|
119
|
+
with self.assertRaisesRegex(ValueError, "cache failed"):
|
|
120
|
+
self.report()
|
|
121
|
+
|
|
122
|
+
def test_implicit_operands_branch_targets_and_prefixed_calls_are_not_candidates(self):
|
|
123
|
+
# MOV AX,[BX]; SHL AX,1; JE 0006; CALL with an operand-size override to 000C; RET.
|
|
124
|
+
data = bytes.fromhex("8b07d1e0740066e800000000c3")
|
|
125
|
+
config = copy.deepcopy(self.config)
|
|
126
|
+
config["regions"][0]["end"] = len(data)
|
|
127
|
+
for kind, value in (("displacement", 0), ("immediate", 1), ("immediate", 6), ("call", 12)):
|
|
128
|
+
config["query"] = {"kind": kind, "value": value}
|
|
129
|
+
self.assertEqual(self.report(config, data)["totalCandidates"], 0, (kind, value))
|
|
130
|
+
# The unprefixed CALL decoded inside the prefixed one still resolves.
|
|
131
|
+
config["query"] = {"kind": "call", "value": 10, "controls": [7]}
|
|
132
|
+
self.assertEqual(self.report(config, data)["rawCandidates"], [{"site": 7, "size": 3}])
|
|
133
|
+
|
|
134
|
+
def test_flat32_has_separate_mapping_and_candidates(self):
|
|
135
|
+
data = bytes.fromhex("b878563412c3")
|
|
136
|
+
config = copy.deepcopy(self.config)
|
|
137
|
+
config.update(bits=32, addressModel="flat32")
|
|
138
|
+
config["regions"][0].update(end=len(data), segment=0, ip=0x1000)
|
|
139
|
+
config["query"] = {"kind": "immediate", "value": 0x12345678, "controls": [0]}
|
|
140
|
+
self.assertEqual(self.report(config, data)["rawCandidates"], [{"site": 0, "size": 5}])
|
|
141
|
+
|
|
142
|
+
def test_concurrent_publication_of_the_same_corpus_is_kept(self):
|
|
143
|
+
# Windows refuses the replace while another run has the published corpus open.
|
|
144
|
+
replace = os.replace
|
|
145
|
+
|
|
146
|
+
def published_by_another_run(source, destination):
|
|
147
|
+
replace(source, destination)
|
|
148
|
+
raise PermissionError("in use")
|
|
149
|
+
with patch("scientific_method_engine.x86.scan_index.os.replace", side_effect=published_by_another_run):
|
|
150
|
+
report = self.report()
|
|
151
|
+
self.assertEqual(report["rawCandidates"], [{"site": 0, "size": 3}])
|
|
152
|
+
self.assertEqual(list(Path(self.folder.name).glob('*.partial')), [])
|
|
153
|
+
|
|
154
|
+
def test_disk_budget_failure_never_publishes_a_partial_index(self):
|
|
155
|
+
with patch("scientific_method_engine.x86.scan_index.MAX_INDEX_BYTES", 4096):
|
|
156
|
+
with self.assertRaisesRegex(ValueError, "build failed and published nothing"):
|
|
157
|
+
self.report()
|
|
158
|
+
self.assertEqual(list(Path(self.folder.name).glob('*.sqlite')), [])
|
|
159
|
+
self.assertEqual(list(Path(self.folder.name).glob('*.partial')), [])
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
if __name__ == "__main__":
|
|
163
|
+
unittest.main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_config_encoding.py
RENAMED
|
File without changes
|
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_derived_counts.py
RENAMED
|
File without changes
|
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_effect_order.py
RENAMED
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_enter_frames.py
RENAMED
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_entry_frame.py
RENAMED
|
File without changes
|
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_interrupt_models.py
RENAMED
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_inventory_check.py
RENAMED
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_join_terms.py
RENAMED
|
File without changes
|
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_memory_scopes.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_return_checks.py
RENAMED
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_successor_rule.py
RENAMED
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_table_bounds.py
RENAMED
|
File without changes
|
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_term_limit.py
RENAMED
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_upstream_gaps.py
RENAMED
|
File without changes
|
{scientific_method_engine-19.8.1 → scientific_method_engine-19.9.0}/tests/test_volatile_memory.py
RENAMED
|
File without changes
|
|
File without changes
|