mirror of
https://github.com/Momoko-Ayase/Senbei.git
synced 2026-11-24 18:52:47 +08:00
fix(inspect): collect full static recovery research by default
Inspect the protected input and actual recovered image, with bounded structural evidence, source identities and scoped recovery traces. Keep ordinary unpack lightweight and retain no research output files or logs. Preserve Android extraction, restoration and metadata evidence, distinguish incomplete analyses from rejected candidates, and render detailed text and JSON reports with explicit address provenance.
This commit is contained in:
1 parent
05a4337a5e
commit
1ee4344c1e
22 files changed
+7776
-758
No files matched your search
+26
-16
@@ -143,16 +143,13 @@ impl Options {
|
||||
// rejected rather than guessed at.
|
||||
if command == Command::Inspect {
|
||||
if dry_run {
|
||||
return Err("'inspect' never recovers; drop --dry-run".to_owned());
|
||||
return Err("'inspect' writes nothing; drop --dry-run".to_owned());
|
||||
}
|
||||
if out.is_some() {
|
||||
return Err("'inspect' writes nothing; --out is not allowed".to_owned());
|
||||
}
|
||||
if strict_integrity {
|
||||
return Err(
|
||||
"'inspect' runs no static check; --strict-integrity is not allowed".to_owned(),
|
||||
);
|
||||
}
|
||||
// `--strict-integrity` is meaningful: inspection runs the real
|
||||
// static check and reports its verdict.
|
||||
}
|
||||
if machine {
|
||||
// Machine output owns stdout; interleaved progress would corrupt it.
|
||||
@@ -205,12 +202,14 @@ fn execute(options: &mut Options) -> u8 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Detection completes even when an input is not a target; only an input that
|
||||
/// could not be read makes inspection fail.
|
||||
/// Inspection reports what the input is and what happened when it was tried;
|
||||
/// only an input that could not be read makes it fail.
|
||||
fn execute_inspect(options: &Options, path: &Path, scan_all: bool) -> u8 {
|
||||
match job::inspect_path(path, scan_all, &options.limits) {
|
||||
Ok(report) => {
|
||||
let code = report_exit_code(&report, false);
|
||||
// A recovery that ran and produced a suspect image is exactly what
|
||||
// `--strict-integrity` asks about, so it applies here too.
|
||||
let code = report_exit_code(&report, options.strict_integrity);
|
||||
if options.machine {
|
||||
println!("{}", report.to_json());
|
||||
} else if options.quiet < 2 {
|
||||
@@ -395,10 +394,13 @@ fn print_help() {
|
||||
);
|
||||
println!(
|
||||
" inspect PATH\n\
|
||||
\x20 detect only. Reports what each input is, whether it is\n\
|
||||
\x20 recognized, and which recovery routes would be tried.\n\
|
||||
\x20 Never recovers, never writes an output, never creates a log,\n\
|
||||
\x20 and never claims a route succeeded or a check passed."
|
||||
\x20 run the real static recovery for research, and report\n\
|
||||
\x20 everything observed: recognition evidence, each route\n\
|
||||
\x20 tried and why it was rejected, the route selected, the\n\
|
||||
\x20 static check's verdict, and the engines' own structural\n\
|
||||
\x20 evidence (protected-header fields, seeds, module records,\n\
|
||||
\x20 restored-image facts). Nothing is kept: no output file,\n\
|
||||
\x20 no log, no temporary tree, and the input is never run."
|
||||
);
|
||||
println!(" unpack PATH detection and recovery; writes outputs (the default).");
|
||||
println!(
|
||||
@@ -423,7 +425,9 @@ fn print_help() {
|
||||
\x20 exit non-zero when any output fails the static integrity\n\
|
||||
\x20 check (a suspect) or a file errors. Off by default: a\n\
|
||||
\x20 suspect is still written and the process exits 0 unless\n\
|
||||
\x20 a file errors. Quiet levels are unchanged."
|
||||
\x20 a file errors. Quiet levels are unchanged.\n\
|
||||
\x20 With 'inspect', the check applies to the image actually\n\
|
||||
\x20 recovered for research; no output file is retained."
|
||||
);
|
||||
let defaults = ResourceLimits::default();
|
||||
println!(
|
||||
@@ -638,16 +642,22 @@ mod command_line {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inspect_rejects_recovery_only_flags() {
|
||||
fn inspect_rejects_writing_flags() {
|
||||
assert!(parse(&["inspect", "--out", "o", "app.exe"]).is_err());
|
||||
assert!(parse(&["inspect", "--dry-run", "app.exe"]).is_err());
|
||||
assert!(parse(&["inspect", "--strict-integrity", "app.exe"]).is_err());
|
||||
assert_eq!(
|
||||
parse(&["inspect", "--json", "a"]).unwrap().command,
|
||||
Command::Inspect
|
||||
);
|
||||
}
|
||||
|
||||
/// Inspection runs the real static check, so asking it to be strict about
|
||||
/// that verdict is meaningful, not contradictory.
|
||||
#[test]
|
||||
fn inspect_accepts_strict_integrity() {
|
||||
assert!(parse(&["inspect", "--strict-integrity", "app.exe"]).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_subcommand_without_a_path_is_a_usage_error() {
|
||||
assert!(parse(&["inspect"]).is_err());
|
||||
|
||||
+159
-12
@@ -175,8 +175,10 @@ fn inspect_writes_no_output_no_log_and_no_temporary() {
|
||||
);
|
||||
}
|
||||
|
||||
/// Inspection reports the real attempt but keeps nothing: no output file is
|
||||
/// advertised, and a non-target never gets a route selected for it.
|
||||
#[test]
|
||||
fn inspect_never_claims_recovery_or_validation() {
|
||||
fn inspect_keeps_nothing_and_claims_no_route_for_a_non_target() {
|
||||
let td = tempfile::tempdir().unwrap();
|
||||
write_plain(td.path(), "plain.exe");
|
||||
let output = run(&["inspect", td.path().to_str().unwrap(), "--json"]);
|
||||
@@ -184,16 +186,17 @@ fn inspect_never_claims_recovery_or_validation() {
|
||||
for input in value["inputs"].as_array().unwrap() {
|
||||
assert!(
|
||||
input["profile"]["selected"].is_null(),
|
||||
"inspect must not select a route: {input}"
|
||||
"a non-target selects no route: {input}"
|
||||
);
|
||||
assert_eq!(input["profile"]["recovery"], "not_attempted");
|
||||
assert_eq!(input["profile"]["static_validation"], "not_run");
|
||||
assert_eq!(input["profile"]["recognition"], "not_recognized");
|
||||
assert_eq!(input["status"], "skipped");
|
||||
assert!(
|
||||
input["outputs"].as_array().unwrap().is_empty(),
|
||||
"inspect must not report outputs"
|
||||
);
|
||||
assert!(input["recovered"].is_null());
|
||||
}
|
||||
assert_eq!(value["summary"]["errors"], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -276,6 +279,32 @@ fn dry_run_json_describes_an_attempt_that_really_ran() {
|
||||
assert_eq!(reported["status"], "error");
|
||||
}
|
||||
|
||||
/// Only inspection keeps the research payloads; an ordinary unpack must not
|
||||
/// serialize or clone them.
|
||||
#[test]
|
||||
fn unpack_and_dry_run_carry_no_research_payload() {
|
||||
let td = tempfile::tempdir().unwrap();
|
||||
let input = write_metadata(td.path(), 31);
|
||||
let out = td.path().join("out");
|
||||
let unpack = run(&[
|
||||
input.to_str().unwrap(),
|
||||
"--out",
|
||||
out.to_str().unwrap(),
|
||||
"--json",
|
||||
]);
|
||||
let dry_run = run(&["--dry-run", input.to_str().unwrap(), "--json"]);
|
||||
|
||||
for output in [unpack, dry_run] {
|
||||
let value = parse_json(&output);
|
||||
for reported in value["inputs"].as_array().unwrap() {
|
||||
assert!(reported["research"].is_null(), "{reported}");
|
||||
assert!(reported["android_research"].is_null(), "{reported}");
|
||||
assert!(reported["metadata_research"].is_null(), "{reported}");
|
||||
assert!(reported["source"].is_null(), "{reported}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dry_run_refuses_an_output_directory() {
|
||||
let output = run(&["--dry-run", "--out", "out", "plain.exe"]);
|
||||
@@ -337,15 +366,27 @@ fn inspect_help_is_documented() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inspect_rejects_recovery_only_flags() {
|
||||
for flag in ["--out", "--strict-integrity"] {
|
||||
let output = run(&["inspect", "--json", flag, "x"]);
|
||||
assert_eq!(output.status.code(), Some(2), "{flag}");
|
||||
}
|
||||
fn inspect_rejects_writing_flags() {
|
||||
let output = run(&["inspect", "--json", "--out", "x"]);
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(2),
|
||||
"--out writes, inspect keeps nothing"
|
||||
);
|
||||
let output = run(&["inspect", "--dry-run", "x"]);
|
||||
assert_eq!(output.status.code(), Some(2));
|
||||
}
|
||||
|
||||
/// Inspection runs the real static check, so `--strict-integrity` is a real
|
||||
/// request about a real verdict rather than a contradiction.
|
||||
#[test]
|
||||
fn inspect_accepts_strict_integrity() {
|
||||
let td = tempfile::tempdir().unwrap();
|
||||
let input = write_plain(td.path(), "plain.exe");
|
||||
let output = run(&["inspect", input.to_str().unwrap(), "--strict-integrity"]);
|
||||
assert_ne!(output.status.code(), Some(2), "not a usage error");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Containers and companions
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -459,13 +500,16 @@ fn inspect_reports_a_container_inventory_separately_from_its_entries() {
|
||||
assert_eq!(manifests[0]["package"], "com.example.app");
|
||||
assert_eq!(manifests[0]["version"], "42");
|
||||
assert_eq!(manifests[0]["named"], true);
|
||||
// Only content-probe candidates are listed, and nothing was probed.
|
||||
// Inventory is membership: the entries a restore would probe, listed apart
|
||||
// from the entries that were actually probed (which have their own result).
|
||||
let candidates = input["container"]["candidate_entries"].as_array().unwrap();
|
||||
assert_eq!(candidates.len(), 1);
|
||||
assert!(candidates[0].as_str().unwrap().ends_with("libil2cpp.so"));
|
||||
assert!(input["entries"].as_array().unwrap().is_empty());
|
||||
let entries = input["entries"].as_array().unwrap();
|
||||
assert_eq!(entries.len(), 1, "the candidate was probed: {input}");
|
||||
assert_eq!(entries[0]["status"], "not_target");
|
||||
assert!(input["outputs"].as_array().unwrap().is_empty());
|
||||
// Inventory is membership, not inspected protected content.
|
||||
// The container itself has no single profile; its entries do the talking.
|
||||
assert!(input["profile"].is_null());
|
||||
assert_eq!(input["status"], "skipped");
|
||||
}
|
||||
@@ -514,6 +558,56 @@ fn inspect_reports_a_non_matching_companion_without_an_error() {
|
||||
assert!(input["outputs"].as_array().unwrap().is_empty());
|
||||
}
|
||||
|
||||
/// Inspection runs the real static recovery, so the report carries the attempt
|
||||
/// and the structural evidence behind it — while still keeping nothing.
|
||||
#[test]
|
||||
fn inspect_reports_the_attempt_and_its_structural_evidence() {
|
||||
let td = tempfile::tempdir().unwrap();
|
||||
let input = td.path().join("app.exe");
|
||||
std::fs::write(&input, fake_protected_pe()).unwrap();
|
||||
let before = tree(td.path());
|
||||
|
||||
let output = run(&["inspect", input.to_str().unwrap(), "--json"]);
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(0),
|
||||
"a failing trial is a finding"
|
||||
);
|
||||
let value = parse_json(&output);
|
||||
let reported = &value["inputs"][0];
|
||||
|
||||
// Recognition is evidence about the input.
|
||||
assert_eq!(reported["status"], "recognized");
|
||||
assert_eq!(reported["profile"]["recognition"], "recognized");
|
||||
// The input identity is the physical file, and it was inspected as-is.
|
||||
assert!(reported["source"]["sha256"].is_string());
|
||||
assert_eq!(reported["source"]["size"], 0x2000);
|
||||
assert_eq!(reported["research"]["input_view"], "original");
|
||||
// The header structure the inspector decoded is there, not just a verdict:
|
||||
// the recognized magic and the decoded header words it came from.
|
||||
let protection = &reported["research"]["input"]["protection"];
|
||||
assert_eq!(protection["magic_recognized"], true);
|
||||
let words = protection["header_words"].as_array().unwrap();
|
||||
assert!(!words.is_empty(), "decoded header words are reported");
|
||||
assert!(
|
||||
words.iter().any(|word| word["semantic"] == "magic"),
|
||||
"each word names the field it decodes to: {protection}"
|
||||
);
|
||||
// The recovery really ran: a candidate was attempted and no route survived
|
||||
// this synthetic image, so nothing is selected.
|
||||
assert_ne!(reported["profile"]["recovery"], "not_attempted");
|
||||
assert!(reported["profile"]["selected"].is_null());
|
||||
// Nothing was kept.
|
||||
assert!(reported["outputs"].as_array().unwrap().is_empty());
|
||||
assert!(reported["recovered"].is_null());
|
||||
assert_eq!(
|
||||
tree(td.path()),
|
||||
before,
|
||||
"inspection must not create, change, or remove anything"
|
||||
);
|
||||
assert_eq!(value["summary"]["errors"], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unpack_reports_the_companion_state_it_actually_used() {
|
||||
let td = tempfile::tempdir().unwrap();
|
||||
@@ -551,3 +645,56 @@ fn a_relative_path_is_reported_verbatim() {
|
||||
let value = parse_json(&output);
|
||||
assert_eq!(value["inputs"][0]["path"], "plain.exe");
|
||||
}
|
||||
|
||||
/// A resource ceiling on a real target is an incomplete analysis: the document
|
||||
/// says so and the process exits 1. It is never "recognized, therefore fine".
|
||||
#[test]
|
||||
fn inspect_of_an_over_limit_target_is_incomplete_and_exits_one() {
|
||||
let td = tempfile::tempdir().unwrap();
|
||||
let apk = td.path().join("base.apk");
|
||||
// A protected-library candidate far larger than the 1 MiB entry ceiling.
|
||||
let large = vec![0x7Fu8; 2 * 1024 * 1024];
|
||||
write_zip(
|
||||
&apk,
|
||||
&[
|
||||
("AndroidManifest.xml", MANIFEST.as_bytes()),
|
||||
("lib/arm64-v8a/libil2cpp.so", &large),
|
||||
],
|
||||
);
|
||||
|
||||
let output = run(&[
|
||||
"inspect",
|
||||
apk.to_str().unwrap(),
|
||||
"--json",
|
||||
"--max-entry-mib",
|
||||
"1",
|
||||
]);
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(1),
|
||||
"a truncated analysis is a failure: {output:?}"
|
||||
);
|
||||
let value = parse_json(&output);
|
||||
assert_eq!(value["summary"]["errors"], 1);
|
||||
let input = &value["inputs"][0];
|
||||
assert_eq!(input["status"], "error");
|
||||
assert_eq!(input["analysis_incomplete"], true);
|
||||
assert!(
|
||||
!input["errors"].as_array().unwrap().is_empty(),
|
||||
"the reason is on the report: {input}"
|
||||
);
|
||||
assert!(input["outputs"].as_array().unwrap().is_empty());
|
||||
}
|
||||
|
||||
/// An unsupported layout is a research finding, not an incomplete analysis.
|
||||
#[test]
|
||||
fn inspect_of_an_unsupported_target_stays_a_finding() {
|
||||
let td = tempfile::tempdir().unwrap();
|
||||
let input = write_plain(td.path(), "plain.exe");
|
||||
let output = run(&["inspect", input.to_str().unwrap(), "--json"]);
|
||||
assert_eq!(output.status.code(), Some(0), "{output:?}");
|
||||
let value = parse_json(&output);
|
||||
assert_eq!(value["summary"]["errors"], 0);
|
||||
let reported = &value["inputs"][0];
|
||||
assert_eq!(reported["analysis_incomplete"], false, "{reported}");
|
||||
}
|
||||
Reference in new issue
Block a user