//! Red-team probes derived from the Santh `sear` detonation engine's adversarial
//! sandbox-detection PoCs (`detonation/sear/tests/adversarial_sandbox_detection.rs`).
//!
//! sear wrote these as a *detector*: JavaScript that decides "am I being analysed
//! in a sandbox / by automation?". **Inverted for stealth**, each becomes a probe
//! a genuinely human browser must PASS — a real, stealthed Chrome must always
//! look like `REAL_BROWSER`, never `SANDBOX_DETECTED`. This is the red-vs-blue
//! self-test the consolidation plan calls for, sourced from our OWN detection
//! engine rather than a public anti-bot library — a capability no off-the-shelf
//! stealth crate has.
//!
//! Every classifier here is pure (`fn(&serde_json::Value) -> ProbeOutcome`) and
//! unit-tested below; the JS runs against a live `chromiumoxide::Page` via
//! [`super::run`].
use super::{Probe, ProbeOutcome, Severity};
/// Busy-loop timing coefficient-of-variation. A real browser on a real OS
/// scheduler shows jitter (CV ≳ 0.1); an instrumented or over-precise timer — a
/// sandbox tell sear exploits — collapses toward zero. Returns the CV, or `-1`
/// if the loop took 0 ms total (suspiciously instant).
const TIMING_CV_JS: &str = "(function(){var m=[];for(var i=0;i<20;i++){var s=Date.now();var x=0;for(var j=0;j<100000;j++){x+=j;}m.push(Date.now()-s);}var n=m.length;var mean=m.reduce(function(a,b){return a+b;},0)/n;if(mean<=0)return -1;var v=m.reduce(function(a,b){return a+(b-mean)*(b-mean);},0)/n;return Math.sqrt(v)/mean;})()";
/// Count of standard web-platform APIs that are ABSENT. A real Chrome has all of
/// them; sear's sandbox (and most headless/instrumented envs) is missing several.
const MISSING_APIS_JS: &str = "(function(){var c=0;if(typeof RTCPeerConnection==='undefined')c++;if(typeof indexedDB==='undefined')c++;if(typeof Notification==='undefined')c++;if(!(typeof navigator!=='undefined'&&'serviceWorker' in navigator))c++;if(!(typeof navigator!=='undefined'&&'mediaDevices' in navigator))c++;try{var cv=document.createElement('canvas');if(!cv.getContext('webgl')&&!cv.getContext('experimental-webgl'))c++;}catch(e){c++;}return c;})()";
/// Names of any automation-framework globals present on `window` (plus
/// `navigator.webdriver === true`). A real, stealthed browser leaks NONE.
const AUTOMATION_GLOBALS_JS: &str = "(function(){var leaks=[];var w=window;['__nightmare','_phantom','__phantomas','callPhantom','_selenium','__selenium_unwrapped','__webdriver_evaluate','__driver_evaluate','__webdriver_script_fn','__fxdriver_evaluate','__playwright','__puppeteer','domAutomation','domAutomationController'].forEach(function(k){try{if(typeof w[k]!=='undefined')leaks.push(k);}catch(e){}});try{for(var k in w){if(/^\\$?cdc_|^\\$?chrome_asyncScriptInfo|webdriver/i.test(k))leaks.push(k);}}catch(e){}try{if(navigator.webdriver===true)leaks.push('navigator.webdriver');}catch(e){}return leaks;})()";
/// `typeof document.all`. The HTML spec's `[[IsHTMLDDA]]` legacy slot makes a
/// real browser return the string `"undefined"` here; a naive shim that recreates
/// `document.all` as an ordinary object returns `"object"` and gives itself away.
const DOCUMENT_ALL_JS: &str = "(typeof document.all)";
/// Smallest positive `performance.now()` delta over a tight loop. A real browser
/// has a sub-millisecond high-resolution timer; `-1` means `performance.now` is
/// missing entirely (a strong sandbox tell sear's timer-entropy PoC keys on).
const TIMER_RESOLUTION_JS: &str = "(function(){if(typeof performance==='undefined'||typeof performance.now!=='function')return -1;var last=performance.now();var deltas=[];for(var i=0;i<50;i++){var n=performance.now();if(n>last)deltas.push(n-last);last=n;}if(deltas.length===0)return 0;return Math.min.apply(null,deltas);})()";
/// UA-vs-modern-fingerprinting-API coherence. Returns the UA plus presence of the
/// surfaces sear's `fingerprint.rs` instruments (the APIs real anti-bot WAFs
/// probe). The set of present APIs is itself a fingerprint: WebUSB/WebHID/Web
/// Serial/Web Bluetooth and `getBattery` are **Chromium-only** — a real Firefox
/// exposes NONE of them. A "Firefox" persona that leaks any is incoherent (UA
/// says Gecko, capability set says Blink). guise's family catalogue merely DROPS
/// these probes for Firefox; it never asserts their ABSENCE, so an accidental
/// leak sails through. This probe closes that gap.
const FP_API_COHERENCE_JS: &str = "(function(){var n=navigator;function has(o,k){try{return k in o;}catch(e){return false;}}return {ua:n.userAgent,usb:has(n,'usb'),hid:has(n,'hid'),serial:has(n,'serial'),bluetooth:has(n,'bluetooth'),getBattery:has(n,'getBattery'),mediaDevices:has(n,'mediaDevices'),storageEstimate:!!(n.storage&&n.storage.estimate),share:has(n,'share'),wakeLock:has(n,'wakeLock'),xr:has(n,'xr')};})()";
/// UA-vs-**error-subsystem** coherence — the CodePath/ErrorMessage analog of the
/// capability probe, sourced from sear's differential divergence kinds. Two tells
/// that value-diffing the *normal* surfaces never sees:
/// 1. `Error.captureStackTrace` / `Error.stackTraceLimit` / `prepareStackTrace`
/// are **V8-only** — a genuine Gecko build exposes NONE. A "Firefox" persona
/// that has them is running on V8 (or was shimmed by a Blink-shaped harness).
/// 2. Stack-frame *framing* differs by engine: V8 renders ` at fn (url:1:2)`,
/// Gecko renders `fn@url:1:2`. A Firefox UA with `at`-style frames is a hard
/// engine mismatch. This is exactly where JS overrides leak — they fake the
/// values a page reads but cannot change how the engine formats a throw.
const ERROR_ENGINE_COHERENCE_JS: &str = "(function(){var n=navigator;function has(o,k){try{return typeof o[k]!=='undefined';}catch(e){return false;}}var stk='';try{throw new Error('probe');}catch(e){stk=String((e&&e.stack)||'');}var shape=/(?:^|\\n)\\s+at\\s/.test(stk)?'v8':(/@/.test(stk)?'gecko':'none');return {ua:n.userAgent,captureStackTrace:has(Error,'captureStackTrace'),stackTraceLimit:has(Error,'stackTraceLimit'),prepareStackTrace:has(Error,'prepareStackTrace'),stackShape:shape};})()";
/// The sear-derived red-team probe set. Folded into [`super::catalogue::probes`].
pub(super) fn redteam_probes() -> Vec<Probe> {
vec![
Probe {
name: "redteam(sear): busy-loop timing shows human scheduler jitter",
js: TIMING_CV_JS,
severity: Severity::Medium,
classifier: classify_timing_cv,
},
Probe {
name: "redteam(sear): standard web-platform APIs all present",
js: MISSING_APIS_JS,
severity: Severity::High,
classifier: classify_missing_apis,
},
Probe {
name: "redteam(sear): no automation-framework globals leak",
js: AUTOMATION_GLOBALS_JS,
severity: Severity::High,
classifier: classify_automation_globals,
},
Probe {
name: "redteam(sear): typeof document.all === 'undefined' (V8/HTML quirk)",
js: DOCUMENT_ALL_JS,
severity: Severity::Medium,
classifier: classify_document_all,
},
Probe {
name: "redteam(sear): performance.now provides a high-resolution timer",
js: TIMER_RESOLUTION_JS,
severity: Severity::Medium,
classifier: classify_timer_resolution,
},
Probe {
name: "redteam(sear): UA browser-family agrees with capability-API surface",
js: FP_API_COHERENCE_JS,
severity: Severity::High,
classifier: classify_fp_api_coherence,
},
Probe {
name: "redteam(sear): UA browser-family agrees with Error subsystem (stack shape)",
js: ERROR_ENGINE_COHERENCE_JS,
severity: Severity::High,
classifier: classify_error_engine_coherence,
},
]
}
fn classify_timing_cv(v: &serde_json::Value) -> ProbeOutcome {
use crate::sampling::{CV_DRIFT_FLOOR, HUMAN_TIMING_CV_FLOOR};
match v.as_f64() {
Some(cv) if cv < 0.0 => {
ProbeOutcome::Critical("busy loop took 0 ms — timer is instrumented/instant".into())
}
Some(cv) if cv >= HUMAN_TIMING_CV_FLOOR => ProbeOutcome::Pass,
Some(cv) if cv >= CV_DRIFT_FLOOR => {
ProbeOutcome::Drift(format!("timing CV {cv:.4} is low — borderline uniform"))
}
Some(cv) => ProbeOutcome::Critical(format!(
"timing CV {cv:.4} < {CV_DRIFT_FLOOR} — near-uniform timer, sandbox tell"
)),
None => ProbeOutcome::ProbeError("timing-CV probe did not return a number".into()),
}
}
fn classify_missing_apis(v: &serde_json::Value) -> ProbeOutcome {
match v.as_u64() {
Some(0) => ProbeOutcome::Pass,
Some(n) if n < 4 => ProbeOutcome::Drift(format!("{n} standard web API(s) missing")),
Some(n) => ProbeOutcome::Critical(format!(
"{n} standard web APIs missing — sandbox-grade surface"
)),
None => ProbeOutcome::ProbeError("missing-API probe did not return an integer".into()),
}
}
fn classify_automation_globals(v: &serde_json::Value) -> ProbeOutcome {
match v.as_array() {
Some(a) if a.is_empty() => ProbeOutcome::Pass,
Some(a) => {
let names: Vec<String> = a
.iter()
.filter_map(|x| x.as_str().map(str::to_string))
.collect();
ProbeOutcome::Critical(format!("automation globals leaked: {}", names.join(", ")))
}
None => ProbeOutcome::ProbeError("automation-globals probe did not return an array".into()),
}
}
fn classify_document_all(v: &serde_json::Value) -> ProbeOutcome {
match v.as_str() {
Some("undefined") => ProbeOutcome::Pass,
Some(other) => ProbeOutcome::Critical(format!(
"typeof document.all is '{other}', not 'undefined' — non-Chrome / shimmed"
)),
None => ProbeOutcome::ProbeError("document.all probe did not return a string".into()),
}
}
fn classify_timer_resolution(v: &serde_json::Value) -> ProbeOutcome {
match v.as_f64() {
Some(r) if r < 0.0 => {
ProbeOutcome::Critical("performance.now() is missing — sandbox tell".into())
}
Some(r) if r == 0.0 => {
ProbeOutcome::Drift("performance.now() never advanced over the sample".into())
}
Some(_) => ProbeOutcome::Pass,
None => ProbeOutcome::ProbeError("timer-resolution probe did not return a number".into()),
}
}
/// Chromium-only capability surfaces — a real Gecko build exposes none of these.
const CHROMIUM_ONLY_CAPABILITY_APIS: &[&str] =
&["usb", "hid", "serial", "bluetooth", "getBattery"];
fn classify_fp_api_coherence(v: &serde_json::Value) -> ProbeOutcome {
let Some(ua) = v.get("ua").and_then(|x| x.as_str()) else {
return ProbeOutcome::ProbeError("api-coherence probe returned no ua".into());
};
let present = |k: &str| v.get(k).and_then(|x| x.as_bool()).unwrap_or(false);
let is_firefox = ua.contains("Firefox") || ua.contains("Gecko/");
let is_chrome = ua.contains("Chrome") || ua.contains("Chromium");
if is_firefox {
let leaked: Vec<&str> = CHROMIUM_ONLY_CAPABILITY_APIS
.iter()
.copied()
.filter(|k| present(k))
.collect();
if !leaked.is_empty() {
return ProbeOutcome::Critical(format!(
"Firefox UA but Chromium-only API(s) present: navigator.{} — capability set says Blink",
leaked.join(", navigator.")
));
}
if !present("mediaDevices") {
return ProbeOutcome::Drift("Firefox persona missing navigator.mediaDevices".into());
}
return ProbeOutcome::Pass;
}
if is_chrome {
let n = CHROMIUM_ONLY_CAPABILITY_APIS
.iter()
.filter(|k| present(k))
.count();
if n == 0 {
return ProbeOutcome::Drift(
"Chrome UA but no Blink capability APIs present — stripped/headless tell".into(),
);
}
return ProbeOutcome::Pass;
}
ProbeOutcome::Pass
}
/// V8/Chromium-only members of the `Error` constructor. A genuine Gecko build
/// exposes NONE of them — the error-subsystem analog of the Blink capability set.
const V8_ONLY_ERROR_MEMBERS: &[&str] =
&["captureStackTrace", "stackTraceLimit", "prepareStackTrace"];
fn classify_error_engine_coherence(v: &serde_json::Value) -> ProbeOutcome {
let Some(ua) = v.get("ua").and_then(|x| x.as_str()) else {
return ProbeOutcome::ProbeError("error-coherence probe returned no ua".into());
};
let present = |k: &str| v.get(k).and_then(|x| x.as_bool()).unwrap_or(false);
let shape = v.get("stackShape").and_then(|x| x.as_str()).unwrap_or("none");
let is_firefox = ua.contains("Firefox") || ua.contains("Gecko/");
let is_chrome = ua.contains("Chrome") || ua.contains("Chromium");
if is_firefox {
let leaked: Vec<&str> = V8_ONLY_ERROR_MEMBERS
.iter()
.copied()
.filter(|k| present(k))
.collect();
if !leaked.is_empty() {
return ProbeOutcome::Critical(format!(
"Firefox UA but V8-only Error.{} present — error subsystem says V8/Chromium",
leaked.join(", Error.")
));
}
if shape == "v8" {
return ProbeOutcome::Critical(
"Firefox UA but Chrome-style ('at') stack frames — running on V8".into(),
);
}
// 'gecko' (the real Firefox shape) and 'none' (no stack captured) both
// pass — a genuine Gecko build never renders V8-style frames.
return ProbeOutcome::Pass;
}
if is_chrome {
if !present("captureStackTrace") {
return ProbeOutcome::Drift(
"Chrome UA but Error.captureStackTrace absent — non-V8/shimmed tell".into(),
);
}
if shape == "gecko" {
return ProbeOutcome::Drift(
"Chrome UA but Gecko-style ('@') stack frames — engine mismatch".into(),
);
}
return ProbeOutcome::Pass;
}
ProbeOutcome::Pass
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn redteam_set_is_nonempty_and_uniquely_named() {
let p = redteam_probes();
assert!(p.len() >= 7, "expected the full red-team set");
let mut names: Vec<&str> = p.iter().map(|x| x.name).collect();
names.sort_unstable();
names.dedup();
assert_eq!(names.len(), p.len(), "probe names must be unique");
// Every probe carries non-empty JS.
assert!(p.iter().all(|x| !x.js.is_empty()));
}
#[test]
fn timing_cv_pass_drift_critical_boundaries() {
assert_eq!(classify_timing_cv(&json!(0.25)), ProbeOutcome::Pass);
assert_eq!(classify_timing_cv(&json!(0.10)), ProbeOutcome::Pass);
assert!(matches!(
classify_timing_cv(&json!(0.05)),
ProbeOutcome::Drift(_)
));
assert!(matches!(
classify_timing_cv(&json!(0.01)),
ProbeOutcome::Critical(_)
));
assert!(matches!(
classify_timing_cv(&json!(-1)),
ProbeOutcome::Critical(_)
));
assert!(matches!(
classify_timing_cv(&json!("x")),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn missing_apis_thresholds() {
assert_eq!(classify_missing_apis(&json!(0)), ProbeOutcome::Pass);
assert!(matches!(
classify_missing_apis(&json!(2)),
ProbeOutcome::Drift(_)
));
assert!(matches!(
classify_missing_apis(&json!(5)),
ProbeOutcome::Critical(_)
));
assert!(matches!(
classify_missing_apis(&json!("x")),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn automation_globals_empty_passes_nonempty_critical() {
assert_eq!(classify_automation_globals(&json!([])), ProbeOutcome::Pass);
match classify_automation_globals(&json!(["navigator.webdriver", "cdc_foo"])) {
ProbeOutcome::Critical(m) => {
assert!(m.contains("navigator.webdriver") && m.contains("cdc_foo"))
}
o => panic!("expected Critical, got {o:?}"),
}
assert!(matches!(
classify_automation_globals(&json!("nope")),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn document_all_quirk() {
assert_eq!(
classify_document_all(&json!("undefined")),
ProbeOutcome::Pass
);
assert!(matches!(
classify_document_all(&json!("object")),
ProbeOutcome::Critical(_)
));
assert!(matches!(
classify_document_all(&json!(1)),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn timer_resolution_outcomes() {
assert_eq!(classify_timer_resolution(&json!(0.05)), ProbeOutcome::Pass);
assert!(matches!(
classify_timer_resolution(&json!(-1)),
ProbeOutcome::Critical(_)
));
assert!(matches!(
classify_timer_resolution(&json!(0.0)),
ProbeOutcome::Drift(_)
));
assert!(matches!(
classify_timer_resolution(&json!("x")),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn fp_api_coherence_firefox_leaking_blink_api_is_critical() {
// Firefox UA but WebUSB present → capability set betrays Blink.
let v = json!({
"ua": "Mozilla/5.0 (X11; Linux x86_64; rv:151.0) Gecko/20100101 Firefox/151.0",
"usb": true, "hid": false, "serial": false, "bluetooth": false,
"getBattery": false, "mediaDevices": true
});
match classify_fp_api_coherence(&v) {
ProbeOutcome::Critical(m) => assert!(m.contains("navigator.usb"), "{m}"),
o => panic!("expected Critical, got {o:?}"),
}
}
#[test]
fn fp_api_coherence_clean_firefox_passes() {
let v = json!({
"ua": "Mozilla/5.0 (X11; Linux x86_64; rv:151.0) Gecko/20100101 Firefox/151.0",
"usb": false, "hid": false, "serial": false, "bluetooth": false,
"getBattery": false, "mediaDevices": true
});
assert_eq!(classify_fp_api_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn fp_api_coherence_firefox_without_mediadevices_drifts() {
let v = json!({
"ua": "Mozilla/5.0 (X11; Linux x86_64; rv:151.0) Gecko/20100101 Firefox/151.0",
"usb": false, "hid": false, "serial": false, "bluetooth": false,
"getBattery": false, "mediaDevices": false
});
assert!(matches!(
classify_fp_api_coherence(&v),
ProbeOutcome::Drift(_)
));
}
#[test]
fn fp_api_coherence_chrome_with_blink_apis_passes() {
let v = json!({
"ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"usb": true, "hid": true, "serial": true, "bluetooth": true,
"getBattery": true, "mediaDevices": true
});
assert_eq!(classify_fp_api_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn fp_api_coherence_stripped_chrome_drifts() {
let v = json!({
"ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"usb": false, "hid": false, "serial": false, "bluetooth": false,
"getBattery": false, "mediaDevices": true
});
assert!(matches!(
classify_fp_api_coherence(&v),
ProbeOutcome::Drift(_)
));
}
#[test]
fn fp_api_coherence_missing_ua_is_probe_error() {
assert!(matches!(
classify_fp_api_coherence(&json!({"usb": false})),
ProbeOutcome::ProbeError(_)
));
}
const FF_UA: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:151.0) Gecko/20100101 Firefox/151.0";
const CR_UA: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
#[test]
fn error_coherence_clean_firefox_passes() {
// Real Gecko: no V8 Error members, '@'-style frames.
let v = json!({
"ua": FF_UA, "captureStackTrace": false, "stackTraceLimit": false,
"prepareStackTrace": false, "stackShape": "gecko"
});
assert_eq!(classify_error_engine_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn error_coherence_firefox_with_capturestacktrace_is_critical() {
// V8-only Error.captureStackTrace on a Firefox persona → running on V8.
let v = json!({
"ua": FF_UA, "captureStackTrace": true, "stackTraceLimit": true,
"prepareStackTrace": false, "stackShape": "gecko"
});
match classify_error_engine_coherence(&v) {
ProbeOutcome::Critical(m) => assert!(m.contains("Error.captureStackTrace"), "{m}"),
o => panic!("expected Critical, got {o:?}"),
}
}
#[test]
fn error_coherence_firefox_with_v8_stack_shape_is_critical() {
// No V8 members, but Chrome-style ' at ' frames → engine mismatch.
let v = json!({
"ua": FF_UA, "captureStackTrace": false, "stackTraceLimit": false,
"prepareStackTrace": false, "stackShape": "v8"
});
assert!(matches!(
classify_error_engine_coherence(&v),
ProbeOutcome::Critical(_)
));
}
#[test]
fn error_coherence_real_chrome_passes() {
let v = json!({
"ua": CR_UA, "captureStackTrace": true, "stackTraceLimit": true,
"prepareStackTrace": true, "stackShape": "v8"
});
assert_eq!(classify_error_engine_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn error_coherence_chrome_without_capturestacktrace_drifts() {
let v = json!({
"ua": CR_UA, "captureStackTrace": false, "stackTraceLimit": false,
"prepareStackTrace": false, "stackShape": "v8"
});
assert!(matches!(
classify_error_engine_coherence(&v),
ProbeOutcome::Drift(_)
));
}
#[test]
fn error_coherence_firefox_no_stack_captured_passes() {
// A throw that yields no stack ('none') is acceptable for Gecko — the
// probe must not manufacture a tell from a missing stack.
let v = json!({
"ua": FF_UA, "captureStackTrace": false, "stackTraceLimit": false,
"prepareStackTrace": false, "stackShape": "none"
});
assert_eq!(classify_error_engine_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn error_coherence_missing_ua_is_probe_error() {
assert!(matches!(
classify_error_engine_coherence(&json!({"captureStackTrace": false})),
ProbeOutcome::ProbeError(_)
));
}
}