Compare commits
7 commits
550f62e473
...
aac7571bbc
| Author | SHA1 | Date | |
|---|---|---|---|
| aac7571bbc | |||
| 1ddc4afbfc | |||
| a4450b69ba | |||
| 6dd9c220d4 | |||
| d19e4f784d | |||
| 59637e50dc | |||
| 2b19ed6096 |
37 changed files with 2349 additions and 117 deletions
71
Cargo.lock
generated
71
Cargo.lock
generated
|
|
@ -309,7 +309,6 @@ dependencies = [
|
|||
"noxis-classfile",
|
||||
"noxis-core",
|
||||
"noxis-structure-check",
|
||||
"regex",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
"tracing",
|
||||
|
|
@ -319,7 +318,6 @@ dependencies = [
|
|||
name = "noxis-classfile"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"noxis-structure-check",
|
||||
"ristretto_classfile",
|
||||
"thiserror",
|
||||
|
|
@ -333,7 +331,6 @@ dependencies = [
|
|||
"noxis-structure-check",
|
||||
"serde",
|
||||
"thiserror",
|
||||
"toml",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
|
|
@ -383,8 +380,8 @@ dependencies = [
|
|||
"noxis-classfile",
|
||||
"noxis-core",
|
||||
"noxis-jar",
|
||||
"noxis-passes",
|
||||
"noxis-structure-check",
|
||||
"regex",
|
||||
"serde",
|
||||
"thiserror",
|
||||
]
|
||||
|
|
@ -440,18 +437,6 @@ version = "5.3.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.13.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.18"
|
||||
|
|
@ -533,15 +518,6 @@ dependencies = [
|
|||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_spanned"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e7523beb55eece201a2356bee0bbca0d1ab466c14c07703b2e0ee6d42cb0c2c"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.7"
|
||||
|
|
@ -620,45 +596,6 @@ dependencies = [
|
|||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "1.1.7+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7874cd34dd99040fc24e30923aa64a60a81ff3ddfe2b8129ccc84ba24949deed"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"serde_core",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"toml_parser",
|
||||
"toml_writer",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "1.1.2+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b86d767906c6c42421dcba507eb9d203e779497710a47782a224bb871653053"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_parser"
|
||||
version = "1.1.4+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bed810ee007d8e7f9cf20b4774e7198142fa27186e0f805871da7f3c39eaf333"
|
||||
dependencies = [
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_writer"
|
||||
version = "1.1.3+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06bdbd8cfc056b8d2e2e85f29b56a3bdbecb527cef81eb39e3e7b98af4652770"
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.44"
|
||||
|
|
@ -819,12 +756,6 @@ dependencies = [
|
|||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81"
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen"
|
||||
version = "0.57.1"
|
||||
|
|
|
|||
|
|
@ -20,12 +20,9 @@ noxis-verify = { path = "crates/noxis-verify" }
|
|||
|
||||
anyhow = "1"
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
indexmap = "2"
|
||||
regex = "1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
thiserror = "2"
|
||||
toml = "1"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
zip = { version = "8", default-features = false, features = ["deflate"] }
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ rust-version.workspace = true
|
|||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
regex = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
noxis-classfile = { workspace = true }
|
||||
noxis-core = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@
|
|||
use std::collections::BTreeSet;
|
||||
|
||||
use noxis_classfile::Program;
|
||||
use regex::Regex;
|
||||
|
||||
use crate::minirx::{self, Regex};
|
||||
|
||||
use crate::entrypoints::Entrypoints;
|
||||
use noxis_core::Config;
|
||||
|
|
@ -14,7 +15,7 @@ mod scans;
|
|||
pub use dispatch::external_dispatch_families;
|
||||
|
||||
/// Compiled glob: `*` - anything but `/`, `**` - anything, `?` - one char.
|
||||
fn compile(pattern: &str) -> Result<Regex, regex::Error> {
|
||||
fn compile(pattern: &str) -> Result<Regex, minirx::Error> {
|
||||
let mut out = String::from("^");
|
||||
let chars: Vec<char> = pattern.chars().collect();
|
||||
let mut i = 0;
|
||||
|
|
@ -34,7 +35,7 @@ fn compile(pattern: &str) -> Result<Regex, regex::Error> {
|
|||
i += 1;
|
||||
}
|
||||
c => {
|
||||
out.push_str(®ex::escape(&c.to_string()));
|
||||
out.push_str(&minirx::escape(&c.to_string()));
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ pub mod entrypoints;
|
|||
pub mod hierarchy;
|
||||
pub mod jdk;
|
||||
pub mod keep;
|
||||
pub mod minirx;
|
||||
|
||||
pub use entrypoints::Entrypoints;
|
||||
pub use hierarchy::{ClassHierarchy, RenameSafety, method_rename_safety};
|
||||
|
|
|
|||
82
crates/noxis-analysis/src/minirx/matcher.rs
Normal file
82
crates/noxis-analysis/src/minirx/matcher.rs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
//! Backtracking matcher over a flat `Node` sequence.
|
||||
|
||||
use super::parse::Node;
|
||||
|
||||
/// True if `nodes` matches anywhere in `text` (unanchored search), honoring
|
||||
/// any `Start`/`End` nodes as zero-width anchors at their actual position.
|
||||
pub(super) fn search(nodes: &[Node], text: &[char]) -> bool {
|
||||
// `^` only makes sense anchored at position 0: if the pattern starts
|
||||
// with it, trying every start offset is still correct (it will simply
|
||||
// fail at every offset except possibly 0) and keeps this function
|
||||
// simple - no need to special-case it.
|
||||
for start in 0..=text.len() {
|
||||
if match_here(nodes, 0, text, start).is_some() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Tries to match `nodes[idx..]` starting at `text[pos..]`; returns the end
|
||||
/// position on success.
|
||||
fn match_here(nodes: &[Node], idx: usize, text: &[char], pos: usize) -> Option<usize> {
|
||||
let Some(node) = nodes.get(idx) else {
|
||||
return Some(pos);
|
||||
};
|
||||
match node {
|
||||
Node::Start => {
|
||||
if pos != 0 {
|
||||
return None;
|
||||
}
|
||||
match_here(nodes, idx + 1, text, pos)
|
||||
}
|
||||
Node::End => {
|
||||
if pos != text.len() {
|
||||
return None;
|
||||
}
|
||||
match_here(nodes, idx + 1, text, pos)
|
||||
}
|
||||
Node::WordBoundary => {
|
||||
if !is_word_boundary(text, pos) {
|
||||
return None;
|
||||
}
|
||||
match_here(nodes, idx + 1, text, pos)
|
||||
}
|
||||
Node::Rep { atom, min, max } => {
|
||||
let run = count_run(atom, text, pos);
|
||||
let hi = max.map_or(run, |m| m.min(run));
|
||||
if hi < *min {
|
||||
return None;
|
||||
}
|
||||
// Greedy: try the longest repetition first, backtrack shorter.
|
||||
for count in (*min..=hi).rev() {
|
||||
if let Some(end) = match_here(nodes, idx + 1, text, pos + count) {
|
||||
return Some(end);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Counts how many consecutive chars from `pos` satisfy `atom`.
|
||||
fn count_run(atom: &super::parse::Atom, text: &[char], pos: usize) -> usize {
|
||||
let mut n = 0;
|
||||
while text.get(pos + n).is_some_and(|&c| atom.matches(c)) {
|
||||
n += 1;
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
/// A JVM/identifier-ish "word" char: alphanumeric or `_`.
|
||||
fn is_word_char(c: char) -> bool {
|
||||
c.is_alphanumeric() || c == '_'
|
||||
}
|
||||
|
||||
fn is_word_boundary(text: &[char], pos: usize) -> bool {
|
||||
let before = pos.checked_sub(1).and_then(|i| text.get(i)).copied();
|
||||
let after = text.get(pos).copied();
|
||||
let before_word = before.is_some_and(is_word_char);
|
||||
let after_word = after.is_some_and(is_word_char);
|
||||
before_word != after_word
|
||||
}
|
||||
135
crates/noxis-analysis/src/minirx/mod.rs
Normal file
135
crates/noxis-analysis/src/minirx/mod.rs
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
//! Minimal std-only regex engine, covering exactly the pattern vocabulary
|
||||
//! used in this codebase (keep-rule globs compiled to regex, plus the
|
||||
//! fixed `RED_PATTERNS` secret-scan list in `noxis-verify`).
|
||||
//!
|
||||
//! Not a general-purpose regex engine: no groups, no alternation, no
|
||||
//! backreferences, no `{n,m}` quantifiers. What it does support:
|
||||
//! literal chars, `.`, character classes (`[...]`, `[^...]`, ranges),
|
||||
//! `\s`, `\b` (word boundary), anchors `^`/`$`, quantifiers `*`/`+`/`?`
|
||||
//! (greedy, backtracking), a leading `(?i)` case-insensitive flag, and
|
||||
//! `\`-escaping of any non-alphanumeric char (what `escape` produces).
|
||||
//!
|
||||
//! Split into `parse` (pattern string -> `Node` sequence) and `matcher`
|
||||
//! (backtracking match over a char slice) to stay under the structure
|
||||
//! gate's per-file line limit.
|
||||
|
||||
mod matcher;
|
||||
mod parse;
|
||||
|
||||
pub use parse::Error;
|
||||
|
||||
/// A compiled pattern.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Regex {
|
||||
nodes: Vec<parse::Node>,
|
||||
case_insensitive: bool,
|
||||
}
|
||||
|
||||
impl Regex {
|
||||
/// Compiles `pattern`. See the module docs for the supported vocabulary.
|
||||
pub fn new(pattern: &str) -> Result<Self, Error> {
|
||||
let (nodes, case_insensitive) = parse::parse(pattern)?;
|
||||
Ok(Self {
|
||||
nodes,
|
||||
case_insensitive,
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether `text` matches anywhere (unanchored, unless the pattern
|
||||
/// itself carries `^`/`$`) - mirrors `regex::Regex::is_match`.
|
||||
pub fn is_match(&self, text: &str) -> bool {
|
||||
let chars: Vec<char> = if self.case_insensitive {
|
||||
text.chars().flat_map(char::to_lowercase).collect()
|
||||
} else {
|
||||
text.chars().collect()
|
||||
};
|
||||
matcher::search(&self.nodes, &chars)
|
||||
}
|
||||
}
|
||||
|
||||
/// Escapes every char that could be special in a pattern, so the result
|
||||
/// matches `s` literally when embedded in a larger pattern.
|
||||
///
|
||||
/// Mirrors the subset of `regex::escape` this codebase actually relies on:
|
||||
/// everything outside `[A-Za-z0-9_]` is escaped (harmless over-escaping for
|
||||
/// chars this engine treats as plain literals anyway).
|
||||
pub fn escape(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len() * 2);
|
||||
for c in s.chars() {
|
||||
if !c.is_alphanumeric() && c != '_' {
|
||||
out.push('\\');
|
||||
}
|
||||
out.push(c);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn literal_and_anchors() {
|
||||
let re = Regex::new("^abc$").unwrap();
|
||||
assert!(re.is_match("abc"));
|
||||
assert!(!re.is_match("abcd"));
|
||||
assert!(!re.is_match("xabc"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glob_star_and_doublestar() {
|
||||
// Mirrors `keep::compile` output for `com.foo.*`.
|
||||
let re = Regex::new("^com/foo/[^/]*$").unwrap();
|
||||
assert!(re.is_match("com/foo/Bar"));
|
||||
assert!(!re.is_match("com/foo/Bar/Baz"));
|
||||
|
||||
let re2 = Regex::new("^com/.*$").unwrap();
|
||||
assert!(re2.is_match("com/foo/Bar/Baz"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn question_mark_class() {
|
||||
let re = Regex::new("^a[^/]b$").unwrap();
|
||||
assert!(re.is_match("axb"));
|
||||
assert!(!re.is_match("a/b"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escaped_literal_dot() {
|
||||
let pat = format!("^{}$", escape("a.b"));
|
||||
let re = Regex::new(&pat).unwrap();
|
||||
assert!(re.is_match("a.b"));
|
||||
assert!(!re.is_match("axb"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn red_patterns_compile_and_match() {
|
||||
let re = Regex::new(r"(?i)password\s*=\s*[^\s;]+").unwrap();
|
||||
assert!(re.is_match("password=hunter2"));
|
||||
assert!(re.is_match("PASSWORD = hunter2"));
|
||||
assert!(!re.is_match("not a secret"));
|
||||
|
||||
let re2 = Regex::new(r"(?i)\bapi[_-]?key\b").unwrap();
|
||||
assert!(re2.is_match("api_key=abc"));
|
||||
assert!(re2.is_match("API-KEY: x"));
|
||||
assert!(!re2.is_match("recapitalized"));
|
||||
|
||||
let re3 = Regex::new(r#"jdbc:[a-z]+://[^\s"']+"#).unwrap();
|
||||
assert!(re3.is_match("jdbc:mysql://host/db"));
|
||||
|
||||
let re4 = Regex::new(r"-----BEGIN [A-Z ]*PRIVATE KEY-----").unwrap();
|
||||
assert!(re4.is_match("-----BEGIN RSA PRIVATE KEY-----"));
|
||||
|
||||
let re5 = Regex::new(r#"https?://[^\s"']+"#).unwrap();
|
||||
assert!(re5.is_match("https://example.com/hook"));
|
||||
assert!(re5.is_match("http://example.com"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clean_text_passes() {
|
||||
let re = Regex::new(r"(?i)password\s*=\s*[^\s;]+").unwrap();
|
||||
assert!(!re.is_match("hello world"));
|
||||
}
|
||||
}
|
||||
202
crates/noxis-analysis/src/minirx/parse.rs
Normal file
202
crates/noxis-analysis/src/minirx/parse.rs
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
//! Pattern string -> flat `Node` sequence (no groups, no alternation).
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// A single class-member: a literal char or an inclusive range.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub(super) enum ClassItem {
|
||||
Char(char),
|
||||
Range(char, char),
|
||||
Space,
|
||||
}
|
||||
|
||||
impl ClassItem {
|
||||
fn matches(&self, c: char) -> bool {
|
||||
match *self {
|
||||
ClassItem::Char(x) => x == c,
|
||||
ClassItem::Range(lo, hi) => lo <= c && c <= hi,
|
||||
ClassItem::Space => c.is_whitespace(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One zero-width or single-char-consuming atom in the pattern.
|
||||
#[derive(Debug, Clone)]
|
||||
pub(super) enum Atom {
|
||||
Char(char),
|
||||
Any,
|
||||
Space,
|
||||
Class {
|
||||
negated: bool,
|
||||
items: Vec<ClassItem>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Atom {
|
||||
pub(super) fn matches(&self, c: char) -> bool {
|
||||
match self {
|
||||
Atom::Char(x) => *x == c,
|
||||
Atom::Any => true,
|
||||
Atom::Space => c.is_whitespace(),
|
||||
Atom::Class { negated, items } => {
|
||||
let hit = items.iter().any(|item| item.matches(c));
|
||||
hit != *negated
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A node in the flat pattern sequence.
|
||||
#[derive(Debug, Clone)]
|
||||
pub(super) enum Node {
|
||||
Start,
|
||||
End,
|
||||
WordBoundary,
|
||||
/// `atom` repeated `min..=max` times (`max = None` means unbounded),
|
||||
/// matched greedily with backtracking.
|
||||
Rep {
|
||||
atom: Atom,
|
||||
min: usize,
|
||||
max: Option<usize>,
|
||||
},
|
||||
}
|
||||
|
||||
/// A pattern compilation error.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Error(pub(super) String);
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
/// Parses `pattern`, returning its node sequence and whether a leading
|
||||
/// `(?i)` flag was present.
|
||||
pub(super) fn parse(pattern: &str) -> Result<(Vec<Node>, bool), Error> {
|
||||
let (rest, case_insensitive) = match pattern.strip_prefix("(?i)") {
|
||||
Some(rest) => (rest, true),
|
||||
None => (pattern, false),
|
||||
};
|
||||
let chars: Vec<char> = if case_insensitive {
|
||||
rest.chars().flat_map(char::to_lowercase).collect()
|
||||
} else {
|
||||
rest.chars().collect()
|
||||
};
|
||||
let mut nodes = Vec::new();
|
||||
let mut i = 0;
|
||||
while i < chars.len() {
|
||||
let c = chars[i];
|
||||
let node = match c {
|
||||
'^' => {
|
||||
i += 1;
|
||||
Node::Start
|
||||
}
|
||||
'$' => {
|
||||
i += 1;
|
||||
Node::End
|
||||
}
|
||||
'\\' => {
|
||||
i += 1;
|
||||
let Some(&esc) = chars.get(i) else {
|
||||
return Err(Error("trailing backslash".into()));
|
||||
};
|
||||
i += 1;
|
||||
if esc == 'b' {
|
||||
Node::WordBoundary
|
||||
} else {
|
||||
let atom = if esc == 's' {
|
||||
Atom::Space
|
||||
} else {
|
||||
Atom::Char(esc)
|
||||
};
|
||||
parse_quantifier(atom, &chars, &mut i)
|
||||
}
|
||||
}
|
||||
'.' => {
|
||||
i += 1;
|
||||
parse_quantifier(Atom::Any, &chars, &mut i)
|
||||
}
|
||||
'[' => {
|
||||
i += 1;
|
||||
let atom = parse_class(&chars, &mut i)?;
|
||||
parse_quantifier(atom, &chars, &mut i)
|
||||
}
|
||||
_ => {
|
||||
i += 1;
|
||||
parse_quantifier(Atom::Char(c), &chars, &mut i)
|
||||
}
|
||||
};
|
||||
nodes.push(node);
|
||||
}
|
||||
Ok((nodes, case_insensitive))
|
||||
}
|
||||
|
||||
/// Reads an optional trailing `*`/`+`/`?` and wraps `atom` into a `Rep`.
|
||||
fn parse_quantifier(atom: Atom, chars: &[char], i: &mut usize) -> Node {
|
||||
let (min, max) = match chars.get(*i) {
|
||||
Some('*') => {
|
||||
*i += 1;
|
||||
(0, None)
|
||||
}
|
||||
Some('+') => {
|
||||
*i += 1;
|
||||
(1, None)
|
||||
}
|
||||
Some('?') => {
|
||||
*i += 1;
|
||||
(0, Some(1))
|
||||
}
|
||||
_ => (1, Some(1)),
|
||||
};
|
||||
Node::Rep { atom, min, max }
|
||||
}
|
||||
|
||||
/// Parses `[...]` starting right after the opening `[`; advances `i` past
|
||||
/// the closing `]`.
|
||||
fn parse_class(chars: &[char], i: &mut usize) -> Result<Atom, Error> {
|
||||
let negated = if matches!(chars.get(*i), Some('^')) {
|
||||
*i += 1;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let mut items = Vec::new();
|
||||
let mut closed = false;
|
||||
while let Some(&c) = chars.get(*i) {
|
||||
if c == ']' {
|
||||
*i += 1;
|
||||
closed = true;
|
||||
break;
|
||||
}
|
||||
let lo = if c == '\\' {
|
||||
*i += 1;
|
||||
let esc = *chars
|
||||
.get(*i)
|
||||
.ok_or_else(|| Error("trailing backslash in class".into()))?;
|
||||
*i += 1;
|
||||
if esc == 's' {
|
||||
items.push(ClassItem::Space);
|
||||
continue;
|
||||
}
|
||||
esc
|
||||
} else {
|
||||
*i += 1;
|
||||
c
|
||||
};
|
||||
if chars.get(*i) == Some(&'-') && chars.get(*i + 1).is_some_and(|&c| c != ']') {
|
||||
*i += 1;
|
||||
let hi = chars[*i];
|
||||
*i += 1;
|
||||
items.push(ClassItem::Range(lo, hi));
|
||||
} else {
|
||||
items.push(ClassItem::Char(lo));
|
||||
}
|
||||
}
|
||||
if !closed {
|
||||
return Err(Error("unterminated character class".into()));
|
||||
}
|
||||
Ok(Atom::Class { negated, items })
|
||||
}
|
||||
|
|
@ -7,7 +7,6 @@ rust-version.workspace = true
|
|||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
indexmap = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
ristretto_classfile = "0.34"
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
//! absolute instruction numbers (`switch` - into deltas from itself), constants
|
||||
//! and references go to the pool via [`Pool`](super::super::pool::Pool).
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use ristretto_classfile::attributes::{Instruction as RtInsn, LookupSwitch, TableSwitch};
|
||||
|
||||
use super::super::pool::Pool;
|
||||
|
|
@ -62,13 +61,23 @@ pub(crate) fn convert(
|
|||
.map(|target| delta(index, resolve(*target)?))
|
||||
.collect::<Result<_, _>>()?,
|
||||
})),
|
||||
Insn::LookupSwitch { default, pairs } => RtInsn::Lookupswitch(Box::new(LookupSwitch {
|
||||
default: delta(index, resolve(*default)?)?,
|
||||
pairs: pairs
|
||||
.iter()
|
||||
.map(|(key, target)| Ok((*key, delta(index, resolve(*target)?)?)))
|
||||
.collect::<Result<IndexMap<_, _>, ClassfileError>>()?,
|
||||
})),
|
||||
Insn::LookupSwitch { default, pairs } => {
|
||||
// `ristretto_classfile::attributes::LookupSwitch::pairs` is typed
|
||||
// `indexmap::IndexMap<i32, i32>` by the (out-of-scope) backend crate
|
||||
// itself, so an insertion-ordered map still ends up on the wire here -
|
||||
// we just build it without naming `indexmap` directly in this crate.
|
||||
// Match order must be preserved: it's the `lookupswitch` operand
|
||||
// order, which the JVM spec (and verifiers) require sorted by key,
|
||||
// and callers already hand us `pairs` pre-sorted.
|
||||
let mut resolved = Vec::with_capacity(pairs.len());
|
||||
for (key, target) in pairs {
|
||||
resolved.push((*key, delta(index, resolve(*target)?)?));
|
||||
}
|
||||
RtInsn::Lookupswitch(Box::new(LookupSwitch {
|
||||
default: delta(index, resolve(*default)?)?,
|
||||
pairs: resolved.into_iter().collect(),
|
||||
}))
|
||||
}
|
||||
Insn::Return(prim) => ret_op(*prim),
|
||||
Insn::AReturn => RtInsn::Areturn,
|
||||
Insn::GetStatic {
|
||||
|
|
|
|||
|
|
@ -31,11 +31,23 @@ pub(super) fn int_const(value: &Const, pool: &mut Pool) -> Result<RtInsn, Classf
|
|||
Const::Double(v) if *v == 0.0 => RtInsn::Dconst_0,
|
||||
Const::Double(v) if *v == 1.0 => RtInsn::Dconst_1,
|
||||
Const::Double(_) => RtInsn::Ldc2_w(pool.constant(value)?),
|
||||
// JVMS §4.4.10: a `CONSTANT_Dynamic`'s category comes from its own
|
||||
// descriptor, not from being `Dynamic` - `J`/`D` are category 2
|
||||
// (`ldc2_w`), everything else (references, `I`/`F`/...) is
|
||||
// category 1 (`ldc_w`). Using `ldc2_w` unconditionally here was a
|
||||
// bug: a condy-encrypted `String` would push the wrong stack shape.
|
||||
Const::Dynamic {
|
||||
name,
|
||||
descriptor,
|
||||
bootstrap,
|
||||
} => RtInsn::Ldc2_w(pool.dynamic(*bootstrap, name, descriptor)?),
|
||||
} => {
|
||||
let index = pool.dynamic(*bootstrap, name, descriptor)?;
|
||||
if matches!(descriptor.as_str(), "J" | "D") {
|
||||
RtInsn::Ldc2_w(index)
|
||||
} else {
|
||||
RtInsn::Ldc_w(index)
|
||||
}
|
||||
}
|
||||
_ => RtInsn::Ldc_w(pool.constant(value)?),
|
||||
})
|
||||
}
|
||||
|
|
|
|||
154
crates/noxis-classfile/tests/roundtrip/attrs/condy.rs
Normal file
154
crates/noxis-classfile/tests/roundtrip/attrs/condy.rs
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
//! `CONSTANT_Dynamic` (condy) round trips: the writer must be able to emit a
|
||||
//! brand-new `ldc` on a `Const::Dynamic`, not just pass one through on read.
|
||||
//!
|
||||
//! Why this matters: a generic sandbox-based deobfuscator (narumii/SSVM)
|
||||
//! confirmed empirically (`todo.md`, 2026-10-14) resolves plain arithmetic
|
||||
//! and runtime-decrypt calls, but does not resolve `invokedynamic`/condy at
|
||||
//! all - 0 changes on a condy-only test class. Strings/Constants moving to
|
||||
//! condy is the first concrete fix that follows from that finding.
|
||||
|
||||
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
||||
|
||||
use super::super::round_trip;
|
||||
use noxis_classfile::{
|
||||
Attribute, BootstrapMethod, ClassBuilder, Code, Const, Insn, Version, format,
|
||||
};
|
||||
use ristretto_classfile::attributes::{
|
||||
Attribute as RtAttr, BootstrapMethod as RtBsm, Instruction as Rt,
|
||||
};
|
||||
use ristretto_classfile::{
|
||||
ClassAccessFlags, ClassFile as RtClass, ConstantPool, JAVA_21, Method as RtMethod,
|
||||
MethodAccessFlags, ReferenceKind,
|
||||
};
|
||||
|
||||
/// `CONSTANT_Dynamic` (condy, `ldc`) survives the round trip - needed for
|
||||
/// passes that emit strings/constants through a bootstrap method instead of
|
||||
/// a plain `ldc` literal.
|
||||
#[test]
|
||||
fn condy_ldc_round_trip() {
|
||||
let mut pool = ConstantPool::default();
|
||||
let this = pool.add_class("demo/Condy").expect("add_class");
|
||||
let object = pool.add_class("java/lang/Object").expect("add_object");
|
||||
let owner = pool.add_class("demo/RT").expect("owner");
|
||||
let method_ref = pool
|
||||
.add_method_ref(
|
||||
owner,
|
||||
"boot",
|
||||
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/String;",
|
||||
)
|
||||
.expect("ref");
|
||||
let handle = pool
|
||||
.add_method_handle(ReferenceKind::InvokeStatic, method_ref)
|
||||
.expect("handle");
|
||||
let bsm_name = pool.add_utf8("BootstrapMethods").expect("attr");
|
||||
let name = pool.add_utf8("run").expect("name");
|
||||
let desc = pool.add_utf8("()Ljava/lang/String;").expect("desc");
|
||||
let code_name = pool.add_utf8("Code").expect("attr");
|
||||
let dynamic = pool
|
||||
.add_dynamic(0, "_", "Ljava/lang/String;")
|
||||
.expect("condy");
|
||||
|
||||
let rt = RtClass {
|
||||
version: JAVA_21,
|
||||
constant_pool: pool,
|
||||
access_flags: ClassAccessFlags::PUBLIC,
|
||||
this_class: this,
|
||||
super_class: object,
|
||||
interfaces: vec![],
|
||||
fields: vec![],
|
||||
methods: vec![RtMethod {
|
||||
access_flags: MethodAccessFlags::PUBLIC | MethodAccessFlags::STATIC,
|
||||
name_index: name,
|
||||
descriptor_index: desc,
|
||||
attributes: vec![RtAttr::Code {
|
||||
name_index: code_name,
|
||||
max_stack: 1,
|
||||
max_locals: 0,
|
||||
code: vec![Rt::Ldc_w(dynamic), Rt::Areturn],
|
||||
exception_table: vec![],
|
||||
attributes: vec![],
|
||||
}],
|
||||
}],
|
||||
attributes: vec![RtAttr::BootstrapMethods {
|
||||
name_index: bsm_name,
|
||||
methods: vec![RtBsm {
|
||||
bootstrap_method_ref: handle,
|
||||
arguments: vec![],
|
||||
}],
|
||||
}],
|
||||
..RtClass::default()
|
||||
};
|
||||
|
||||
let ir = round_trip(&rt);
|
||||
assert_eq!(ir.attributes.len(), 1);
|
||||
assert!(format!("{:?}", ir.attributes[0]).contains("BootstrapMethods"));
|
||||
let code = ir.methods[0].code.as_ref().expect("code");
|
||||
assert!(
|
||||
format!("{:?}", code.insns).contains("Dynamic"),
|
||||
"condy ldc должен пережить round-trip как Const::Dynamic: {:?}",
|
||||
code.insns
|
||||
);
|
||||
}
|
||||
|
||||
/// Регрессия: писатель раньше эмитил `Const::Dynamic` ВСЕГДА через
|
||||
/// `ldc2_w` (категория 2 - long/double, 2 слота стека), даже когда BSM
|
||||
/// возвращает `String` (категория 1). `ldc2_w` на ссылочном типе -
|
||||
/// категория инструкции не совпадает с категорией значения, кадры стека
|
||||
/// и верификатор на реальной JVM это не пропустят. Категория должна
|
||||
/// читаться из дескриптора condy-константы (JVMS §4.4.10), не быть
|
||||
/// захардкожена.
|
||||
#[test]
|
||||
fn condy_string_writes_ldc_w_not_ldc2_w() {
|
||||
let class = ClassBuilder::new("demo/CondyOp", None)
|
||||
.version(Version::JAVA_21)
|
||||
.attribute(Attribute::BootstrapMethods(vec![BootstrapMethod {
|
||||
kind: 6, // InvokeStatic
|
||||
owner: "demo/RT".to_string(),
|
||||
name: "boot".to_string(),
|
||||
descriptor: "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/String;"
|
||||
.to_string(),
|
||||
itf: false,
|
||||
args: vec![],
|
||||
}]))
|
||||
.method_with_code(
|
||||
0x0009,
|
||||
"run",
|
||||
"()Ljava/lang/String;",
|
||||
Code {
|
||||
max_stack: 1,
|
||||
max_locals: 0,
|
||||
insns: vec![
|
||||
Insn::Const(Const::Dynamic {
|
||||
name: "_".to_string(),
|
||||
descriptor: "Ljava/lang/String;".to_string(),
|
||||
bootstrap: 0,
|
||||
}),
|
||||
Insn::AReturn,
|
||||
],
|
||||
exception_table: Vec::new(),
|
||||
frames: Vec::new(),
|
||||
attributes: Vec::new(),
|
||||
},
|
||||
)
|
||||
.build();
|
||||
|
||||
let bytes = format::write(&class).expect("write");
|
||||
let written = RtClass::from_bytes(&bytes).expect("parse back");
|
||||
let method = &written.methods[0];
|
||||
let RtAttr::Code { code, .. } = method
|
||||
.attributes
|
||||
.iter()
|
||||
.find(|a| matches!(a, RtAttr::Code { .. }))
|
||||
.expect("Code attribute")
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
assert!(
|
||||
matches!(code.first(), Some(Rt::Ldc_w(_))),
|
||||
"condy String должен писаться через ldc_w (категория 1), не ldc2_w: {code:?}"
|
||||
);
|
||||
assert!(
|
||||
!code.iter().any(|insn| matches!(insn, Rt::Ldc2_w(_))),
|
||||
"ldc2_w не должен появляться для condy-строки: {code:?}"
|
||||
);
|
||||
}
|
||||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
||||
|
||||
mod condy;
|
||||
|
||||
use super::round_trip;
|
||||
use ristretto_classfile::attributes::{
|
||||
Attribute as RtAttr, BootstrapMethod as RtBsm, Instruction as Rt,
|
||||
|
|
@ -10,7 +10,6 @@ license.workspace = true
|
|||
noxis-classfile = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
toml = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
|
||||
[lints]
|
||||
|
|
|
|||
172
crates/noxis-core/src/config/from_toml/mod.rs
Normal file
172
crates/noxis-core/src/config/from_toml/mod.rs
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
//! Builds [`Config`](super::Config) from a parsed [`toml_lite::Table`].
|
||||
//!
|
||||
//! Deliberately *not* going through `serde::Deserialize`: `Config` has
|
||||
//! only a handful of fields and three small kebab-case enums, so a tiny
|
||||
//! hand-written visitor is less machinery (and fewer edge cases to worry
|
||||
//! about) than writing a generic `serde::Deserializer` over our `Value`
|
||||
//! tree. This keeps the TOML replacement fully decoupled from serde -
|
||||
//! the `Serialize`/`Deserialize` derives left on `Config` and friends are
|
||||
//! unrelated to this path and can be dropped independently later.
|
||||
|
||||
mod sections;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use super::toml_lite::{Table, Value};
|
||||
use super::{Config, PassId, Profile};
|
||||
|
||||
type Res<T> = Result<T, String>;
|
||||
|
||||
fn unknown_key(section: &str, key: &str) -> String {
|
||||
format!("неизвестный ключ `{key}` в секции `{section}`")
|
||||
}
|
||||
|
||||
fn expect_str<'a>(v: &'a Value, path: &str) -> Res<&'a str> {
|
||||
match v {
|
||||
Value::String(s) => Ok(s),
|
||||
_ => Err(format!("`{path}` должен быть строкой")),
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_bool(v: &Value, path: &str) -> Res<bool> {
|
||||
match v {
|
||||
Value::Bool(b) => Ok(*b),
|
||||
_ => Err(format!("`{path}` должен быть bool")),
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_usize(v: &Value, path: &str) -> Res<usize> {
|
||||
match v {
|
||||
Value::Integer(i) if *i >= 0 => Ok(*i as usize),
|
||||
_ => Err(format!("`{path}` должен быть неотрицательным целым")),
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_u64(v: &Value, path: &str) -> Res<u64> {
|
||||
match v {
|
||||
Value::Integer(i) if *i >= 0 => Ok(*i as u64),
|
||||
_ => Err(format!("`{path}` должен быть неотрицательным целым")),
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_string_vec(v: &Value, path: &str) -> Res<Vec<String>> {
|
||||
match v {
|
||||
Value::Array(items) => items
|
||||
.iter()
|
||||
.map(|item| expect_str(item, path).map(str::to_string))
|
||||
.collect(),
|
||||
_ => Err(format!("`{path}` должен быть массивом строк")),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_profile(s: &str) -> Res<Profile> {
|
||||
match s {
|
||||
"light" => Ok(Profile::Light),
|
||||
"standard" => Ok(Profile::Standard),
|
||||
"paranoid" => Ok(Profile::Paranoid),
|
||||
"manual" => Ok(Profile::Manual),
|
||||
other => Err(format!("неизвестный профиль `{other}`")),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_pass_id(s: &str) -> Res<PassId> {
|
||||
[
|
||||
PassId::Rename,
|
||||
PassId::Strings,
|
||||
PassId::Constants,
|
||||
PassId::Flow,
|
||||
PassId::Noise,
|
||||
PassId::DebugInfo,
|
||||
PassId::Resources,
|
||||
]
|
||||
.into_iter()
|
||||
.find(|id| id.as_str() == s)
|
||||
.ok_or_else(|| format!("неизвестный пасс `{s}`"))
|
||||
}
|
||||
|
||||
fn parse_pass_id_vec(v: &Value, path: &str) -> Res<Vec<PassId>> {
|
||||
expect_string_vec(v, path)?
|
||||
.iter()
|
||||
.map(|s| parse_pass_id(s))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Reject any key in `table` that isn't in `known` (mirrors serde's
|
||||
/// `deny_unknown_fields`).
|
||||
fn check_known_keys(table: &Table, known: &[&str], section: &str) -> Res<()> {
|
||||
for key in table.keys() {
|
||||
if !known.contains(&key.as_str()) {
|
||||
return Err(unknown_key(section, key));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sub_table<'a>(table: &'a Table, key: &str) -> Res<Option<&'a Table>> {
|
||||
match table.get(key) {
|
||||
None => Ok(None),
|
||||
Some(Value::Table(t)) => Ok(Some(t)),
|
||||
Some(_) => Err(format!("`{key}` должен быть секцией `[{key}]`")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a full [`Config`] from a parsed root table.
|
||||
pub(super) fn build(root: &Table) -> Res<Config> {
|
||||
check_known_keys(
|
||||
root,
|
||||
&[
|
||||
"profile",
|
||||
"seed",
|
||||
"enable",
|
||||
"disable",
|
||||
"keep",
|
||||
"keep_resources",
|
||||
"rename",
|
||||
"strings",
|
||||
"constants",
|
||||
"flow",
|
||||
"noise",
|
||||
"debug",
|
||||
],
|
||||
"<root>",
|
||||
)?;
|
||||
|
||||
let mut cfg = Config::default();
|
||||
if let Some(v) = root.get("profile") {
|
||||
cfg.profile = parse_profile(expect_str(v, "profile")?)?;
|
||||
}
|
||||
if let Some(v) = root.get("seed") {
|
||||
cfg.seed = Some(expect_u64(v, "seed")?);
|
||||
}
|
||||
if let Some(v) = root.get("enable") {
|
||||
cfg.enable = parse_pass_id_vec(v, "enable")?;
|
||||
}
|
||||
if let Some(v) = root.get("disable") {
|
||||
cfg.disable = parse_pass_id_vec(v, "disable")?;
|
||||
}
|
||||
if let Some(v) = root.get("keep") {
|
||||
cfg.keep = expect_string_vec(v, "keep")?;
|
||||
}
|
||||
if let Some(v) = root.get("keep_resources") {
|
||||
cfg.keep_resources = expect_string_vec(v, "keep_resources")?;
|
||||
}
|
||||
if let Some(t) = sub_table(root, "rename")? {
|
||||
cfg.rename = sections::build_rename(t)?;
|
||||
}
|
||||
if let Some(t) = sub_table(root, "strings")? {
|
||||
cfg.strings = sections::build_strings(t)?;
|
||||
}
|
||||
if let Some(t) = sub_table(root, "constants")? {
|
||||
cfg.constants = sections::build_constants(t)?;
|
||||
}
|
||||
if let Some(t) = sub_table(root, "flow")? {
|
||||
cfg.flow = sections::build_flow(t)?;
|
||||
}
|
||||
if let Some(t) = sub_table(root, "noise")? {
|
||||
cfg.noise = sections::build_noise(t)?;
|
||||
}
|
||||
if let Some(t) = sub_table(root, "debug")? {
|
||||
cfg.debug = sections::build_debug(t)?;
|
||||
}
|
||||
Ok(cfg)
|
||||
}
|
||||
194
crates/noxis-core/src/config/from_toml/sections.rs
Normal file
194
crates/noxis-core/src/config/from_toml/sections.rs
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
//! Per-section struct population, split out of `from_toml::build` purely to
|
||||
//! stay under the per-file line budget (see `docs/README.md`).
|
||||
|
||||
use super::super::sections::{
|
||||
ConstantsConfig, DebugConfig, FlowConfig, NameStyle, NoiseConfig, RenameConfig, StringsConfig,
|
||||
};
|
||||
use super::super::toml_lite::Table;
|
||||
use super::{Res, check_known_keys, expect_bool, expect_str, expect_string_vec, expect_usize};
|
||||
|
||||
fn parse_name_style(s: &str) -> Res<NameStyle> {
|
||||
match s {
|
||||
"short-latin" => Ok(NameStyle::ShortLatin),
|
||||
"il-o0" => Ok(NameStyle::IlO0),
|
||||
"unicode" => Ok(NameStyle::Unicode),
|
||||
"mixed-latin" => Ok(NameStyle::MixedLatin),
|
||||
"decoy-words" => Ok(NameStyle::DecoyWords),
|
||||
"plausible" => Ok(NameStyle::Plausible),
|
||||
other => Err(format!("неизвестный стиль имён `{other}`")),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn build_rename(t: &Table) -> Res<RenameConfig> {
|
||||
check_known_keys(
|
||||
t,
|
||||
&[
|
||||
"spread_packages",
|
||||
"package_pool",
|
||||
"class_style",
|
||||
"member_style",
|
||||
"local_style",
|
||||
"widen_access",
|
||||
"rename_enum_constants",
|
||||
"rename_locals",
|
||||
"rename_public_members",
|
||||
],
|
||||
"rename",
|
||||
)?;
|
||||
let mut c = RenameConfig::default();
|
||||
if let Some(v) = t.get("spread_packages") {
|
||||
c.spread_packages = expect_bool(v, "rename.spread_packages")?;
|
||||
}
|
||||
if let Some(v) = t.get("package_pool") {
|
||||
c.package_pool = expect_usize(v, "rename.package_pool")?;
|
||||
}
|
||||
if let Some(v) = t.get("class_style") {
|
||||
c.class_style = parse_name_style(expect_str(v, "rename.class_style")?)?;
|
||||
}
|
||||
if let Some(v) = t.get("member_style") {
|
||||
c.member_style = parse_name_style(expect_str(v, "rename.member_style")?)?;
|
||||
}
|
||||
if let Some(v) = t.get("local_style") {
|
||||
c.local_style = parse_name_style(expect_str(v, "rename.local_style")?)?;
|
||||
}
|
||||
if let Some(v) = t.get("widen_access") {
|
||||
c.widen_access = expect_bool(v, "rename.widen_access")?;
|
||||
}
|
||||
if let Some(v) = t.get("rename_enum_constants") {
|
||||
c.rename_enum_constants = expect_bool(v, "rename.rename_enum_constants")?;
|
||||
}
|
||||
if let Some(v) = t.get("rename_locals") {
|
||||
c.rename_locals = expect_bool(v, "rename.rename_locals")?;
|
||||
}
|
||||
if let Some(v) = t.get("rename_public_members") {
|
||||
c.rename_public_members = expect_bool(v, "rename.rename_public_members")?;
|
||||
}
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
pub(super) fn build_strings(t: &Table) -> Res<StringsConfig> {
|
||||
check_known_keys(
|
||||
t,
|
||||
&[
|
||||
"schemes",
|
||||
"min_len",
|
||||
"split_above",
|
||||
"skip",
|
||||
"derive_key",
|
||||
"condy",
|
||||
],
|
||||
"strings",
|
||||
)?;
|
||||
let mut c = StringsConfig::default();
|
||||
if let Some(v) = t.get("schemes") {
|
||||
c.schemes = expect_string_vec(v, "strings.schemes")?;
|
||||
}
|
||||
if let Some(v) = t.get("min_len") {
|
||||
c.min_len = expect_usize(v, "strings.min_len")?;
|
||||
}
|
||||
if let Some(v) = t.get("split_above") {
|
||||
c.split_above = expect_usize(v, "strings.split_above")?;
|
||||
}
|
||||
if let Some(v) = t.get("skip") {
|
||||
c.skip = expect_string_vec(v, "strings.skip")?;
|
||||
}
|
||||
if let Some(v) = t.get("derive_key") {
|
||||
c.derive_key = expect_bool(v, "strings.derive_key")?;
|
||||
}
|
||||
if let Some(v) = t.get("condy") {
|
||||
c.condy = expect_bool(v, "strings.condy")?;
|
||||
}
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
pub(super) fn build_constants(t: &Table) -> Res<ConstantsConfig> {
|
||||
check_known_keys(t, &["schemes", "max_predicates", "skip"], "constants")?;
|
||||
let mut c = ConstantsConfig::default();
|
||||
if let Some(v) = t.get("schemes") {
|
||||
c.schemes = expect_string_vec(v, "constants.schemes")?;
|
||||
}
|
||||
if let Some(v) = t.get("max_predicates") {
|
||||
c.max_predicates = expect_usize(v, "constants.max_predicates")?;
|
||||
}
|
||||
if let Some(v) = t.get("skip") {
|
||||
c.skip = expect_string_vec(v, "constants.skip")?;
|
||||
}
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
pub(super) fn build_flow(t: &Table) -> Res<FlowConfig> {
|
||||
check_known_keys(t, &["schemes", "max_insns", "encrypt_block_bodies"], "flow")?;
|
||||
let mut c = FlowConfig::default();
|
||||
if let Some(v) = t.get("schemes") {
|
||||
c.schemes = expect_string_vec(v, "flow.schemes")?;
|
||||
}
|
||||
if let Some(v) = t.get("max_insns") {
|
||||
c.max_insns = expect_usize(v, "flow.max_insns")?;
|
||||
}
|
||||
if let Some(v) = t.get("encrypt_block_bodies") {
|
||||
c.encrypt_block_bodies = expect_bool(v, "flow.encrypt_block_bodies")?;
|
||||
}
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
pub(super) fn build_noise(t: &Table) -> Res<NoiseConfig> {
|
||||
check_known_keys(
|
||||
t,
|
||||
&[
|
||||
"junk_ratio",
|
||||
"junk_methods_per_class",
|
||||
"junk_annotations",
|
||||
"weave_reachable",
|
||||
],
|
||||
"noise",
|
||||
)?;
|
||||
let mut c = NoiseConfig::default();
|
||||
if let Some(v) = t.get("junk_ratio") {
|
||||
c.junk_ratio = expect_usize(v, "noise.junk_ratio")?;
|
||||
}
|
||||
if let Some(v) = t.get("junk_methods_per_class") {
|
||||
c.junk_methods_per_class = expect_usize(v, "noise.junk_methods_per_class")?;
|
||||
}
|
||||
if let Some(v) = t.get("junk_annotations") {
|
||||
c.junk_annotations = expect_usize(v, "noise.junk_annotations")?;
|
||||
}
|
||||
if let Some(v) = t.get("weave_reachable") {
|
||||
c.weave_reachable = expect_bool(v, "noise.weave_reachable")?;
|
||||
}
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
pub(super) fn build_debug(t: &Table) -> Res<DebugConfig> {
|
||||
check_known_keys(
|
||||
t,
|
||||
&[
|
||||
"strip_line_numbers",
|
||||
"fake_line_numbers",
|
||||
"strip_local_variables",
|
||||
"spoof_source_file",
|
||||
"strip_source_file",
|
||||
"strip_signatures",
|
||||
],
|
||||
"debug",
|
||||
)?;
|
||||
let mut c = DebugConfig::default();
|
||||
if let Some(v) = t.get("strip_line_numbers") {
|
||||
c.strip_line_numbers = expect_bool(v, "debug.strip_line_numbers")?;
|
||||
}
|
||||
if let Some(v) = t.get("fake_line_numbers") {
|
||||
c.fake_line_numbers = expect_bool(v, "debug.fake_line_numbers")?;
|
||||
}
|
||||
if let Some(v) = t.get("strip_local_variables") {
|
||||
c.strip_local_variables = expect_bool(v, "debug.strip_local_variables")?;
|
||||
}
|
||||
if let Some(v) = t.get("spoof_source_file") {
|
||||
c.spoof_source_file = expect_bool(v, "debug.spoof_source_file")?;
|
||||
}
|
||||
if let Some(v) = t.get("strip_source_file") {
|
||||
c.strip_source_file = expect_bool(v, "debug.strip_source_file")?;
|
||||
}
|
||||
if let Some(v) = t.get("strip_signatures") {
|
||||
c.strip_signatures = expect_bool(v, "debug.strip_signatures")?;
|
||||
}
|
||||
Ok(c)
|
||||
}
|
||||
71
crates/noxis-core/src/config/from_toml/tests.rs
Normal file
71
crates/noxis-core/src/config/from_toml/tests.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
//! Tests for TOML-subset → `Config` struct population.
|
||||
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
||||
|
||||
use super::*;
|
||||
use crate::config::sections::NameStyle;
|
||||
use crate::config::toml_lite;
|
||||
use crate::config::{PassId, Profile};
|
||||
|
||||
fn build_from(text: &str) -> Res<Config> {
|
||||
let table = toml_lite::parse(text).map_err(|e| e.to_string())?;
|
||||
build(&table)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_defaults_from_empty_input() {
|
||||
let cfg = build_from("").unwrap();
|
||||
assert_eq!(cfg, Config::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overrides_top_level_and_section_fields() {
|
||||
let cfg = build_from(
|
||||
r#"
|
||||
profile = "paranoid"
|
||||
seed = 123
|
||||
keep = ["com/example/Main"]
|
||||
[rename]
|
||||
package_pool = 4
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(cfg.profile, Profile::Paranoid);
|
||||
assert_eq!(cfg.seed, Some(123));
|
||||
assert_eq!(cfg.keep, vec!["com/example/Main".to_string()]);
|
||||
assert_eq!(cfg.rename.package_pool, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_unknown_top_level_key() {
|
||||
assert!(build_from("totally_unknown = 1").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_unknown_section_key() {
|
||||
assert!(build_from("[rename]\nbogus = 1\n").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_enable_disable_pass_lists() {
|
||||
let cfg = build_from(
|
||||
r#"enable = ["flow"]
|
||||
disable = ["strings"]
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(cfg.enable, vec![PassId::Flow]);
|
||||
assert_eq!(cfg.disable, vec![PassId::Strings]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_name_style_in_rename_section() {
|
||||
let cfg = build_from("[rename]\nclass_style = \"il-o0\"\n").unwrap();
|
||||
assert_eq!(cfg.rename.class_style, NameStyle::IlO0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_condy_flag_in_strings_section() {
|
||||
let cfg = build_from("[strings]\ncondy = true\n").unwrap();
|
||||
assert!(cfg.strings.condy);
|
||||
assert!(!Config::default().strings.condy);
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
//! Конфигурация обфускации: профили, список пассов, keep-правила.
|
||||
|
||||
mod from_toml;
|
||||
pub mod sections;
|
||||
mod toml_lite;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
|
@ -146,9 +148,10 @@ impl Config {
|
|||
self.profile.baseline().contains(&id)
|
||||
}
|
||||
|
||||
/// Загрузить конфиг из TOML.
|
||||
/// Загрузить конфиг из TOML (наш подмножественный парсер, см. `toml_lite`).
|
||||
pub fn from_toml(text: &str) -> Result<Self, CoreError> {
|
||||
toml::from_str(text).map_err(|e| CoreError::Config(e.to_string()))
|
||||
let table = toml_lite::parse(text).map_err(|e| CoreError::Config(e.to_string()))?;
|
||||
from_toml::build(&table).map_err(CoreError::Config)
|
||||
}
|
||||
|
||||
/// Нормализовать значения, которые легко задать нулем/пустым списком.
|
||||
|
|
|
|||
|
|
@ -79,6 +79,12 @@ pub struct StringsConfig {
|
|||
/// Derive the key from KDF(class, method, index) instead of placing it
|
||||
/// next to the ciphertext.
|
||||
pub derive_key: bool,
|
||||
/// Emit strings as `CONSTANT_Dynamic` (condy) resolved by the decoder's
|
||||
/// bootstrap method instead of direct `d(cipher, salt)` calls. A
|
||||
/// concrete-execution deobfuscator cannot follow the condy resolution
|
||||
/// (todo.md, 2026-10-14: SSVM made 0 changes on a condy-only class).
|
||||
/// Requires classfile >= 55; older classes keep direct calls.
|
||||
pub condy: bool,
|
||||
}
|
||||
|
||||
impl Default for StringsConfig {
|
||||
|
|
@ -89,6 +95,7 @@ impl Default for StringsConfig {
|
|||
split_above: 64,
|
||||
skip: Vec::new(),
|
||||
derive_key: true,
|
||||
condy: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -124,7 +131,8 @@ impl Default for FlowConfig {
|
|||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct ConstantsConfig {
|
||||
/// Splitting schemes (`xor`, `add`, `shift`), picked by RNG per constant.
|
||||
/// Splitting schemes (`xor`, `add`, `shift`, `mba`), picked by RNG per
|
||||
/// constant.
|
||||
pub schemes: Vec<String>,
|
||||
/// Insert opaque predicates (at most this many per method).
|
||||
pub max_predicates: usize,
|
||||
|
|
@ -135,7 +143,12 @@ pub struct ConstantsConfig {
|
|||
impl Default for ConstantsConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
schemes: vec!["xor".to_string(), "add".to_string(), "shift".to_string()],
|
||||
schemes: vec![
|
||||
"xor".to_string(),
|
||||
"add".to_string(),
|
||||
"shift".to_string(),
|
||||
"mba".to_string(),
|
||||
],
|
||||
max_predicates: 1,
|
||||
skip: Vec::new(),
|
||||
}
|
||||
|
|
|
|||
218
crates/noxis-core/src/config/toml_lite/mod.rs
Normal file
218
crates/noxis-core/src/config/toml_lite/mod.rs
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
//! Hand-rolled parser for the small TOML subset `Config` actually uses.
|
||||
//!
|
||||
//! We don't need full TOML (dates, multi-line strings, inline tables,
|
||||
//! arrays-of-tables): every fixture and hand-written config in this repo
|
||||
//! is flat `key = value` pairs grouped by `[section]` / `[section.sub]`
|
||||
//! headers, with scalars limited to strings, integers, bools and arrays
|
||||
//! of those. Replacing the `toml` crate with the full spec would be far
|
||||
//! more code than this project needs; this covers exactly the syntax
|
||||
//! `Config::from_toml` has ever been asked to parse (see
|
||||
//! `crates/noxis-core/src/config/from_toml.rs` for the struct-population
|
||||
//! side), erroring out loudly on anything else instead of guessing.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt;
|
||||
|
||||
/// A parsed scalar or compound TOML-subset value.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
String(String),
|
||||
Integer(i64),
|
||||
Bool(bool),
|
||||
Array(Vec<Value>),
|
||||
Table(Table),
|
||||
}
|
||||
|
||||
/// A `[section]` body: key → value, in the order keys were written.
|
||||
pub type Table = BTreeMap<String, Value>;
|
||||
|
||||
/// A parse failure, with a human-readable message (no line/col tracking -
|
||||
/// configs here are short enough that the offending text is enough).
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ParseError(pub String);
|
||||
|
||||
impl fmt::Display for ParseError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
fn err(msg: impl Into<String>) -> ParseError {
|
||||
ParseError(msg.into())
|
||||
}
|
||||
|
||||
/// Parse TOML-subset source text into a root [`Table`].
|
||||
pub fn parse(text: &str) -> Result<Table, ParseError> {
|
||||
let mut root: Table = Table::new();
|
||||
let mut path: Vec<String> = Vec::new();
|
||||
|
||||
for raw_line in text.lines() {
|
||||
let line = strip_comment(raw_line).trim();
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if let Some(header) = line.strip_prefix('[') {
|
||||
let header = header
|
||||
.strip_suffix(']')
|
||||
.ok_or_else(|| err(format!("незакрытый заголовок секции: `{raw_line}`")))?;
|
||||
path = header
|
||||
.split('.')
|
||||
.map(|seg| seg.trim().to_string())
|
||||
.collect();
|
||||
if path.iter().any(|seg| seg.is_empty()) {
|
||||
return Err(err(format!("пустой сегмент в заголовке: `{raw_line}`")));
|
||||
}
|
||||
ensure_table_path(&mut root, &path)?;
|
||||
continue;
|
||||
}
|
||||
let eq = find_top_level_eq(line)
|
||||
.ok_or_else(|| err(format!("ожидалось `key = value`: `{raw_line}`")))?;
|
||||
let key = line[..eq].trim();
|
||||
if key.is_empty() {
|
||||
return Err(err(format!("пустой ключ: `{raw_line}`")));
|
||||
}
|
||||
let value = parse_value(line[eq + 1..].trim())?;
|
||||
let table = table_at_path(&mut root, &path)?;
|
||||
table.insert(key.to_string(), value);
|
||||
}
|
||||
|
||||
Ok(root)
|
||||
}
|
||||
|
||||
/// Drop a `#`-started comment, unless the `#` is inside a quoted string.
|
||||
fn strip_comment(line: &str) -> &str {
|
||||
let mut in_string = false;
|
||||
for (i, c) in line.char_indices() {
|
||||
match c {
|
||||
'"' => in_string = !in_string,
|
||||
'#' if !in_string => return &line[..i],
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
line
|
||||
}
|
||||
|
||||
/// Find the first `=` that isn't inside a quoted string.
|
||||
fn find_top_level_eq(line: &str) -> Option<usize> {
|
||||
let mut in_string = false;
|
||||
for (i, c) in line.char_indices() {
|
||||
match c {
|
||||
'"' => in_string = !in_string,
|
||||
'=' if !in_string => return Some(i),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn ensure_table_path(root: &mut Table, path: &[String]) -> Result<(), ParseError> {
|
||||
let mut cur = root;
|
||||
for seg in path {
|
||||
let entry = cur
|
||||
.entry(seg.clone())
|
||||
.or_insert_with(|| Value::Table(Table::new()));
|
||||
match entry {
|
||||
Value::Table(t) => cur = t,
|
||||
_ => return Err(err(format!("`{seg}` уже используется как не-секция"))),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn table_at_path<'a>(root: &'a mut Table, path: &[String]) -> Result<&'a mut Table, ParseError> {
|
||||
let mut cur = root;
|
||||
for seg in path {
|
||||
match cur.get_mut(seg.as_str()) {
|
||||
Some(Value::Table(t)) => cur = t,
|
||||
_ => return Err(err(format!("секция `{seg}` не найдена"))),
|
||||
}
|
||||
}
|
||||
Ok(cur)
|
||||
}
|
||||
|
||||
fn parse_value(s: &str) -> Result<Value, ParseError> {
|
||||
let s = s.trim();
|
||||
if let Some(inner) = s.strip_prefix('"') {
|
||||
let inner = inner
|
||||
.strip_suffix('"')
|
||||
.ok_or_else(|| err(format!("незакрытая строка: `{s}`")))?;
|
||||
return Ok(Value::String(unescape(inner)?));
|
||||
}
|
||||
if let Some(inner) = s.strip_prefix('[') {
|
||||
let inner = inner
|
||||
.strip_suffix(']')
|
||||
.ok_or_else(|| err(format!("незакрытый массив: `{s}`")))?;
|
||||
return Ok(Value::Array(parse_array_elements(inner)?));
|
||||
}
|
||||
match s {
|
||||
"true" => return Ok(Value::Bool(true)),
|
||||
"false" => return Ok(Value::Bool(false)),
|
||||
_ => {}
|
||||
}
|
||||
s.parse::<i64>()
|
||||
.map(Value::Integer)
|
||||
.map_err(|_| err(format!("неподдерживаемое значение: `{s}`")))
|
||||
}
|
||||
|
||||
fn parse_array_elements(inner: &str) -> Result<Vec<Value>, ParseError> {
|
||||
let mut out = Vec::new();
|
||||
let mut depth = 0i32;
|
||||
let mut in_string = false;
|
||||
let mut start = 0usize;
|
||||
let bytes = inner.as_bytes();
|
||||
let push = |out: &mut Vec<Value>, chunk: &str| -> Result<(), ParseError> {
|
||||
let chunk = chunk.trim();
|
||||
if chunk.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
out.push(parse_value(chunk)?);
|
||||
Ok(())
|
||||
};
|
||||
for (i, c) in inner.char_indices() {
|
||||
match c {
|
||||
'"' => in_string = !in_string,
|
||||
'[' if !in_string => depth += 1,
|
||||
']' if !in_string => depth -= 1,
|
||||
',' if !in_string && depth == 0 => {
|
||||
push(&mut out, &inner[start..i])?;
|
||||
start = i + 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if start < bytes.len() {
|
||||
push(&mut out, &inner[start..])?;
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Basic escapes actually seen in configs: `\"`, `\\`, `\n`, `\t`.
|
||||
fn unescape(s: &str) -> Result<String, ParseError> {
|
||||
if !s.contains('\\') {
|
||||
return Ok(s.to_string());
|
||||
}
|
||||
let mut out = String::with_capacity(s.len());
|
||||
let mut chars = s.chars();
|
||||
while let Some(c) = chars.next() {
|
||||
if c != '\\' {
|
||||
out.push(c);
|
||||
continue;
|
||||
}
|
||||
match chars.next() {
|
||||
Some('"') => out.push('"'),
|
||||
Some('\\') => out.push('\\'),
|
||||
Some('n') => out.push('\n'),
|
||||
Some('t') => out.push('\t'),
|
||||
other => {
|
||||
return Err(err(format!(
|
||||
"неподдерживаемый escape-символ: `\\{}`",
|
||||
other.unwrap_or(' ')
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
83
crates/noxis-core/src/config/toml_lite/tests.rs
Normal file
83
crates/noxis-core/src/config/toml_lite/tests.rs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
//! Tests for the TOML-subset parser.
|
||||
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parses_flat_pairs() {
|
||||
let t = parse("name = \"x\"\ncount = 4\nflag = true\n").unwrap();
|
||||
assert_eq!(t.get("name"), Some(&Value::String("x".into())));
|
||||
assert_eq!(t.get("count"), Some(&Value::Integer(4)));
|
||||
assert_eq!(t.get("flag"), Some(&Value::Bool(true)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_negative_integer() {
|
||||
let t = parse("x = -12").unwrap();
|
||||
assert_eq!(t.get("x"), Some(&Value::Integer(-12)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_section_header() {
|
||||
let t = parse("[rename]\npackage_pool = 4\n").unwrap();
|
||||
let Some(Value::Table(section)) = t.get("rename") else {
|
||||
panic!("expected table");
|
||||
};
|
||||
assert_eq!(section.get("package_pool"), Some(&Value::Integer(4)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_nested_section_header() {
|
||||
let t = parse("[a.b]\nx = 1\n").unwrap();
|
||||
let Some(Value::Table(a)) = t.get("a") else {
|
||||
panic!("expected table a")
|
||||
};
|
||||
let Some(Value::Table(b)) = a.get("b") else {
|
||||
panic!("expected table b")
|
||||
};
|
||||
assert_eq!(b.get("x"), Some(&Value::Integer(1)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_string_array() {
|
||||
let t = parse(r#"keep = ["com/example/Main", "*Service"]"#).unwrap();
|
||||
assert_eq!(
|
||||
t.get("keep"),
|
||||
Some(&Value::Array(vec![
|
||||
Value::String("com/example/Main".into()),
|
||||
Value::String("*Service".into()),
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_empty_array() {
|
||||
let t = parse("keep = []").unwrap();
|
||||
assert_eq!(t.get("keep"), Some(&Value::Array(vec![])));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skips_comments_and_blank_lines() {
|
||||
let t = parse("# comment\n\nx = 1 # trailing\n").unwrap();
|
||||
assert_eq!(t.get("x"), Some(&Value::Integer(1)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_inside_string_is_not_a_comment() {
|
||||
let t = parse(r#"x = "a#b""#).unwrap();
|
||||
assert_eq!(t.get("x"), Some(&Value::String("a#b".into())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_float() {
|
||||
assert!(parse("x = 1.5").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_garbage_line() {
|
||||
assert!(parse("not a key value line").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_unterminated_string() {
|
||||
assert!(parse("x = \"unterminated").is_err());
|
||||
}
|
||||
|
|
@ -1,8 +1,16 @@
|
|||
//! Constant-splitting schemes and opaque predicates.
|
||||
//!
|
||||
//! Integer constants outside the `iconst` range are replaced by triples
|
||||
//! (`x^k`, `a+b`, shift); an opaque predicate is an always-true condition with
|
||||
//! a one-`nop` dead branch (valid, never executed, breaks linear disassembly).
|
||||
//! (`x^k`, `a+b`, shift) or the `mba` form below; an opaque predicate is an
|
||||
//! always-true condition with a one-`nop` dead branch (valid, never
|
||||
//! executed, breaks linear disassembly).
|
||||
//!
|
||||
//! None of this survives a real constant-propagation pass (every operand is
|
||||
//! a literal, so a general evaluator folds the whole expression regardless
|
||||
//! of depth) - that is a known, documented limit, not a claim otherwise.
|
||||
//! `mba` raises the bar against a *pattern* matcher instead: `xor`/`add`
|
||||
//! hardcode one opcode triple as "this is a split constant", `mba` needs the
|
||||
//! `(a&b)+(a|b) = a+b` identity specifically.
|
||||
|
||||
use noxis_classfile::{Arith, BranchCond, Cond, Const, Insn, Prim};
|
||||
use noxis_core::Rng;
|
||||
|
|
@ -13,6 +21,7 @@ pub(super) enum Scheme {
|
|||
Xor,
|
||||
Add,
|
||||
Shift,
|
||||
Mba,
|
||||
}
|
||||
|
||||
impl Scheme {
|
||||
|
|
@ -21,6 +30,7 @@ impl Scheme {
|
|||
"xor" => Some(Scheme::Xor),
|
||||
"add" => Some(Scheme::Add),
|
||||
"shift" => Some(Scheme::Shift),
|
||||
"mba" => Some(Scheme::Mba),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
@ -36,7 +46,7 @@ pub(super) fn encrypt_int(value: i32, scheme: Scheme, rng: &mut Rng) -> Vec<Insn
|
|||
}
|
||||
(value ^ key, key)
|
||||
}
|
||||
Scheme::Add => {
|
||||
Scheme::Add | Scheme::Mba => {
|
||||
let part = rng.next_u64() as i32;
|
||||
(part, value.wrapping_sub(part))
|
||||
}
|
||||
|
|
@ -46,6 +56,7 @@ pub(super) fn encrypt_int(value: i32, scheme: Scheme, rng: &mut Rng) -> Vec<Insn
|
|||
Scheme::Xor => binary(first, second, Arith::Xor, Prim::Int),
|
||||
Scheme::Add => binary(first, second, Arith::Add, Prim::Int),
|
||||
Scheme::Shift => shift_form(value),
|
||||
Scheme::Mba => mba_form(first, second),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -59,7 +70,7 @@ pub(super) fn encrypt_long(value: i64, scheme: Scheme, rng: &mut Rng) -> Vec<Ins
|
|||
}
|
||||
(value ^ key, key)
|
||||
}
|
||||
Scheme::Add => {
|
||||
Scheme::Add | Scheme::Mba => {
|
||||
let part = rng.next_u64() as i64;
|
||||
(part, value.wrapping_sub(part))
|
||||
}
|
||||
|
|
@ -69,6 +80,7 @@ pub(super) fn encrypt_long(value: i64, scheme: Scheme, rng: &mut Rng) -> Vec<Ins
|
|||
Scheme::Xor => binary_long(first, second, Arith::Xor),
|
||||
Scheme::Add => binary_long(first, second, Arith::Add),
|
||||
Scheme::Shift => shift_form_long(value),
|
||||
Scheme::Mba => mba_form_long(first, second),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -92,6 +104,53 @@ fn binary_long(first: i64, second: i64, op: Arith) -> Vec<Insn> {
|
|||
]
|
||||
}
|
||||
|
||||
/// MBA identity: `a+b == (a&b) + (a|b)` (exact, bit-exact under wrapping).
|
||||
/// `a`/`b` are pushed twice (no stack juggling needed) so each sub-expression
|
||||
/// stays a plain, correct `and`/`or` - the shape, not a single opcode, is
|
||||
/// what a matcher has to recognize.
|
||||
fn mba_form(a: i32, b: i32) -> Vec<Insn> {
|
||||
vec![
|
||||
Insn::Const(Const::Int(a)),
|
||||
Insn::Const(Const::Int(b)),
|
||||
Insn::Binary {
|
||||
prim: Prim::Int,
|
||||
op: Arith::And,
|
||||
},
|
||||
Insn::Const(Const::Int(a)),
|
||||
Insn::Const(Const::Int(b)),
|
||||
Insn::Binary {
|
||||
prim: Prim::Int,
|
||||
op: Arith::Or,
|
||||
},
|
||||
Insn::Binary {
|
||||
prim: Prim::Int,
|
||||
op: Arith::Add,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
/// The same for `long`.
|
||||
fn mba_form_long(a: i64, b: i64) -> Vec<Insn> {
|
||||
vec![
|
||||
Insn::Const(Const::Long(a)),
|
||||
Insn::Const(Const::Long(b)),
|
||||
Insn::Binary {
|
||||
prim: Prim::Long,
|
||||
op: Arith::And,
|
||||
},
|
||||
Insn::Const(Const::Long(a)),
|
||||
Insn::Const(Const::Long(b)),
|
||||
Insn::Binary {
|
||||
prim: Prim::Long,
|
||||
op: Arith::Or,
|
||||
},
|
||||
Insn::Binary {
|
||||
prim: Prim::Long,
|
||||
op: Arith::Add,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
/// `v = ((v >> 1) << 1) | (v & 1)` - works for any value, including negatives.
|
||||
fn shift_form(value: i32) -> Vec<Insn> {
|
||||
vec![
|
||||
|
|
|
|||
|
|
@ -30,6 +30,30 @@ fn eval_int(insns: &[Insn]) -> i32 {
|
|||
Insn::Const(Const::Int(b)),
|
||||
Insn::Binary { .. },
|
||||
] => (a << 1) | (b & 1),
|
||||
// MBA: `(a&b) + (a|b) == a+b`. Each sub-expression is still a
|
||||
// foldable `LDC,LDC,BINOP` triple - a generic constant evaluator
|
||||
// still resolves this instantly. What it defeats is a *pattern*
|
||||
// matcher hardcoded to one opcode (`ldc,ldc,ixor` = "this is a
|
||||
// split constant"): it would need to recognize the `and+or+add`
|
||||
// identity specifically, not just fold a literal pair.
|
||||
[
|
||||
Insn::Const(Const::Int(a1)),
|
||||
Insn::Const(Const::Int(b1)),
|
||||
Insn::Binary {
|
||||
op: noxis_classfile::Arith::And,
|
||||
..
|
||||
},
|
||||
Insn::Const(Const::Int(a2)),
|
||||
Insn::Const(Const::Int(b2)),
|
||||
Insn::Binary {
|
||||
op: noxis_classfile::Arith::Or,
|
||||
..
|
||||
},
|
||||
Insn::Binary {
|
||||
op: noxis_classfile::Arith::Add,
|
||||
..
|
||||
},
|
||||
] if a1 == a2 && b1 == b2 => (a1 & b1).wrapping_add(a1 | b1),
|
||||
_ => panic!("not a form: {insns:?}"),
|
||||
}
|
||||
}
|
||||
|
|
@ -38,7 +62,7 @@ fn eval_int(insns: &[Insn]) -> i32 {
|
|||
fn int_forms_round_trip() {
|
||||
let mut rng = noxis_core::Rng::from_seed(11);
|
||||
for value in [-1000, -7, -6, 6, 7, 42, 65536, i32::MAX, i32::MIN] {
|
||||
for scheme in [Scheme::Xor, Scheme::Add, Scheme::Shift] {
|
||||
for scheme in [Scheme::Xor, Scheme::Add, Scheme::Shift, Scheme::Mba] {
|
||||
assert_eq!(eval_int(&encrypt_int(value, scheme, &mut rng)), value);
|
||||
}
|
||||
}
|
||||
|
|
@ -47,7 +71,7 @@ fn int_forms_round_trip() {
|
|||
#[test]
|
||||
fn long_forms_keep_shape() {
|
||||
let mut rng = noxis_core::Rng::from_seed(12);
|
||||
for scheme in [Scheme::Xor, Scheme::Add, Scheme::Shift] {
|
||||
for scheme in [Scheme::Xor, Scheme::Add, Scheme::Shift, Scheme::Mba] {
|
||||
let insns = encrypt_long(1_000_000_000_000, scheme, &mut rng);
|
||||
assert!(matches!(
|
||||
insns.last(),
|
||||
|
|
@ -59,6 +83,34 @@ fn long_forms_keep_shape() {
|
|||
}
|
||||
}
|
||||
|
||||
/// MBA shape: `and`/`or`/`add` on two reused operands, not `xor`/`add` on a
|
||||
/// single pair - a matcher hardcoded to one opcode triple (`ldc,ldc,ixor`)
|
||||
/// doesn't recognize this as "a split constant" without the identity.
|
||||
#[test]
|
||||
fn mba_form_uses_and_or_add_not_a_single_binop() {
|
||||
let mut rng = noxis_core::Rng::from_seed(13);
|
||||
for value in [-1000, -6, 6, 42, 65536, i32::MAX, i32::MIN] {
|
||||
let insns = encrypt_int(value, Scheme::Mba, &mut rng);
|
||||
let ops: Vec<_> = insns
|
||||
.iter()
|
||||
.filter_map(|i| match i {
|
||||
Insn::Binary { op, .. } => Some(*op),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(
|
||||
ops,
|
||||
[
|
||||
noxis_classfile::Arith::And,
|
||||
noxis_classfile::Arith::Or,
|
||||
noxis_classfile::Arith::Add,
|
||||
],
|
||||
"unexpected mba shape: {insns:?}"
|
||||
);
|
||||
assert_eq!(insns.len(), 7, "unexpected mba insn count: {insns:?}");
|
||||
}
|
||||
}
|
||||
|
||||
fn sample() -> Program {
|
||||
let code = Code {
|
||||
max_stack: 1,
|
||||
|
|
|
|||
|
|
@ -120,11 +120,12 @@ pub(super) const DECOY_RIGHT: &[&str] = &[
|
|||
|
||||
/// `Plausible` style word bank. Mixes domain-neutral software nouns with
|
||||
/// terms that are boring, structural scaffolding in the client/mod
|
||||
/// ecosystem noxis is actually used on (see
|
||||
/// `/storage/project/jvm/LoVisual/ref/clients/*`: `Module`, `Setting`,
|
||||
/// `Event`, `Mixin`, `Manager`, `Component`, `Accessor`, `Hud`, `Overlay`,
|
||||
/// `Theme`, `Timer`, `Animation`, `Rotation` all show up dozens of times
|
||||
/// across real client codebases). Deliberately excludes anything that
|
||||
/// ecosystem noxis is actually used on (surveyed against decompiled
|
||||
/// reference client/mod sources, not shipped in this repo: `Module`,
|
||||
/// `Setting`, `Event`, `Mixin`, `Manager`, `Component`, `Accessor`, `Hud`,
|
||||
/// `Overlay`, `Theme`, `Timer`, `Animation`, `Rotation` all show up dozens
|
||||
/// of times across real client codebases). Deliberately excludes anything
|
||||
/// that
|
||||
/// itself reads as a "feature flag" worth an analyst's attention (no
|
||||
/// `Esp`/`Aimbot`/`KillAura`/`Chams`): a decoy name that announces
|
||||
/// "interesting code lives here" defeats the point of blending in.
|
||||
|
|
|
|||
76
crates/noxis-passes/src/strings/condy.rs
Normal file
76
crates/noxis-passes/src/strings/condy.rs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
//! Condensed emission (`CONSTANT_Dynamic`): the JVM resolves the string
|
||||
//! itself through the decoder's bootstrap method instead of us calling `d`.
|
||||
//!
|
||||
//! Why: a concrete-execution deobfuscator (narumii/SSVM, todo.md 2026-10-14)
|
||||
//! follows `invokestatic d` and recovers the plaintext, but made **0
|
||||
//! changes** on a condy-only class - it does not resolve `CONSTANT_Dynamic`
|
||||
//! at all. The plaintext then lives only inside JVM constant caching.
|
||||
|
||||
use noxis_classfile::{Attribute, BootstrapMethod, ClassFile, Const, Insn};
|
||||
use noxis_runtime::{BOOTSTRAP_DESC, BOOTSTRAP_METHOD};
|
||||
|
||||
/// `REF_invokeStatic` (JVMS §5.4.3.5).
|
||||
const INVOKE_STATIC: u8 = 6;
|
||||
|
||||
/// Class-file version introducing `CONSTANT_Dynamic` (Java 11).
|
||||
pub(super) const CONDY_MIN_MAJOR: u16 = 55;
|
||||
|
||||
/// The class supports condy constants at all.
|
||||
pub(super) fn supported(class: &ClassFile) -> bool {
|
||||
class.version.major >= CONDY_MIN_MAJOR
|
||||
}
|
||||
|
||||
/// Where new bootstrap entries would start: right after the existing ones
|
||||
/// (Kotlin lambdas etc. already own the front of the attribute).
|
||||
pub(super) fn base_index(class: &ClassFile) -> u16 {
|
||||
class
|
||||
.attributes
|
||||
.iter()
|
||||
.find_map(|attribute| match attribute {
|
||||
Attribute::BootstrapMethods(list) => Some(list.len() as u16),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// One bootstrap entry per chunk: the decoder's `b` with `cipher` + `salt`
|
||||
/// as static arguments. The BSM prefix (`lookup`, `name`, `type`) is filled
|
||||
/// in by the JVM (JVMS §5.4.3.5).
|
||||
pub(super) fn entry(owner: &str, cipher: &str, salt: i32) -> BootstrapMethod {
|
||||
BootstrapMethod {
|
||||
kind: INVOKE_STATIC,
|
||||
owner: owner.to_string(),
|
||||
name: BOOTSTRAP_METHOD.to_string(),
|
||||
descriptor: BOOTSTRAP_DESC.to_string(),
|
||||
itf: false,
|
||||
args: vec![Const::String(cipher.to_string()), Const::Int(salt)],
|
||||
}
|
||||
}
|
||||
|
||||
/// Condensed replacements for one string's chunks: a `ldc` per chunk
|
||||
/// (bootstrap indices `base..base+n`) joined by `String.concat`.
|
||||
pub(super) fn sequence(base: u16, chunks: usize) -> Vec<Insn> {
|
||||
let mut out = Vec::with_capacity(chunks * 2);
|
||||
for position in 0..chunks {
|
||||
out.push(Insn::Const(Const::Dynamic {
|
||||
name: "_".to_string(),
|
||||
descriptor: "Ljava/lang/String;".to_string(),
|
||||
bootstrap: base + position as u16,
|
||||
}));
|
||||
if position > 0 {
|
||||
out.push(concat());
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// `String.concat` - the same chunk join the direct path uses.
|
||||
fn concat() -> Insn {
|
||||
Insn::Invoke {
|
||||
kind: noxis_classfile::InvokeKind::Virtual,
|
||||
owner: "java/lang/String".to_string(),
|
||||
name: "concat".to_string(),
|
||||
descriptor: "(Ljava/lang/String;)Ljava/lang/String;".to_string(),
|
||||
itf: false,
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@
|
|||
//! (KDF or RNG stream - see `derive_key`). The decoder is generated once per
|
||||
//! program and only if at least one string was encrypted.
|
||||
|
||||
mod condy;
|
||||
mod crypt;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -12,7 +13,9 @@ mod tests;
|
|||
use std::collections::BTreeMap;
|
||||
|
||||
use noxis_analysis::{ClassHierarchy, JdkHierarchy};
|
||||
use noxis_classfile::{Const, Insn, MAX_METHOD_INSNS, Program, compute_frames};
|
||||
use noxis_classfile::{
|
||||
Attribute, BootstrapMethod, Const, Insn, MAX_METHOD_INSNS, Program, compute_frames,
|
||||
};
|
||||
use noxis_core::{Context, CoreError, NameStyle, Pass, PassId, PassReport, derive_seed};
|
||||
use noxis_runtime::{Scheme, decoder_class, decoder_owner, encrypt};
|
||||
|
||||
|
|
@ -69,6 +72,17 @@ impl Pass for StringsPass {
|
|||
let owner = decoder_name(ctx, program);
|
||||
let mut chunk_counter = 0usize;
|
||||
let mut salt_counter = 0u64;
|
||||
// Condy mode: per-class next bootstrap index + accumulated entries
|
||||
// (appended to the class attribute after the rewrite below).
|
||||
let mut bsm_next: BTreeMap<usize, u16> = BTreeMap::new();
|
||||
let mut bsm_entries: BTreeMap<usize, Vec<BootstrapMethod>> = BTreeMap::new();
|
||||
if cfg.condy {
|
||||
for (ci, class) in program.classes.iter().enumerate() {
|
||||
if condy::supported(class) {
|
||||
bsm_next.insert(ci, condy::base_index(class));
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut replacements: BTreeMap<(usize, usize, usize), Vec<Insn>> = BTreeMap::new();
|
||||
for (ci, mi, ii, text) in &targets {
|
||||
let mut parts = Vec::new();
|
||||
|
|
@ -80,7 +94,19 @@ impl Pass for StringsPass {
|
|||
parts.push((blob.cipher, salt));
|
||||
chunk_counter += 1;
|
||||
}
|
||||
replacements.insert((*ci, *mi, *ii), decode_sequence(&owner, &parts));
|
||||
if let Some(next) = bsm_next.get_mut(ci) {
|
||||
let base = *next;
|
||||
for (cipher, salt) in &parts {
|
||||
bsm_entries
|
||||
.entry(*ci)
|
||||
.or_default()
|
||||
.push(condy::entry(&owner, cipher, *salt));
|
||||
}
|
||||
*next = base + parts.len() as u16;
|
||||
replacements.insert((*ci, *mi, *ii), condy::sequence(base, parts.len()));
|
||||
} else {
|
||||
replacements.insert((*ci, *mi, *ii), decode_sequence(&owner, &parts));
|
||||
}
|
||||
}
|
||||
let mut mutated: Vec<(usize, usize)> = Vec::new();
|
||||
let mut applied = 0usize;
|
||||
|
|
@ -126,6 +152,21 @@ impl Pass for StringsPass {
|
|||
decoder_class(&owner, master, &scheme_names).map_err(|e| pass_error(e.to_string()))?;
|
||||
program.classes.push(decoder);
|
||||
ctx.note(format!("string decoder: {owner}"));
|
||||
// Phase 3b: condy bootstrap entries land on their classes (appended
|
||||
// after existing lambdas - the indices computed above assumed that).
|
||||
for (ci, entries) in bsm_entries {
|
||||
let class = program
|
||||
.classes
|
||||
.get_mut(ci)
|
||||
.ok_or_else(|| pass_error("class vanished"))?;
|
||||
match class.attributes.iter_mut().find_map(|a| match a {
|
||||
Attribute::BootstrapMethods(list) => Some(list),
|
||||
_ => None,
|
||||
}) {
|
||||
Some(list) => list.extend(entries),
|
||||
None => class.attributes.push(Attribute::BootstrapMethods(entries)),
|
||||
}
|
||||
}
|
||||
// Phase 4: frames of the touched methods.
|
||||
let hierarchy = ClassHierarchy::build(program);
|
||||
let frames_hierarchy = JdkHierarchy::wrap(&hierarchy);
|
||||
|
|
|
|||
201
crates/noxis-passes/src/strings/tests/condy.rs
Normal file
201
crates/noxis-passes/src/strings/tests/condy.rs
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
//! Condy-mode tests: `CONSTANT_Dynamic` emission, version gate, attribute
|
||||
//! merging.
|
||||
|
||||
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
||||
|
||||
use noxis_classfile::{Attribute, BootstrapMethod, Const, Insn, Version};
|
||||
use noxis_core::{Config, Context, Pass, StringsConfig};
|
||||
use noxis_runtime::{Scheme, encrypt};
|
||||
|
||||
use super::super::StringsPass;
|
||||
use super::sample;
|
||||
|
||||
/// Condensed mode: strings become `CONSTANT_Dynamic` resolved by the
|
||||
/// decoder's bootstrap method - a concrete-execution deobfuscator sees no
|
||||
/// decrypt call to follow (todo.md, 2026-10-14: 0 condy changes from SSVM).
|
||||
#[test]
|
||||
fn condy_mode_emits_dynamic_constants() {
|
||||
let mut program = sample();
|
||||
let config = Config {
|
||||
seed: Some(7),
|
||||
strings: StringsConfig {
|
||||
condy: true,
|
||||
..StringsConfig::default()
|
||||
},
|
||||
..Config::default()
|
||||
};
|
||||
let mut ctx = Context::new(config);
|
||||
let report = StringsPass.apply(&mut ctx, &mut program).expect("strings");
|
||||
|
||||
assert_eq!(report.transformed, 2);
|
||||
let class = &program.classes[0];
|
||||
let code = class.methods[0].code.as_ref().expect("code");
|
||||
// Both encrypted strings are condy constants, no direct decoder calls.
|
||||
let dynamics: Vec<&Const> = code
|
||||
.insns
|
||||
.iter()
|
||||
.filter_map(|insn| match insn {
|
||||
Insn::Const(c @ Const::Dynamic { .. }) => Some(c),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(dynamics.len(), 3, "1 chunk + 2 chunks: {code:?}");
|
||||
for value in &dynamics {
|
||||
let Const::Dynamic { descriptor, .. } = value else {
|
||||
unreachable!()
|
||||
};
|
||||
assert_eq!(descriptor, "Ljava/lang/String;");
|
||||
}
|
||||
assert!(
|
||||
!code
|
||||
.insns
|
||||
.iter()
|
||||
.any(|insn| matches!(insn, Insn::Invoke { name, .. } if name == "d")),
|
||||
"condy mode must not emit direct decoder calls: {code:?}"
|
||||
);
|
||||
// The bootstrap entries live on the class and point at the decoder's `b`.
|
||||
let decoder = program
|
||||
.classes
|
||||
.iter()
|
||||
.find(|c| c.methods.iter().any(|m| m.name == "b"))
|
||||
.expect("decoder with bootstrap method");
|
||||
let Attribute::BootstrapMethods(bsm) = class
|
||||
.attributes
|
||||
.iter()
|
||||
.find(|a| matches!(a, Attribute::BootstrapMethods(_)))
|
||||
.expect("BootstrapMethods attribute")
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
assert_eq!(bsm.len(), 3, "one entry per chunk");
|
||||
for entry in bsm {
|
||||
assert_eq!(entry.kind, 6, "REF_invokeStatic");
|
||||
assert_eq!(entry.owner, decoder.this_class);
|
||||
assert_eq!(entry.name, "b");
|
||||
assert!(
|
||||
matches!(entry.args.as_slice(), [Const::String(_), Const::Int(_)]),
|
||||
"args are cipher + salt: {:?}",
|
||||
entry.args
|
||||
);
|
||||
}
|
||||
// Bootstrap indices in the constants match the entry positions.
|
||||
let indices: Vec<u16> = dynamics
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let Const::Dynamic { bootstrap, .. } = c else {
|
||||
unreachable!()
|
||||
};
|
||||
*bootstrap
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(indices, vec![0, 1, 2]);
|
||||
// Ciphertext is still the direct encryption of the originals.
|
||||
let schemes = [Scheme::XorSplitmix, Scheme::XteaCtr];
|
||||
let originals = ["hello world".to_string(), "y".repeat(50), "y".repeat(50)];
|
||||
let ciphers: Vec<String> = bsm
|
||||
.iter()
|
||||
.map(|e| match e.args[0] {
|
||||
Const::String(ref s) => s.clone(),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.collect();
|
||||
let salts: Vec<i32> = bsm
|
||||
.iter()
|
||||
.map(|e| match e.args[1] {
|
||||
Const::Int(s) => s,
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.collect();
|
||||
for ((cipher, salt), original) in ciphers.iter().zip(&salts).zip(originals) {
|
||||
let scheme = schemes[*salt as usize % schemes.len()];
|
||||
assert_eq!(cipher, &encrypt(scheme, 7, *salt, &original).cipher);
|
||||
}
|
||||
}
|
||||
|
||||
/// Condy is a Java 11+ feature (classfile 55): older classes keep the direct
|
||||
/// decoder calls even when the mode is on.
|
||||
#[test]
|
||||
fn condy_falls_back_for_old_class_versions() {
|
||||
let mut program = sample();
|
||||
program.classes[0].version = Version::JAVA_8;
|
||||
let config = Config {
|
||||
seed: Some(7),
|
||||
strings: StringsConfig {
|
||||
condy: true,
|
||||
..StringsConfig::default()
|
||||
},
|
||||
..Config::default()
|
||||
};
|
||||
let mut ctx = Context::new(config);
|
||||
StringsPass.apply(&mut ctx, &mut program).expect("strings");
|
||||
|
||||
let code = program.classes[0].methods[0].code.as_ref().expect("code");
|
||||
assert!(
|
||||
code.insns
|
||||
.iter()
|
||||
.any(|insn| matches!(insn, Insn::Invoke { name, .. } if name == "d")),
|
||||
"pre-11 class must keep direct calls: {code:?}"
|
||||
);
|
||||
assert!(
|
||||
!code
|
||||
.insns
|
||||
.iter()
|
||||
.any(|insn| matches!(insn, Insn::Const(Const::Dynamic { .. }))),
|
||||
"no condy constants below classfile 55: {code:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// A class that already has `BootstrapMethods` (Kotlin lambdas) keeps its
|
||||
/// entries: new condy ones are appended and referenced by the right index.
|
||||
#[test]
|
||||
fn condy_appends_to_existing_bootstrap_methods() {
|
||||
let mut program = sample();
|
||||
let existing = BootstrapMethod {
|
||||
kind: 6,
|
||||
owner: "java/lang/invoke/StringConcatFactory".to_string(),
|
||||
name: "makeConcatWithConstants".to_string(),
|
||||
descriptor: "([Ljava/lang/Object;)Ljava/lang/String;".to_string(),
|
||||
itf: false,
|
||||
args: vec![Const::String("\u{1}".to_string())],
|
||||
};
|
||||
program.classes[0]
|
||||
.attributes
|
||||
.push(Attribute::BootstrapMethods(vec![existing]));
|
||||
let config = Config {
|
||||
seed: Some(7),
|
||||
strings: StringsConfig {
|
||||
condy: true,
|
||||
..StringsConfig::default()
|
||||
},
|
||||
..Config::default()
|
||||
};
|
||||
let mut ctx = Context::new(config);
|
||||
StringsPass.apply(&mut ctx, &mut program).expect("strings");
|
||||
|
||||
let class = &program.classes[0];
|
||||
let Attribute::BootstrapMethods(bsm) = class
|
||||
.attributes
|
||||
.iter()
|
||||
.find(|a| matches!(a, Attribute::BootstrapMethods(_)))
|
||||
.expect("BootstrapMethods attribute")
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
// 1 lambda entry + 3 chunk entries.
|
||||
assert_eq!(bsm.len(), 4);
|
||||
assert_eq!(bsm[0].name, "makeConcatWithConstants");
|
||||
let code = class.methods[0].code.as_ref().expect("code");
|
||||
let indices: Vec<u16> = code
|
||||
.insns
|
||||
.iter()
|
||||
.filter_map(|insn| match insn {
|
||||
Insn::Const(Const::Dynamic { bootstrap, .. }) => Some(*bootstrap),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(
|
||||
indices,
|
||||
vec![1, 2, 3],
|
||||
"condy entries start after the lambda"
|
||||
);
|
||||
}
|
||||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
||||
|
||||
mod condy;
|
||||
|
||||
use noxis_classfile::{ClassBuilder, Code, Const, Insn, MAX_METHOD_INSNS, Program, Version};
|
||||
use noxis_core::{Config, Context};
|
||||
use noxis_runtime::{Scheme, encrypt};
|
||||
|
|
@ -10,7 +12,7 @@ use super::StringsPass;
|
|||
use super::crypt::{decode_sequence, salt_for_scheme, split_chunks};
|
||||
use noxis_core::Pass;
|
||||
|
||||
fn sample() -> Program {
|
||||
pub(super) fn sample() -> Program {
|
||||
let code = Code {
|
||||
max_stack: 1,
|
||||
max_locals: 0,
|
||||
|
|
@ -31,6 +31,11 @@ use kdf::{Gen, c_int, c_long, c_str, call, load, store};
|
|||
pub const DECODER_METHOD: &str = "d";
|
||||
/// Дескриптор метода декодера.
|
||||
pub const DECODER_DESC: &str = "(Ljava/lang/String;I)Ljava/lang/String;";
|
||||
/// Имя bootstrap-метода для `CONSTANT_Dynamic` (condy-строки).
|
||||
pub const BOOTSTRAP_METHOD: &str = "b";
|
||||
/// Дескриптор bootstrap-метода: стандартный префикс BSM
|
||||
/// (`Lookup`, `name`, `type`) плюс наши аргументы `cipher` и `salt`.
|
||||
pub const BOOTSTRAP_DESC: &str = "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/String;I)Ljava/lang/String;";
|
||||
|
||||
/// Ошибка генерации рантайма.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
|
|
@ -78,9 +83,38 @@ pub fn decoder_class(
|
|||
major: 49,
|
||||
})
|
||||
.method_with_code(0x0009, DECODER_METHOD, DECODER_DESC, code)
|
||||
.method_with_code(
|
||||
0x0009,
|
||||
BOOTSTRAP_METHOD,
|
||||
BOOTSTRAP_DESC,
|
||||
bootstrap_code(internal_name),
|
||||
)
|
||||
.build())
|
||||
}
|
||||
|
||||
/// Тело bootstrap-метода: `b(lookup, name, type, cipher, salt) = d(cipher, salt)`.
|
||||
///
|
||||
/// JVM вызывает его сама при первом `ldc` condy-константы и кеширует
|
||||
/// результат - метод должен быть `public static` и возвращать значение
|
||||
/// дескриптора константы (`Ljava/lang/String;`).
|
||||
fn bootstrap_code(owner: &str) -> Code {
|
||||
Code {
|
||||
max_stack: 2,
|
||||
// Слоты 0-2 (lookup/name/type) заняты стандартным префиксом BSM,
|
||||
// cipher и salt живут в 3 и 4.
|
||||
max_locals: 5,
|
||||
insns: vec![
|
||||
load(3, SlotKind::Ref),
|
||||
load(4, SlotKind::Int),
|
||||
call(owner, DECODER_METHOD, DECODER_DESC, InvokeKind::Static),
|
||||
Insn::AReturn,
|
||||
],
|
||||
exception_table: Vec::new(),
|
||||
frames: Vec::new(),
|
||||
attributes: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Имя класса-декодера для прогона: детерминированно от сида, но выглядит
|
||||
/// как обычный обфусцированный класс.
|
||||
pub fn decoder_owner(seed_name: &str) -> String {
|
||||
|
|
|
|||
|
|
@ -6,5 +6,7 @@ pub mod codec;
|
|||
pub mod decoder;
|
||||
pub mod scheme;
|
||||
|
||||
pub use decoder::{DECODER_DESC, DECODER_METHOD, decoder_class, decoder_owner};
|
||||
pub use decoder::{
|
||||
BOOTSTRAP_DESC, BOOTSTRAP_METHOD, DECODER_DESC, DECODER_METHOD, decoder_class, decoder_owner,
|
||||
};
|
||||
pub use scheme::{Blob, Scheme, decrypt, encrypt};
|
||||
|
|
|
|||
132
crates/noxis-runtime/tests/exec_condy.rs
Normal file
132
crates/noxis-runtime/tests/exec_condy.rs
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
//! Condy-приёмка: шифруем в Rust, расшифровывает настоящая JVM через
|
||||
//! bootstrap-метод декодера (`CONSTANT_Dynamic`).
|
||||
//!
|
||||
//! Это приёмка приоритета 1 из todo.md (2026-10-14): sandbox-деобфускатор
|
||||
//! (narumii/SSVM) не резолвит condy вообще - 0 изменений на condy-классе,
|
||||
//! тогда как прямой вызов декодера он исполняет и снимает строку. Метод `b`
|
||||
//! живёт в декодере и вызывается самой JVM в момент `ldc`.
|
||||
|
||||
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use noxis_classfile::{Attribute, BootstrapMethod, ClassBuilder, Code, Const, Insn, format};
|
||||
use noxis_runtime::{BOOTSTRAP_DESC, BOOTSTRAP_METHOD, Scheme, decoder_class, encrypt};
|
||||
|
||||
const MASTER: u64 = 0x1234_5678_9ABC_DEF0;
|
||||
const DECODER: &str = "test/CondDecoder";
|
||||
const CALLER: &str = "test/CondCall";
|
||||
|
||||
/// Строки обоих схем - тот же набор, что у прямого вызова.
|
||||
fn samples() -> Vec<(String, Scheme)> {
|
||||
vec![
|
||||
("hi".to_string(), Scheme::XorSplitmix),
|
||||
("Hello, мир!".to_string(), Scheme::XorSplitmix),
|
||||
("x".repeat(100), Scheme::XorSplitmix),
|
||||
("hi".to_string(), Scheme::XteaCtr),
|
||||
("Hello, мир!".to_string(), Scheme::XteaCtr),
|
||||
("x".repeat(100), Scheme::XteaCtr),
|
||||
]
|
||||
}
|
||||
|
||||
/// Класс-вызыватель condy-режима: `main` печатает `ldc CONSTANT_Dynamic`.
|
||||
///
|
||||
/// Ровно то, что эмитит пасс `strings` при `condy = true`: по одной
|
||||
/// bootstrap-записи на чанк (cipher + salt как статические аргументы),
|
||||
/// константа со стандартным дескриптором `Ljava/lang/String;`.
|
||||
fn condy_caller_class(parts: &[(String, i32)]) -> noxis_classfile::ClassFile {
|
||||
let mut insns = Vec::new();
|
||||
let mut bootstrap: Vec<BootstrapMethod> = Vec::new();
|
||||
for (cipher, salt) in parts {
|
||||
bootstrap.push(BootstrapMethod {
|
||||
kind: 6, // REF_invokeStatic
|
||||
owner: DECODER.to_string(),
|
||||
name: BOOTSTRAP_METHOD.to_string(),
|
||||
descriptor: BOOTSTRAP_DESC.to_string(),
|
||||
itf: false,
|
||||
args: vec![Const::String(cipher.clone()), Const::Int(*salt)],
|
||||
});
|
||||
insns.push(Insn::GetStatic {
|
||||
owner: "java/lang/System".to_string(),
|
||||
name: "out".to_string(),
|
||||
descriptor: "Ljava/io/PrintStream;".to_string(),
|
||||
});
|
||||
insns.push(Insn::Const(Const::Dynamic {
|
||||
name: "_".to_string(),
|
||||
descriptor: "Ljava/lang/String;".to_string(),
|
||||
bootstrap: (bootstrap.len() - 1) as u16,
|
||||
}));
|
||||
insns.push(Insn::Invoke {
|
||||
kind: noxis_classfile::InvokeKind::Virtual,
|
||||
owner: "java/io/PrintStream".to_string(),
|
||||
name: "println".to_string(),
|
||||
descriptor: "(Ljava/lang/String;)V".to_string(),
|
||||
itf: false,
|
||||
});
|
||||
}
|
||||
insns.push(Insn::Return(None));
|
||||
let code = Code {
|
||||
max_stack: 2,
|
||||
max_locals: 1,
|
||||
insns,
|
||||
exception_table: Vec::new(),
|
||||
frames: Vec::new(),
|
||||
attributes: Vec::new(),
|
||||
};
|
||||
ClassBuilder::new(CALLER, None)
|
||||
.attribute(Attribute::BootstrapMethods(bootstrap))
|
||||
.method_with_code(0x0009, "main", "([Ljava/lang/String;)V", code)
|
||||
.build()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn condy_bootstrap_executes_under_java() {
|
||||
if std::process::Command::new("java")
|
||||
.arg("-version")
|
||||
.output()
|
||||
.is_err()
|
||||
{
|
||||
eprintln!("SKIP: нет java");
|
||||
return;
|
||||
}
|
||||
let mut parts = Vec::new();
|
||||
let mut expected = Vec::new();
|
||||
for (index, (text, scheme)) in samples().iter().enumerate() {
|
||||
let salt = (index as i32)
|
||||
.saturating_mul(2)
|
||||
.saturating_add(match scheme {
|
||||
Scheme::XorSplitmix => 0,
|
||||
Scheme::XteaCtr => 1,
|
||||
});
|
||||
let blob = encrypt(*scheme, MASTER, salt, text);
|
||||
parts.push((blob.cipher, salt));
|
||||
expected.push(text.to_string());
|
||||
}
|
||||
let decoder = decoder_class(DECODER, MASTER, &["xor-splitmix", "xtea-ctr"]).expect("decoder");
|
||||
let caller = condy_caller_class(&parts);
|
||||
|
||||
let dir: PathBuf = std::env::temp_dir().join(format!("noxis-condy-{}", std::process::id()));
|
||||
for (name, class) in [(DECODER, &decoder), (CALLER, &caller)] {
|
||||
let mut path = dir.clone();
|
||||
for part in name.split('/') {
|
||||
path.push(part);
|
||||
}
|
||||
path.set_extension("class");
|
||||
std::fs::create_dir_all(path.parent().expect("parent")).expect("mkdir");
|
||||
std::fs::write(&path, format::write(class).expect("write")).expect("write class");
|
||||
}
|
||||
let out = std::process::Command::new("java")
|
||||
.arg("-Xverify:all")
|
||||
.arg("-cp")
|
||||
.arg(&dir)
|
||||
.arg(CALLER.replace('/', "."))
|
||||
.output()
|
||||
.expect("запуск java");
|
||||
let stdout = String::from_utf8_lossy(&out.stdout);
|
||||
let stderr = String::from_utf8_lossy(&out.stderr);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
assert!(out.status.success(), "java упал:\n{stderr}\n{stdout}");
|
||||
let lines: Vec<&str> = stdout.lines().collect();
|
||||
let want: Vec<&str> = expected.iter().map(String::as_str).collect();
|
||||
assert_eq!(lines, want, "condy-расшифровка не сошлась");
|
||||
}
|
||||
|
|
@ -7,7 +7,6 @@ rust-version.workspace = true
|
|||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
regex = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
noxis-analysis = { workspace = true }
|
||||
noxis-classfile = { workspace = true }
|
||||
|
|
@ -18,5 +17,8 @@ thiserror = { workspace = true }
|
|||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
noxis-passes = { workspace = true }
|
||||
|
||||
[build-dependencies]
|
||||
noxis-structure-check = { workspace = true }
|
||||
|
|
|
|||
252
crates/noxis-verify/src/adversary/mod.rs
Normal file
252
crates/noxis-verify/src/adversary/mod.rs
Normal file
|
|
@ -0,0 +1,252 @@
|
|||
//! A minimal constant-folding adversary: the kind of tool any attacker
|
||||
//! reaches for first (and the one a forum reader pointed at - two adjacent
|
||||
//! literals and a binary op fold in your head). Used two ways:
|
||||
//!
|
||||
//! * as an honest measurement for §7 - "how much of `Constants` survives a
|
||||
//! generic literal evaluator", instead of trusting the docs' own claims;
|
||||
//! * as a correctness oracle - whatever a splitting scheme emits must fold
|
||||
//! back to the exact source value, for every scheme we ship, forever.
|
||||
//!
|
||||
//! Deliberately NOT a full symbolic/SMT solver: it only walks maximal
|
||||
//! straight-line runs of `Const(Int/Long)` + `Binary{Int,Long}`, the same
|
||||
//! vocabulary `noxis_passes::constants` writes. That is the realistic floor,
|
||||
//! not a strawman - a generic constant-propagation pass in any real
|
||||
//! deobfuscator does at least this much.
|
||||
//!
|
||||
//! Each stack slot carries its own provenance (the span of instructions that
|
||||
//! produced it), not just a bare value: several independent split constants
|
||||
//! placed back to back (no separator between them) must be reported as
|
||||
//! separate findings, not glommed into one run just because nothing happens
|
||||
//! to separate them on the instruction stream.
|
||||
|
||||
use noxis_classfile::{Arith, Const, Insn, Prim, Program};
|
||||
|
||||
/// One straight-line run the adversary folded to a single value.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct FoldedConstant {
|
||||
pub class: String,
|
||||
pub method: String,
|
||||
/// Instruction index the run starts at (first `Const`).
|
||||
pub start: usize,
|
||||
/// Instruction count the run spans.
|
||||
pub len: usize,
|
||||
/// The folded value (`Int` results are sign-extended).
|
||||
pub value: i64,
|
||||
}
|
||||
|
||||
/// Fold every maximal literal-arithmetic run in the program.
|
||||
pub fn fold_constants(program: &Program) -> Vec<FoldedConstant> {
|
||||
let mut found = Vec::new();
|
||||
for class in &program.classes {
|
||||
for method in &class.methods {
|
||||
let Some(code) = method.code.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
fold_method(&class.this_class, &method.name, &code.insns, &mut found);
|
||||
}
|
||||
}
|
||||
found
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum Val {
|
||||
I(i32),
|
||||
L(i64),
|
||||
}
|
||||
|
||||
impl Val {
|
||||
fn widen(self) -> i64 {
|
||||
match self {
|
||||
Val::I(v) => i64::from(v),
|
||||
Val::L(v) => v,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A stack slot: the abstract value plus the instruction span that produced
|
||||
/// it (`start` of its earliest literal, `end` exclusive of the op that
|
||||
/// closed it).
|
||||
struct Entry {
|
||||
val: Val,
|
||||
start: usize,
|
||||
end: usize,
|
||||
}
|
||||
|
||||
fn fold_method(class: &str, method: &str, insns: &[Insn], out: &mut Vec<FoldedConstant>) {
|
||||
let mut i = 0;
|
||||
while i < insns.len() {
|
||||
if !is_literal(&insns[i]) {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
let mut stack: Vec<Entry> = Vec::new();
|
||||
let mut j = i;
|
||||
while j < insns.len() {
|
||||
match &insns[j] {
|
||||
Insn::Const(Const::Int(v)) => {
|
||||
stack.push(Entry {
|
||||
val: Val::I(*v),
|
||||
start: j,
|
||||
end: j + 1,
|
||||
});
|
||||
}
|
||||
Insn::Const(Const::Long(v)) => {
|
||||
stack.push(Entry {
|
||||
val: Val::L(*v),
|
||||
start: j,
|
||||
end: j + 1,
|
||||
});
|
||||
}
|
||||
Insn::Binary {
|
||||
prim: prim @ (Prim::Int | Prim::Long),
|
||||
op,
|
||||
} => {
|
||||
let Some(b) = stack.pop() else { break };
|
||||
let Some(a) = stack.pop() else { break };
|
||||
let Some(val) = apply(*prim, *op, a.val, b.val) else {
|
||||
break;
|
||||
};
|
||||
stack.push(Entry {
|
||||
val,
|
||||
start: a.start.min(b.start),
|
||||
end: j + 1,
|
||||
});
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
j += 1;
|
||||
}
|
||||
// Every stack entry still standing when the run ends is a completed,
|
||||
// never-consumed sub-expression. A bare literal (span of 1) isn't an
|
||||
// obfuscated form - only a multi-instruction reduction counts.
|
||||
for entry in &stack {
|
||||
if entry.end - entry.start > 1 {
|
||||
out.push(FoldedConstant {
|
||||
class: class.to_string(),
|
||||
method: method.to_string(),
|
||||
start: entry.start,
|
||||
len: entry.end - entry.start,
|
||||
value: entry.val.widen(),
|
||||
});
|
||||
}
|
||||
}
|
||||
i = if j > i { j } else { i + 1 };
|
||||
}
|
||||
}
|
||||
|
||||
fn is_literal(insn: &Insn) -> bool {
|
||||
matches!(insn, Insn::Const(Const::Int(_) | Const::Long(_)))
|
||||
}
|
||||
|
||||
fn apply(prim: Prim, op: Arith, a: Val, b: Val) -> Option<Val> {
|
||||
match (prim, a, b) {
|
||||
(Prim::Int, Val::I(a), Val::I(b)) => int_op(op, a, b).map(Val::I),
|
||||
(Prim::Long, Val::L(a), Val::L(b)) => long_op(op, a, b).map(Val::L),
|
||||
// A splitting scheme never mixes widths mid-run; a mismatch here
|
||||
// means this isn't one of ours - stop folding rather than guess.
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn int_op(op: Arith, a: i32, b: i32) -> Option<i32> {
|
||||
Some(match op {
|
||||
Arith::Add => a.wrapping_add(b),
|
||||
Arith::Sub => a.wrapping_sub(b),
|
||||
Arith::Mul => a.wrapping_mul(b),
|
||||
Arith::Div => a.checked_div(b)?,
|
||||
Arith::Rem => a.checked_rem(b)?,
|
||||
Arith::And => a & b,
|
||||
Arith::Or => a | b,
|
||||
Arith::Xor => a ^ b,
|
||||
Arith::Shl => a.wrapping_shl(b as u32),
|
||||
Arith::Shr => a.wrapping_shr(b as u32),
|
||||
Arith::Ushr => (a as u32).wrapping_shr(b as u32) as i32,
|
||||
Arith::Neg => return None,
|
||||
})
|
||||
}
|
||||
|
||||
fn long_op(op: Arith, a: i64, b: i64) -> Option<i64> {
|
||||
Some(match op {
|
||||
Arith::Add => a.wrapping_add(b),
|
||||
Arith::Sub => a.wrapping_sub(b),
|
||||
Arith::Mul => a.wrapping_mul(b),
|
||||
Arith::Div => a.checked_div(b)?,
|
||||
Arith::Rem => a.checked_rem(b)?,
|
||||
Arith::And => a & b,
|
||||
Arith::Or => a | b,
|
||||
Arith::Xor => a ^ b,
|
||||
Arith::Shl => a.wrapping_shl(b as u32),
|
||||
Arith::Shr => a.wrapping_shr(b as u32),
|
||||
Arith::Ushr => (a as u64).wrapping_shr(b as u32) as i64,
|
||||
Arith::Neg => return None,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
||||
|
||||
use noxis_classfile::{ClassBuilder, Code, Version};
|
||||
use noxis_core::{Config, Context, Pass};
|
||||
use noxis_passes::ConstantsPass;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn sample(values: &[i32]) -> Program {
|
||||
let mut insns: Vec<Insn> = values.iter().map(|v| Insn::Const(Const::Int(*v))).collect();
|
||||
insns.push(Insn::Return(None));
|
||||
let code = Code {
|
||||
max_stack: 1,
|
||||
max_locals: 0,
|
||||
insns,
|
||||
exception_table: Vec::new(),
|
||||
frames: Vec::new(),
|
||||
attributes: Vec::new(),
|
||||
};
|
||||
Program {
|
||||
classes: vec![
|
||||
ClassBuilder::new("demo/App", Some("java/lang/Object"))
|
||||
.version(Version::JAVA_21)
|
||||
.method_with_code(0x0009, "run", "()V", code)
|
||||
.build(),
|
||||
],
|
||||
resources: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// A plain literal is not itself an obfuscated form (no arithmetic to
|
||||
/// fold) - the adversary must not flag it.
|
||||
#[test]
|
||||
fn bare_literals_are_not_folded() {
|
||||
let program = sample(&[1, 2, 3]);
|
||||
assert!(fold_constants(&program).is_empty());
|
||||
}
|
||||
|
||||
/// The correctness oracle: every scheme `Constants` ships must fold back
|
||||
/// to the exact source value, in source order, even when several split
|
||||
/// constants sit back to back with nothing separating them - a
|
||||
/// regression here is a silently wrong obfuscation, not just a weak one.
|
||||
#[test]
|
||||
fn every_constants_scheme_folds_back_to_the_source_value() {
|
||||
for (seed, values) in [
|
||||
(1u64, vec![1000, -7, 65536, i32::MAX, i32::MIN]),
|
||||
(2, vec![42, -65536, 123456789]),
|
||||
] {
|
||||
let mut program = sample(&values);
|
||||
let mut ctx = Context::new(Config {
|
||||
seed: Some(seed),
|
||||
..Config::default()
|
||||
});
|
||||
ctx.config.constants.max_predicates = 0; // isolate splitting from predicates
|
||||
ConstantsPass.apply(&mut ctx, &mut program).expect("pass");
|
||||
|
||||
let folded = fold_constants(&program);
|
||||
let recovered: Vec<i64> = folded.iter().map(|f| f.value).collect();
|
||||
let expected: Vec<i64> = values.iter().map(|v| i64::from(*v)).collect();
|
||||
assert_eq!(
|
||||
recovered, expected,
|
||||
"adversary disagrees with source for seed {seed}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -57,15 +57,13 @@ pub fn find_leaks(before: &BTreeSet<String>, after: &Program, keep: &KeepRules)
|
|||
pub fn check_patterns(text: &str) -> Vec<&'static str> {
|
||||
use std::sync::OnceLock;
|
||||
|
||||
static COMPILED: OnceLock<Vec<(&'static str, regex::Regex)>> = OnceLock::new();
|
||||
use noxis_analysis::minirx::Regex;
|
||||
|
||||
static COMPILED: OnceLock<Vec<(&'static str, Regex)>> = OnceLock::new();
|
||||
let compiled = COMPILED.get_or_init(|| {
|
||||
RED_PATTERNS
|
||||
.iter()
|
||||
.filter_map(|pattern| {
|
||||
regex::Regex::new(pattern)
|
||||
.ok()
|
||||
.map(|regex| (*pattern, regex))
|
||||
})
|
||||
.filter_map(|pattern| Regex::new(pattern).ok().map(|regex| (*pattern, regex)))
|
||||
.collect()
|
||||
});
|
||||
compiled
|
||||
|
|
|
|||
|
|
@ -4,12 +4,16 @@
|
|||
//!
|
||||
//! * [`leaks`] - утечки исходных имён + секреты открытым текстом;
|
||||
//! * [`java`] - настоящий `java -Xverify:all` по байтам на диске;
|
||||
//! * [`metrics`] - счётчики программы, раздувание, стабильность маппингов.
|
||||
//! * [`metrics`] - счётчики программы, раздувание, стабильность маппингов;
|
||||
//! * [`adversary`] - противник-скрипт: generic свёртка констант как
|
||||
//! честная мера (§7) и как oracle корректности схем `Constants`.
|
||||
|
||||
pub mod adversary;
|
||||
pub mod java;
|
||||
pub mod leaks;
|
||||
pub mod metrics;
|
||||
|
||||
pub use adversary::{FoldedConstant, fold_constants};
|
||||
pub use java::{
|
||||
DecompileReport, JavaVerdict, decompile_report, jadx_available, java_available,
|
||||
verify_class_bytes, verify_jar,
|
||||
|
|
|
|||
93
crates/noxis/examples/frame_repro.rs
Normal file
93
crates/noxis/examples/frame_repro.rs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
//! Repro for the predecessor frame bug (todo.md §3.4): recompute frames on
|
||||
//! every method of an UNTOUCHED jar (no pass runs), write, verify with real
|
||||
//! `java -Xverify:all`, report which classes fail and why.
|
||||
//!
|
||||
//! ```text
|
||||
//! cargo run --release --example frame_repro -- --jar <path> [--out <path>]
|
||||
//! ```
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use noxis_analysis::{ClassHierarchy, JdkHierarchy};
|
||||
use noxis_classfile::{compute_frames, format};
|
||||
use noxis_jar::{load, read_file, save, write_file};
|
||||
use noxis_verify::verify_jar;
|
||||
|
||||
fn main() {
|
||||
let jar = std::env::args()
|
||||
.skip_while(|a| a != "--jar")
|
||||
.nth(1)
|
||||
.expect("--jar <path>");
|
||||
let entries = read_file(&PathBuf::from(&jar)).expect("read jar");
|
||||
let mut program = load(entries).expect("load program");
|
||||
|
||||
let base = ClassHierarchy::build(&program);
|
||||
let hierarchy = JdkHierarchy::wrap(&base);
|
||||
|
||||
let mut recomputed = 0usize;
|
||||
let mut failed_recompute = 0usize;
|
||||
for ci in 0..program.classes.len() {
|
||||
let class_name = program.classes[ci].this_class.clone();
|
||||
let methods = program.classes[ci].methods.len();
|
||||
for mi in 0..methods {
|
||||
let Some(snapshot) = program.classes[ci].methods[mi].code.clone() else {
|
||||
continue;
|
||||
};
|
||||
let method = program.classes[ci].methods[mi].clone();
|
||||
match compute_frames(&class_name, &method, &hierarchy) {
|
||||
Ok(frames) => {
|
||||
program.classes[ci].methods[mi]
|
||||
.code
|
||||
.as_mut()
|
||||
.expect("code")
|
||||
.frames = frames;
|
||||
recomputed += 1;
|
||||
}
|
||||
Err(e) => {
|
||||
failed_recompute += 1;
|
||||
eprintln!("compute_frames FAILED {class_name}#{} : {e}", method.name);
|
||||
// Keep the stored frames instead of giving up the class.
|
||||
program.classes[ci].methods[mi].code = Some(snapshot);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("recomputed {recomputed} methods, {failed_recompute} compute_frames errors");
|
||||
|
||||
// Round-trip every class through the writer, so a write-time bug shows
|
||||
// up the same way it would in the real pipeline.
|
||||
let mut write_failed = 0usize;
|
||||
for class in &program.classes {
|
||||
if let Err(e) = format::write(class) {
|
||||
write_failed += 1;
|
||||
eprintln!("write FAILED {}: {e}", class.this_class);
|
||||
}
|
||||
}
|
||||
println!("write errors: {write_failed}");
|
||||
|
||||
let entries = save(&program).expect("save program");
|
||||
let out = std::env::args()
|
||||
.skip_while(|a| a != "--out")
|
||||
.nth(1)
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| std::env::temp_dir().join("frame_repro_out.jar"));
|
||||
write_file(&out, &entries).expect("write jar");
|
||||
println!("wrote {}", out.display());
|
||||
|
||||
let report = verify_jar(&out, &[]);
|
||||
let mut pass = 0usize;
|
||||
let mut fail = 0usize;
|
||||
let mut skip = 0usize;
|
||||
for (name, verdict) in &report {
|
||||
match verdict {
|
||||
noxis_verify::JavaVerdict::Verified => pass += 1,
|
||||
noxis_verify::JavaVerdict::Failed(msg) => {
|
||||
fail += 1;
|
||||
println!("FAIL {name}: {msg}");
|
||||
}
|
||||
noxis_verify::JavaVerdict::Skipped(_) => skip += 1,
|
||||
}
|
||||
}
|
||||
println!("PASS={pass} FAIL={fail} SKIP={skip}");
|
||||
}
|
||||
|
|
@ -82,8 +82,14 @@ opaque-предикаты - условия, истинность которых
|
|||
- Шаблонный opaque-предикат (всегда один и тот же `A+B`) узнаётся
|
||||
паттерном - отсюда требование нескольких схем.
|
||||
|
||||
**Статус: готово** для `int`/`long` (`xor`/`add`/`shift` + opaque-предикаты);
|
||||
`float`/`double` не трогаются.
|
||||
**Статус: готово** для `int`/`long` (`xor`/`add`/`shift`/`mba` +
|
||||
opaque-предикаты); `float`/`double` не трогаются. `mba` (`(a&b)+(a|b)=a+b`,
|
||||
13.10.2026) не ломает честную оговорку выше - он раскрывается тем же
|
||||
символьным вычислителем, что и `xor`/`add`/`shift`. Разница в том, что
|
||||
бьёт: `xor`/`add` сворачиваются одним паттерном «два литерала + одна
|
||||
операция»; `mba` требует опознать саму тождество `and+or+add`, а не просто
|
||||
свернуть литералы - отсекает деобфускаторы с жёстко вшитым паттерном на
|
||||
один опкод, но не символьные вычислители.
|
||||
|
||||
## 4. Обфускация контрольного потока (`Flow`)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue