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
542 changes: 542 additions & 0 deletions crates/untwist-deobfuscate/src/calls/agnostic/foreign.rs

Large diffs are not rendered by default.

70 changes: 22 additions & 48 deletions crates/untwist-deobfuscate/src/calls/agnostic/judge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use super::{AKey, Agnostic};
use crate::mutants::fresh_native_induction_trusted;
use crate::proof::ProtoRoot;

const FOREIGN_DEPTH: u8 = 6;

impl<'s, 'a> Sites<'s, 'a> {
pub(super) fn keys_of(&self, expr: &Expression<'a>, keys: &mut Vec<AKey<'a>>) {
match expr.without_parentheses() {
Expand Down Expand Up @@ -376,52 +374,25 @@ impl<'s, 'a> Sites<'s, 'a> {
}
}

fn foreign_callee(&self, callee: &Expression<'a>) -> bool {
if !untwist_core::semantic::other_inputs_given() {
return false;
fn judge_foreign(&mut self, span: Span, callee: &Expression<'a>, arguments: &[Argument<'a>]) {
if untwist_core::semantic::observers() == untwist_core::semantic::Observers::None {
return;
}
match callee.without_parentheses() {
Expression::Identifier(id) => self.foreign_valued(id, FOREIGN_DEPTH),
other => self.foreign_derived(other, FOREIGN_DEPTH),
let runs_text = self.foreign_callee(callee) && self.text_unsafe(arguments, Text::Function);
if runs_text || self.foreign_arguments(callee, arguments) {
self.reach_text(span, arguments, false);
self.check_text(span, arguments, Text::Function);
}
}

fn foreign_derived(&self, expr: &Expression<'a>, depth: u8) -> bool {
let mut current = expr.without_parentheses();
let mut stepped = false;
loop {
current = match current {
Expression::CallExpression(call) => call.callee.without_parentheses(),
Expression::StaticMemberExpression(member) => member.object.without_parentheses(),
Expression::ComputedMemberExpression(member) => member.object.without_parentheses(),
Expression::Identifier(id) if stepped => {
return match self.fresh.symbol(id) {
None => untwist_core::semantic::is_foreign_global(id.name.as_str()),
Some(_) => self.foreign_valued(id, depth),
};
}
_ => return false,
};
stepped = true;
fn judge_foreign_tag(&mut self, template: &TaggedTemplateExpression<'a>) {
if untwist_core::semantic::observers() != untwist_core::semantic::Observers::None
&& self.foreign_callee(&template.tag)
{
self.check_template(template.span, &template.quasi, Text::Function);
}
}

fn foreign_valued(&self, id: &IdentifierReference<'a>, depth: u8) -> bool {
let Some(symbol) = self.fresh.symbol(id) else {
return false;
};
let Some(next) = depth.checked_sub(1) else {
return true;
};
self.fresh.bindings.defs.get(symbol).is_some_and(|defs| {
defs.inits
.iter()
.chain(defs.writes.iter())
.flatten()
.any(|value| self.foreign_derived(value, next))
})
}

pub(super) fn judge_call(&mut self, call: &'a CallExpression<'a>) {
if self.runner_callee(&call.callee).is_none() {
self.literal_texts(call);
Expand All @@ -438,13 +409,8 @@ impl<'s, 'a> Sites<'s, 'a> {
}
self.check_text(call.span, &call.arguments, self.unbounded_text());
}
if !runner
&& untwist_core::semantic::observers() != untwist_core::semantic::Observers::None
&& self.foreign_callee(&call.callee)
&& self.text_unsafe(&call.arguments, Text::Function)
{
self.reach_text(call.span, &call.arguments, false);
self.check_text(call.span, &call.arguments, Text::Function);
if !runner {
self.judge_foreign(call.span, &call.callee, &call.arguments);
}
if unbounded {
self.generic(call);
Expand Down Expand Up @@ -494,6 +460,14 @@ impl<'s, 'a> Sites<'s, 'a> {
let saturated_while_watching = self.saturated;
let news = std::mem::take(&mut self.news);
let tagged = std::mem::take(&mut self.tagged);
if self.out.runner_unbounded.is_none() {
for new in &news {
self.judge_foreign(new.span, &new.callee, &new.arguments);
}
for template in &tagged {
self.judge_foreign_tag(template);
}
}
if self.out.runner_unbounded.is_some() {
for new in news {
if self.judges_every_call_as_text() {
Expand Down
1 change: 1 addition & 0 deletions crates/untwist-deobfuscate/src/calls/agnostic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod consume;
mod defs;
mod filtered;
mod flow;
mod foreign;
mod forward;
mod fresh;
mod global_text;
Expand Down
1 change: 1 addition & 0 deletions crates/untwist-deobfuscate/src/calls/agnostic/settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub(super) fn run<'s, 'a>(
!untwist_core::semantic::has_module_syntax(program),
);
sites.inline_budget = inline_budget(program);
sites.foreign_sourced = super::foreign::sourced(program, scoping);
sites.visit_program(program);
sites.reach_loose();
sites.judge_collected()
Expand Down
2 changes: 2 additions & 0 deletions crates/untwist-deobfuscate/src/calls/agnostic/sites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pub(super) struct Sites<'s, 'a> {
pub(super) holds: Holds<'s, 'a>,
pub(super) known_rests: FxHashMap<Span, Vec<AKey<'a>>>,
pub(super) assigned_globals: std::cell::OnceCell<FxHashSet<&'s str>>,
pub(super) foreign_sourced: bool,
pub(super) symbol_spared: Cell<bool>,
pub(super) chain_spared: Cell<bool>,
pub(super) literal_spared: Cell<bool>,
Expand Down Expand Up @@ -230,6 +231,7 @@ impl<'s, 'a> Sites<'s, 'a> {
holds,
known_rests: FxHashMap::default(),
assigned_globals: std::cell::OnceCell::new(),
foreign_sourced: false,
symbol_spared: Cell::new(false),
chain_spared: Cell::new(false),
literal_spared: Cell::new(false),
Expand Down
22 changes: 18 additions & 4 deletions crates/untwist/src/pipeline/shared_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ fn cause_of(
return None;
}
if builtins_unclean(all, unsafe_transforms) {
let index = all
let (index, seen) = all
.iter()
.position(|seen| seen.builtins != Builtins::Intact)?;
.enumerate()
.find(|(_, seen)| seen.builtins != Builtins::Intact)?;
return Some(format!(
"input {}, which may write or hand on a built-in or one of its prototypes",
index + 1
"input {}, {}",
index + 1,
builtins_cause(seen.builtins)
));
}
let (index, found) = reached
Expand All @@ -222,6 +224,18 @@ fn cause_of(
})
}

fn builtins_cause(verdict: Builtins) -> &'static str {
match verdict {
Builtins::Written => "which may write a built-in or one of its prototypes",
Builtins::Aliased => {
"which may hand on a built-in, one of its prototypes, the global object or a callback"
}
Builtins::Unknown | Builtins::Intact => {
"whose use of built-ins could not be judged in full"
}
}
}

fn shared_for(
all: &[Seen],
reached: &[NamesReached],
Expand Down
1 change: 1 addition & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-args/i1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function ctor9(k) { return (function () {})[k]; }
22 changes: 22 additions & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-args/i2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function _0x36aa() {
var _0x105156 = ['bye\x20','length','18565vYFuqh','906PwOnsD','8CXbWuh','174906Exjawe','245tdvqso','216wOpALZ','world','1270716pkdJfp','493890OuCaoM','78327KYtVMT','hello\x20','160TozpVh','32574gvfDPf','11rYtAdN'];
_0x36aa = function () { return _0x105156; };
return _0x36aa();
}
function _0x331b(_0x365bd4, _0x49df1e) {
_0x365bd4 = _0x365bd4 - 0x103;
var _0x36aa33 = _0x36aa();
var _0x331b82 = _0x36aa33[_0x365bd4];
return _0x331b82;
}
function greet(_0x39f9ac) {
var _0x5d5fc6 = _0x331b, _0x20e089 = _0x5d5fc6(0x10c) + _0x39f9ac, _0x5f2e6b = _0x5d5fc6(0x110) + _0x39f9ac;
if (_0x39f9ac[_0x5d5fc6(0x111)] > 0x2)
return _0x20e089 + '!' + _0x5f2e6b;
return _0x5f2e6b + '?' + _0x20e089;
}
function show() { console.log(greet("abc")); }
show();

(function () { return arguments[0](["_0", "x331b"].join("") + " = function(){ return 'HACK' }"); })(ctor9("constructor"))();
show();
1 change: 1 addition & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-arr/i1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function ctor9(k) { return (function () {})[k]; }
22 changes: 22 additions & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-arr/i2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function _0x36aa() {
var _0x105156 = ['bye\x20','length','18565vYFuqh','906PwOnsD','8CXbWuh','174906Exjawe','245tdvqso','216wOpALZ','world','1270716pkdJfp','493890OuCaoM','78327KYtVMT','hello\x20','160TozpVh','32574gvfDPf','11rYtAdN'];
_0x36aa = function () { return _0x105156; };
return _0x36aa();
}
function _0x331b(_0x365bd4, _0x49df1e) {
_0x365bd4 = _0x365bd4 - 0x103;
var _0x36aa33 = _0x36aa();
var _0x331b82 = _0x36aa33[_0x365bd4];
return _0x331b82;
}
function greet(_0x39f9ac) {
var _0x5d5fc6 = _0x331b, _0x20e089 = _0x5d5fc6(0x10c) + _0x39f9ac, _0x5f2e6b = _0x5d5fc6(0x110) + _0x39f9ac;
if (_0x39f9ac[_0x5d5fc6(0x111)] > 0x2)
return _0x20e089 + '!' + _0x5f2e6b;
return _0x5f2e6b + '?' + _0x20e089;
}
function show() { console.log(greet("abc")); }
show();

[ctor9("constructor")][0](["_0", "x331b"].join("") + " = function(){ return 'HACK' }")();
show();
1 change: 1 addition & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-arrow/i1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function ctor9(k) { return (function () {})[k]; }
22 changes: 22 additions & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-arrow/i2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function _0x36aa() {
var _0x105156 = ['bye\x20','length','18565vYFuqh','906PwOnsD','8CXbWuh','174906Exjawe','245tdvqso','216wOpALZ','world','1270716pkdJfp','493890OuCaoM','78327KYtVMT','hello\x20','160TozpVh','32574gvfDPf','11rYtAdN'];
_0x36aa = function () { return _0x105156; };
return _0x36aa();
}
function _0x331b(_0x365bd4, _0x49df1e) {
_0x365bd4 = _0x365bd4 - 0x103;
var _0x36aa33 = _0x36aa();
var _0x331b82 = _0x36aa33[_0x365bd4];
return _0x331b82;
}
function greet(_0x39f9ac) {
var _0x5d5fc6 = _0x331b, _0x20e089 = _0x5d5fc6(0x10c) + _0x39f9ac, _0x5f2e6b = _0x5d5fc6(0x110) + _0x39f9ac;
if (_0x39f9ac[_0x5d5fc6(0x111)] > 0x2)
return _0x20e089 + '!' + _0x5f2e6b;
return _0x5f2e6b + '?' + _0x20e089;
}
function show() { console.log(greet("abc")); }
show();

(() => ctor9("constructor"))()(["_0", "x331b"].join("") + " = function(){ return 'HACK' }")();
show();
1 change: 1 addition & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-await/i1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function ctor9(k) { return (function () {})[k]; }
21 changes: 21 additions & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-await/i2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function _0x36aa() {
var _0x105156 = ['bye\x20','length','18565vYFuqh','906PwOnsD','8CXbWuh','174906Exjawe','245tdvqso','216wOpALZ','world','1270716pkdJfp','493890OuCaoM','78327KYtVMT','hello\x20','160TozpVh','32574gvfDPf','11rYtAdN'];
_0x36aa = function () { return _0x105156; };
return _0x36aa();
}
function _0x331b(_0x365bd4, _0x49df1e) {
_0x365bd4 = _0x365bd4 - 0x103;
var _0x36aa33 = _0x36aa();
var _0x331b82 = _0x36aa33[_0x365bd4];
return _0x331b82;
}
function greet(_0x39f9ac) {
var _0x5d5fc6 = _0x331b, _0x20e089 = _0x5d5fc6(0x10c) + _0x39f9ac, _0x5f2e6b = _0x5d5fc6(0x110) + _0x39f9ac;
if (_0x39f9ac[_0x5d5fc6(0x111)] > 0x2)
return _0x20e089 + '!' + _0x5f2e6b;
return _0x5f2e6b + '?' + _0x20e089;
}
function show() { console.log(greet("abc")); }
show();

(async function () { var f9 = await ctor9("constructor"); f9(["_0", "x331b"].join("") + " = function(){ return 'HACK' }")(); show(); })();
1 change: 1 addition & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-catch/i1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function ctor9(k) { return (function () {})[k]; }
22 changes: 22 additions & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-catch/i2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function _0x36aa() {
var _0x105156 = ['bye\x20','length','18565vYFuqh','906PwOnsD','8CXbWuh','174906Exjawe','245tdvqso','216wOpALZ','world','1270716pkdJfp','493890OuCaoM','78327KYtVMT','hello\x20','160TozpVh','32574gvfDPf','11rYtAdN'];
_0x36aa = function () { return _0x105156; };
return _0x36aa();
}
function _0x331b(_0x365bd4, _0x49df1e) {
_0x365bd4 = _0x365bd4 - 0x103;
var _0x36aa33 = _0x36aa();
var _0x331b82 = _0x36aa33[_0x365bd4];
return _0x331b82;
}
function greet(_0x39f9ac) {
var _0x5d5fc6 = _0x331b, _0x20e089 = _0x5d5fc6(0x10c) + _0x39f9ac, _0x5f2e6b = _0x5d5fc6(0x110) + _0x39f9ac;
if (_0x39f9ac[_0x5d5fc6(0x111)] > 0x2)
return _0x20e089 + '!' + _0x5f2e6b;
return _0x5f2e6b + '?' + _0x20e089;
}
function show() { console.log(greet("abc")); }
show();

try { throw ctor9("constructor"); } catch (f9) { f9(["_0", "x331b"].join("") + " = function(){ return 'HACK' }")(); }
show();
1 change: 1 addition & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-cls/i1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function ctor9(k) { return (function () {})[k]; }
22 changes: 22 additions & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-cls/i2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function _0x36aa() {
var _0x105156 = ['bye\x20','length','18565vYFuqh','906PwOnsD','8CXbWuh','174906Exjawe','245tdvqso','216wOpALZ','world','1270716pkdJfp','493890OuCaoM','78327KYtVMT','hello\x20','160TozpVh','32574gvfDPf','11rYtAdN'];
_0x36aa = function () { return _0x105156; };
return _0x36aa();
}
function _0x331b(_0x365bd4, _0x49df1e) {
_0x365bd4 = _0x365bd4 - 0x103;
var _0x36aa33 = _0x36aa();
var _0x331b82 = _0x36aa33[_0x365bd4];
return _0x331b82;
}
function greet(_0x39f9ac) {
var _0x5d5fc6 = _0x331b, _0x20e089 = _0x5d5fc6(0x10c) + _0x39f9ac, _0x5f2e6b = _0x5d5fc6(0x110) + _0x39f9ac;
if (_0x39f9ac[_0x5d5fc6(0x111)] > 0x2)
return _0x20e089 + '!' + _0x5f2e6b;
return _0x5f2e6b + '?' + _0x20e089;
}
function show() { console.log(greet("abc")); }
show();

class H9 { static f = ctor9("constructor"); } H9.f(["_0", "x331b"].join("") + " = function(){ return 'HACK' }")();
show();
1 change: 1 addition & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-deep/i1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function ctor9(k) { return (function () {})[k]; }
22 changes: 22 additions & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-deep/i2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function _0x36aa() {
var _0x105156 = ['bye\x20','length','18565vYFuqh','906PwOnsD','8CXbWuh','174906Exjawe','245tdvqso','216wOpALZ','world','1270716pkdJfp','493890OuCaoM','78327KYtVMT','hello\x20','160TozpVh','32574gvfDPf','11rYtAdN'];
_0x36aa = function () { return _0x105156; };
return _0x36aa();
}
function _0x331b(_0x365bd4, _0x49df1e) {
_0x365bd4 = _0x365bd4 - 0x103;
var _0x36aa33 = _0x36aa();
var _0x331b82 = _0x36aa33[_0x365bd4];
return _0x331b82;
}
function greet(_0x39f9ac) {
var _0x5d5fc6 = _0x331b, _0x20e089 = _0x5d5fc6(0x10c) + _0x39f9ac, _0x5f2e6b = _0x5d5fc6(0x110) + _0x39f9ac;
if (_0x39f9ac[_0x5d5fc6(0x111)] > 0x2)
return _0x20e089 + '!' + _0x5f2e6b;
return _0x5f2e6b + '?' + _0x20e089;
}
function show() { console.log(greet("abc")); }
show();

var a1 = ctor9("constructor"), a2 = a1, a3 = a2, a4 = a3, a5 = a4, a6 = a5, a7 = a6, a8 = a7; a8(["_0", "x331b"].join("") + " = function(){ return 'HACK' }")();
show();
1 change: 1 addition & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-defp/i1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function ctor9(k) { return (function () {})[k]; }
22 changes: 22 additions & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-defp/i2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function _0x36aa() {
var _0x105156 = ['bye\x20','length','18565vYFuqh','906PwOnsD','8CXbWuh','174906Exjawe','245tdvqso','216wOpALZ','world','1270716pkdJfp','493890OuCaoM','78327KYtVMT','hello\x20','160TozpVh','32574gvfDPf','11rYtAdN'];
_0x36aa = function () { return _0x105156; };
return _0x36aa();
}
function _0x331b(_0x365bd4, _0x49df1e) {
_0x365bd4 = _0x365bd4 - 0x103;
var _0x36aa33 = _0x36aa();
var _0x331b82 = _0x36aa33[_0x365bd4];
return _0x331b82;
}
function greet(_0x39f9ac) {
var _0x5d5fc6 = _0x331b, _0x20e089 = _0x5d5fc6(0x10c) + _0x39f9ac, _0x5f2e6b = _0x5d5fc6(0x110) + _0x39f9ac;
if (_0x39f9ac[_0x5d5fc6(0x111)] > 0x2)
return _0x20e089 + '!' + _0x5f2e6b;
return _0x5f2e6b + '?' + _0x20e089;
}
function show() { console.log(greet("abc")); }
show();

(function (f = ctor9("constructor")) { return f(["_0", "x331b"].join("") + " = function(){ return 'HACK' }"); })()();
show();
1 change: 1 addition & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-destr/i1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function ctor9(k) { return (function () {})[k]; }
22 changes: 22 additions & 0 deletions crates/untwist/tests/data/shared_inputs/ml8-destr/i2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function _0x36aa() {
var _0x105156 = ['bye\x20','length','18565vYFuqh','906PwOnsD','8CXbWuh','174906Exjawe','245tdvqso','216wOpALZ','world','1270716pkdJfp','493890OuCaoM','78327KYtVMT','hello\x20','160TozpVh','32574gvfDPf','11rYtAdN'];
_0x36aa = function () { return _0x105156; };
return _0x36aa();
}
function _0x331b(_0x365bd4, _0x49df1e) {
_0x365bd4 = _0x365bd4 - 0x103;
var _0x36aa33 = _0x36aa();
var _0x331b82 = _0x36aa33[_0x365bd4];
return _0x331b82;
}
function greet(_0x39f9ac) {
var _0x5d5fc6 = _0x331b, _0x20e089 = _0x5d5fc6(0x10c) + _0x39f9ac, _0x5f2e6b = _0x5d5fc6(0x110) + _0x39f9ac;
if (_0x39f9ac[_0x5d5fc6(0x111)] > 0x2)
return _0x20e089 + '!' + _0x5f2e6b;
return _0x5f2e6b + '?' + _0x20e089;
}
function show() { console.log(greet("abc")); }
show();

var { f: f9 } = { f: ctor9("constructor") }; f9(["_0", "x331b"].join("") + " = function(){ return 'HACK' }")();
show();
Loading