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
5 changes: 4 additions & 1 deletion crates/untwist-deobfuscate/src/string_array/cookie_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use untwist_core::pass::Cx;
use super::array_declaration;
use super::checksum::parsed_statement;
use super::cookie_minified::countdown_declaration;
use super::inlined::used_only_in;
use super::inlined::{copied_out_of_block, used_only_in};
use super::legacy::is_countdown;
use crate::stamp::declaration_symbol;

Expand Down Expand Up @@ -367,6 +367,9 @@ fn rotator_of(
let Statement::FunctionDeclaration(function) = &body[at] else {
return None;
};
if copied_out_of_block(function, scoping) {
return None;
}
countdown_declaration(&body[at], alias_name)?;
let id = function.id.as_ref()?;
Some(Rotator {
Expand Down
63 changes: 51 additions & 12 deletions crates/untwist-deobfuscate/src/string_array/inlined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,45 @@ fn owned_function(
let Statement::FunctionDeclaration(function) = stmt else {
return false;
};
function
.id
.as_ref()
.and_then(|id| id.symbol_id.get())
.is_some_and(|symbol| used_only_in(scoping, symbol, moved.iter().map(|i| &body[*i])))
!copied_out_of_block(function, scoping)
&& function
.id
.as_ref()
.and_then(|id| id.symbol_id.get())
.is_some_and(|symbol| used_only_in(scoping, symbol, moved.iter().map(|i| &body[*i])))
}

pub(super) fn copied_out_of_block(function: &Function<'_>, scoping: &Scoping) -> bool {
let Some(id) = function.id.as_ref() else {
return true;
};
let Some(block) = function
.scope_id
.get()
.and_then(|scope| scoping.scope_parent_id(scope))
else {
return true;
};
let flags = scoping.scope_flags(block);
if flags.is_var() || flags.is_strict_mode() {
return false;
}
let own = id.symbol_id.get();
let name = id.name;
let bound_outside = scoping.scope_ancestors(block).skip(1).any(|scope| {
scoping
.get_binding(scope, name)
.is_some_and(|symbol| Some(symbol) != own)
});
let global = scoping
.scope_ancestors(block)
.find(|scope| scoping.scope_flags(*scope).is_var())
.is_none_or(|scope| scope == scoping.root_scope_id());
bound_outside
|| global
|| scoping
.root_unresolved_references()
.contains_key(name.as_str())
}

fn rewrite_called<'a>(it: &mut AVec<'a, Statement<'a>>, plan: &Plan, cx: &Cx<'a, '_>) -> bool {
Expand All @@ -554,13 +588,18 @@ fn rewrite_called<'a>(it: &mut AVec<'a, Statement<'a>>, plan: &Plan, cx: &Cx<'a,
true
}

fn block_declaration(stmt: &Statement<'_>) -> bool {
fn block_declaration(stmt: &Statement<'_>, scoping: &Scoping) -> bool {
match stmt {
Statement::FunctionDeclaration(_) => true,
Statement::VariableDeclaration(declaration) => declaration
.declarations
.iter()
.all(|declarator| matches!(declarator.id, BindingPattern::BindingIdentifier(_))),
Statement::FunctionDeclaration(function) => !copied_out_of_block(function, scoping),
Statement::VariableDeclaration(declaration) => {
!matches!(
declaration.kind,
VariableDeclarationKind::Using | VariableDeclarationKind::AwaitUsing
) && declaration
.declarations
.iter()
.all(|declarator| matches!(declarator.id, BindingPattern::BindingIdentifier(_)))
}
_ => false,
}
}
Expand Down Expand Up @@ -624,7 +663,7 @@ fn block_rotator(stmt: &Statement<'_>, scoping: &Scoping) -> bool {
if before.is_empty()
|| !is_infinite_rotator_loop(rotator)
|| !closed_loop(rotator, scoping)
|| !before.iter().all(block_declaration)
|| !before.iter().all(|stmt| block_declaration(stmt, scoping))
|| !before.iter().all(closed_initialisers)
|| reads_own_frame(stmt)
|| stmt.span().start >= SYNTHETIC_BASE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function outer(arr) { var h = 1; { var al = arr(); function h() { return 2; } while (true) { try { var n = parseInt(al[0]); if (n === 3) break; al.push(al.shift()); } catch (e) { al.push(al.shift()); } } } return typeof h; }
console.log(outer(function () { return ["1", "2", "3"]; }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function outer(arr) { var h = 1; var g = function () { return typeof h; }; { function h() { return 2; } var al = arr(); for (;;) { try { if (al[0] === "c") break; al.push(al.shift()); } catch (e) { al.push(al.shift()); } } } return g(); }
console.log(outer(function () { return ["a", "b", "c"]; }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var outer = function (arr) { var h = 1; { function h() { return 2; } const al = arr(); for (;;) { try { if (al[0] === "c") break; al.push(al.shift()); } catch (e) { al.push(al.shift()); } } } return typeof h; };
console.log(outer(function () { return ["a", "b", "c"]; }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function outer(arr) { for (var h = 0; h < 1; h++) { } { function h() { return 2; } var al = arr(); for (;;) { try { if (al[0] === "c") break; al.push(al.shift()); } catch (e) { al.push(al.shift()); } } } return typeof h; }
console.log(outer(function () { return ["a", "b", "c"]; }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function outer(arr) { function h() { return 1; } { function h() { return 2; } var al = arr(); for (;;) { try { if (al[0] === "c") break; al.push(al.shift()); } catch (e) { al.push(al.shift()); } } } return h(); }
console.log(outer(function () { return ["a", "b", "c"]; }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function outer(arr) { var h = 1; { let al = arr(); function h() { return 2; } for (;;) { try { if (al[0] === "c") break; al.push(al.shift()); } catch (e) { al.push(al.shift()); } } } return typeof h; }
console.log(outer(function () { return ["a", "b", "c"]; }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var arr = function () { return ["a", "b", "c"]; }; var h9 = 1;
{ function h9() { return 2; } let al = arr(); for (;;) { try { if (al[0] === "c") break; al.push(al.shift()); } catch (e) { al.push(al.shift()); } } }
console.log(typeof h9);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function outer(arr) { var h = 1; { function h() { return 2; } var al = arr(); for (;;) { try { if (al[0] === "c") break; al.push(al.shift()); } catch (e) { al.push(al.shift()); } } } return typeof h; }
console.log(outer(function () { return ["a", "b", "c"]; }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
async function outer(arr) { { await using r = { [Symbol.asyncDispose]: async function () { console.log("disposed"); } }; var al = arr(); for (;;) { try { if (al[0] === "c") break; al.push(al.shift()); } catch (e) { al.push(al.shift()); } } } return "done"; }
outer(function () { return ["a", "b", "c"]; }).then(console.log);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function outer(arr) { { using r = { [Symbol.dispose]: function () { console.log("disposed"); } }; var al = arr(); for (;;) { try { if (al[0] === "c") break; al.push(al.shift()); } catch (e) { al.push(al.shift()); } } } console.log("after"); return "done"; }
console.log(outer(function () { return ["a", "b", "c"]; }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function outer() {
var h = 1;
{
function arr() { var a = ["a", "b", "c"]; arr = function () { return a; }; return arr(); }
var al = arr();
function h() { return 2; }
for (;;) { try { if (al[0] === "c") break; al.push(al.shift()); } catch (e) { al.push(al.shift()); } }
console.log(arr()[0]);
}
return typeof h;
}
console.log(outer());
31 changes: 31 additions & 0 deletions crates/untwist/tests/obfuscator_sweep_rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,9 +741,24 @@ const BLOCK_ROTATOR_ESCAPES: &[&str] = &[
"block-rotator-leading-await",
"block-rotator-leading-yield",
"block-rotator-leading-super",
"block-rotator-annexb-var",
"block-rotator-annexb-function",
"block-rotator-annexb-top-level",
"block-rotator-annexb-second",
"block-rotator-annexb-closure",
"block-rotator-annexb-for-var",
"block-rotator-annexb-expression",
"block-rotator-annexb-checksum",
"block-rotator-closed",
];

const CALLED_ROTATOR_ANNEX_B: &[&str] = &["called-rotator-annexb"];

const BLOCK_ROTATOR_USING: &[&str] = &[
"block-rotator-leading-using",
"block-rotator-leading-await-using",
];

#[test]
fn a_block_rotator_whose_loop_or_declarations_reach_outside_the_block_prints_like_node_in_both_lanes(
) {
Expand All @@ -764,6 +779,22 @@ fn a_block_rotator_whose_loop_or_declarations_reach_outside_the_block_is_not_wra
}
}

#[test]
fn a_called_rotator_beside_a_block_function_copied_to_an_outer_binding_prints_like_node_in_both_lanes(
) {
prints_like_node_in(CALLED_ROTATOR_ANNEX_B, &[default_code, unsafe_lane]);
}

#[test]
fn a_block_rotator_led_by_a_using_declaration_is_not_wrapped_in_both_lanes() {
for label in BLOCK_ROTATOR_USING {
let source = read(&format!("{label}.js"));
for output in [default_code(&source), unsafe_lane(&source)] {
assert!(!output.contains("})();"), "{label}:\n{output}");
}
}
}

#[test]
fn a_closed_block_rotator_is_still_wrapped_in_both_lanes() {
let source = read("block-rotator-closed.js");
Expand Down