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
133 changes: 133 additions & 0 deletions crates/untwist-deobfuscate/src/branch_reach.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use oxc_ast::ast::*;
use oxc_ast_visit::{walk_js, VisitJs};
use oxc_semantic::Scoping;
use oxc_span::Span;
use oxc_syntax::scope::ScopeFlags;
use oxc_syntax::symbol::SymbolId;
use rustc_hash::FxHashSet;

struct InBranch<'e> {
scoping: &'e Scoping,
escapes: &'e FxHashSet<Span>,
dormant: &'e FxHashSet<SymbolId>,
depth: usize,
found: FxHashSet<Span>,
read_outside: FxHashSet<SymbolId>,
declared: FxHashSet<SymbolId>,
}

impl InBranch<'_> {
fn note(&mut self, span: Span) {
if self.depth > 0 && self.escapes.contains(&span) {
self.found.insert(span);
}
}

fn branch<T>(&mut self, visit: impl FnOnce(&mut Self) -> T) -> T {
self.depth += 1;
let out = visit(self);
self.depth -= 1;
out
}
}

fn declared_symbol(function: &Function<'_>) -> Option<SymbolId> {
match function.is_declaration() {
true => function.id.as_ref()?.symbol_id.get(),
false => None,
}
}

impl<'a> VisitJs<'a> for InBranch<'_> {
fn visit_function(&mut self, it: &Function<'a>, flags: ScopeFlags) {
let symbol = declared_symbol(it);
if let Some(symbol) = symbol {
self.declared.insert(symbol);
}
match symbol.is_some_and(|symbol| self.dormant.contains(&symbol)) {
true => self.branch(|scan| walk_js::walk_function(scan, it, flags)),
false => walk_js::walk_function(self, it, flags),
}
}

fn visit_if_statement(&mut self, it: &IfStatement<'a>) {
self.visit_expression(&it.test);
self.branch(|scan| scan.visit_statement(&it.consequent));
if let Some(alternate) = &it.alternate {
self.branch(|scan| scan.visit_statement(alternate));
}
}

fn visit_conditional_expression(&mut self, it: &ConditionalExpression<'a>) {
self.visit_expression(&it.test);
self.branch(|scan| scan.visit_expression(&it.consequent));
self.branch(|scan| scan.visit_expression(&it.alternate));
}

fn visit_logical_expression(&mut self, it: &LogicalExpression<'a>) {
self.visit_expression(&it.left);
self.branch(|scan| scan.visit_expression(&it.right));
}

fn visit_switch_case(&mut self, it: &SwitchCase<'a>) {
if let Some(test) = &it.test {
self.visit_expression(test);
}
self.branch(|scan| scan.visit_statements(&it.consequent));
}

fn visit_call_expression(&mut self, it: &CallExpression<'a>) {
self.note(it.span);
walk_js::walk_call_expression(self, it);
}

fn visit_identifier_reference(&mut self, it: &IdentifierReference<'a>) {
self.note(it.span);
if self.depth > 0 {
return;
}
let Some(reference) = it.reference_id.get() else {
return;
};
let reference = self.scoping.get_reference(reference);
if let Some(symbol) = reference.symbol_id().filter(|_| reference.is_read()) {
self.read_outside.insert(symbol);
}
}
}

fn scan<'e>(
(program, scoping): (&Program<'_>, &'e Scoping),
escapes: &'e FxHashSet<Span>,
dormant: &'e FxHashSet<SymbolId>,
) -> InBranch<'e> {
let mut scan = InBranch {
scoping,
escapes,
dormant,
depth: 0,
found: FxHashSet::default(),
read_outside: FxHashSet::default(),
declared: FxHashSet::default(),
};
scan.visit_program(program);
scan
}

pub fn in_branches(
program: &Program<'_>,
scoping: &Scoping,
escapes: &FxHashSet<Span>,
) -> FxHashSet<Span> {
let none = FxHashSet::default();
let first = scan((program, scoping), escapes, &none);
let dormant: FxHashSet<SymbolId> = first
.declared
.difference(&first.read_outside)
.copied()
.collect();
if dormant.is_empty() {
return first.found;
}
scan((program, scoping), escapes, &dormant).found
}
33 changes: 31 additions & 2 deletions crates/untwist-deobfuscate/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use untwist_eval::decode::{decoded_to_expression, Call, Decoded, DecoderProgram, FnRef};

use crate::array_rotator::find_rotators;
use crate::branch_reach::in_branches;
use crate::calls::{collect_base, Collected};
use crate::dead_guards::{enclosing_guards, BranchGuard, EscapeGuards};
use crate::notes;
Expand Down Expand Up @@ -114,9 +115,10 @@ fn escaped_decoders_by_unit(
}

fn guarded_escapes(
program: &Program<'_>,
(program, scoping): (&Program<'_>, &Scoping),
collected: &mut Collected<'_>,
(sites, rotators): (&Sites, &FxHashMap<usize, Span>),
decoder_spans: impl Fn(usize) -> Vec<Span>,
) -> FxHashMap<usize, EscapeGuards> {
let in_own_rotator = |(index, span): &(usize, Span)| {
sites
Expand All @@ -136,7 +138,12 @@ fn guarded_escapes(
return by_unit;
}
let enclosing = enclosing_guards(program, &escapes);
let branched = match crate::legacy_reach::speculates_escapes() {
true => in_branches(program, scoping, &escapes),
false => FxHashSet::default(),
};
let mut unguarded: FxHashSet<usize> = FxHashSet::default();
let mut speculated: 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 @@ -146,6 +153,9 @@ fn guarded_escapes(
};
match enclosing.get(span).filter(|guards| !guards.is_empty()) {
Some(guards) => found.push((unit, is_value, guards.clone())),
None if branched.contains(span) => {
speculated.insert(unit);
}
None => {
unguarded.insert(unit);
}
Expand All @@ -162,6 +172,11 @@ fn guarded_escapes(
collected
.dynamic_calls
.retain(|(index, _)| !is_guarded(index));
for unit in speculated {
if !unguarded.contains(&unit) {
by_unit.entry(unit).or_default().speculative = decoder_spans(unit);
}
}
for (unit, is_value, guards) in found {
if unguarded.contains(&unit) {
continue;
Expand Down Expand Up @@ -573,7 +588,21 @@ pub fn build_requests_in_lane<'p>(
.zip(&rotators)
.filter_map(|((unit, _, _), located)| Some((*unit, located.as_ref()?.0)))
.collect();
guarded_escapes(program, &mut collected, (&sites, &rotator_spans))
let decoder_spans = |unit: usize| -> Vec<Span> {
members
.get(&unit)
.into_iter()
.flatten()
.filter_map(|position| stamped.decoders.get(*position)?.symbol)
.map(|symbol| scoping.symbol_span(symbol))
.collect()
};
guarded_escapes(
(program, scoping),
&mut collected,
(&sites, &rotator_spans),
decoder_spans,
)
} else {
FxHashMap::default()
};
Expand Down
18 changes: 17 additions & 1 deletion crates/untwist-deobfuscate/src/calls/agnostic/holds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ fn prototype_base(member: &MemberExpression<'_>) -> bool {
}

fn literal_key(property: &ObjectProperty<'_>) -> bool {
!property.computed || matches!(property.key, PropertyKey::StringLiteral(_))
!property.computed
|| matches!(
property.key,
PropertyKey::StringLiteral(_)
| PropertyKey::NumericLiteral(_)
| PropertyKey::Identifier(_)
)
}

impl<'a> Sites<'_, 'a> {
Expand Down Expand Up @@ -137,6 +143,7 @@ impl<'a> Sites<'_, 'a> {
None => self.binding_hold(inner, id, next, false),
},
Expression::CallExpression(call) => self.call_hold(call, next),
Expression::NewExpression(new) if self.fresh_instance(new, next) => Hold::Stored,
_ if self.string_valued(inner) => Hold::No,
_ => Hold::Yes,
}
Expand All @@ -151,6 +158,15 @@ impl<'a> Sites<'_, 'a> {
self.watched_element(expr) || self.str_value(expr, super::roles::KEY_DEPTH).is_some()
}

fn fresh_instance(&self, new: &NewExpression<'a>, depth: u8) -> bool {
self.fresh_construction(new)
&& new.arguments.iter().all(|argument| {
argument
.as_expression()
.is_some_and(|expr| self.hold_at(expr, depth) == Hold::No)
})
}

fn call_hold(&self, call: &CallExpression<'a>, depth: u8) -> Hold {
if self.resolve.is_some_and(|map| map.contains_key(&call.span)) {
return Hold::No;
Expand Down
13 changes: 13 additions & 0 deletions crates/untwist-deobfuscate/src/calls/agnostic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ pub fn global_reach<'a>(
reach_of(&found)
}

pub fn global_observation<'a>(
program: &'a Program<'a>,
scoping: &Scoping,
machinery: &[Span],
reach: ReachWatch<'_>,
) -> Option<Span> {
let watch = settle::Watch {
printers_quiet: true,
..reach_watch(reach)
};
reach_of(&settle(program, scoping, (None, machinery, watch)).0).observed
}

pub fn global_reach_with_texts<'a>(
program: &'a Program<'a>,
scoping: &Scoping,
Expand Down
2 changes: 1 addition & 1 deletion crates/untwist-deobfuscate/src/calls/agnostic/observe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'a> Sites<'_, 'a> {
}
}

fn name_assigned(&self, name: &str) -> bool {
pub(super) fn name_assigned(&self, name: &str) -> bool {
if is_foreign_global(name) {
return true;
}
Expand Down
35 changes: 30 additions & 5 deletions crates/untwist-deobfuscate/src/calls/agnostic/reach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ use oxc_syntax::operator::BinaryOperator;
use untwist_core::semantic::{observers, Observers};
use untwist_eval::coerce::number_to_string;

const FRESH_CONSTRUCTORS: [&str; 13] = [
"Error",
"EvalError",
"RangeError",
"ReferenceError",
"SyntaxError",
"TypeError",
"URIError",
"Date",
"RegExp",
"Map",
"Set",
"WeakMap",
"WeakSet",
];

#[derive(Default)]
struct ReturnExprs<'a> {
exprs: Vec<&'a Expression<'a>>,
Expand Down Expand Up @@ -166,6 +182,16 @@ impl<'a> Sites<'_, 'a> {
&& self.fresh.bindings.is_array_function(symbol))
}

pub(super) fn fresh_construction(&self, new: &NewExpression<'a>) -> bool {
let Expression::Identifier(callee) = new.callee.without_parentheses() else {
return false;
};
let name = callee.name.as_str();
FRESH_CONSTRUCTORS.contains(&name)
&& self.fresh.symbol(callee).is_none()
&& !self.name_assigned(name)
}

fn ambient_creation(&self, expr: &Expression<'a>) -> bool {
if !self.extended.get() {
return false;
Expand Down Expand Up @@ -240,6 +266,7 @@ impl<'a> Sites<'_, 'a> {
self.identifier_non_global(id, depth)
}
Expression::CallExpression(call) => self.call_non_global(call, depth),
Expression::NewExpression(new) => self.fresh_construction(new),
Expression::StaticMemberExpression(member) => {
member.property.name == "length" && self.fresh.holds_fresh_sequence(&member.object)
}
Expand Down Expand Up @@ -306,11 +333,9 @@ impl<'a> Sites<'_, 'a> {
if name_key {
return true;
}
if self
.fresh
.literal_init(id)
.is_some_and(|init| self.is_array_result(init) || self.ambient_creation(init))
{
if self.fresh.literal_init(id).is_some_and(|init| {
self.fresh.is_fresh(init) || self.is_array_result(init) || self.ambient_creation(init)
}) {
return true;
}
let Some((symbol, func, index)) = self.resolvable_param(id) else {
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 @@ -57,6 +57,7 @@ fn comparison(test: &Expression<'_>) -> Option<Comparison> {
pub struct EscapeGuards {
pub values: Vec<Vec<BranchGuard>>,
pub dynamic: Vec<Vec<BranchGuard>>,
pub speculative: Vec<Span>,
}

struct Enclosing<'e> {
Expand Down
3 changes: 3 additions & 0 deletions crates/untwist-deobfuscate/src/decoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ fn apply_decoded_values<'a>(
if returned != request.calls.len() {
decoded.partial.push((returned, request.calls.len()));
}
if evaluated > 0 {
crate::legacy_reach::defer_escapes(&request.escape_guards.speculative);
}
inlined && returned == request.calls.len() && evaluated == returned
}

Expand Down
8 changes: 3 additions & 5 deletions crates/untwist-deobfuscate/src/decoded/layered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use super::conflict::prototype_conflict;
use super::values::reaches_intrinsics;
use crate::bridge::DecodeRequest;
use crate::calls::agnostic::{global_reach, guard_writes, ReachWatch};
use crate::calls::agnostic::{global_observation, global_reach, guard_writes, ReachWatch};
use crate::legacy_reach::{self, Layered};
use crate::proof::{alias_refusal, alias_sites, builtin_aliases, points_to_sites, UsedNames};
use crate::root_reach::root_alias_names;
Expand All @@ -21,7 +21,7 @@ use untwist_eval::object::{ProtoRead, PROTOTYPE_LINKS};

const TOKEN_PREFIX: &str = "layered:";

const MIN_LATENT_ENTRIES: usize = 2;
const MIN_LATENT_ENTRIES: usize = 1;

#[derive(Default)]
struct Frame {
Expand Down Expand Up @@ -352,8 +352,6 @@ pub fn settle_observations(program: &Program<'_>, scoping: &Scoping) {
fns: &[],
observe: true,
};
global_reach(program, scoping, (None, machinery), watch)
.observed
.is_some()
global_observation(program, scoping, machinery, watch).is_some()
});
}
Loading