From e137bd4f7f47394d4da4146d36977aa015566e00 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Mon, 8 Jan 2024 19:14:41 -0600 Subject: [PATCH 1/4] added stringLiteralCounter and tests, not functioning currently --- auto_obfuscate/src/string.rs | 3 +++ auto_obfuscate/src/string/string_tests.rs | 27 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/auto_obfuscate/src/string.rs b/auto_obfuscate/src/string.rs index 08d8c55..593ac42 100644 --- a/auto_obfuscate/src/string.rs +++ b/auto_obfuscate/src/string.rs @@ -84,10 +84,13 @@ impl StringObfuscator { let ast = parse_file(code).expect("Failed to parse code"); let total_strings = count_string_literals(&ast); + println!("total strings: {}", total_strings); let strings_to_encrypt = ( ((self.percentage as f32) / 100.0) * (total_strings as f32) ).ceil() as usize; + println!("percentage: {}", self.percentage); + println!("Encrypting {} strings", strings_to_encrypt); self.encrypted_count = 0; self.strings_to_encrypt = strings_to_encrypt; diff --git a/auto_obfuscate/src/string/string_tests.rs b/auto_obfuscate/src/string/string_tests.rs index 7499b1d..495a10c 100644 --- a/auto_obfuscate/src/string/string_tests.rs +++ b/auto_obfuscate/src/string/string_tests.rs @@ -55,3 +55,30 @@ fn test_replacement_in_nested_macro() { let parse_result = syn::parse_file(&obfuscated_code); assert!(parse_result.is_ok(), "Modified code is not valid Rust code"); } + +#[test] +fn test_percentage() { + let code = + r#" + fn main() { + let a = "d"; + println!("Hello"); + println!("Hello"); + println!("Hello"); + println!("Hello"); + println!("Hello"); + println!("Hello"); + } +"#; + + let mut string_config = StringConfig::default(); + string_config.percentage = 80; + let mut string_obfuscator = StringObfuscator::new(string_config); + let obfuscated_code = string_obfuscator.obfuscate_strings(code); + assert_ne!(code, obfuscated_code); + assert!(obfuscated_code.contains("encrypt_string")); + println!("{}", obfuscated_code); + + let parse_result = syn::parse_file(&obfuscated_code); + assert!(parse_result.is_ok(), "Modified code is not valid Rust code"); +} From 32aa4c9ef5ef8a3e56d3d3a37ed922e924b78ff7 Mon Sep 17 00:00:00 2001 From: Lucas Scharenbroch Date: Mon, 15 Jan 2024 11:51:17 -0600 Subject: [PATCH 2/4] Add percentage limit to string literal encryption (in library) --- auto_obfuscate/src/string.rs | 11 ++++++++--- auto_obfuscate/src/string/string_tests.rs | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/auto_obfuscate/src/string.rs b/auto_obfuscate/src/string.rs index 0bf467a..7850a50 100644 --- a/auto_obfuscate/src/string.rs +++ b/auto_obfuscate/src/string.rs @@ -37,6 +37,7 @@ pub struct StringObfuscator { percentage: u8, encrypted_count: usize, strings_to_encrypt: usize, + num_strings_encrypted: usize, } impl StringObfuscator { @@ -46,6 +47,7 @@ impl StringObfuscator { percentage: config.percentage, encrypted_count: 0, strings_to_encrypt: 0, + num_strings_encrypted: 0, } } #[allow(dead_code)] @@ -86,13 +88,10 @@ impl StringObfuscator { let ast = parse_file(code).expect("Failed to parse code"); let total_strings = count_string_literals(&ast); - println!("total strings: {}", total_strings); let strings_to_encrypt = ( ((self.percentage as f32) / 100.0) * (total_strings as f32) ).ceil() as usize; - println!("percentage: {}", self.percentage); - println!("Encrypting {} strings", strings_to_encrypt); self.encrypted_count = 0; self.strings_to_encrypt = strings_to_encrypt; @@ -107,6 +106,12 @@ impl VisitMut for StringObfuscator { //replace all string literals with call to obfuscation macro fn visit_local_mut(&mut self, local: &mut Local) { if let Some(local_init) = &mut local.init { + if self.num_strings_encrypted >= self.strings_to_encrypt { + return; + } + self.num_strings_encrypted += 1; + + //match on local variables that contain string literal assignments if let Expr::Lit(ExprLit { lit: Lit::Str(lit_str), .. }) = &*local_init.expr { let encrypted = quote! { cryptify::encrypt_string!(#lit_str) }; diff --git a/auto_obfuscate/src/string/string_tests.rs b/auto_obfuscate/src/string/string_tests.rs index 8139468..d6cf9cb 100644 --- a/auto_obfuscate/src/string/string_tests.rs +++ b/auto_obfuscate/src/string/string_tests.rs @@ -40,7 +40,16 @@ fn test_percentage() { let code = r#" fn main() { - let a = "d"; + let a = "a"; + let b = "b"; + let c = "c"; + let d = "d"; + let e = "e"; + let f = "f"; + let g = "g"; + let h = "h"; + let i = "i"; + let j = "j"; println!("Hello"); println!("Hello"); println!("Hello"); @@ -55,7 +64,8 @@ fn test_percentage() { let mut string_obfuscator = StringObfuscator::new(string_config); let obfuscated_code = string_obfuscator.obfuscate_strings(code); assert_ne!(code, obfuscated_code); - assert!(obfuscated_code.contains("encrypt_string")); + assert!(obfuscated_code.contains("encrypt_string ! (\"h\")")); + assert!(obfuscated_code.contains("let i = \"i\"")); println!("{}", obfuscated_code); let parse_result = syn::parse_file(&obfuscated_code); From 8598e4cee1b8da283b326affba65ae4eb046ba8b Mon Sep 17 00:00:00 2001 From: Lucas Scharenbroch Date: Mon, 15 Jan 2024 12:17:37 -0600 Subject: [PATCH 3/4] Fix command line argument for string encryption percentage --- auto_obfuscate/src/main.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/auto_obfuscate/src/main.rs b/auto_obfuscate/src/main.rs index 05e6184..8e3caef 100644 --- a/auto_obfuscate/src/main.rs +++ b/auto_obfuscate/src/main.rs @@ -28,7 +28,11 @@ fn main() { .help("disable macro and modify source directly for flow obfuscation") ) .arg(Arg::with_name("var").long("var").help("Enable variable renaming")) - .arg(Arg::with_name("p").long("p").help("set upper bound for string literal encryption")) + .arg(Arg::with_name("p") + .short('p') + .long("percent_strings_to_encrypt") + .help("set upper bound for string literal encryption") + .value_name("PERCENTAGE")) .get_matches(); let path = matches.value_of("path").unwrap(); @@ -53,7 +57,14 @@ fn main() { } //set upper bound for string literal encryption if let Some(percentage) = matches.value_of("p") { - config.string_config.percentage = percentage.parse().unwrap_or(100); + config.string_config.percentage = match percentage.parse() { + Ok(n) if n <= 100 => n, + _ => { + eprintln!("-p: expected integer between 0 and 100, got: `{}`", percentage); + eprintln!("defaulting to 100%"); + 100 + } + }; } process_path(&path, &config); From 9f0d6975f75a134515a7811d2ba12f8d1f678212 Mon Sep 17 00:00:00 2001 From: Lucas Scharenbroch Date: Mon, 15 Jan 2024 12:17:37 -0600 Subject: [PATCH 4/4] Fix command line argument for string encryption percentage --- auto_obfuscate/src/main.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/auto_obfuscate/src/main.rs b/auto_obfuscate/src/main.rs index dff3342..0a634e4 100644 --- a/auto_obfuscate/src/main.rs +++ b/auto_obfuscate/src/main.rs @@ -28,7 +28,11 @@ fn main() { .help("disable macro and modify source directly for flow obfuscation") ) .arg(Arg::with_name("var").long("var").help("Enable variable renaming")) - .arg(Arg::with_name("p").long("p").help("set upper bound for string literal encryption")) + .arg(Arg::with_name("p") + .short('p') + .long("percent_strings_to_encrypt") + .help("set upper bound for string literal encryption") + .value_name("PERCENTAGE")) .get_matches(); let path = matches.value_of("path").unwrap(); @@ -53,7 +57,14 @@ fn main() { } //set upper bound for string literal encryption if let Some(percentage) = matches.value_of("p") { - config.string_config.percentage = percentage.parse().unwrap_or(100); + config.string_config.percentage = match percentage.parse() { + Ok(n) if n <= 100 => n, + _ => { + eprintln!("-p: expected integer between 0 and 100, got: `{}`", percentage); + eprintln!("defaulting to 100%"); + 100 + } + }; } process_path(&path, &config);