From 742b0edb3dee91d89360e6f1b9efb481e7641504 Mon Sep 17 00:00:00 2001 From: sorte1 Date: Fri, 20 Nov 2026 18:31:20 +0100 Subject: [PATCH 1/5] fix: decode an inner unit behind a decoded outer rotator or a debug timer under --unsafe Under --unsafe a debug-protection timer shell before a rotator is inert, a fully decoded foreign unit counts as held for the units that rely on its rotator, and the foreign unit proof skips the intrinsic-reach scan the unsafe premises cover. A string array or decoder reached only from branches is speculated, and a deferred escape that survives one layer round is carried by name to the next round instead of forcing the rerun. --- crates/untwist-deobfuscate/src/bridge.rs | 87 +++++++++-- crates/untwist-deobfuscate/src/dead_guards.rs | 1 + .../src/debug_protection.rs | 20 +++ crates/untwist-deobfuscate/src/decoded.rs | 13 +- crates/untwist-deobfuscate/src/decoder.rs | 65 ++++++-- crates/untwist-deobfuscate/src/driver.rs | 7 +- .../untwist-deobfuscate/src/legacy_reach.rs | 141 ++++++++++++++++-- .../untwist-deobfuscate/src/proof/foreign.rs | 13 +- .../untwist-deobfuscate/src/proof/rotator.rs | 10 +- .../src/wrapper_speculation.rs | 33 +++- crates/untwist/src/pipeline/input.rs | 4 + crates/untwist/src/pipeline/rounds.rs | 2 + .../decoded-foreign-rotator.js | 1 + .../decoded-foreign-rotator.source.js | 1 + .../interval-before-rotator.js | 1 + .../interval-before-rotator.source.js | 45 ++++++ crates/untwist/tests/obfuscator_sweep_rows.rs | 10 ++ 17 files changed, 402 insertions(+), 52 deletions(-) create mode 100644 crates/untwist/tests/data/obfuscator_sweep/decoded-foreign-rotator.js create mode 100644 crates/untwist/tests/data/obfuscator_sweep/decoded-foreign-rotator.source.js create mode 100644 crates/untwist/tests/data/obfuscator_sweep/interval-before-rotator.js create mode 100644 crates/untwist/tests/data/obfuscator_sweep/interval-before-rotator.source.js diff --git a/crates/untwist-deobfuscate/src/bridge.rs b/crates/untwist-deobfuscate/src/bridge.rs index e20ecad5e..a7e7f5cd7 100644 --- a/crates/untwist-deobfuscate/src/bridge.rs +++ b/crates/untwist-deobfuscate/src/bridge.rs @@ -114,7 +114,55 @@ fn escaped_decoders_by_unit( by_unit } +fn speculated_array_escapes( + (program, scoping): (&Program<'_>, &Scoping), + collected: &mut Collected<'_>, + array_span: &impl Fn(usize) -> Option, +) -> FxHashMap { + if collected.escapes.is_empty() || !crate::legacy_reach::speculates_escapes() { + return FxHashMap::default(); + } + let spans: FxHashSet = collected.escapes.iter().map(|(_, span)| *span).collect(); + let branched = in_branches(program, scoping, &spans); + let unguarded: FxHashSet = collected + .escapes + .iter() + .filter(|(_, span)| !branched.contains(span)) + .map(|(unit, _)| *unit) + .collect(); + let speculated: FxHashMap = collected + .escapes + .iter() + .map(|(unit, _)| *unit) + .filter(|unit| !unguarded.contains(unit)) + .filter_map(|unit| Some((unit, array_span(unit)?))) + .collect(); + collected + .escapes + .retain(|(unit, _)| !speculated.contains_key(unit)); + speculated +} + fn guarded_escapes( + (program, scoping): (&Program<'_>, &Scoping), + collected: &mut Collected<'_>, + (sites, rotators): (&Sites, &FxHashMap), + (decoder_spans, array_span): (impl Fn(usize) -> Vec, impl Fn(usize) -> Option), +) -> FxHashMap { + let arrays = speculated_array_escapes((program, scoping), collected, &array_span); + let mut by_unit = guarded_decoder_escapes( + (program, scoping), + collected, + (sites, rotators), + decoder_spans, + ); + for (unit, span) in arrays { + by_unit.entry(unit).or_default().speculative.push(span); + } + by_unit +} + +fn guarded_decoder_escapes( (program, scoping): (&Program<'_>, &Scoping), collected: &mut Collected<'_>, (sites, rotators): (&Sites, &FxHashMap), @@ -144,6 +192,7 @@ fn guarded_escapes( }; let mut unguarded: FxHashSet = FxHashSet::default(); let mut speculated: FxHashSet = FxHashSet::default(); + let mut unbranched: FxHashSet = FxHashSet::default(); let mut found: Vec<(usize, bool, Vec)> = Vec::new(); let values = collected.decoder_values.iter().map(|escape| (escape, true)); let dynamic = collected.dynamic_calls.iter().map(|escape| (escape, false)); @@ -152,7 +201,12 @@ fn guarded_escapes( continue; }; match enclosing.get(span).filter(|guards| !guards.is_empty()) { - Some(guards) => found.push((unit, is_value, guards.clone())), + Some(guards) => { + if !branched.contains(span) { + unbranched.insert(unit); + } + found.push((unit, is_value, guards.clone())); + } None if branched.contains(span) => { speculated.insert(unit); } @@ -182,6 +236,9 @@ fn guarded_escapes( continue; } let entry = by_unit.entry(unit).or_default(); + if !unbranched.contains(&unit) && entry.fallback.is_empty() { + entry.fallback = decoder_spans(unit); + } if is_value { entry.values.push(guards); } else { @@ -212,6 +269,7 @@ struct Evidence<'r, 'p> { with_statement: Option, rotator_sites: Vec<(usize, RotatorSite)>, foreign: ForeignUnits<'r, 'p>, + protections: FxHashSet, } fn first_per_unit(found: impl Iterator) -> FxHashMap { @@ -248,6 +306,7 @@ fn evidence<'r, 'p>( with_statement: with_statement(program, scoping), rotator_sites: collected.rotator_sites.clone(), foreign: ForeignUnits::new(stamped, sites, &collected.rotator_sites), + protections: FxHashSet::default(), } } @@ -282,7 +341,7 @@ fn rotator_check<'p>( .filter_map(|decoder| decoder.symbol) .chain(inputs.array.symbol) .collect(); - let foreign = Some((&evidence.foreign, unit)); + let foreign = (Some((&evidence.foreign, unit)), &evidence.protections); if let Some(refusal) = rotator_refusal_at(inputs.program, inputs.scoping, &sites, &allowed, foreign) { @@ -508,12 +567,12 @@ fn orphan_entries<'p>(stamped: &Stamped<'p>, orphans: &[usize]) -> Vec<(&'p str, type PlanOutcome<'p> = Result, Refusal>; -fn refuse_unsupported(plan: &mut DecodePlan<'_>) { +fn refuse_unsupported(plan: &mut DecodePlan<'_>, unsafe_lane: bool) { loop { let decoding: Vec = plan .requests .iter() - .filter(|request| !request.calls.is_empty()) + .filter(|request| unsafe_lane || !request.calls.is_empty()) .map(|request| request.unit) .collect(); let before = plan.requests.len(); @@ -581,7 +640,7 @@ pub fn build_requests_in_lane<'p>( } let (members, orphans) = unit_members(&stamped); if stamped.is_empty() { - return finish_plan(DecodePlan::default(), &stamped, &orphans); + return finish_plan(DecodePlan::default(), &stamped, (&orphans, unsafe_lane)); } let sites = local_indices(&stamped, &members); let mut collected = collect_base(program, decoder_units_of(&sites), unsafe_lane); @@ -607,11 +666,15 @@ pub fn build_requests_in_lane<'p>( .map(|symbol| scoping.symbol_span(symbol)) .collect() }; + let array_span = |unit: usize| -> Option { + let symbol = stamped.array(unit)?.symbol?; + Some(scoping.symbol_span(symbol)) + }; guarded_escapes( (program, scoping), &mut collected, (&sites, &rotator_spans), - decoder_spans, + (decoder_spans, array_span), ) } else { FxHashMap::default() @@ -622,7 +685,11 @@ pub fn build_requests_in_lane<'p>( .decoder_values .retain(|(_, span)| !dead.contains(span)); } - let evidence = evidence(program, &stamped, &orphans, &collected, &sites, scoping); + let mut evidence = evidence(program, &stamped, &orphans, &collected, &sites, scoping); + if unsafe_lane { + evidence.protections = crate::debug_protection::interval_statements(program, scoping); + evidence.foreign.unsafe_lane = true; + } let mut escaped_by_unit = escaped_decoders_by_unit(&collected, &sites); let mut plan = DecodePlan { intrinsic_object: object_operand_is_intrinsic(&collected.patches), @@ -649,15 +716,15 @@ pub fn build_requests_in_lane<'p>( }); push_outcome(&mut plan, outcome); } - finish_plan(plan, &stamped, &orphans) + finish_plan(plan, &stamped, (&orphans, unsafe_lane)) } fn finish_plan<'p>( mut plan: DecodePlan<'p>, stamped: &Stamped<'p>, - orphans: &[usize], + (orphans, unsafe_lane): (&[usize], bool), ) -> DecodePlan<'p> { - refuse_unsupported(&mut plan); + refuse_unsupported(&mut plan, unsafe_lane); plan.unattributed = orphan_entries(stamped, orphans); plan.refused = !stamped.is_empty() && plan.requests.is_empty() diff --git a/crates/untwist-deobfuscate/src/dead_guards.rs b/crates/untwist-deobfuscate/src/dead_guards.rs index 4ae16db8c..d6da5a440 100644 --- a/crates/untwist-deobfuscate/src/dead_guards.rs +++ b/crates/untwist-deobfuscate/src/dead_guards.rs @@ -58,6 +58,7 @@ pub struct EscapeGuards { pub values: Vec>, pub dynamic: Vec>, pub speculative: Vec, + pub fallback: Vec, } struct Enclosing<'e> { diff --git a/crates/untwist-deobfuscate/src/debug_protection.rs b/crates/untwist-deobfuscate/src/debug_protection.rs index 0bb9fe37e..be388b7b1 100644 --- a/crates/untwist-deobfuscate/src/debug_protection.rs +++ b/crates/untwist-deobfuscate/src/debug_protection.rs @@ -159,6 +159,26 @@ pub(crate) fn protection_symbols(program: &Program<'_>, scoping: &Scoping) -> Fx declarations(program, scoping).symbols } +pub(crate) fn interval_statements(program: &Program<'_>, scoping: &Scoping) -> FxHashSet { + let protections = protection_symbols(program, scoping); + if protections.is_empty() { + return FxHashSet::default(); + } + let intervals = Intervals { + protections: &protections, + scoping, + spans: Vec::new(), + }; + program + .body + .iter() + .filter(|statement| { + intervals.is_interval_shell(statement) || intervals.is_interval_statement(statement) + }) + .map(GetSpan::span) + .collect() +} + pub(crate) fn keeps_a_live_protection(output: &str) -> bool { let allocator = Allocator::default(); let parsed = parse_program(&allocator, output); diff --git a/crates/untwist-deobfuscate/src/decoded.rs b/crates/untwist-deobfuscate/src/decoded.rs index 257ba2db2..0bcef034f 100644 --- a/crates/untwist-deobfuscate/src/decoded.rs +++ b/crates/untwist-deobfuscate/src/decoded.rs @@ -217,7 +217,8 @@ fn apply_decoded_values<'a>( evaluator: &mut InterpEvaluator, values: Vec>, ) -> bool { - if !guarded_escapes_dead(request, &values) { + let guards_dead = guarded_escapes_dead(request, &values); + if !guards_dead && request.escape_guards.fallback.is_empty() { decoded.notes.extend(drained(evaluator)); decoded .notes @@ -252,6 +253,9 @@ fn apply_decoded_values<'a>( } if evaluated > 0 { crate::legacy_reach::defer_escapes(&request.escape_guards.speculative); + if !guards_dead { + crate::legacy_reach::defer_escapes(&request.escape_guards.fallback); + } } inlined && returned == request.calls.len() && evaluated == returned } @@ -819,7 +823,12 @@ fn judge_runs<'p>( .into_iter() .zip(spelled) .partition(|(run, _)| run.request.reliance.is_some()); - let mut decoding = Vec::new(); + let mut decoding: Vec = plan + .requests + .iter() + .filter(|request| unsafe_lane && request.calls.is_empty()) + .map(|request| request.unit) + .collect(); let watched = !unsafe_lane && !nested_root; for (run, spelled) in independent.into_iter().chain(relying) { let unit = run.request.unit; diff --git a/crates/untwist-deobfuscate/src/decoder.rs b/crates/untwist-deobfuscate/src/decoder.rs index 0e6439f52..88c405db8 100644 --- a/crates/untwist-deobfuscate/src/decoder.rs +++ b/crates/untwist-deobfuscate/src/decoder.rs @@ -4,6 +4,7 @@ use oxc_semantic::Scoping; use oxc_span::{GetSpan, Span}; use oxc_str::Ident; use oxc_syntax::symbol::SymbolId; +use rustc_hash::{FxHashMap, FxHashSet}; use untwist_core::ast_util::matcher::{call_parts, var_function_or_declaration}; use untwist_core::ast_util::prop::identifier_name; use untwist_core::ast_util::rename::Renames; @@ -19,6 +20,7 @@ use crate::stamp::{ declaration_symbol, decoder_index, decoder_name, owning_unit, reached_outside_its_block, string_array_unit, IndexAllocator, RefusedByDecode, }; +use crate::wrapper_speculation::{branched_writes_outside, declared_by_function}; const SHORT_DECODER_BODY: usize = 4; @@ -232,8 +234,10 @@ impl<'a> Pass<'a> for FindDecoders { .map(|shape| (shape.statement, shape.symbol)) .collect(); let early = read_before_declaration(program, cx.scoping(), &bound); + let speculated = speculated_decoders(program, cx, &unstamped, &watched); let mut indices = IndexAllocator::decoders(cx.scoping()); let mut renames = Renames::new(); + let mut deferred = Vec::new(); for (index, shape) in unstamped.iter().enumerate() { if refused.contains(&shape.statement) { continue; @@ -242,14 +246,21 @@ impl<'a> Pass<'a> for FindDecoders { cx.note(note, Some(shape.statement)); continue; } - if let Some((note, span)) = decoder_refusal(cx.scoping(), shape, index, &watched) { + let written = speculated.get(&shape.symbol); + let checked = (cx.scoping(), &watched, written.is_some()); + if let Some((note, span)) = decoder_refusal(checked, shape, index) { cx.note(note, Some(span)); continue; } + if let Some(writes) = written { + deferred.push(cx.scoping().symbol_span(shape.symbol)); + deferred.extend(writes); + } let name = decoder_name(indices.allocate()); renames.insert(shape.symbol, Ident::from_str_in(&name, &*cx)); } drop(refused); + crate::legacy_reach::defer_escapes(&deferred); if renames.is_empty() { return; } @@ -260,19 +271,53 @@ impl<'a> Pass<'a> for FindDecoders { } } +fn speculated_decoders( + program: &Program<'_>, + cx: &Cx<'_, '_>, + unstamped: &[DecoderShape], + watched: &Watched, +) -> FxHashMap> { + if !cx.opts.unsafe_transforms || !crate::legacy_reach::speculates_escapes() { + return FxHashMap::default(); + } + let scoping = cx.scoping(); + let homes: FxHashMap = unstamped + .iter() + .enumerate() + .filter(|(index, shape)| { + !shape.bound + && declared_by_function(scoping, shape.symbol) + && writes_outside( + scoping, + shape.symbol, + shape.statement, + &watched.writes, + *index, + ) + }) + .map(|(_, shape)| (shape.symbol, shape.statement)) + .collect(); + if homes.is_empty() { + return FxHashMap::default(); + } + let candidates: FxHashSet = homes.keys().copied().collect(); + branched_writes_outside(program, scoping, &candidates, &homes) +} + fn decoder_refusal( - scoping: &Scoping, + (scoping, watched, speculated): (&Scoping, &Watched, bool), shape: &DecoderShape, index: usize, - watched: &Watched, ) -> Option<(&'static str, Span)> { - if writes_outside( - scoping, - shape.symbol, - shape.statement, - &watched.writes, - index, - ) { + if !speculated + && writes_outside( + scoping, + shape.symbol, + shape.statement, + &watched.writes, + index, + ) + { return Some((notes::DECODER_WRITTEN_OUTSIDE, shape.statement)); } if let Some(span) = watched.spelled.get(index).copied().flatten() { diff --git a/crates/untwist-deobfuscate/src/driver.rs b/crates/untwist-deobfuscate/src/driver.rs index 35104615f..aa230ed4a 100644 --- a/crates/untwist-deobfuscate/src/driver.rs +++ b/crates/untwist-deobfuscate/src/driver.rs @@ -974,17 +974,12 @@ fn deobfuscate_with<'a>( if opts.round_cap_undo.get() == RoundCapUndo::Requested { return (sem, aggregate.finish()); } - crate::legacy_reach::settle_escapes(|decoders| { - sem.scoping - .symbol_ids() - .any(|symbol| decoders.contains(&sem.scoping.symbol_span(symbol))) - || crate::legacy_reach::identifiers_survive(program, decoders) - }); let sem = finalize_unresolved_machinery(alloc, program, sem, (opts, &shared), &mut aggregate); let sem = match &before { Some(before) => kept_when_stamps_survive(alloc, program, (sem, before), &mut aggregate), None => sem, }; + crate::legacy_reach::settle_escapes(program, &sem.scoping); crate::decoded::settle_layered(program, &sem.scoping); let mut report = aggregate.finish(); diff --git a/crates/untwist-deobfuscate/src/legacy_reach.rs b/crates/untwist-deobfuscate/src/legacy_reach.rs index 1dd477756..466e9d1e8 100644 --- a/crates/untwist-deobfuscate/src/legacy_reach.rs +++ b/crates/untwist-deobfuscate/src/legacy_reach.rs @@ -1,8 +1,10 @@ use std::cell::RefCell; -use oxc_ast::ast::{BindingIdentifier, CallExpression, IdentifierReference, Program}; +use oxc_ast::ast::{BindingIdentifier, CallExpression, Expression, IdentifierReference, Program}; use oxc_ast_visit::VisitJs; +use oxc_semantic::Scoping; use oxc_span::Span; +use rustc_hash::FxHashSet; use crate::mutants::deferred_legacy_reach_trusted; @@ -27,6 +29,9 @@ struct Deferral { unverified: bool, escapes_open: bool, escapes: Vec, + escape_names: Vec>, + escape_names_opened: Vec>, + carries_names: bool, reached: bool, } @@ -101,37 +106,143 @@ pub(crate) fn defer_escapes(decoders: &[Span]) { struct Surviving<'s> { spans: &'s [Span], - found: bool, + found: Vec>, + unnamed: bool, +} + +impl Surviving<'_> { + fn note(&mut self, span: Span, name: &str) { + if self.spans.contains(&span) { + self.found.push(vec![name.to_string()]); + } + } } impl<'a> VisitJs<'a> for Surviving<'_> { fn visit_identifier_reference(&mut self, it: &IdentifierReference<'a>) { - self.found |= self.spans.contains(&it.span); + self.note(it.span, it.name.as_str()); } fn visit_binding_identifier(&mut self, it: &BindingIdentifier<'a>) { - self.found |= self.spans.contains(&it.span); + self.note(it.span, it.name.as_str()); } fn visit_call_expression(&mut self, it: &CallExpression<'a>) { - self.found |= self.spans.contains(&it.span); + if self.spans.contains(&it.span) { + match &it.callee { + Expression::Identifier(callee) => self.found.push(vec![callee.name.to_string()]), + _ => self.unnamed = true, + } + } oxc_ast_visit::walk_js::walk_call_expression(self, it); } } -pub(crate) fn identifiers_survive(program: &Program<'_>, spans: &[Span]) -> bool { +#[derive(Default)] +struct Names { + present: FxHashSet, +} + +impl<'a> VisitJs<'a> for Names { + fn visit_identifier_reference(&mut self, it: &IdentifierReference<'a>) { + self.present.insert(it.name.to_string()); + } + + fn visit_binding_identifier(&mut self, it: &BindingIdentifier<'a>) { + self.present.insert(it.name.to_string()); + } +} + +fn surviving_names( + (program, scoping): (&Program<'_>, &Scoping), + escapes: &[Span], +) -> Option>> { let mut surviving = Surviving { - spans, - found: false, + spans: escapes, + found: Vec::new(), + unnamed: false, }; surviving.visit_program(program); - surviving.found + for symbol in scoping.symbol_ids() { + let span = scoping.symbol_span(symbol); + if !escapes.contains(&span) { + continue; + } + let mut group = vec![scoping.symbol_name(symbol).to_string()]; + if let Some(text) = program + .source_text + .get(span.start as usize..span.end as usize) + { + group.push(text.to_string()); + } + surviving.found.push(group); + } + (!surviving.unnamed).then_some(surviving.found) } -pub(crate) fn settle_escapes(survives: impl FnOnce(&[Span]) -> bool) { - let escapes = DEFERRAL.with(|deferral| std::mem::take(&mut deferral.borrow_mut().escapes)); - if !escapes.is_empty() && survives(&escapes) { +pub(crate) fn settle_escapes(program: &Program<'_>, scoping: &Scoping) { + let (escapes, carried, carries) = DEFERRAL.with(|deferral| { + let mut deferral = deferral.borrow_mut(); + ( + std::mem::take(&mut deferral.escapes), + std::mem::take(&mut deferral.escape_names), + deferral.carries_names, + ) + }); + if escapes.is_empty() && carried.is_empty() { + return; + } + let Some(mut pending) = surviving_names((program, scoping), &escapes) else { DEFERRAL.with(|deferral| deferral.borrow_mut().unverified = true); + return; + }; + pending.extend(carried); + if pending.is_empty() { + return; + } + let mut names = Names::default(); + names.visit_program(program); + pending.retain(|group| group.iter().any(|name| names.present.contains(name))); + if pending.is_empty() { + return; + } + DEFERRAL.with(|deferral| { + let mut deferral = deferral.borrow_mut(); + match carries { + true => deferral.escape_names = pending, + false => deferral.unverified = true, + } + }); +} + +pub fn round_opened() { + DEFERRAL.with(|deferral| { + let mut deferral = deferral.borrow_mut(); + deferral.escape_names_opened = deferral.escape_names.clone(); + }); +} + +pub fn round_discarded() { + DEFERRAL.with(|deferral| { + let mut deferral = deferral.borrow_mut(); + deferral.escape_names = std::mem::take(&mut deferral.escape_names_opened); + }); +} + +pub struct NameCarry(bool); + +impl NameCarry { + pub fn enter(on: bool) -> Self { + Self( + DEFERRAL + .with(|deferral| std::mem::replace(&mut deferral.borrow_mut().carries_names, on)), + ) + } +} + +impl Drop for NameCarry { + fn drop(&mut self) { + DEFERRAL.with(|deferral| deferral.borrow_mut().carries_names = self.0); } } @@ -316,7 +427,9 @@ fn verify_one(name: &str) { pub fn unverified() -> bool { DEFERRAL.with(|deferral| { let deferral = deferral.borrow(); - deferral.unverified || !deferral.observations.is_empty() + deferral.unverified + || !deferral.observations.is_empty() + || !deferral.escape_names.is_empty() }) } @@ -333,6 +446,8 @@ pub(crate) fn forget() { deferral.layered.clear(); deferral.observations.clear(); deferral.escapes.clear(); + deferral.escape_names.clear(); + deferral.escape_names_opened.clear(); }); } diff --git a/crates/untwist-deobfuscate/src/proof/foreign.rs b/crates/untwist-deobfuscate/src/proof/foreign.rs index e5a351b40..7863a60f9 100644 --- a/crates/untwist-deobfuscate/src/proof/foreign.rs +++ b/crates/untwist-deobfuscate/src/proof/foreign.rs @@ -64,7 +64,12 @@ impl<'r, 'p> ForeignUnit<'r, 'p> { functions.chain(statements).collect() } - fn touches_only_itself(&self, program: &Program<'p>, scoping: &Scoping) -> bool { + fn touches_only_itself( + &self, + program: &Program<'p>, + scoping: &Scoping, + unsafe_lane: bool, + ) -> bool { let allowed = self.allowed(); if uninitialised(scoping, &allowed, &FxHashSet::default()).is_some() { return false; @@ -90,7 +95,7 @@ impl<'r, 'p> ForeignUnit<'r, 'p> { { return false; } - machinery_unreachable(scoping, &self.parts(program, &rotators)) + unsafe_lane || machinery_unreachable(scoping, &self.parts(program, &rotators)) } } @@ -111,6 +116,7 @@ pub struct ForeignUnits<'r, 'p> { decoder_units: Vec<(SymbolId, usize)>, rotator_units: Vec<(Span, usize)>, relied: RefCell>, + pub unsafe_lane: bool, } fn is_host_sink(scoping: &Scoping, expr: &Expression<'_>) -> bool { @@ -159,6 +165,7 @@ impl<'r, 'p> ForeignUnits<'r, 'p> { .map(|(unit, site)| (site.span, *unit)) .collect(), relied: RefCell::new(Vec::new()), + unsafe_lane: false, } } @@ -244,7 +251,7 @@ impl<'r, 'p> ForeignUnits<'r, 'p> { .is_some_and(|foreign| { *foreign .proven - .get_or_init(|| foreign.touches_only_itself(program, scoping)) + .get_or_init(|| foreign.touches_only_itself(program, scoping, self.unsafe_lane)) }) } diff --git a/crates/untwist-deobfuscate/src/proof/rotator.rs b/crates/untwist-deobfuscate/src/proof/rotator.rs index 4bebc43b7..594da4f7f 100644 --- a/crates/untwist-deobfuscate/src/proof/rotator.rs +++ b/crates/untwist-deobfuscate/src/proof/rotator.rs @@ -323,13 +323,17 @@ fn first_active<'s, 'a>( program: &Program<'a>, scoping: &Scoping, (prefix, scope): (&'s [Statement<'a>], Option), - foreign: Option<(&ForeignUnits<'_, 'a>, usize)>, + (foreign, protections): (Option<(&ForeignUnits<'_, 'a>, usize)>, &FxHashSet), ) -> Option<&'s Statement<'a>> { if prefix.iter().all(trivially_inert) { return None; } let aliases = match scope { - None => crate::removal::dead_alias_statements(program, Some(scoping)), + None => { + let mut aliases = crate::removal::dead_alias_statements(program, Some(scoping)); + aliases.extend(protections); + aliases + } Some(_) => FxHashSet::default(), }; let inert = |statement: &Statement<'a>| { @@ -405,7 +409,7 @@ pub fn rotator_refusal_at<'a>( scoping: &Scoping, sites: &[RotatorSite], allowed: &FxHashSet, - foreign: Option<(&ForeignUnits<'_, 'a>, usize)>, + foreign: (Option<(&ForeignUnits<'_, 'a>, usize)>, &FxHashSet), ) -> Option { let mut first: Option> = None; for site in sites { diff --git a/crates/untwist-deobfuscate/src/wrapper_speculation.rs b/crates/untwist-deobfuscate/src/wrapper_speculation.rs index 8e56b308d..5a8aacbf6 100644 --- a/crates/untwist-deobfuscate/src/wrapper_speculation.rs +++ b/crates/untwist-deobfuscate/src/wrapper_speculation.rs @@ -3,7 +3,7 @@ use oxc_ast_visit::VisitJs; use oxc_semantic::Scoping; use oxc_span::Span; use oxc_syntax::symbol::SymbolId; -use rustc_hash::FxHashSet; +use rustc_hash::{FxHashMap, FxHashSet}; use crate::branch_reach::in_branches; use crate::notes; @@ -29,7 +29,7 @@ impl<'a> VisitJs<'a> for Writes<'_> { } } -fn declared_by_function(scoping: &Scoping, symbol: SymbolId) -> bool { +pub(crate) fn declared_by_function(scoping: &Scoping, symbol: SymbolId) -> bool { scoping.symbol_flags(symbol).is_function() && scoping .scope_flags(scoping.symbol_scope_id(symbol)) @@ -41,16 +41,39 @@ fn branched_writes_only( scoping: &Scoping, candidates: &FxHashSet, ) -> FxHashSet { + branched_writes_outside(program, scoping, candidates, &FxHashMap::default()) + .into_keys() + .collect() +} + +pub(crate) fn branched_writes_outside( + program: &Program<'_>, + scoping: &Scoping, + candidates: &FxHashSet, + homes: &FxHashMap, +) -> FxHashMap> { let mut writes = Writes { scoping, symbols: candidates, found: Vec::new(), }; writes.visit_program(program); - let spans: FxHashSet = writes.found.iter().map(|(_, span)| *span).collect(); + let outside: Vec<(SymbolId, Span)> = writes + .found + .into_iter() + .filter(|(symbol, span)| { + homes + .get(symbol) + .is_none_or(|home| span.start < home.start || home.end < span.end) + }) + .collect(); + let spans: FxHashSet = outside.iter().map(|(_, span)| *span).collect(); let branched = in_branches(program, scoping, &spans); - let mut held: FxHashSet = writes.found.iter().map(|(symbol, _)| *symbol).collect(); - for (symbol, span) in &writes.found { + let mut held: FxHashMap> = FxHashMap::default(); + for (symbol, span) in &outside { + held.entry(*symbol).or_default().push(*span); + } + for (symbol, span) in &outside { if !branched.contains(span) { held.remove(symbol); } diff --git a/crates/untwist/src/pipeline/input.rs b/crates/untwist/src/pipeline/input.rs index c9e8700ad..62d73497b 100644 --- a/crates/untwist/src/pipeline/input.rs +++ b/crates/untwist/src/pipeline/input.rs @@ -237,7 +237,11 @@ impl Pipeline { (room, reserved): (Option, u64), ) -> InputResult { let rerunnable = untwist_deobfuscate::legacy_reach::Rerunnable::enter(); + let carry = untwist_deobfuscate::legacy_reach::NameCarry::enter( + self.options.rename == RenameMode::None, + ); let first = self.run_admitted(source, reservation, (room.clone(), reserved)); + drop(carry); drop(rerunnable); let forbidden = untwist_deobfuscate::legacy_reach::unverified() .then(untwist_deobfuscate::legacy_reach::Forbidden::enter); diff --git a/crates/untwist/src/pipeline/rounds.rs b/crates/untwist/src/pipeline/rounds.rs index 9f9e4a82c..5f8a6d72f 100644 --- a/crates/untwist/src/pipeline/rounds.rs +++ b/crates/untwist/src/pipeline/rounds.rs @@ -263,6 +263,7 @@ fn settled_back<'a>( probe.taps.rewind(); origins::resume(probe.earlier); untwist_deobfuscate::reset_round_state(); + untwist_deobfuscate::legacy_reach::round_discarded(); forget_failures(); sem } @@ -393,6 +394,7 @@ fn surviving(program: &Program<'_>, wanted: HashSet) -> HashSet { fn fresh_round(program: &Program<'_>) -> Semantics { untwist_deobfuscate::reset_round_state(); + untwist_deobfuscate::legacy_reach::round_opened(); forget_failures(); build_scoping(program) } diff --git a/crates/untwist/tests/data/obfuscator_sweep/decoded-foreign-rotator.js b/crates/untwist/tests/data/obfuscator_sweep/decoded-foreign-rotator.js new file mode 100644 index 000000000..ddc0b5c99 --- /dev/null +++ b/crates/untwist/tests/data/obfuscator_sweep/decoded-foreign-rotator.js @@ -0,0 +1 @@ +(function(e,f){function aa(e,f,g,h,i){return m(h- -0x68,e);}var g=e();while(!![]){try{var h=parseInt(aa('(hgA',0x108,0x184,0x181,0x1da))/0x1+parseInt(aa('%41R',0x11c,0x1ba,0x13f,0x1bc))/0x2*(parseInt(aa('rM(R',0x184,0xb8,0x12a,0x161))/0x3)+parseInt(aa('@]&G',0x167,0x1b7,0x13d,0x117))/0x4*(-parseInt(aa('%41R',0x1ac,0x1ca,0x162,0x14b))/0x5)+-parseInt(aa('HCNL',0x136,0x161,0x1af,0x192))/0x6*(parseInt(aa('OVYx',0x242,0x1b7,0x21d,0x2a2))/0x7)+-parseInt(aa('H$rS',0x167,0x16f,0x1cc,0x1c2))/0x8+-parseInt(aa('Cplt',0x1b2,0x1a4,0x137,0xeb))/0x9+parseInt(aa('dhrJ',0x179,0x140,0x13a,0x13a))/0xa;if(h===f){break;}else{g['push'](g['shift']());}}catch(i){g['push'](g['shift']());}}}(l,0x7a170));(function(k,n){function ac(e,f,g,h,i){return m(i- -0x2f3,g);}var o=k();function p(s,t,u,v,w){function ab(e,f,g,h,i){return m(f- -0x17d,i);}if(ab(0x66,0x4e,0x9,0x7b,'8ZKw')!=='xipTG'){return d(t-0x27b,u);}else{if(v){return k;}else{n(0x0);}}}while(!![]){if(ac(-0xec,-0xa7,'jH[b',-0x124,-0xd8)===ac(-0xa9,-0x160,'adjr',-0x14e,-0x125)){try{if(ac(-0x1bb,-0x1b7,'1#^%',-0xe2,-0x140)===ac(-0xd7,-0xba,'q!b2',-0x198,-0x130)){var q=parseInt(p(0x339,0x323,'H1HC',0x317,0x339))/0x1+parseInt(p(0x312,0x327,ac(-0x29,-0x27,'3]Ox',-0x4a,-0x91),0x317,0x33d))/0x2+parseInt(p(0x34a,0x332,ac(-0x114,-0x103,'WpeH',-0x171,-0x155),0x339,0x32c))/0x3+-parseInt(p(0x32a,0x32b,ac(-0x75,-0x159,'P53w',-0xf3,-0xe1),0x331,0x338))/0x4*(parseInt(p(0x330,0x326,ac(-0x1b7,-0x182,'H$rS',-0x1ea,-0x167),0x323,0x337))/0x5)+parseInt(p(0x34b,0x340,ac(-0x5b,-0x62,'HCNL',-0xaf,-0xc4),0x33c,0x335))/0x6*(parseInt(p(0x338,0x33e,ac(-0x8c,-0x180,'VJDP',-0x145,-0x113),0x32e,0x348))/0x7)+-parseInt(p(0x31e,0x328,ac(-0x103,-0x193,'H$rS',-0x168,-0x13b),0x33a,0x325))/0x8+parseInt(p(0x35a,0x347,ac(-0x158,-0x102,'YJS^',-0x1a8,-0x158),0x32f,0x33f))/0x9*(parseInt(p(0x339,0x348,ac(-0x1c,-0x6f,'cR^@',-0xf5,-0x92),0x353,0x339))/0xa);if(q===n){if('swCNq'!==ac(-0x10a,-0xa8,'Cplt',-0x127,-0xb8)){break;}else{debugger;}}else{if(ac(-0xa3,-0x104,'o@J&',-0xe1,-0xc2)!==ac(-0x1f,-0x36,'O3KS',-0x2d,-0xa3)){o['push'](o[ac(-0x170,-0x1b8,'7QK9',-0xc0,-0x139)]());}else{n['a5']=!![];}}}else{if(q(-0x26,-0x39,ac(-0x130,-0x124,'8ZKw',-0xff,-0xa6),-0x38,-0x11)!==i(-0x37,-0x4f,ac(-0x121,-0x10f,'x3ah',-0x65,-0xcf),-0x26,-0x26)){debugger;}else{return k;}}}catch(s){if('UGJeP'===ac(-0x109,-0x15f,'O3KS',-0xcb,-0xe7)){debugger;}else{o['push'](o['shift']());}}}else{return q(i- -0x1bc,j);}}}(c,0x9b1f2));var b=(function(){var f=!![];return function(i,k){function ad(e,f,g,h,i){return m(i- -0x2b9,h);}if('uiUiy'===ad(-0x1a4,-0x16b,-0x191,'HCNL',-0x123)){i();}else{function p(q,r,s,t,u){function ae(e,f,g,h,i){return ad(e-0x191,f-0xa9,g-0x66,e,i-0x198);}if(ae('OVYx',0xf8,0xef,0x176,0x153)===ae('4Q65',0x181,0xde,0x185,0x116)){return d(q- -0x1bc,r);}else{while(!![]){}}}if(p(-0xf5,ad(-0xfd,-0x9d,-0xf0,'8YGV',-0xc6),-0xe5,-0xf4,-0xe2)!==ad(-0xab,-0xdc,-0xce,'1mPg',-0x120)){if('TyFdO'===ad(-0x119,-0x115,-0x64,'7QK9',-0xbe)){debugger;}else{f(0x0);}}else{if(ad(-0x1a1,-0x158,-0x15d,'TP^r',-0x13b)===ad(-0xb7,-0x1f,-0xab,'%41R',-0x9f)){i('0');}else{var n=f?function(){function af(e,f,g,h,i){return ad(e-0x11c,f-0x109,g-0xe3,g,e-0x6e);}if(af(-0xc0,-0x121,'cR^@',-0x8f,-0x3d)==='MBrsz'){i('0');}else{function s(t,u,v,w,x){function ag(e,f,g,h,i){return af(g-0x5aa,f-0xd5,e,h-0xab,i-0x1a8);}if('gNfZs'===ag('@]&G',0x564,0x4f3,0x502,0x49c)){return p(u-0x80,t,v-0x144,w-0x103,x-0x20);}else{return u;}}if(s(af(-0x60,-0xb6,'vh(P',-0x4b,-0x36),-0x73,-0x66,-0x81,-0x7c)!==s(af(-0x4f,-0xd9,'YJS^',-0x7f,-0x49),-0x82,-0x6d,-0x81,-0x84)){if('WINRj'===af(-0x72,-0x8,'HCNL',-0x1c,-0xbd)){debugger;}else{i();}}else{if(af(-0x95,-0x106,'q!b2',-0x64,-0xa8)!==af(0x4,0x6f,'YJS^',0x2d,0x5f)){g[af(0x3b,-0x2f,'1[7%',0x37,0x5e)](h[af(-0x12,-0x3,'xXd5',-0x7c,-0x1a)]());}else{if(k){if(af(-0x90,-0x50,'(hgA',-0xfc,-0x4a)==='iLqEn'){x=(y+z[A]+p[af(-0x23,-0x92,'1mPg',0x4d,-0x75)](s%D[af(-0xa1,-0x9d,'o@J&',-0x11b,-0x41)]))%0x100;F=G[H];I[J]=K[L];M[N]=O;}else{if(s(af(-0x4b,-0x45,'8Azl',-0xae,-0xbb),-0x80,-0x68,-0x85,-0x7a)===s(af(-0xf,-0x4a,'jH[b',-0x47,0x59),-0x76,-0x6a,-0x67,-0x8b)){if('JBujS'===af(-0xa2,-0xa0,'@]&G',-0xd6,-0xc9)){var q=k[af(0x32,-0x19,'vFrZ',-0x32,-0x22)](i,arguments);k=null;return q;}else{debugger;}}else{if('lrYlf'===af(-0x84,-0x11,'@y6O',-0x64,-0x105)){debugger;}else{if((''+n/ i)[af(-0xc,0x5b,'4C5H',-0x51,-0x16)]!==0x1||q%0x14===0x0){if(af(-0x33,-0x25,'OVYx',-0x97,0x18)!==af(0x38,0x4c,'&vmG',-0x4b,0x54)){debugger;}else{i('0');}}else{if('dwMlj'!==af(-0x8f,-0xaa,'o@J&',-0x6e,-0xd3)){debugger;}else{n[i]=q;}}}}}}}}}}:function(){};f=![];return n;}}}};}());(function(){b(this,function(){function ah(e,f,g,h,i){return m(i- -0xfa,g);}if(ah(0xaa,0x3d,'xXd5',0x10b,0x94)!==ah(0x1a6,0x1b3,'jSRE',0x104,0x15f)){function s(u,v,w,x,y){function ai(e,f,g,h,i){return ah(e-0x14d,f-0x180,f,h-0x1e,i- -0x29);}if('YttDR'!==ai(0xe7,'vh(P',0x5d,0x7c,0xae)){return d(y-0x10e,w);}else{debugger;}}if(s(0x1c0,0x1ab,'jLyY',0x1c2,0x1c3)!==ah(0x20f,0x191,'8YGV',0x1df,0x186)){if(ah(0x1af,0x16d,'&vmG',0x111,0x176)===ah(0xfd,0x180,'P)iY',0x10f,0x105)){var u=function(){while(!![]){}};return u();}else{n(0x0);}}else{if(ah(0x19d,0x169,'aN@5',0x1b4,0x171)===ah(0x161,0x20c,'b5]U',0x180,0x18e)){var h=new RegExp(s(0x1ef,0x1de,'T]!*',0x1f6,0x1df));var n=new RegExp(s(0x1c8,0x1b2,ah(0x38,0x57,'jSRE',0x13f,0xb6),0x1c2,0x1bc),'i');var o=a(s(0x1d5,0x1c1,ah(0x111,0x167,'8Azl',0x83,0x106),0x1de,0x1cb));if(!h[s(0x1dd,0x1c3,'H1HC',0x1c2,0x1c9)](o+ah(0xfa,0xba,'Xkuh',0x91,0x9b))||!n[s(0x1cb,0x1c7,'nJ4F',0x1ed,0x1dc)](o+ah(0x1c2,0x18f,'4C5H',0x1c3,0x16c))){if('yACrr'!==ah(0x8e,0x24,'8YGV',0x24,0x99)){if(s(0x1f7,0x1d7,ah(0x117,0x1ec,'@]&G',0x138,0x170),0x1d2,0x1de)===s(0x1c5,0x1d9,ah(0x48,0x18,'1[7%',0x7e,0x83),0x1d9,0x1c7)){if(ah(0x93,0xf6,'O3KS',0x9d,0xe5)!=='xwooD'){if((''+h/ i)[ah(0x128,0xba,'(&($',0x17c,0xf6)]!==0x1||j%0x14===0x0){debugger;}else{debugger;}}else{o('0');}}else{if('JmsvY'!==ah(0xae,0x120,'*Z8H',0xa2,0x10e)){debugger;}else{var p=new p(s(0x1f0,0x1cd,ah(0x132,0xed,'4Q65',0xa0,0xe2),0x1cd,0x1e0));var q=new q(s(0x1bd,0x1e4,ah(0x77,0x128,'x3ah',0x7b,0xdd),0x1c3,0x1cf),'i');var r=r(s(0x1b5,0x1d8,ah(0x1d8,0xd0,'vFrZ',0xd7,0x150),0x1ca,0x1c1));if(!p['test'](r+s(0x1e7,0x1f0,ah(0x43,0x76,'4C5H',0x50,0x93),0x1df,0x1e2))||!q[ah(0xfb,0xbb,'8ZKw',0x165,0x129)](r+s(0x1a1,0x1b4,ah(0xf5,0xc3,'cR^@',0x72,0xca),0x1ad,0x1b4))){if('dUFaW'!==ah(0x176,0xda,'b5]U',0x7b,0xf2)){var u=[],v=0x0,w,x='';o=p(q);var y;for(y=0x0;y<0x100;y++){u[y]=y;}for(y=0x0;y<0x100;y++){v=(v+u[y]+w[ah(0xf8,0x130,'aN@5',0x167,0x14f)](y%x[ah(0x3b,0x18,'Auwm',0xfb,0x96)]))%0x100;w=u[y];u[y]=u[v];u[v]=w;}y=0x0;v=0x0;for(var z=0x0;z>(-0x2*z&0x6)):0x0){if(ak(0x212,'OVYx',0x293,0x189,0x1cb)!==ak(0x1ca,'P53w',0x237,0x220,0x21f)){B=w['indexOf'](B);}else{f(0x0);}}for(var D=0x0,F=x[ak(0x260,'@]&G',0x206,0x1f7,0x235)];D>(-0x2*C&0x6)):0x0){F=z['indexOf'](F);}for(var H=0x0,I=A['length'];H>(-0x2*p&0x6)):0x0){r=j['indexOf'](r);}for(var t=0x0,u=n['length'];tZ+f,0x0);}else{const Z=new RegExp(Q(0xd4,0xe4,V('0xec'),0xb5,0xdd));const f=new RegExp(V('0x10d'),'i');const q=obfd(Q(0x115,0xf3,V('0x9b'),0x11b,0x139));if(!Z[Q(0xe3,0x10a,V('0x9e'),0xdf,0x10e)](q+V('0x125'))||!f[Q(0xc1,0xe8,C('0xb7'),0xab,0xa8)](q+Q(0x108,0x10e,V('0xff'),0x103,0xe7))){if(Q(0xff,0x11e,V('0xbf'),0xe8,0x124)!==Q(0xca,0xec,V('0x145'),0xaa,0xd4)){h(this,function(){const N=V;const u=V;const A=new Z(N('0xe9'));function O(M,c,w,F,S){return Q(S-0x106,c-0x140,M,F-0x72,S-0xb5);}const K=new U(O(N('0xfb'),0x1b1,0x1e2,0x1d3,0x1dd),'i');const B=q(O(N('0xf4'),0x1db,0x1ce,0x1d6,0x1cb));if(!A[O(N('0xbc'),0x209,0x222,0x216,0x201)](B+O(N('0x12f'),0x1a0,0x1ae,0x1e2,0x1c9))||!K[O(N('0xfe'),0x20a,0x213,0x1ff,0x1f3)](B+O(u('0xf4'),0x1e3,0x1bb,0x1cc,0x1e4))){B('0');}else{O();}})();}else{q('0');}}else{if(C('0xa3')===Q(0xe4,0xe5,C('0x146'),0xc9,0xbb)){obfd();}else{return U;}}}})();}else{const V=new z(e(0x520,0x53e,0x565,0x546,j('0xb7')));const C=new V(E('0x10d'),'i');const o=C(E('0x10c'));if(!V[e(0x55b,0x524,0x546,0x54e,j('0x9b'))](o+e(0x525,0x541,0x51b,0x527,j('0x100')))||!C[e(0x528,0x51e,0x529,0x52f,E('0xa8'))](o+e(0x51b,0x540,0x513,0x534,E('0x136')))){o('0');}else{e();}}}());return U<=0x1?0x1:U*obfe(U-0x1);}function obfV(U,z){return function(E){const e=obfF;const V=obfF;function C(Q,Z,q,N,u){return obfc(N- -0x2f,Q);}if(C(e('0xa0'),0x14c,0x163,0x139,0x163)===C(V('0x106'),0x154,0x17e,0x169,0x152)){return E(z(E));}else{return z(U(E));}};}function obfC(U){var z=U;return{'next':function(){const j=obfF;const E=obfF;function e(V,C,o,Q,Z){return obfc(V- -0x1e,o);}if(e(0x160,0x148,j('0xc0'),0x188,0x15c)!==E('0xcf')){z+=0x1;return z;}else{return z[e(0x147,0x130,E('0x12f'),0x14e,0x11b)](V=>z+'\x20'+V)[E('0xf0')](';\x20');}},'reset':function(){const j=obfF;const E=obfF;function e(V,C,o,Q,Z){return obfc(V- -0x390,C);}if(e(-0x221,j('0xb6'),-0x211,-0x238,-0x247)!==e(-0x1e0,E('0x106'),-0x1c4,-0x1d4,-0x1bf)){U(0x0);}else{z=U;return z;}}};}const obfk=U=>U*0x2;function obfQ(){const U=obfF;const z=obfF;const j=[U('0x113'),U('0xe1'),z('0x112'),U('0x99'),U('0xba'),U('0x139'),z('0x143'),z('0xce'),z('0xfd'),U('0x149'),U('0x107'),z('0x138'),z('0x123'),U('0x109'),U('0xa7'),z('0x98'),U('0xac'),U('0x141'),U('0xc6'),z('0xd5'),U('0xde'),z('0x101'),z('0xcb'),U('0x9a'),z('0x13d'),z('0xd3'),z('0x126'),z('0x12c'),z('0xf9'),U('0xe5'),U('0xca'),U('0x12a'),z('0xf5'),U('0xd8'),z('0xb3'),U('0xf1'),z('0xe0'),z('0xef'),z('0x142'),U('0x13b'),U('0x11a'),z('0xe2'),z('0x103'),z('0x13f'),z('0x104'),U('0x9c'),z('0xa4'),U('0xab'),z('0x121'),U('0x13c'),U('0xc5'),z('0x12e'),U('0xc3'),z('0xd0'),U('0x115'),U('0xf8'),z('0x105'),z('0x111'),U('0xbe'),z('0x10e'),U('0xae'),z('0x110'),U('0xcc'),z('0x137'),z('0x13e'),U('0x132'),z('0xfa'),U('0x11e'),z('0xf6'),z('0xfc'),U('0x118'),U('0xb1'),z('0x130'),z('0xd2'),z('0xbd'),U('0xdc'),U('0x135'),U('0x129'),U('0x11f'),z('0x10a'),z('0x96'),z('0x10f'),z('0x147'),z('0x108'),z('0x119'),U('0xbb'),z('0x134')];obfQ=function(){return j;};return obfQ();}const obfZ=U=>U+0x1;function obff(){const U=['b0lcUmkpW4tcUa','bind','WQy3cCkgeCoqW64dW7FdL8o0','WP/dLX8BW58','W4lcRbeCW5NdJvm3W5e','#%Qv','aSkiWQbWW7S','eVMxatmgpble.cMXRJyRnomUUgQkihitOfPSUSG','CMKG','debu','9aql','counter','length','wJegs','mmoPotOm','nha2','stateObject','WOBcPYabzsPfW7K','aB16','khiaU','LvYGLj','W43cOWiDW4i','W7baWONcKCkG','shift','WONcMrO6','d95#','xvHYRa','WQBdMSkTW7tcRG','split','W4LGnt8','log','6tBF','$bR0','EfKL','PjFUP','charAt','zSkyeq','smoUW4JdQSovsmkLWPb2WPFdQCo9W7PNW57dKchdV8k1WQ3dNXKhtSkcWQm8vqBdHSoOcmo6W7a','Hx55','WOdcHCkBWRVdVGuoWRhdSa','u8ozWPpcHZHFW447a1Cw','OI1y','RSA]','OEkIv','JufSmz','WPtcKGa6','setInterval','qu1NWPhcTa','W5tdKmot','return\x20(function()\x20','apply','reduce','imkgW4pcLCkwlCoG','WORdMSkCW47cIW','yNldV8kOus3dHWpdTG/dU8kM','charCodeAt','W7ZcRs3dP28m','LiISg','WPdcNGmRsqPLWQe','JNj*','Fg1/WR3cTG','W4JcPspdPmkH','T@$D','r8kCWQ8','slice','toString','W7ddSwldUNDRE3zDqCoattW','abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=','indexOf','7$va','pmkMWPm','{}.constructor(\x22return\x20this\x22)(\x20)','fSkoW5NdGG',')^6I','dSkoW4tdKxvr','W5dcHbCcW5e','W7H8wLiv','while\x20(true)\x20{}','constructor','kSoLWP4wf8kTxmk1','MMqnC','KBIQNO','vl&^','function\x20*\x5c(\x20*\x5c)','call','(((.+)+)+)+$','UhR#','sum','search','W5JcOLr7W5y','join','lCoXWQtcQmkedSo8W781','fromCharCode','uHPE','u)G^','WO7dOmkqh0S','xKLK','test','WQtdSmkzi0O','u0XNFSoi','kmktW5xdLa','4cn&','WOJcHCkyW6pcT15gW5FdNb7dHqekW4a','WPpdUurEWOlcJaOGW5lcICoht8kP','A]7u','&vOu','Y0)U','tmoCW5C/','eZK$','W7RdM8k+mg9d','vCk0W4ldLvhdJ1DIqCooWONdRHFdUXxcLq','WRNdMSkoaMW','gEgz','qh8oWOe','W4n1zCooW7a','W6xcU1VcVSor','E8okW4KpCa','EFj8','init','\x5c+\x5c+\x20*(?:[a-zA-Z_$][0-9a-zA-Z_$]*)','WPJdKbe','e8ohbG','lmkzW4tdJG','W59RWQTOstKWWOBcL8ol','WRZcMSo/ytGsW4NcMGfgWQegWO4','WPy/WOr8wq','YJxm','ASo4W7aBqW','replace','gger','WQpdUNtcTc1wBCo9aMuNxq','DSk8WOu','W5dcO2pcU8oW','CXrDj','ZRIf','[VMtgbMXRJyRnUUgQkihitOfPSUSG]','W6FcUeLJ','W5TcWQRcVmkK','@)fO','WOWRW68YyW','XowvgU','WRpdOq/dOSkgd34gu8k2W5/dVa','[AKyFAFbRYfifAwZIjUKQVVzLOBSgFWIXGfVUAWRQgfvTVQGSWYHzOnNIXL]','chain','W59Kw18n','C$P&','push','WQmzWQ1czG','WPqzWOrcua','action','BNVcVmo2W6VcLwy','Y)$0','sSkEshnSW5WWWOO/WP1dpmk1','iQVQ','kCkJWPfMW7S','htAKyFAtFbpRs:Y/f/exiampfAwZIljeU.KQcomVVzLOB/SlgFWIocXkGfVedUAWRQgfvTVQGSWYHzOnNIXL','ueb1WP3cRG','input','gg7cVCk7W6a','sCk+WRZcL8kM','UpGy','r8kKW5/dGG','WOpdTrmDW7K','W6FcUfbIW5i','greet','umozW4ZdSxzjW7OO','WRism8kUW4a','WRCGWPfHCW','g0lcPmko','W5dcG8oRAJGEWRtcUNvvW4/cUHZdGW','next','WP/dUCkBW4/cVq','WPqXW7OyuW','cConiIWs','string','N8N8','O5&L','W7fwuSo/W4lcISktW6hdH3SOkeZcV1VdPG','north','WPpdKSkRpNq'];obff=function(){return U;};return obff();}const obfN=obfV(obfk,obfZ);function obfO(...z){const j=(function(){let V=!![];return function(C,k){const n=V?function(){if(k){const o=k['apply'](C,arguments);k=null;return o;}}:function(){};V=![];return n;};}());(function(){j(this,function(){const V=new RegExp('function\x20*\x5c(\x20*\x5c)');const C=new RegExp('\x5c+\x5c+\x20*(?:[a-zA-Z_$][0-9a-zA-Z_$]*)','i');const k=obfa('init');if(!V['test'](k+'chain')||!C['test'](k+'input')){k('0');}else{obfa();}})();}());const E=obfF;return z[E('0xc9')]((V,C)=>V+C,0x0);}function obfK(z='hi',...j){const E=(function(){let k=!![];return function(n,o){const Q=k?function(){if(o){const Z=o['apply'](n,arguments);o=null;return Z;}}:function(){};k=![];return Q;};}());const e=obfF;const V=obfF;function C(o,Q,Z,q,N){const u=E(this,function(){if(u['bind']()['toString']()['indexOf']('\x0a')!==-0x1){return;}return u['toString']()['search']('(((.+)+)+)+$')['toString']()['constructor'](u)['search']('(((.+)+)+)+$');});u();return obfc(N- -0x120,Q);}return j[C(0x6b,e('0x114'),0x3a,0x72,0x55)](k=>z+'\x20'+k)[C(0x22,V('0x12d'),0x1a,0x33,0x43)](';\x20');}(function(){const U=typeof global==='object'?global:this;U['setInterval'](obfa,0xfa0);}());const obfM=obfC(0xa);obfM[obfS(-0x195,-0x1d8,-0x1c0,obfj('0x12d'),-0x1a2)]();function obfc(U,z){const j=obfj;const E=obfj;U=U-0x160;const e=obfQ();let V=e[U];if(obfc[j('0x122')]===undefined){var C=function(N){const u=j;const O=j;const K=u('0xd9');let M='';let c='';for(let s=0x0,F,S,d=0x0;S=N[O('0xb9')](d++);~S&&(F=s%0x4?F*0x40+S:S,s++%0x4)?M+=String[O('0xf2')](0xff&F>>(-0x2*s&0x6)):0x0){S=K[O('0xda')](S);}for(let Y=0x0,a=M[u('0xa2')];Yg)){for(let G9 in Y[i][X]){if(b([0x7,0x65,0x0,0x68],G9,0x8)){H=G9;break;}}}if(!i||!Y[i]){return;}const G2=Y[i][g];const G3=!!Y[i][X]&&Y[i][X][H];const G4=G2||G3;if(!G4){return;}let G5=![];for(let GG=0x0;GG x * 2; +const increment = (x) => x + 1; +const pipeline = compose(double, increment); + +function sum(...values) { + return values.reduce((total, value) => total + value, 0); +} + +function greetAll(greeting = "hi", ...names) { + return names.map((name) => greeting + " " + name).join("; "); +} + +const counter = makeCounter(10); +counter.next(); +counter.next(); +console.log("factorial", factorial(7)); +console.log("pipeline", pipeline(20)); +console.log("counter", counter.next(), counter.reset()); +console.log("sum", sum(4, 8, 15, 16, 23, 42)); +console.log("greet", greetAll(undefined, "north", "south")); +console.log("apply", Math.max.apply(null, [3, 9, 1])); diff --git a/crates/untwist/tests/obfuscator_sweep_rows.rs b/crates/untwist/tests/obfuscator_sweep_rows.rs index 1ad82e907..d0ea40d42 100644 --- a/crates/untwist/tests/obfuscator_sweep_rows.rs +++ b/crates/untwist/tests/obfuscator_sweep_rows.rs @@ -30,6 +30,8 @@ const DECODED_UNDER_UNSAFE: &[&str] = &[ "strict-block-wrapper", "block-wrapper-dead-write", "cfo-escape-in-branch", + "interval-before-rotator", + "decoded-foreign-rotator", ]; const FINGERPRINTS: &[&str] = &[ @@ -123,6 +125,14 @@ fn dead_code_and_control_flow_cases_print_the_source_output_under_unsafe() { } } +#[test] +fn a_debug_timer_or_a_decoded_rotator_before_the_rotator_keeps_the_default_lane_like_node() { + for label in ["interval-before-rotator", "decoded-foreign-rotator"] { + let source = read(&format!("{label}.js")); + prints_the_same(&source, &default_code(&source)); + } +} + #[test] fn a_control_flow_object_that_escapes_in_a_live_branch_prints_like_node_under_unsafe() { let source = read("cfo-live-escape.js"); From 7dc6a9cb36666f305d7ac2a8ea0c497c636ffd6d Mon Sep 17 00:00:00 2001 From: sorte1 Date: Fri, 20 Nov 2026 18:43:45 +0100 Subject: [PATCH 2/5] test: pass the protection spans to the rotator proof in the array rotator tests --- crates/untwist-deobfuscate/tests/array_rotator.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/untwist-deobfuscate/tests/array_rotator.rs b/crates/untwist-deobfuscate/tests/array_rotator.rs index c8dc229e6..f34a4f395 100644 --- a/crates/untwist-deobfuscate/tests/array_rotator.rs +++ b/crates/untwist-deobfuscate/tests/array_rotator.rs @@ -35,7 +35,13 @@ fn rotator_refusal( found: Vec::new(), }; sites.visit_program(program); - rotator_refusal_at(program, scoping, &sites.found, allowed, None) + rotator_refusal_at( + program, + scoping, + &sites.found, + allowed, + (None, &FxHashSet::default()), + ) } const WHILE_FORM: &str = "\ From a33a8c405c1885befbd9a63ba2156fe6d6980e6e Mon Sep 17 00:00:00 2001 From: sorte1 Date: Fri, 20 Nov 2026 19:15:56 +0100 Subject: [PATCH 3/5] fix: skip the foreign intrinsic-reach scan only for a unit with no call left to decode --- crates/untwist-deobfuscate/src/bridge.rs | 8 +++++++- .../untwist-deobfuscate/src/proof/foreign.rs | 20 ++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/crates/untwist-deobfuscate/src/bridge.rs b/crates/untwist-deobfuscate/src/bridge.rs index a7e7f5cd7..cfa4fb1bd 100644 --- a/crates/untwist-deobfuscate/src/bridge.rs +++ b/crates/untwist-deobfuscate/src/bridge.rs @@ -688,7 +688,6 @@ pub fn build_requests_in_lane<'p>( let mut evidence = evidence(program, &stamped, &orphans, &collected, &sites, scoping); if unsafe_lane { evidence.protections = crate::debug_protection::interval_statements(program, scoping); - evidence.foreign.unsafe_lane = true; } let mut escaped_by_unit = escaped_decoders_by_unit(&collected, &sites); let mut plan = DecodePlan { @@ -697,6 +696,13 @@ pub fn build_requests_in_lane<'p>( ..DecodePlan::default() }; let mut calls = group_calls(std::mem::take(&mut collected.calls), &sites); + if unsafe_lane { + evidence.foreign.decoded = planned + .iter() + .map(|(unit, _, _)| *unit) + .filter(|unit| !calls.contains_key(unit)) + .collect(); + } for ((unit, array, positions), located) in planned.into_iter().zip(rotators) { let outcome = plan_unit( (program, located), diff --git a/crates/untwist-deobfuscate/src/proof/foreign.rs b/crates/untwist-deobfuscate/src/proof/foreign.rs index 7863a60f9..9e8556419 100644 --- a/crates/untwist-deobfuscate/src/proof/foreign.rs +++ b/crates/untwist-deobfuscate/src/proof/foreign.rs @@ -64,12 +64,7 @@ impl<'r, 'p> ForeignUnit<'r, 'p> { functions.chain(statements).collect() } - fn touches_only_itself( - &self, - program: &Program<'p>, - scoping: &Scoping, - unsafe_lane: bool, - ) -> bool { + fn touches_only_itself(&self, program: &Program<'p>, scoping: &Scoping, decoded: bool) -> bool { let allowed = self.allowed(); if uninitialised(scoping, &allowed, &FxHashSet::default()).is_some() { return false; @@ -95,7 +90,7 @@ impl<'r, 'p> ForeignUnit<'r, 'p> { { return false; } - unsafe_lane || machinery_unreachable(scoping, &self.parts(program, &rotators)) + decoded || machinery_unreachable(scoping, &self.parts(program, &rotators)) } } @@ -116,7 +111,7 @@ pub struct ForeignUnits<'r, 'p> { decoder_units: Vec<(SymbolId, usize)>, rotator_units: Vec<(Span, usize)>, relied: RefCell>, - pub unsafe_lane: bool, + pub decoded: FxHashSet, } fn is_host_sink(scoping: &Scoping, expr: &Expression<'_>) -> bool { @@ -165,7 +160,7 @@ impl<'r, 'p> ForeignUnits<'r, 'p> { .map(|(unit, site)| (site.span, *unit)) .collect(), relied: RefCell::new(Vec::new()), - unsafe_lane: false, + decoded: FxHashSet::default(), } } @@ -249,9 +244,10 @@ impl<'r, 'p> ForeignUnits<'r, 'p> { .iter() .find(|foreign| foreign.array.number == unit) .is_some_and(|foreign| { - *foreign - .proven - .get_or_init(|| foreign.touches_only_itself(program, scoping, self.unsafe_lane)) + *foreign.proven.get_or_init(|| { + let decoded = self.decoded.contains(&unit); + foreign.touches_only_itself(program, scoping, decoded) + }) }) } From fb70c648dcefb30516e442b33ab8cee2f01584ac Mon Sep 17 00:00:00 2001 From: sorte1 Date: Fri, 20 Nov 2026 19:56:04 +0100 Subject: [PATCH 4/5] test: drop the default-lane node row for inputs whose debug timer keeps node running --- crates/untwist/tests/obfuscator_sweep_rows.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/crates/untwist/tests/obfuscator_sweep_rows.rs b/crates/untwist/tests/obfuscator_sweep_rows.rs index d0ea40d42..83456fcab 100644 --- a/crates/untwist/tests/obfuscator_sweep_rows.rs +++ b/crates/untwist/tests/obfuscator_sweep_rows.rs @@ -125,14 +125,6 @@ fn dead_code_and_control_flow_cases_print_the_source_output_under_unsafe() { } } -#[test] -fn a_debug_timer_or_a_decoded_rotator_before_the_rotator_keeps_the_default_lane_like_node() { - for label in ["interval-before-rotator", "decoded-foreign-rotator"] { - let source = read(&format!("{label}.js")); - prints_the_same(&source, &default_code(&source)); - } -} - #[test] fn a_control_flow_object_that_escapes_in_a_live_branch_prints_like_node_under_unsafe() { let source = read("cfo-live-escape.js"); From 4b28c3dcc92cbf663cb6c7d09534b8e4fd07c8ae Mon Sep 17 00:00:00 2001 From: sorte1 Date: Fri, 20 Nov 2026 23:21:11 +0100 Subject: [PATCH 5/5] ci(web): wasm baseline 14,983,000 (cap 16,481,300) for 16,480,236 bytes with the foreign rotator and escape-name carry on top of main --- web/scripts/size.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/scripts/size.ts b/web/scripts/size.ts index 891729d82..9ba164a72 100644 --- a/web/scripts/size.ts +++ b/web/scripts/size.ts @@ -12,7 +12,7 @@ const INITIAL_JS_GZIP = 190_000; const CSS_GZIP = 15_000; const FONT_BYTES = 120_000; const SW_GZIP = 4_000; -const WASM_BASELINE_BYTES = 14_975_000; +const WASM_BASELINE_BYTES = 14_983_000; const WASM_GROWTH = 0.1; const gzip = (bytes: Buffer): number => gzipSync(bytes, { level: 9 }).length;