Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 83 additions & 10 deletions crates/untwist-deobfuscate/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Span>,
) -> FxHashMap<usize, Span> {
if collected.escapes.is_empty() || !crate::legacy_reach::speculates_escapes() {
return FxHashMap::default();
}
let spans: FxHashSet<Span> = collected.escapes.iter().map(|(_, span)| *span).collect();
let branched = in_branches(program, scoping, &spans);
let unguarded: FxHashSet<usize> = collected
.escapes
.iter()
.filter(|(_, span)| !branched.contains(span))
.map(|(unit, _)| *unit)
.collect();
let speculated: FxHashMap<usize, Span> = 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<usize, Span>),
(decoder_spans, array_span): (impl Fn(usize) -> Vec<Span>, impl Fn(usize) -> Option<Span>),
) -> FxHashMap<usize, EscapeGuards> {
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<usize, Span>),
Expand Down Expand Up @@ -144,6 +192,7 @@ fn guarded_escapes(
};
let mut unguarded: FxHashSet<usize> = FxHashSet::default();
let mut speculated: FxHashSet<usize> = FxHashSet::default();
let mut unbranched: FxHashSet<usize> = FxHashSet::default();
let mut found: Vec<(usize, bool, Vec<BranchGuard>)> = Vec::new();
let values = collected.decoder_values.iter().map(|escape| (escape, true));
let dynamic = collected.dynamic_calls.iter().map(|escape| (escape, false));
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -212,6 +269,7 @@ struct Evidence<'r, 'p> {
with_statement: Option<Span>,
rotator_sites: Vec<(usize, RotatorSite)>,
foreign: ForeignUnits<'r, 'p>,
protections: FxHashSet<Span>,
}

fn first_per_unit(found: impl Iterator<Item = (usize, Span)>) -> FxHashMap<usize, Span> {
Expand Down Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -508,12 +567,12 @@ fn orphan_entries<'p>(stamped: &Stamped<'p>, orphans: &[usize]) -> Vec<(&'p str,

type PlanOutcome<'p> = Result<DecodeRequest<'p>, Refusal>;

fn refuse_unsupported(plan: &mut DecodePlan<'_>) {
fn refuse_unsupported(plan: &mut DecodePlan<'_>, unsafe_lane: bool) {
loop {
let decoding: Vec<usize> = 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();
Expand Down Expand Up @@ -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);
Expand All @@ -607,11 +666,15 @@ pub fn build_requests_in_lane<'p>(
.map(|symbol| scoping.symbol_span(symbol))
.collect()
};
let array_span = |unit: usize| -> Option<Span> {
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()
Expand All @@ -622,14 +685,24 @@ 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);
}
let mut escaped_by_unit = escaped_decoders_by_unit(&collected, &sites);
let mut plan = DecodePlan {
intrinsic_object: object_operand_is_intrinsic(&collected.patches),
requests: Vec::with_capacity(members.len()),
..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),
Expand All @@ -649,15 +722,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()
Expand Down
1 change: 1 addition & 0 deletions crates/untwist-deobfuscate/src/dead_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub struct EscapeGuards {
pub values: Vec<Vec<BranchGuard>>,
pub dynamic: Vec<Vec<BranchGuard>>,
pub speculative: Vec<Span>,
pub fallback: Vec<Span>,
}

struct Enclosing<'e> {
Expand Down
20 changes: 20 additions & 0 deletions crates/untwist-deobfuscate/src/debug_protection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Span> {
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);
Expand Down
13 changes: 11 additions & 2 deletions crates/untwist-deobfuscate/src/decoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ fn apply_decoded_values<'a>(
evaluator: &mut InterpEvaluator,
values: Vec<Option<DecodedValue>>,
) -> 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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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<usize> = 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;
Expand Down
65 changes: 55 additions & 10 deletions crates/untwist-deobfuscate/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -260,19 +271,53 @@ impl<'a> Pass<'a> for FindDecoders {
}
}

fn speculated_decoders(
program: &Program<'_>,
cx: &Cx<'_, '_>,
unstamped: &[DecoderShape],
watched: &Watched,
) -> FxHashMap<SymbolId, Vec<Span>> {
if !cx.opts.unsafe_transforms || !crate::legacy_reach::speculates_escapes() {
return FxHashMap::default();
}
let scoping = cx.scoping();
let homes: FxHashMap<SymbolId, Span> = 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<SymbolId> = 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() {
Expand Down
7 changes: 1 addition & 6 deletions crates/untwist-deobfuscate/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading