Skip to content

Commit

Permalink
added integer obfuscation
Browse files Browse the repository at this point in the history
  • Loading branch information
maitrecraft1234 committed Sep 7, 2024
1 parent b85271d commit 0e30383
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:",
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Obfuscator {
}
})
.collect::<String>();
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");
}
Expand Down
55 changes: 55 additions & 0 deletions src/obfuscator/intergers.rs
Original file line number Diff line number Diff line change
@@ -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<std::ops::Range<usize>> {
fn go(cursor: &mut TreeCursor, ints: &mut Vec<std::ops::Range<usize>>) {
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::<i128>().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();
}
}
4 changes: 3 additions & 1 deletion src/obfuscator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion src/obfuscator/obfuscator_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 0e30383

Please sign in to comment.