From 0e303832bce6494ae528c1febf51d53f4c00ddae Mon Sep 17 00:00:00 2001 From: maitrecraft1234 Date: Sat, 7 Sep 2024 23:41:04 +0200 Subject: [PATCH] added integer obfuscation --- src/main.rs | 3 +- ...dead_code.rs => dead_code_entry_points.rs} | 6 +- src/obfuscator/intergers.rs | 55 +++++++++++++++++++ src/obfuscator/mod.rs | 4 +- src/obfuscator/obfuscator_struct.rs | 6 +- 5 files changed, 68 insertions(+), 6 deletions(-) rename src/obfuscator/{dead_code.rs => dead_code_entry_points.rs} (95%) create mode 100644 src/obfuscator/intergers.rs diff --git a/src/main.rs b/src/main.rs index d9ecb87..d1d8805 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ fn main() { obfuscator.instert_dead_branches(); obfuscator.obfuscate_strings(); obfuscator.obfuctate_functions(); - // obfuscator.print_tree(); + obfuscator.print_tree(); + obfuscator.obfuscate_integers(); println!("{}", obfuscator); } diff --git a/src/obfuscator/dead_code.rs b/src/obfuscator/dead_code_entry_points.rs similarity index 95% rename from src/obfuscator/dead_code.rs rename to src/obfuscator/dead_code_entry_points.rs index 9bce903..0288aef 100644 --- a/src/obfuscator/dead_code.rs +++ b/src/obfuscator/dead_code_entry_points.rs @@ -10,8 +10,8 @@ const RANDOM_USELESS_CODE: [&str; 5] = [ ]; const DEAD_CODE_ENTRY_POINT: [&str; 6] = [ - "if false:", - "if !true:", + "if False:", + "if !True:", "if 0 == 1:", "if 1 == 0:", "if 1 != 1:", @@ -63,7 +63,7 @@ impl Obfuscator { } }) .collect::(); - println!("self.code: {}", self.code); + eprintln!("self.code: {}", self.code); } self.tree = self.parser.parse(&self.code, None).expect("error reparsing after dead code insertion"); } diff --git a/src/obfuscator/intergers.rs b/src/obfuscator/intergers.rs new file mode 100644 index 0000000..6aabf76 --- /dev/null +++ b/src/obfuscator/intergers.rs @@ -0,0 +1,55 @@ +use rand::{random, thread_rng, Rng}; +use tree_sitter::{Tree, TreeCursor}; + +use super::{Obfuscator, Shiftable}; + +fn get_ints(tree: &Tree) -> Vec> { + fn go(cursor: &mut TreeCursor, ints: &mut Vec>) { + let node = cursor.node(); + if node.kind() == "integer" { + ints.push(node.start_byte()..node.end_byte()); + } + if cursor.goto_first_child() { + go(cursor, ints); + cursor.goto_parent(); + } + while cursor.goto_next_sibling() { + go(cursor, ints); + } + } + let mut ints = Vec::new(); + let mut cursor = tree.walk(); + + go(&mut cursor, &mut ints); + + ints +} + +fn encode_int(int: &str) -> String { + let int = int.parse::().expect("int where fake ints =("); + let shift_key: i128 = thread_rng().gen_range(1..16); + let xor_key: i128 = thread_rng().gen_range(1..u32::MAX as i128); + + let encoded = int ^ (xor_key); + let xor_key = xor_key << shift_key; + + format!("({encoded}) ^ (({xor_key} ) >> {shift_key})") +} + +impl Obfuscator { + pub fn obfuscate_integers(&mut self) { + let ints = get_ints(&self.tree); + let mut shift = 0; + + ints.into_iter().skip(3).for_each(|int| { + let int = int.shift(shift); + let val = &self.code[int.clone()]; + shift -= val.len() as i32; + let encoded = encode_int(val); + + self.code.replace_range(int, &encoded); + shift += encoded.len() as i32; + }); + self.reparse(); + } +} diff --git a/src/obfuscator/mod.rs b/src/obfuscator/mod.rs index 62a6f40..e60af33 100644 --- a/src/obfuscator/mod.rs +++ b/src/obfuscator/mod.rs @@ -2,7 +2,9 @@ mod strings; mod print; mod functions; mod obfuscator_struct; -mod dead_code; +mod intergers; +mod boolean; +mod dead_code_entry_points; pub use obfuscator_struct::Obfuscator; mod random_identifiers; diff --git a/src/obfuscator/obfuscator_struct.rs b/src/obfuscator/obfuscator_struct.rs index 3aecc61..10aff2b 100644 --- a/src/obfuscator/obfuscator_struct.rs +++ b/src/obfuscator/obfuscator_struct.rs @@ -27,8 +27,12 @@ impl Obfuscator { .expect("error setting language"); let tree = parser .parse(code.as_bytes(), None) - .expect("error parsing code"); + .expect("error parsing code syntax error might be a reason"); Obfuscator { code, parser, tree } } + + pub fn reparse(&mut self) { + self.tree = self.parser.parse(&self.code, None).expect("error reparsing"); + } }