Files
Senbei/senbei-io/src/report.rs
T
Momoko-Ayase 21236e8b98 feat(web): bring the Senbei workbench to the browser
Add folder scanning, Android library and package recovery, and the localized desktop-style workbench. Keep browser-refused outputs in a fallback ZIP and include logs with folder and ZIP exports.

Align the desktop and web modal transitions and polish the desktop About and legal dialogs.
2026-11-18 17:10:12 +08:00

1083 lines
39 KiB
Rust

//! Versioned, machine-readable run report.
//!
//! One [`Report`] describes one CLI invocation: which inputs were examined,
//! what each of them turned out to be, which routes were tried and selected,
//! what was written, and which defects were found. It is the only structure the
//! `--json` output serializes, so its shape is the contract reported by
//! [`REPORT_FORMAT_VERSION`].
//!
//! Two rules shape the fields:
//!
//! * Every value is something that was actually observed. Timings are recorded
//! only where a stage was measured (`None` otherwise); hashes are taken from
//! the bytes that were actually produced; a route is only marked selected if
//! a pipeline accepted it.
//! * Research inspection retains no output files. An [`Mode::Inspect`] run may
//! perform full static recovery to discover parameters, but selected routes,
//! recovered identities and validation verdicts come only from actual work.
//! Parsed input facts, bounded probe candidates and recovery traces stay
//! separate so local plausibility is never mistaken for a recovery verdict.
//!
//! A container (`.apk`/`.apks`/`.xapk`) is reported as one input with
//! [`Role::Container`]: its manifest inventory is listed separately from the
//! entries that were actually probed and restored, so a manifest list is never
//! mistaken for inspected protected content.
use std::path::Path;
use std::sync::Arc;
use std::time::Instant;
use serde::Serialize;
use sha2::{Digest as _, Sha256};
use senbei_i18n::{Cli, Lang, Localize, Message, Msg};
use senbei_engine::{
AttemptOutcome, LayoutKind, ProtectionEvidence, ProtectionProfile, RecognitionStatus,
RecoveryStatus, ValidationStatus,
};
mod text;
/// Version of the serialized [`Report`] schema.
pub const REPORT_FORMAT_VERSION: u32 = 1;
/// What the invocation was asked to do.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Mode {
/// Research analysis, including static recovery; retain no output or log.
Inspect,
/// Restore and write outputs.
Unpack,
/// Run the real recovery, keep nothing (no ordinary output, no log).
DryRun,
}
/// Whether the input is a protected file or a container of them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Role {
/// A single protected file, metadata blob, or unrecognized file.
Content,
/// An app package whose entries are handled one by one.
Container,
}
/// Outcome of one input, from the caller's point of view.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum InputStatus {
/// Produced output.
Ok,
/// Produced output that fails the static integrity check.
Suspect,
/// Inspection recognized a protected target. Its profile separately records
/// whether static recovery succeeded; no output file is retained.
Recognized,
/// Not a target, already handled, or a layout this build does not know.
Skipped,
/// A target that failed.
Error,
}
/// Outcome of one container entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EntryStatus {
/// Restored or de-obfuscated and written.
Restored,
/// Byte-identical content was already restored from another source.
Duplicate,
/// Probed and not a protected target.
NotTarget,
/// A metadata blob whose protected fields were already canonical.
Unchanged,
/// Listed in a container inventory. Not probed, so not a target claim.
Inventoried,
/// Recognized as a target but the restore failed.
Error,
}
/// What a container entry is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EntryKind {
SharedLibrary,
Metadata,
}
/// One measured or declared stage of an input.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Stage {
Detection,
ManifestInventory,
EntryProbe,
Recovery,
StaticValidation,
OutputWrite,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum StageStatus {
Ok,
Skipped,
Failed,
Blocked,
}
/// State of the `<input>._` companion relationship.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum CompanionState {
/// No sibling `._` file.
Absent,
/// A `._` payload was found and is the input's payload.
Spliced,
/// A sibling `._` exists but does not pair with this stub.
Unmatched,
/// A sibling `._` exists but could not be read.
Unreadable,
}
/// Size and SHA-256 of bytes that were actually produced.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Digest {
pub size: u64,
pub sha256: String,
}
impl Digest {
/// Digest of `bytes`.
pub fn of(bytes: &[u8]) -> Self {
let mut hasher = Sha256::new();
hasher.update(bytes);
Self {
size: bytes.len() as u64,
sha256: senbei_crypto::hex_digest(&hasher.finalize()),
}
}
}
/// One output file that was left on disk.
///
/// `sha256` is present only when the producing step had the bytes in hand. An
/// engine that streams straight to its output file reports size alone rather
/// than re-reading the file just to hash it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct OutputFile {
pub path: String,
pub size: u64,
pub sha256: Option<String>,
}
impl OutputFile {
/// Output whose bytes are known.
pub fn from_bytes(path: &Path, bytes: &[u8]) -> Self {
Self {
path: path.display().to_string(),
size: bytes.len() as u64,
sha256: Some(Digest::of(bytes).sha256),
}
}
/// Output that an engine streamed to disk.
///
/// Size is observed from the file; no hash is reported because hashing
/// would mean reading a potentially huge output back solely for the
/// report. `sha256` is `Some` only where the producing step already held
/// the bytes.
pub fn from_path(path: &Path) -> Self {
Self {
path: path.display().to_string(),
size: std::fs::metadata(path).map(|meta| meta.len()).unwrap_or(0),
sha256: None,
}
}
}
/// Non-target tallies from a folder scan, kept apart from input results.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
pub struct ScanReport {
/// Named candidates that were probed and matched no detector.
pub non_target_skipped: usize,
/// Directory entries the walk could not read.
pub walk_errors: usize,
/// Selected files whose content probe failed.
pub probe_errors: usize,
}
/// The companion relationship recorded for one Windows input.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CompanionReport {
pub state: CompanionState,
pub path: Option<String>,
pub detail: Option<Message>,
/// Identity of the physical companion, distinct from the spliced view.
pub source: Option<Digest>,
}
/// Which bytes the research report's input offsets address.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ResearchInputView {
Original,
/// The loader's header page followed by its matching external payload.
SplicedCompanion,
}
/// Structural observations and actual static-recovery discoveries for a PE.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WindowsResearchReport {
pub input_view: ResearchInputView,
pub input: senbei_engine::windows::WindowsInspection,
/// The post-overlay image, inspected before its bytes were discarded.
pub restored: Option<senbei_engine::windows::WindowsInspection>,
pub trace: senbei_engine::windows::ResearchTrace,
}
/// Existing Android engine reports captured from the actual static pipeline.
///
/// The nested schemas belong to the extraction/restoration engines. Temporary
/// output locations are replaced with logical labels by the I/O adapter.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AndroidResearchReport {
pub extraction: serde_json::Value,
pub restoration: Option<serde_json::Value>,
}
/// One manifest parsed from a container.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ManifestReport {
/// Where the manifest was read from: the package, or `package::entry`.
pub source: String,
pub package: Option<String>,
pub version: Option<String>,
/// True only when the manifest named both a package and a version.
pub named: bool,
}
/// Container inventory: membership facts, not inspected content.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ContainerReport {
/// `apk`, `apks`, or `xapk`.
pub kind: String,
pub manifests: Vec<ManifestReport>,
/// Central-directory entry count of the top-level package.
pub entry_count: usize,
/// Entry names that would be probed during a restore.
pub candidate_entries: Vec<String>,
/// Nested packages discovered at the top level of a bundle.
pub nested_packages: Vec<String>,
}
/// One stage of an input, with its measured duration when it was timed.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StageReport {
pub stage: Stage,
pub status: StageStatus,
pub detail: Option<Message>,
pub duration_ms: Option<u64>,
}
impl StageReport {
pub fn new(stage: Stage, status: StageStatus) -> Self {
Self {
stage,
status,
detail: None,
duration_ms: None,
}
}
pub fn detail(mut self, detail: impl Into<Message>) -> Self {
self.detail = Some(detail.into());
self
}
pub fn timed(mut self, elapsed_ms: u64) -> Self {
self.duration_ms = Some(elapsed_ms);
self
}
}
/// One entry of a container, or one artifact produced for it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct EntryReport {
/// Source label, e.g. `base.apk::lib/arm64-v8a/libil2cpp.so`.
pub name: String,
pub status: EntryStatus,
pub analysis_incomplete: bool,
pub kind: Option<EntryKind>,
/// Route selected for this entry, when one was.
pub layout: Option<LayoutKind>,
pub metadata_version: Option<u32>,
pub remapped: Option<usize>,
pub android_research: Option<AndroidResearchReport>,
pub metadata_research: Option<serde_json::Value>,
/// Libraries whose decoded module was paired with this metadata entry.
pub provenance: Vec<String>,
/// Where the restored bytes were written (absent in dry-run/inspect).
pub output: Option<OutputFile>,
pub warnings: Vec<Message>,
pub errors: Vec<Message>,
}
impl EntryReport {
pub fn new(name: impl Into<String>, status: EntryStatus) -> Self {
Self {
name: name.into(),
status,
analysis_incomplete: false,
kind: None,
layout: None,
metadata_version: None,
remapped: None,
android_research: None,
metadata_research: None,
provenance: Vec::new(),
output: None,
warnings: Vec::new(),
errors: Vec::new(),
}
}
}
/// Everything observed for one input.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct InputReport {
pub path: String,
pub role: Role,
pub status: InputStatus,
/// I/O, resource limits or infrastructure prevented the requested analysis.
/// A rejected recovery candidate alone does not set this flag.
pub analysis_incomplete: bool,
/// Identity of the physical input, never of a synthetic spliced view.
pub source: Option<Digest>,
pub research: Option<WindowsResearchReport>,
pub android_research: Option<AndroidResearchReport>,
pub metadata_research: Option<serde_json::Value>,
/// Engine profile: recognition evidence, candidate routes, and — only when
/// recovery actually ran — the selected route and static-validation verdict.
pub profile: Option<ProtectionProfile>,
pub companion: Option<CompanionReport>,
pub container: Option<ContainerReport>,
pub entries: Vec<EntryReport>,
/// Output files left on disk. Empty for inspect and dry-run.
pub outputs: Vec<OutputFile>,
/// Digest of the recovered image, when recovery produced one.
pub recovered: Option<Digest>,
/// Metadata header identity, when the input was an il2cpp metadata blob.
pub metadata: Option<senbei_metadata::MetadataIdentity>,
/// Raw il2cpp metadata version, duplicated for flat consumers.
pub metadata_version: Option<u32>,
/// Libraries whose decoded module was paired with this metadata input.
pub provenance: Vec<String>,
pub stages: Vec<StageReport>,
pub warnings: Vec<Message>,
pub errors: Vec<Message>,
pub duration_ms: u64,
}
impl InputReport {
pub fn new(path: &Path, role: Role) -> Self {
Self {
path: path.display().to_string(),
role,
status: InputStatus::Skipped,
analysis_incomplete: false,
source: None,
research: None,
android_research: None,
metadata_research: None,
profile: None,
companion: None,
container: None,
entries: Vec::new(),
outputs: Vec::new(),
recovered: None,
metadata: None,
metadata_version: None,
provenance: Vec::new(),
stages: Vec::new(),
warnings: Vec::new(),
errors: Vec::new(),
duration_ms: 0,
}
}
pub fn warning(&mut self, message: impl Into<Message>) {
self.warnings.push(message.into());
}
pub fn error(&mut self, message: impl Into<Message>) {
self.errors.push(message.into());
}
}
/// Counts over the whole invocation.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct SummaryReport {
/// Inputs examined.
pub inputs: usize,
/// Inputs that produced output.
pub recovered: usize,
/// Inputs whose protection was recognized during research inspection.
/// This count is independent of whether their static recovery succeeded.
pub recognized: usize,
/// Inputs that were not targets or were left untouched.
pub skipped: usize,
/// Inputs whose output fails the static integrity check.
pub suspect: usize,
/// Inputs that failed.
pub errors: usize,
/// il2cpp metadata blobs de-obfuscated, including embedded ones.
pub metadata: usize,
/// App packages opened.
pub packages: usize,
}
/// Identity of the tool that produced the report.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Tool {
pub name: String,
pub version: String,
}
/// One invocation's report.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Report {
pub format_version: u32,
pub tool: Tool,
pub mode: Mode,
/// Folder scan tallies. `None` for a single-file run.
pub scan: Option<ScanReport>,
pub inputs: Vec<InputReport>,
pub summary: SummaryReport,
/// Run-level failures that are not attributable to one input.
pub errors: Vec<Message>,
pub duration_ms: u64,
/// The run was cancelled before every input was processed. Only a front
/// end can cancel, so the CLI's JSON never carries this field.
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub cancelled: bool,
}
impl Report {
/// Serialize as one JSON document.
pub fn to_json(&self) -> String {
// The report contains only owned, serializable values.
serde_json::to_string(self).unwrap_or_else(|error| {
format!(
"{{\"format_version\":{REPORT_FORMAT_VERSION},\"error\":\"report serialization failed: {error}\"}}"
)
})
}
/// Plain English summary in the same shape as [`crate::job::Summary::line`].
pub fn summary_line(&self) -> String {
self.summary_line_in(crate::ui::Palette::plain(), Lang::En)
}
/// Console summary in the process language, with colors for actual
/// nonzero outcomes.
pub fn summary_line_with_palette(&self, palette: crate::ui::Palette) -> String {
self.summary_line_in(palette, senbei_i18n::current())
}
/// The summary line in `lang`.
pub fn summary_line_in(&self, palette: crate::ui::Palette, lang: Lang) -> String {
let s = &self.summary;
crate::ui::summary_line(
palette,
lang,
[
s.recovered,
s.skipped,
s.errors,
s.suspect,
s.metadata,
s.packages,
],
)
}
/// Plain English rendering, independent of the destination terminal.
pub fn render_text(&self) -> String {
self.render_text_in(crate::ui::Palette::plain(), Lang::En)
}
/// Console rendering in the process language. Report data and JSON remain
/// unstyled; callers select the destination's color policy.
pub fn render_text_with_palette(&self, palette: crate::ui::Palette) -> String {
self.render_text_in(palette, senbei_i18n::current())
}
/// Human-readable rendering in `lang`. Only the summary lines are
/// translated; structural research evidence stays English.
pub fn render_text_in(&self, palette: crate::ui::Palette, lang: Lang) -> String {
use crate::ui::Tone;
let mut text = String::new();
if let Some(scan) = self.scan {
text.push_str(&format!(
"{}: {}\n",
palette.paint(Tone::Heading, Cli::ReportScanHeading.localize(lang)),
Cli::ReportScan {
skipped: scan.non_target_skipped,
walk_errors: scan.walk_errors,
probe_errors: scan.probe_errors,
}
.localize(lang)
));
if scan.walk_errors > 0 || scan.probe_errors > 0 {
text.push_str(&format!(
"{}: {}\n",
palette.paint(Tone::Warning, Cli::Warning.localize(lang)),
Cli::ReportScanIncomplete.localize(lang)
));
}
}
for input in &self.inputs {
text.push_str(&text::render_input(input, palette, lang));
}
for error in &self.errors {
text.push_str(&crate::ui::error_lines(palette, lang, error));
}
text
}
}
/// Accumulates inputs during one invocation.
pub struct Collector {
mode: Mode,
started: Instant,
scan: Option<ScanReport>,
inputs: Vec<InputReport>,
summary: SummaryReport,
errors: Vec<Message>,
observer: Option<Arc<dyn crate::observe::RunObserver>>,
cancel: Option<crate::observe::CancelToken>,
cancelled: bool,
}
impl Collector {
pub fn new(mode: Mode) -> Self {
Self {
mode,
started: Instant::now(),
scan: None,
inputs: Vec::new(),
summary: SummaryReport::default(),
errors: Vec::new(),
observer: None,
cancel: None,
cancelled: false,
}
}
/// Tell `observer` about each input as the run reaches and finishes it.
pub fn with_observer(mut self, observer: Arc<dyn crate::observe::RunObserver>) -> Self {
self.observer = Some(observer);
self
}
/// Stop the run at the next input once `cancel` is set.
pub fn with_cancel(mut self, cancel: crate::observe::CancelToken) -> Self {
self.cancel = Some(cancel);
self
}
pub(crate) fn observer(&self) -> Option<&Arc<dyn crate::observe::RunObserver>> {
self.observer.as_ref()
}
/// True once cancellation was requested. The report then records that the
/// run stopped early.
pub(crate) fn should_stop(&mut self) -> bool {
if self
.cancel
.as_ref()
.is_some_and(crate::observe::CancelToken::is_cancelled)
{
self.cancelled = true;
}
self.cancelled
}
pub fn mode(&self) -> Mode {
self.mode
}
/// Record scan tallies and keep unexamined inputs visible to exit policies.
pub fn set_scan(&mut self, scan: ScanReport) {
let previous = self
.scan
.map_or(0, |scan| scan.walk_errors.saturating_add(scan.probe_errors));
let failed = scan.walk_errors.saturating_add(scan.probe_errors);
self.summary.errors = self
.summary
.errors
.saturating_sub(previous)
.saturating_add(failed);
self.scan = Some(scan);
}
/// Record one finished input and fold its status into the summary.
pub fn push(&mut self, mut input: InputReport) {
if self.mode == Mode::Inspect
&& (input.analysis_incomplete
|| input.entries.iter().any(|entry| entry.analysis_incomplete))
{
input.analysis_incomplete = true;
input.status = InputStatus::Error;
}
if let Some(observer) = &self.observer {
observer.produced(&input);
}
if matches!(self.mode, Mode::DryRun | Mode::Inspect) {
// Research inspection and dry runs keep their actual recovery
// outcomes, but never advertise temporary artifacts as outputs.
let discarded = if self.mode == Mode::Inspect {
Msg::OutputNotRetained
} else {
Msg::DryRunDiscarded
};
if !input.outputs.is_empty() {
// The produced byte count is a real result of the attempt even
// though the file itself is not kept. A hash is only carried
// over when the producing step actually computed one.
if input.recovered.is_none()
&& let Some(first) = input.outputs.first()
&& let Some(sha256) = &first.sha256
{
input.recovered = Some(Digest {
size: first.size,
sha256: sha256.clone(),
});
}
for stage in &mut input.stages {
if stage.stage == Stage::OutputWrite {
stage.status = StageStatus::Skipped;
stage.detail = Some(Message::new(discarded.clone()));
}
}
if !input
.stages
.iter()
.any(|stage| stage.stage == Stage::OutputWrite)
{
input.stages.push(
StageReport::new(Stage::OutputWrite, StageStatus::Skipped)
.detail(discarded),
);
}
input.outputs.clear();
}
for entry in &mut input.entries {
entry.output = None;
}
}
self.summary.inputs += 1;
match input.status {
InputStatus::Ok => self.summary.recovered += 1,
InputStatus::Suspect => {
self.summary.recovered += 1;
self.summary.suspect += 1;
}
InputStatus::Recognized => {
self.summary.recognized += 1;
if self.mode == Mode::Inspect
&& let Some(profile) = &input.profile
{
if profile.recovery == RecoveryStatus::Succeeded {
self.summary.recovered += 1;
}
if matches!(
profile.static_validation,
ValidationStatus::Warnings | ValidationStatus::Failed
) {
self.summary.suspect += 1;
}
}
}
InputStatus::Skipped => self.summary.skipped += 1,
InputStatus::Error => self.summary.errors += 1,
}
if let Some(observer) = &self.observer {
observer.input_finished(&input);
}
self.inputs.push(input);
}
pub fn summary_mut(&mut self) -> &mut SummaryReport {
&mut self.summary
}
/// Record a run-level failure that is not attributable to one input.
pub fn push_error(&mut self, message: impl Into<Message>) {
self.errors.push(message.into());
}
pub fn finish(self) -> Report {
Report {
format_version: REPORT_FORMAT_VERSION,
tool: Tool {
name: "senbei".to_owned(),
version: env!("CARGO_PKG_VERSION").to_owned(),
},
mode: self.mode,
scan: self.scan,
inputs: self.inputs,
summary: self.summary,
errors: self.errors,
duration_ms: self.started.elapsed().as_millis() as u64,
cancelled: self.cancelled,
}
}
}
pub(crate) fn now_ms(since: Instant) -> u64 {
since.elapsed().as_millis() as u64
}
// ---------------------------------------------------------------------------
// Profile helpers shared by the platform adapters
// ---------------------------------------------------------------------------
/// Profile for a protected Android library whose restore actually succeeded.
///
/// The staged route is recorded as attempted and selected because the restore
/// wrote an image; no probe is repeated here.
pub(crate) fn android_recovered_profile() -> ProtectionProfile {
let mut profile = ProtectionProfile::new(senbei_engine::ObservedTarget::android(Some(
senbei_engine::TargetFormat::Elf64Aarch64,
)));
profile.recognition = RecognitionStatus::Recognized;
profile.evidence = vec![
ProtectionEvidence::PrivateSection,
ProtectionEvidence::AesSchedule,
];
profile.candidates = vec![senbei_engine::LayoutAttempt {
layout: LayoutKind::AndroidStaged,
outcome: AttemptOutcome::Succeeded,
warnings: Vec::new(),
errors: Vec::new(),
}];
profile.selected = Some(LayoutKind::AndroidStaged);
profile.recovery = RecoveryStatus::Succeeded;
profile
}
/// Profile for a library the scan selected but whose restore failed.
///
/// Recognition is inherited from the scan probe that selected the file; the
/// route is recorded as failed with the error that stopped it.
pub(crate) fn android_failed_profile(error: &Message) -> ProtectionProfile {
let mut profile = ProtectionProfile::new(senbei_engine::ObservedTarget::android(None));
profile.recognition = RecognitionStatus::Recognized;
profile.candidates = vec![senbei_engine::LayoutAttempt {
layout: LayoutKind::AndroidStaged,
outcome: AttemptOutcome::Failed,
warnings: Vec::new(),
errors: vec![error.clone()],
}];
profile.recovery = RecoveryStatus::Failed;
profile.push_error(error.clone());
profile
}
/// Profile for a metadata blob whose transform changed protected fields.
///
/// A metadata magic match alone is not protection evidence, so this is only
/// used after the transform returned `remapped > 0` and validated.
pub(crate) fn metadata_recovered_profile(
identity: Option<senbei_metadata::MetadataIdentity>,
route: LayoutKind,
) -> ProtectionProfile {
let mut profile = ProtectionProfile::new(senbei_engine::ObservedTarget::metadata(
identity.map(|identity| identity.raw_version),
));
profile.recognition = RecognitionStatus::Recognized;
profile.evidence = vec![ProtectionEvidence::ValidatedMetadataTransform];
profile.candidates = vec![senbei_engine::LayoutAttempt {
layout: route,
outcome: AttemptOutcome::Succeeded,
warnings: Vec::new(),
errors: Vec::new(),
}];
profile.selected = Some(route);
profile.recovery = RecoveryStatus::Succeeded;
// Not a PE image: the PE static check does not apply to metadata output.
profile.static_validation = ValidationStatus::NotRun;
profile
}
/// Profile for a metadata blob that was identified but needed no change.
///
/// Nothing was transformed, so no protection evidence is claimed.
pub(crate) fn metadata_unchanged_profile(
identity: Option<senbei_metadata::MetadataIdentity>,
) -> ProtectionProfile {
let mut profile = ProtectionProfile::new(senbei_engine::ObservedTarget::metadata(
identity.map(|identity| identity.raw_version),
));
profile.recognition = RecognitionStatus::NotRecognized;
profile.static_validation = ValidationStatus::NotRun;
profile
}
/// Profile for a metadata blob whose transform ran, validated, and needed no
/// change.
///
/// The route was really taken and the bytes really validated, so the attempt is
/// recorded — but a clean blob is no protection evidence, so recognition stays
/// `not_recognized` and no protection verdict is claimed. Without this an
/// inspection of an already-clean blob would say `not_attempted` while the
/// research section next to it showed the route and its report.
pub(crate) fn metadata_validated_unchanged_profile(
identity: Option<senbei_metadata::MetadataIdentity>,
route: LayoutKind,
) -> ProtectionProfile {
let mut profile = ProtectionProfile::new(senbei_engine::ObservedTarget::metadata(
identity.map(|identity| identity.raw_version),
));
profile.candidates = vec![senbei_engine::LayoutAttempt {
layout: route,
outcome: AttemptOutcome::Succeeded,
warnings: Vec::new(),
errors: Vec::new(),
}];
profile.selected = Some(route);
profile.recovery = RecoveryStatus::Succeeded;
profile.static_validation = ValidationStatus::NotRun;
profile.recognition = RecognitionStatus::NotRecognized;
profile
}
/// Profile for a metadata blob whose transform failed.
pub(crate) fn metadata_failed_profile(
identity: Option<senbei_metadata::MetadataIdentity>,
route: LayoutKind,
error: &Message,
) -> ProtectionProfile {
let mut profile = ProtectionProfile::new(senbei_engine::ObservedTarget::metadata(
identity.map(|identity| identity.raw_version),
));
// An attempted transform that did not validate establishes no protection
// verdict, even when the metadata header identifies a known version.
profile.recognition = RecognitionStatus::Blocked;
profile.candidates = vec![senbei_engine::LayoutAttempt {
layout: route,
outcome: AttemptOutcome::Failed,
warnings: Vec::new(),
errors: vec![error.clone()],
}];
profile.recovery = RecoveryStatus::Failed;
profile.push_error(error.clone());
profile.static_validation = ValidationStatus::NotRun;
profile
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_input(status: InputStatus) -> InputReport {
let mut input = InputReport::new(Path::new("a.exe"), Role::Content);
input.status = status;
input.recovered = Some(Digest::of(b"bytes"));
input
}
#[test]
fn a_failed_metadata_transform_cannot_claim_protection() {
for version in [31u32, 999] {
let mut header = 0xFAB1_1BAFu32.to_le_bytes().to_vec();
header.extend_from_slice(&version.to_le_bytes());
let profile = metadata_failed_profile(
senbei_metadata::identify(&header),
LayoutKind::MetadataStructural,
&"transform did not validate".into(),
);
assert_eq!(profile.observed.metadata_version, Some(version));
assert_eq!(profile.recognition, RecognitionStatus::Blocked);
assert!(profile.evidence.is_empty());
assert_eq!(profile.recovery, RecoveryStatus::Failed);
assert_eq!(profile.static_validation, ValidationStatus::NotRun);
assert!(profile.selected.is_none());
}
}
#[test]
fn report_round_trips_through_json_with_a_version_field() {
let mut collector = Collector::new(Mode::DryRun);
collector.push(sample_input(InputStatus::Ok));
collector.push(sample_input(InputStatus::Suspect));
let report = collector.finish();
assert_eq!(report.format_version, REPORT_FORMAT_VERSION);
let json = report.to_json();
let value: serde_json::Value = serde_json::from_str(&json).expect("valid json");
assert_eq!(value["format_version"], REPORT_FORMAT_VERSION);
assert_eq!(value["mode"], "dry_run");
assert_eq!(value["summary"]["inputs"], 2);
assert_eq!(value["summary"]["suspect"], 1);
assert_eq!(value["summary"]["errors"], 0);
assert_eq!(value["tool"]["name"], "senbei");
assert!(value["inputs"][0]["recovered"]["sha256"].is_string());
}
#[test]
fn console_colors_never_enter_report_data_or_plain_rendering() {
use crate::ui::{Palette, Tone};
let mut collector = Collector::new(Mode::Inspect);
collector.push(sample_input(InputStatus::Error));
collector.set_scan(ScanReport {
walk_errors: 1,
..ScanReport::default()
});
collector.push_error("input could not be examined");
let report = collector.finish();
let json = report.to_json();
let plain = report.render_text();
let colored = report.render_text_with_palette(Palette::colored());
assert!(colored.contains(&Palette::colored().paint(Tone::Heading, "scan").to_string()));
assert!(colored.contains(&Palette::colored().paint(Tone::Error, "error").to_string()));
assert!(!plain.contains('\x1b'));
assert!(!json.contains('\x1b'));
assert!(!json.contains("\\u001b"));
assert_eq!(report.to_json(), json);
assert_eq!(report.render_text_with_palette(Palette::plain()), plain);
}
#[test]
fn error_status_counts_and_surfaces_in_the_summary() {
let mut collector = Collector::new(Mode::Unpack);
collector.push(sample_input(InputStatus::Error));
collector.push(sample_input(InputStatus::Skipped));
let report = collector.finish();
assert_eq!(report.summary.errors, 1);
assert_eq!(report.summary.skipped, 1);
assert_eq!(report.summary.recovered, 0);
}
#[test]
fn research_counts_actual_recovery_and_suspect_without_retaining_output_paths() {
let mut input = sample_input(InputStatus::Recognized);
let mut profile =
ProtectionProfile::new(senbei_engine::ObservedTarget::windows(None, None));
profile.recognition = RecognitionStatus::Recognized;
profile.recovery = RecoveryStatus::Succeeded;
profile.static_validation = ValidationStatus::Warnings;
input.profile = Some(profile);
input.outputs.push(OutputFile::from_bytes(
Path::new("temporary/image.exe"),
b"bytes",
));
let mut collector = Collector::new(Mode::Inspect);
collector.push(input);
let report = collector.finish();
assert_eq!(report.summary.recognized, 1);
assert_eq!(report.summary.recovered, 1);
assert_eq!(report.summary.suspect, 1);
assert_eq!(report.summary.errors, 0);
assert!(report.inputs[0].outputs.is_empty());
assert!(!report.to_json().contains("temporary/image.exe"));
assert!(
report.inputs[0]
.stages
.iter()
.any(|stage| stage.stage == Stage::OutputWrite
&& stage.status == StageStatus::Skipped)
);
}
#[test]
fn research_recovery_failure_is_a_finding_not_a_read_failure() {
let mut input = sample_input(InputStatus::Recognized);
let mut profile =
ProtectionProfile::new(senbei_engine::ObservedTarget::windows(None, None));
profile.recognition = RecognitionStatus::Recognized;
profile.recovery = RecoveryStatus::Failed;
profile.push_error("no candidate validated");
input.profile = Some(profile);
let mut collector = Collector::new(Mode::Inspect);
collector.push(input);
let report = collector.finish();
assert_eq!(report.summary.recognized, 1);
assert_eq!(report.summary.recovered, 0);
assert_eq!(report.summary.errors, 0);
assert!(report.render_text().contains("no candidate validated"));
}
#[test]
fn incomplete_research_cannot_be_hidden_by_a_recognized_status() {
let mut input = sample_input(InputStatus::Recognized);
let mut entry = EntryReport::new("package::library.so", EntryStatus::Error);
entry.analysis_incomplete = true;
entry
.errors
.push("resource ceiling prevented restoration".into());
input.entries.push(entry);
let mut collector = Collector::new(Mode::Inspect);
collector.push(input);
let report = collector.finish();
assert_eq!(report.summary.errors, 1);
assert_eq!(report.inputs[0].status, InputStatus::Error);
assert!(report.inputs[0].analysis_incomplete);
assert!(report.render_text().contains("analysis incomplete"));
}
#[test]
fn scan_failures_fail_report_only_modes_and_do_not_erase_input_errors() {
for mode in [Mode::Inspect, Mode::DryRun] {
let mut collector = Collector::new(mode);
collector.push(sample_input(InputStatus::Error));
collector.set_scan(ScanReport {
walk_errors: 2,
probe_errors: 3,
non_target_skipped: 4,
});
assert_eq!(collector.summary.errors, 6);
collector.set_scan(ScanReport {
walk_errors: 1,
..ScanReport::default()
});
let report = collector.finish();
assert_eq!(report.summary.errors, 2);
assert!(report.render_text().contains("analysis is incomplete"));
assert!(report.render_text().contains("1 walk errors"));
}
}
#[test]
fn digest_matches_a_known_sha256() {
let digest = Digest::of(b"abc");
assert_eq!(digest.size, 3);
assert_eq!(
digest.sha256,
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
}
#[test]
fn inspect_profile_helpers_never_claim_recovery() {
let unchanged = metadata_unchanged_profile(None);
assert_eq!(unchanged.recovery, RecoveryStatus::NotAttempted);
assert!(unchanged.selected.is_none());
assert!(unchanged.evidence.is_empty());
assert_eq!(unchanged.static_validation, ValidationStatus::NotRun);
let recovered = metadata_recovered_profile(None, LayoutKind::MetadataSeeded);
assert_eq!(recovered.selected, Some(LayoutKind::MetadataSeeded));
assert_eq!(
recovered.evidence,
vec![ProtectionEvidence::ValidatedMetadataTransform]
);
assert_eq!(recovered.static_validation, ValidationStatus::NotRun);
}
}