generated from PoCInnovation/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b85271d
commit 0e30383
Showing
5 changed files
with
68 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters