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
61 changes: 59 additions & 2 deletions crates/untwist-deobfuscate/src/control_flow_object/declared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use untwist_core::ast_util::search::expressions_at;
use untwist_core::pass::{Cx, NoteKind, RepeatedSites};
use untwist_core::semantic::is_constant_binding;

use crate::branch_reach::in_branches;
use crate::init_order::{
reads_are_initialised, refuse_read_before_declaration, scan_init_order, InitOrder,
};
Expand Down Expand Up @@ -298,7 +299,12 @@ fn substitutable_uses<'u>(
let branches = located.branches(cx.scoping());
let dead = dead_branch_parts(branches, candidate.symbol, &props, &*cx);
if dead.is_empty() {
return None;
return speculated_mutations(located.program, candidate, uses, cx).then_some(
Substitutable {
uses,
dead: Vec::new(),
},
);
}
let only_dead = !uses.reads.is_empty()
&& mutations_only_in_dead_parts(
Expand All @@ -308,7 +314,7 @@ fn substitutable_uses<'u>(
&mutation_spans(uses),
cx.scoping(),
);
if !only_dead {
if !only_dead && !speculated_mutations(located.program, candidate, uses, cx) {
cx.note(notes::CONTROL_FLOW_LIVE_MUTATION, Some(candidate.statement));
return None;
}
Expand All @@ -320,6 +326,57 @@ fn substitutable_uses<'u>(
Some(Substitutable { uses, dead })
}

fn speculated_mutations(
program: &Program<'_>,
candidate: &DeclaredObject,
uses: &ObjectUses,
cx: &Cx<'_, '_>,
) -> bool {
if !cx.opts.unsafe_transforms
|| !crate::legacy_reach::speculates_escapes()
|| uses.reads.is_empty()
{
return false;
}
if !passes_decoders_to_members(program) {
return false;
}
let mutations = mutation_spans(uses);
let spans: FxHashSet<Span> = mutations.iter().copied().collect();
if spans.is_empty() || in_branches(program, cx.scoping(), &spans).len() != spans.len() {
return false;
}
let mut deferred = vec![cx.scoping().symbol_span(candidate.symbol)];
deferred.extend(mutations);
crate::legacy_reach::defer_escapes(&deferred);
true
}

#[derive(Default)]
struct DecoderArguments {
found: bool,
}

impl<'a> VisitJs<'a> for DecoderArguments {
fn visit_call_expression(&mut self, it: &CallExpression<'a>) {
if self.found {
return;
}
self.found = it.callee.without_parentheses().is_member_expression()
&& it.arguments.iter().any(|argument| {
matches!(argument, Argument::Identifier(id)
if untwist_core::machinery_name::decoder_index(id.name.as_str()).is_some())
});
walk_js::walk_call_expression(self, it);
}
}

fn passes_decoders_to_members(program: &Program<'_>) -> bool {
let mut search = DecoderArguments::default();
search.visit_program(program);
search.found
}

fn is_redeclared(scoping: &Scoping, symbol: SymbolId) -> bool {
!scoping.symbol_redeclarations(symbol).is_empty()
}
Expand Down
1 change: 1 addition & 0 deletions crates/untwist-deobfuscate/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,7 @@ fn deobfuscate_with<'a>(
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 {
Expand Down
31 changes: 31 additions & 0 deletions crates/untwist-deobfuscate/src/legacy_reach.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::cell::RefCell;

use oxc_ast::ast::{BindingIdentifier, CallExpression, IdentifierReference, Program};
use oxc_ast_visit::VisitJs;
use oxc_span::Span;

use crate::mutants::deferred_legacy_reach_trusted;
Expand Down Expand Up @@ -97,6 +99,35 @@ pub(crate) fn defer_escapes(decoders: &[Span]) {
DEFERRAL.with(|deferral| deferral.borrow_mut().escapes.extend_from_slice(decoders));
}

struct Surviving<'s> {
spans: &'s [Span],
found: bool,
}

impl<'a> VisitJs<'a> for Surviving<'_> {
fn visit_identifier_reference(&mut self, it: &IdentifierReference<'a>) {
self.found |= self.spans.contains(&it.span);
}

fn visit_binding_identifier(&mut self, it: &BindingIdentifier<'a>) {
self.found |= self.spans.contains(&it.span);
}

fn visit_call_expression(&mut self, it: &CallExpression<'a>) {
self.found |= self.spans.contains(&it.span);
oxc_ast_visit::walk_js::walk_call_expression(self, it);
}
}

pub(crate) fn identifiers_survive(program: &Program<'_>, spans: &[Span]) -> bool {
let mut surviving = Surviving {
spans,
found: false,
};
surviving.visit_program(program);
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) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}

function compose(first, second) {
return function (value) {
return second(first(value));
};
}

function makeCounter(start) {
var count = start;
return {
next: function () {
count += 1;
return count;
},
reset: function () {
count = start;
return count;
},
};
}

const double = (x) => 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]));
137 changes: 137 additions & 0 deletions crates/untwist/tests/data/obfuscator_sweep/cfo-live-escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
function l(a) {
if (a <= 1) {
return 1;
} else {
return a * l(a - 1);
}
}
function m(a, b) {
const f = {
q: function (i, j) {
return i(j);
},
o: function (i, j) {
return i !== j;
},
k: 396
};
return function (d) {
if (f.o(d, 20)) {
return f.q(b, a(d));
} else {
return f.q(String, f.k) + [f].length + d;
}
};
}
function n() {
const a = ["sCkmBq", "W57dMZzE", "CKFdJSkSWPy", "W6hdGmkWymkudq", "krpcNa", "imk7W5PQ", "hfFcR8ot", "WORdLMu", "FSkala", "ev3cOSojWQBdPLK", "kmoKW4tdMG", "auFcUG", "DSoRW65Exa", "dvldOLxcQa", "WR1MW7G", "WRDMhCokbq"];
n = function () {
return a;
};
return n();
}
function o(a) {
let b = a;
return {
next: function () {
b += 1;
return b;
},
reset: function () {
if (s(398, "8Xtm") !== "oLSdd") {
r = a;
return s;
} else {
b = a;
return b;
}
}
};
}
const p = (a) => a * 2;
const q = (a) => a + 1;
function r(y, z) {
return s(y - 540, z);
}
function s(b, d) {
b = b - 396;
const e = n();
let f = e[b];
if (s.g === undefined) {
var y = function (G) {
const H = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=";
let I = "";
let J = "";
for (let K = 0, M, O, P = 0; O = G.charAt(P++); ~O && (M = K % 4 ? M * 64 + O : O, K++ % 4) ? I += String.fromCharCode(M >> (K * -2 & 6) & 255) : 0) {
O = H.indexOf(O);
}
for (let Q = 0, R = I.length; Q < R; Q++) {
J += "%" + ("00" + I.charCodeAt(Q).toString(16)).slice(-2);
}
return decodeURIComponent(J);
};
const C = function (G, H) {
const I = [];
let J = 0;
let K;
let M = "";
G = y(G);
let N;
for (N = 0; N < 256; N++) {
I[N] = N;
}
for (N = 0; N < 256; N++) {
J = (J + I[N] + H.charCodeAt(N % H.length)) % 256;
K = I[N];
I[N] = I[J];
I[J] = K;
}
N = 0;
J = 0;
for (let O = 0; O < G.length; O++) {
N = (N + 1) % 256;
J = (J + I[N]) % 256;
K = I[N];
I[N] = I[J];
I[J] = K;
M += String.fromCharCode(G.charCodeAt(O) ^ I[(I[N] + I[J]) % 256]);
}
return M;
};
s.h = C;
s.i = {};
s.g = true;
}
const z = e[0];
if (s.j !== z) {
s.i = {};
s.j = z;
}
const A = s.i[b];
if (A === undefined) {
if (s.k === undefined) {
s.k = true;
}
f = s.h(f, d);
s.i[b] = f;
} else {
f = A;
}
return f;
}
const t = m(p, q);
function u(...a) {
return a[s(399, "tGUI")]((d, e) => d + e, 0);
}
function v(a = "hi", ...b) {
return b[s(400, "*K8E")]((e) => a + " " + e).join("; ");
}
const w = o(10);
w[r(941, "9rMg")]();
w[r(942, "Thtg")]();
console[r(943, "7[SN")]("factorial", l(7));
console.log("pipeline", t(20));
console[r(944, "0BiL")](r(945, "Thtg"), w[r(946, "&cNR")](), w.reset());
console.log(r(947, "Thtg"), u(4, 8, 15, 16, 23, 42));
console.log(r(948, "tfR)"), v(undefined, r(949, "si0]"), "south"));
console[r(950, "WQxH")](r(951, "4*4b"), Math.max.apply(null, [3, 9, 1]));
9 changes: 9 additions & 0 deletions crates/untwist/tests/obfuscator_sweep_rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const DECODED_UNDER_UNSAFE: &[&str] = &[
"wrapper-in-inner-decoder",
"strict-block-wrapper",
"block-wrapper-dead-write",
"cfo-escape-in-branch",
];

const FINGERPRINTS: &[&str] = &[
Expand Down Expand Up @@ -122,6 +123,14 @@ fn dead_code_and_control_flow_cases_print_the_source_output_under_unsafe() {
}
}

#[test]
fn a_control_flow_object_that_escapes_in_a_live_branch_prints_like_node_under_unsafe() {
let source = read("cfo-live-escape.js");
let output = unsafe_lane(&source);
assert!(output.contains("[f].length"), "{output}");
prints_the_same(&source, &output);
}

#[test]
fn a_self_defending_block_a_later_layer_put_in_a_control_flow_function_is_removed() {
let output = unsafe_lane(&read("sd-controller-survives.js"));
Expand Down
2 changes: 1 addition & 1 deletion web/scripts/size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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_910_000;
const WASM_BASELINE_BYTES = 14_926_000;
const WASM_GROWTH = 0.1;

const gzip = (bytes: Buffer): number => gzipSync(bytes, { level: 9 }).length;
Expand Down