From fe1f0404d61cab6d5e48a2b4469646a3d573a882 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli <62522813+dronavallipranav@users.noreply.github.com> Date: Mon, 8 Jan 2024 19:40:07 -0600 Subject: [PATCH 01/45] Create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..11b85ad --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Pranav Dronavalli + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From a59bc8f07ac428bb0441812e78a5b56517363783 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Mon, 8 Jan 2024 19:50:19 -0600 Subject: [PATCH 02/45] prepare proc macro for publish --- labyrinth_macros/Cargo.toml | 5 ++++- labyrinth_macros/README.md | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 labyrinth_macros/README.md diff --git a/labyrinth_macros/Cargo.toml b/labyrinth_macros/Cargo.toml index 038908e..0369651 100644 --- a/labyrinth_macros/Cargo.toml +++ b/labyrinth_macros/Cargo.toml @@ -1,7 +1,10 @@ [package] name = "labyrinth_macros" version = "0.1.0" -edition = "2021" +authors = ["Pranav Dronavalli "] +description = "A procedural macro crate for the labyrinth super crate. provides compiletime string and flow obfuscation" +license = "MIT" +repository = "https://github.com/dronavallipranav/rust-obfuscator/tree/main/labyrinth_macros" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] diff --git a/labyrinth_macros/README.md b/labyrinth_macros/README.md new file mode 100644 index 0000000..2f11c22 --- /dev/null +++ b/labyrinth_macros/README.md @@ -0,0 +1,8 @@ +# Labyrinth Macros + +`labyrinth_macros` is a procedural macro crate designed to complement the `labyrinth` super crate. It provides powerful compile-time string and control flow obfuscation capabilities, aimed at enhancing the security and complexity of Rust code. Not meant to be used standalone, necessary obfuscation features are in the super crate `labyrinth` + +## Features + +- **String Obfuscation**: Automatically encrypts string literals in your code at compile time, making them harder to read and understand. +- **Flow Obfuscation**: Introduces dummy loops and random variables into control flows, enhancing the overall obfuscation of the logic. From 285dd91e361a0cafd9b67415bac5fc6c4dbdb76c Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Mon, 8 Jan 2024 19:57:40 -0600 Subject: [PATCH 03/45] modify cargo.toml --- labyrinth_macros/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/labyrinth_macros/Cargo.toml b/labyrinth_macros/Cargo.toml index 0369651..ee9a5d3 100644 --- a/labyrinth_macros/Cargo.toml +++ b/labyrinth_macros/Cargo.toml @@ -1,8 +1,9 @@ [package] name = "labyrinth_macros" version = "0.1.0" +edition = "2021" authors = ["Pranav Dronavalli "] -description = "A procedural macro crate for the labyrinth super crate. provides compiletime string and flow obfuscation" +description = "A procedural macro crate for the labyrinth crate meant to provide obfuscation through compile time string encryption. not meant to be used standalone" license = "MIT" repository = "https://github.com/dronavallipranav/rust-obfuscator/tree/main/labyrinth_macros" From daff11e236038de6fd3940da31eb4e75855a19ef Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 16:13:25 -0600 Subject: [PATCH 04/45] continue documentation, prepare labyrinth for publish --- labyrinth/Cargo.toml | 4 +++ labyrinth/README.md | 73 ++++++++++++++++++++++++++++++++++++++ labyrinth_macros/README.md | 5 ++- 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 labyrinth/README.md diff --git a/labyrinth/Cargo.toml b/labyrinth/Cargo.toml index d5c3300..c0cbaa3 100644 --- a/labyrinth/Cargo.toml +++ b/labyrinth/Cargo.toml @@ -2,6 +2,10 @@ name = "labyrinth" version = "0.1.0" edition = "2021" +authors = ["Pranav Dronavalli "] +description = "A procedural macro library to obfuscate Rust code. Provides compile-time string encryption and random flow obfuscation." +license = "MIT" +repository = "https://github.com/dronavallipranav/rust-obfuscator/tree/main/labyrinth_macros" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/labyrinth/README.md b/labyrinth/README.md new file mode 100644 index 0000000..48285ed --- /dev/null +++ b/labyrinth/README.md @@ -0,0 +1,73 @@ +# labyrinth + +`labyrinth` is a procedural macro crate for compile-time rust obfuscation. It provides the user with string encryption and compile-time determined flow obfuscation and random variables which survive compile-time optimization. + + +[rust-obfuscator](https://github.com/dronavallipranav/rust-obfuscator) - Check out this auto obfuscator tool for easier usage and integration +## Features + +- **String Obfuscation**: Automatically encrypts string literals in your code at compile time, making them harder to read and understand. +- **Flow Obfuscation**: Introduces dummy loops and random variables into control flows, enhancing the overall obfuscation of the logic. + +# Installation + +Add `labyrinth` to your `Cargo.toml` as a dependency: + +```toml +[dependencies] +labyrinth = "0.1.0" +``` + +# Usage + +## Bring macro into scope +``` +use labyrinth; + +fn main(){ + println!(labyrinth::encrypt_string("Hello, World!")); +} +``` +## Output +``` +Hello World! +``` +## Example of expanded Flow_Stmt! + +``` + { + let _is_dummy_145 = true; + let _dummy_upper_bound = 100; + let _dummy_increment = 1i32; + let mut _dummy_counter = 10i32; + let _extra_dummy_var = 2i32; + loop { + if _dummy_counter > _dummy_upper_bound { + break; + } + unsafe { + std::ptr::write_volatile( + &mut _dummy_counter, + _dummy_counter + _dummy_increment, + ); + } + } + }; + match (&1, &1) { + (left_val, right_val) => { + if !(*left_val == *right_val) { + let kind = ::core::panicking::AssertKind::Eq; + ::core::panicking::assert_failed( + kind, + &*left_val, + &*right_val, + ::core::option::Option::None, + ); + } + } + }; + +``` + +# License +labyrinth is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. diff --git a/labyrinth_macros/README.md b/labyrinth_macros/README.md index 2f11c22..7bb981d 100644 --- a/labyrinth_macros/README.md +++ b/labyrinth_macros/README.md @@ -1,4 +1,4 @@ -# Labyrinth Macros +# labyrinth_macros `labyrinth_macros` is a procedural macro crate designed to complement the `labyrinth` super crate. It provides powerful compile-time string and control flow obfuscation capabilities, aimed at enhancing the security and complexity of Rust code. Not meant to be used standalone, necessary obfuscation features are in the super crate `labyrinth` @@ -6,3 +6,6 @@ - **String Obfuscation**: Automatically encrypts string literals in your code at compile time, making them harder to read and understand. - **Flow Obfuscation**: Introduces dummy loops and random variables into control flows, enhancing the overall obfuscation of the logic. + +# License +labyrinth_macros is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. \ No newline at end of file From 4e4eeae0cdc16d0c1862cd1fcc51744d9aa20f8e Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 16:31:10 -0600 Subject: [PATCH 05/45] bump to production versions --- auto_obfuscate/Cargo.toml | 2 +- labyrinth_macros/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/auto_obfuscate/Cargo.toml b/auto_obfuscate/Cargo.toml index 1a9ced3..9b39edb 100644 --- a/auto_obfuscate/Cargo.toml +++ b/auto_obfuscate/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust-obfuscator" -version = "0.1.0" +version = "1.0.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/labyrinth_macros/Cargo.toml b/labyrinth_macros/Cargo.toml index ee9a5d3..bdc6478 100644 --- a/labyrinth_macros/Cargo.toml +++ b/labyrinth_macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "labyrinth_macros" -version = "0.1.0" +version = "1.0.0" edition = "2021" authors = ["Pranav Dronavalli "] description = "A procedural macro crate for the labyrinth crate meant to provide obfuscation through compile time string encryption. not meant to be used standalone" From 9e8e8adf474a3358d1c4782718d01c592833aaa9 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 16:31:51 -0600 Subject: [PATCH 06/45] update documentation --- labyrinth/src/lib.rs | 8 +++++++- labyrinth/tests/macro_tests.rs | 1 + labyrinth_macros/src/lib.rs | 16 +++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/labyrinth/src/lib.rs b/labyrinth/src/lib.rs index 687ca6e..ad588fd 100644 --- a/labyrinth/src/lib.rs +++ b/labyrinth/src/lib.rs @@ -1,5 +1,11 @@ +//re-export proc macro crate pub use labyrinth_macros::*; -//helper function to decrypt string at runtime + +/// A helper decryption function meant to decrypt encrypted strings at runtime +/// +/// # Parameters +/// - `input`: The encrypted string literal +/// pub fn decrypt_string(encrypted: &str) -> String { let key = std::env::var("LABYRINTH_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); encrypted diff --git a/labyrinth/tests/macro_tests.rs b/labyrinth/tests/macro_tests.rs index d14d6d1..b80e634 100644 --- a/labyrinth/tests/macro_tests.rs +++ b/labyrinth/tests/macro_tests.rs @@ -1,3 +1,4 @@ +//integration tests testing whole crate #[test] fn test_encrypt_macro() { let decrypted = labyrinth::encrypt_string!("Hello World"); diff --git a/labyrinth_macros/src/lib.rs b/labyrinth_macros/src/lib.rs index 619dafb..84b92c1 100644 --- a/labyrinth_macros/src/lib.rs +++ b/labyrinth_macros/src/lib.rs @@ -1,3 +1,7 @@ +//! `labyrinth_macros` crate provides procedural macros for compile-time obfuscation. NOT MEANT TO BE USED STANDALONE. +//! +//! This crate includes macros like `encrypt_string` and `flow_stmt` which are used +//! to enhance the security of Rust code by obfuscating strings and control flows. use proc_macro::TokenStream; use quote::quote; use syn::*; @@ -5,6 +9,11 @@ use std::env; use rand::Rng; use rand::seq::SliceRandom; +/// A procedural macro that adds a compile-time randomly generated loop and variables. +/// +/// # Note +/// The unsafe operation is meant to help the dummy loop survive compiler optimizations. only writes to dummy variable +/// #[proc_macro] pub fn flow_stmt(_: TokenStream) -> TokenStream { let mut rng = rand::thread_rng(); @@ -52,7 +61,11 @@ pub fn flow_stmt(_: TokenStream) -> TokenStream { TokenStream::from(generated_loop) } - +/// A procedural macro that encrypts a string literal at compile time. +/// +/// # Parameters +/// - `input`: The string literal to be encrypted. +/// #[proc_macro] pub fn encrypt_string(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as LitStr); @@ -89,6 +102,7 @@ fn decrypt_string(encrypted: &str) -> String { .collect() } +//unit tests testing decryption logic #[cfg(test)] mod tests { use super::*; From ba37a578bb370e534d19d6c9f679dc790f52f708 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 16:34:25 -0600 Subject: [PATCH 07/45] bump labyrinth to production version --- Cargo.lock | 20 ++++++++++++++++---- labyrinth/Cargo.toml | 4 ++-- labyrinth/README.md | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f5b0353..6f232c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -102,14 +102,26 @@ dependencies = [ [[package]] name = "labyrinth" -version = "0.1.0" +version = "1.0.0" +dependencies = [ + "labyrinth_macros 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "labyrinth_macros" +version = "1.0.0" dependencies = [ - "labyrinth_macros", + "proc-macro2", + "quote", + "rand", + "syn", ] [[package]] name = "labyrinth_macros" -version = "0.1.0" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841dc375e835e94528593b9de4a668f28663df4aff4a03a6cfeaf70c00603ec4" dependencies = [ "proc-macro2", "quote", @@ -220,7 +232,7 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "rust-obfuscator" -version = "0.1.0" +version = "1.0.0" dependencies = [ "clap", "labyrinth", diff --git a/labyrinth/Cargo.toml b/labyrinth/Cargo.toml index c0cbaa3..b2faf38 100644 --- a/labyrinth/Cargo.toml +++ b/labyrinth/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "labyrinth" -version = "0.1.0" +version = "1.0.0" edition = "2021" authors = ["Pranav Dronavalli "] description = "A procedural macro library to obfuscate Rust code. Provides compile-time string encryption and random flow obfuscation." @@ -10,4 +10,4 @@ repository = "https://github.com/dronavallipranav/rust-obfuscator/tree/main/laby # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -labyrinth_macros = { path = "../labyrinth_macros" } \ No newline at end of file +labyrinth_macros = "1.0.0" \ No newline at end of file diff --git a/labyrinth/README.md b/labyrinth/README.md index 48285ed..612f59d 100644 --- a/labyrinth/README.md +++ b/labyrinth/README.md @@ -15,7 +15,7 @@ Add `labyrinth` to your `Cargo.toml` as a dependency: ```toml [dependencies] -labyrinth = "0.1.0" +labyrinth = "1.0.0" ``` # Usage From 33e30840689e767174ed3a049a0ecdfbb14d62ac Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 16:59:00 -0600 Subject: [PATCH 08/45] update to use new crate name --- labyrinth_macros/Cargo.toml | 2 +- labyrinth_macros/README.md | 2 +- labyrinth_macros/src/lib.rs | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/labyrinth_macros/Cargo.toml b/labyrinth_macros/Cargo.toml index bdc6478..67350ff 100644 --- a/labyrinth_macros/Cargo.toml +++ b/labyrinth_macros/Cargo.toml @@ -3,7 +3,7 @@ name = "labyrinth_macros" version = "1.0.0" edition = "2021" authors = ["Pranav Dronavalli "] -description = "A procedural macro crate for the labyrinth crate meant to provide obfuscation through compile time string encryption. not meant to be used standalone" +description = "A procedural macro crate for the cryptify crate meant to provide obfuscation through compile time string encryption. not meant to be used standalone" license = "MIT" repository = "https://github.com/dronavallipranav/rust-obfuscator/tree/main/labyrinth_macros" diff --git a/labyrinth_macros/README.md b/labyrinth_macros/README.md index 7bb981d..a441f5e 100644 --- a/labyrinth_macros/README.md +++ b/labyrinth_macros/README.md @@ -1,6 +1,6 @@ # labyrinth_macros -`labyrinth_macros` is a procedural macro crate designed to complement the `labyrinth` super crate. It provides powerful compile-time string and control flow obfuscation capabilities, aimed at enhancing the security and complexity of Rust code. Not meant to be used standalone, necessary obfuscation features are in the super crate `labyrinth` +`labyrinth_macros` is a procedural macro crate designed to complement the `cryptify` super crate. It provides compile-time string and control flow obfuscation capabilities, aimed at enhancing the security and complexity of Rust code. Not meant to be used standalone, necessary obfuscation features are in the super crate `cryptify` ## Features diff --git a/labyrinth_macros/src/lib.rs b/labyrinth_macros/src/lib.rs index 84b92c1..15c80c9 100644 --- a/labyrinth_macros/src/lib.rs +++ b/labyrinth_macros/src/lib.rs @@ -72,12 +72,12 @@ pub fn encrypt_string(input: TokenStream) -> TokenStream { let string = input.value(); //set key to seeded env key or default - let key = env::var("LABYRINTH_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); + let key = env::var("CRYPTIFY_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); let encrypted_string = xor_cipher(&string, &key); let output = quote! { - labyrinth::decrypt_string(#encrypted_string) + cryptify::decrypt_string(#encrypted_string) }; TokenStream::from(output) @@ -94,7 +94,7 @@ fn xor_cipher(input: &str, key: &str) -> String { //for self-contained tests #[allow(dead_code)] fn decrypt_string(encrypted: &str) -> String { - let key = std::env::var("LABYRINTH_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); + let key = std::env::var("CRYPTIFY_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); encrypted .chars() .zip(key.chars().cycle()) @@ -121,9 +121,9 @@ mod tests { #[test] fn test_xor_cipher_and_decrypt_customkey() { //set key - std::env::set_var("LABYRINTH_KEY", "testkey"); + std::env::set_var("CRYPTIFY_KEY", "testkey"); //test loc from encrypt_string meant to extract key - let key = env::var("LABYRINTH_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); + let key = env::var("CRYPTIFY_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); assert_eq!(key, "testkey"); let test_strings = ["Hello", "World", "1234", "!@#$%^&*()"]; From 885abbc5c7da1fc197b4ee922040b56d49b61c58 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 17:00:14 -0600 Subject: [PATCH 09/45] new version, use new crate --- labyrinth_macros/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/labyrinth_macros/Cargo.toml b/labyrinth_macros/Cargo.toml index 67350ff..959e93a 100644 --- a/labyrinth_macros/Cargo.toml +++ b/labyrinth_macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "labyrinth_macros" -version = "1.0.0" +version = "2.0.0" edition = "2021" authors = ["Pranav Dronavalli "] description = "A procedural macro crate for the cryptify crate meant to provide obfuscation through compile time string encryption. not meant to be used standalone" From 562b1cd46f2a22ef23a6fb613318239ebba6648d Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 17:02:25 -0600 Subject: [PATCH 10/45] renamed labyrinth to cryptify --- {labyrinth => cryptify}/Cargo.toml | 4 ++-- {labyrinth => cryptify}/README.md | 17 ++++++++++------- {labyrinth => cryptify}/src/lib.rs | 2 +- {labyrinth => cryptify}/tests/macro_tests.rs | 4 ++-- 4 files changed, 15 insertions(+), 12 deletions(-) rename {labyrinth => cryptify}/Cargo.toml (91%) rename {labyrinth => cryptify}/README.md (71%) rename {labyrinth => cryptify}/src/lib.rs (82%) rename {labyrinth => cryptify}/tests/macro_tests.rs (70%) diff --git a/labyrinth/Cargo.toml b/cryptify/Cargo.toml similarity index 91% rename from labyrinth/Cargo.toml rename to cryptify/Cargo.toml index b2faf38..9568672 100644 --- a/labyrinth/Cargo.toml +++ b/cryptify/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "labyrinth" +name = "cryptify" version = "1.0.0" edition = "2021" authors = ["Pranav Dronavalli "] @@ -10,4 +10,4 @@ repository = "https://github.com/dronavallipranav/rust-obfuscator/tree/main/laby # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -labyrinth_macros = "1.0.0" \ No newline at end of file +labyrinth_macros = "2.0.0" \ No newline at end of file diff --git a/labyrinth/README.md b/cryptify/README.md similarity index 71% rename from labyrinth/README.md rename to cryptify/README.md index 612f59d..2251add 100644 --- a/labyrinth/README.md +++ b/cryptify/README.md @@ -1,6 +1,6 @@ -# labyrinth +# cryptify -`labyrinth` is a procedural macro crate for compile-time rust obfuscation. It provides the user with string encryption and compile-time determined flow obfuscation and random variables which survive compile-time optimization. +`cryptify` is a procedural macro crate for compile-time rust obfuscation. It provides the user with string encryption and compile-time determined flow obfuscation and random variables which survive compile-time optimization. [rust-obfuscator](https://github.com/dronavallipranav/rust-obfuscator) - Check out this auto obfuscator tool for easier usage and integration @@ -11,23 +11,26 @@ # Installation -Add `labyrinth` to your `Cargo.toml` as a dependency: +Add `cryptify` to your `Cargo.toml` as a dependency: ```toml [dependencies] -labyrinth = "1.0.0" +cryptify = "1.0.0" ``` # Usage ## Bring macro into scope ``` -use labyrinth; +use cryptify; fn main(){ - println!(labyrinth::encrypt_string("Hello, World!")); + println!(cryptify::encrypt_string("Hello, World!")); } ``` + +Set the **CRYPTIFY_KEY** environment variable for custom encryption defaults to fixed key + ## Output ``` Hello World! @@ -70,4 +73,4 @@ Hello World! ``` # License -labyrinth is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. +cryptify is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. diff --git a/labyrinth/src/lib.rs b/cryptify/src/lib.rs similarity index 82% rename from labyrinth/src/lib.rs rename to cryptify/src/lib.rs index ad588fd..517343a 100644 --- a/labyrinth/src/lib.rs +++ b/cryptify/src/lib.rs @@ -7,7 +7,7 @@ pub use labyrinth_macros::*; /// - `input`: The encrypted string literal /// pub fn decrypt_string(encrypted: &str) -> String { - let key = std::env::var("LABYRINTH_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); + let key = std::env::var("CRYPTIFY_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); encrypted .chars() .zip(key.chars().cycle()) diff --git a/labyrinth/tests/macro_tests.rs b/cryptify/tests/macro_tests.rs similarity index 70% rename from labyrinth/tests/macro_tests.rs rename to cryptify/tests/macro_tests.rs index b80e634..cf31103 100644 --- a/labyrinth/tests/macro_tests.rs +++ b/cryptify/tests/macro_tests.rs @@ -1,13 +1,13 @@ //integration tests testing whole crate #[test] fn test_encrypt_macro() { - let decrypted = labyrinth::encrypt_string!("Hello World"); + let decrypted = cryptify::encrypt_string!("Hello World"); assert_eq!("Hello World", decrypted); } #[test] fn test_flow_macro() { //manually test for now with cargo expand - labyrinth::flow_stmt!(); + cryptify::flow_stmt!(); assert_eq!(1, 1); } From dc9e6eeb328164d397f85e59d35e19a163ca2d6b Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 17:02:42 -0600 Subject: [PATCH 11/45] update references to use cryptify --- Cargo.lock | 22 +++++++++++----------- Cargo.toml | 2 +- auto_obfuscate/Cargo.toml | 2 +- auto_obfuscate/src/flow.rs | 2 +- auto_obfuscate/src/string.rs | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f232c9..702d92a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,6 +64,13 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "cryptify" +version = "1.0.0" +dependencies = [ + "labyrinth_macros 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "getrandom" version = "0.2.11" @@ -100,16 +107,9 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "labyrinth" -version = "1.0.0" -dependencies = [ - "labyrinth_macros 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "labyrinth_macros" -version = "1.0.0" +version = "2.0.0" dependencies = [ "proc-macro2", "quote", @@ -119,9 +119,9 @@ dependencies = [ [[package]] name = "labyrinth_macros" -version = "1.0.0" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "841dc375e835e94528593b9de4a668f28663df4aff4a03a6cfeaf70c00603ec4" +checksum = "a49a429784bf214985bd5e5c058a89889a2e5b9fc60ebd60ced2bf0429791fa4" dependencies = [ "proc-macro2", "quote", @@ -235,7 +235,7 @@ name = "rust-obfuscator" version = "1.0.0" dependencies = [ "clap", - "labyrinth", + "cryptify", "proc-macro2", "quote", "rand", diff --git a/Cargo.toml b/Cargo.toml index 713a81f..1a43b1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = [ "auto_obfuscate", - "labyrinth", + "cryptify", "labyrinth_macros", ] resolver = "2" \ No newline at end of file diff --git a/auto_obfuscate/Cargo.toml b/auto_obfuscate/Cargo.toml index 9b39edb..d7b1fb8 100644 --- a/auto_obfuscate/Cargo.toml +++ b/auto_obfuscate/Cargo.toml @@ -12,4 +12,4 @@ proc-macro2 = "1.0" rand = "0.8.0" regex = "1.5.4" clap = "3.0" -labyrinth = { path = "../labyrinth" } \ No newline at end of file +cryptify = { path = "../cryptify" } \ No newline at end of file diff --git a/auto_obfuscate/src/flow.rs b/auto_obfuscate/src/flow.rs index 766996d..60fe3ed 100644 --- a/auto_obfuscate/src/flow.rs +++ b/auto_obfuscate/src/flow.rs @@ -113,7 +113,7 @@ impl VisitMut for FlowObfuscator { //if use macro enabled, use macro to expand to dummy loop if self.use_macro { let macro_call = syn::parse_quote! { - labyrinth::flow_stmt!(); + cryptify::flow_stmt!(); }; block.stmts.insert(0, macro_call); } else { diff --git a/auto_obfuscate/src/string.rs b/auto_obfuscate/src/string.rs index 08d8c55..6a8ce17 100644 --- a/auto_obfuscate/src/string.rs +++ b/auto_obfuscate/src/string.rs @@ -61,7 +61,7 @@ impl StringObfuscator { if let Ok(lit_str) = parse_str::(&lit_str) { let macro_call: TokenStream = quote! { - labyrinth::encrypt_string!(#lit_str) + cryptify::encrypt_string!(#lit_str) }; return macro_call; } @@ -112,7 +112,7 @@ impl VisitMut for StringObfuscator { //replace string literal with macro call let macro_call = quote! { - labyrinth::encrypt_string!(#expr_lit) + cryptify::encrypt_string!(#expr_lit) }; self.encrypted_count += 1; //replace expression to use macro call @@ -129,7 +129,7 @@ impl VisitMut for StringObfuscator { //check to see if macro is not obfuscation macro if mac.path.segments.len() == 2 && - mac.path.segments[0].ident == "labyrinth" && + mac.path.segments[0].ident == "cryptify" && mac.path.segments[1].ident == "encrypt_string" { return; From af8c1a3569c2f3fb5f93ab0fec3ccd27d57d771b Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 17:05:11 -0600 Subject: [PATCH 12/45] use published crate instead --- Cargo.lock | 11 ++++++++++- auto_obfuscate/Cargo.toml | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 702d92a..7bf00a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,6 +71,15 @@ dependencies = [ "labyrinth_macros 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cryptify" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9177f77c37d98aab6a95246f73a33333e124389dc9811d61d03a2abd0443f8ee" +dependencies = [ + "labyrinth_macros 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "getrandom" version = "0.2.11" @@ -235,7 +244,7 @@ name = "rust-obfuscator" version = "1.0.0" dependencies = [ "clap", - "cryptify", + "cryptify 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2", "quote", "rand", diff --git a/auto_obfuscate/Cargo.toml b/auto_obfuscate/Cargo.toml index d7b1fb8..466f6d9 100644 --- a/auto_obfuscate/Cargo.toml +++ b/auto_obfuscate/Cargo.toml @@ -12,4 +12,4 @@ proc-macro2 = "1.0" rand = "0.8.0" regex = "1.5.4" clap = "3.0" -cryptify = { path = "../cryptify" } \ No newline at end of file +cryptify = "1.0.0" \ No newline at end of file From c1f06e3d5665876fa9edea654961122cecf17478 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 17:43:46 -0600 Subject: [PATCH 13/45] update documentation --- Cargo.toml | 1 + auto_obfuscate/Cargo.toml | 4 ++- auto_obfuscate/README.md | 51 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1a43b1c..0faa24f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ members = [ "cryptify", "labyrinth_macros", ] +default-members = ["auto_obfuscate"] resolver = "2" \ No newline at end of file diff --git a/auto_obfuscate/Cargo.toml b/auto_obfuscate/Cargo.toml index 466f6d9..171e766 100644 --- a/auto_obfuscate/Cargo.toml +++ b/auto_obfuscate/Cargo.toml @@ -3,7 +3,9 @@ name = "rust-obfuscator" version = "1.0.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[[bin]] +name = "rust-obfuscator" +path = "src/main.rs" [dependencies] syn = { version = "2.0", features = ["full", "visit-mut", "visit"] } diff --git a/auto_obfuscate/README.md b/auto_obfuscate/README.md index 83618f1..79c1a56 100644 --- a/auto_obfuscate/README.md +++ b/auto_obfuscate/README.md @@ -1 +1,50 @@ -# rust-obfuscator \ No newline at end of file +# Rust-Obfuscator + +`rust-obfuscator` is a set of tools designed to automatically obfuscate Rust source code by automatically inserting procedural macros or by (optionally) providing the obfuscation in the source code directly. For more fine-grained obfuscation a procedural macro library [cryptify](https://crates.io/crates/cryptify) is also provided. + +## Currently Supported +1. String literal encryption +2. control-flow obfuscation +3. control-flow obfuscation (source code) +4. variable renaming (source code) + +## Features + +- **String Encryption**: Encrypts string literals in your code at compile time. +- **Control Flow Obfuscation**: Introduces compile-dummy dummy loops and random variables into control flows complicating the logic and structure of the code. +- Note: for truly random control flow and variables, you can disable the insertion of the flow_macro using the **disable_macro** flag, but this will directly affect the source code. +- **Customizable Obfuscation**: Offers flexibility to enable or disable specific obfuscation features based on your requirements. +- **Variable Renaming**: Obfuscation of the source code directly, if you'd like to ship the code or just want to make your code look worse. + +## Installation + +To install `rust-obfuscator`, clone the repository and build the tool using Cargo from the root: +``` +cargo build --release --bin rust-obfuscator +``` +The binary can then be found under /target/release, you can copy it to the root of the project like such +``` +cp ./target/release/rust-obfuscator . +``` + +# Usage +The binary can be used on either a file or a directory. If provided with a directory it will only modify rust source files within that directory not any subdirectories +``` +rust-obfuscator path/to/your_project +``` +- All Obfuscated code will be under the **obfuscated_code** directory that is created from the directory the tool was run. + +## Option Flags +- --no_string: Disables string obfuscation. +- --no_flow: Disables control flow obfuscation. +- --disable_macro: Uses direct source manipulation for flow obfuscation instead of procedural macros. +- --var: Enables variable renaming source code obfuscation. + +### Example usage with flag +``` +rust-obfuscator path/to/your_project --no_flow +``` +(disables flow obfuscation) + +# License +labyrinth_macros is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. \ No newline at end of file From 49bc58bb18566da24a3523a0d6d79c100afd1405 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 17:44:39 -0600 Subject: [PATCH 14/45] update docs --- README.md | 51 +++++++++++++++++++++++++++++++++++++++- auto_obfuscate/README.md | 50 --------------------------------------- 2 files changed, 50 insertions(+), 51 deletions(-) delete mode 100644 auto_obfuscate/README.md diff --git a/README.md b/README.md index 83618f1..79c1a56 100644 --- a/README.md +++ b/README.md @@ -1 +1,50 @@ -# rust-obfuscator \ No newline at end of file +# Rust-Obfuscator + +`rust-obfuscator` is a set of tools designed to automatically obfuscate Rust source code by automatically inserting procedural macros or by (optionally) providing the obfuscation in the source code directly. For more fine-grained obfuscation a procedural macro library [cryptify](https://crates.io/crates/cryptify) is also provided. + +## Currently Supported +1. String literal encryption +2. control-flow obfuscation +3. control-flow obfuscation (source code) +4. variable renaming (source code) + +## Features + +- **String Encryption**: Encrypts string literals in your code at compile time. +- **Control Flow Obfuscation**: Introduces compile-dummy dummy loops and random variables into control flows complicating the logic and structure of the code. +- Note: for truly random control flow and variables, you can disable the insertion of the flow_macro using the **disable_macro** flag, but this will directly affect the source code. +- **Customizable Obfuscation**: Offers flexibility to enable or disable specific obfuscation features based on your requirements. +- **Variable Renaming**: Obfuscation of the source code directly, if you'd like to ship the code or just want to make your code look worse. + +## Installation + +To install `rust-obfuscator`, clone the repository and build the tool using Cargo from the root: +``` +cargo build --release --bin rust-obfuscator +``` +The binary can then be found under /target/release, you can copy it to the root of the project like such +``` +cp ./target/release/rust-obfuscator . +``` + +# Usage +The binary can be used on either a file or a directory. If provided with a directory it will only modify rust source files within that directory not any subdirectories +``` +rust-obfuscator path/to/your_project +``` +- All Obfuscated code will be under the **obfuscated_code** directory that is created from the directory the tool was run. + +## Option Flags +- --no_string: Disables string obfuscation. +- --no_flow: Disables control flow obfuscation. +- --disable_macro: Uses direct source manipulation for flow obfuscation instead of procedural macros. +- --var: Enables variable renaming source code obfuscation. + +### Example usage with flag +``` +rust-obfuscator path/to/your_project --no_flow +``` +(disables flow obfuscation) + +# License +labyrinth_macros is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. \ No newline at end of file diff --git a/auto_obfuscate/README.md b/auto_obfuscate/README.md deleted file mode 100644 index 79c1a56..0000000 --- a/auto_obfuscate/README.md +++ /dev/null @@ -1,50 +0,0 @@ -# Rust-Obfuscator - -`rust-obfuscator` is a set of tools designed to automatically obfuscate Rust source code by automatically inserting procedural macros or by (optionally) providing the obfuscation in the source code directly. For more fine-grained obfuscation a procedural macro library [cryptify](https://crates.io/crates/cryptify) is also provided. - -## Currently Supported -1. String literal encryption -2. control-flow obfuscation -3. control-flow obfuscation (source code) -4. variable renaming (source code) - -## Features - -- **String Encryption**: Encrypts string literals in your code at compile time. -- **Control Flow Obfuscation**: Introduces compile-dummy dummy loops and random variables into control flows complicating the logic and structure of the code. -- Note: for truly random control flow and variables, you can disable the insertion of the flow_macro using the **disable_macro** flag, but this will directly affect the source code. -- **Customizable Obfuscation**: Offers flexibility to enable or disable specific obfuscation features based on your requirements. -- **Variable Renaming**: Obfuscation of the source code directly, if you'd like to ship the code or just want to make your code look worse. - -## Installation - -To install `rust-obfuscator`, clone the repository and build the tool using Cargo from the root: -``` -cargo build --release --bin rust-obfuscator -``` -The binary can then be found under /target/release, you can copy it to the root of the project like such -``` -cp ./target/release/rust-obfuscator . -``` - -# Usage -The binary can be used on either a file or a directory. If provided with a directory it will only modify rust source files within that directory not any subdirectories -``` -rust-obfuscator path/to/your_project -``` -- All Obfuscated code will be under the **obfuscated_code** directory that is created from the directory the tool was run. - -## Option Flags -- --no_string: Disables string obfuscation. -- --no_flow: Disables control flow obfuscation. -- --disable_macro: Uses direct source manipulation for flow obfuscation instead of procedural macros. -- --var: Enables variable renaming source code obfuscation. - -### Example usage with flag -``` -rust-obfuscator path/to/your_project --no_flow -``` -(disables flow obfuscation) - -# License -labyrinth_macros is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. \ No newline at end of file From d0b89bffe46542719001a0621e5ace25f363e29a Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 17:52:15 -0600 Subject: [PATCH 15/45] update docs --- README.md | 13 ++++++++++++- cryptify/README.md | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 79c1a56..7c0575c 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,13 @@ ## Installation +Add `cryptify` to your `Cargo.toml` as a dependency: + +```toml +[dependencies] +cryptify = "1.0.0" +``` + To install `rust-obfuscator`, clone the repository and build the tool using Cargo from the root: ``` cargo build --release --bin rust-obfuscator @@ -29,7 +36,11 @@ cp ./target/release/rust-obfuscator . # Usage The binary can be used on either a file or a directory. If provided with a directory it will only modify rust source files within that directory not any subdirectories +- Add to source code you'd like to modify +```rs +use cryptify; ``` +```sh rust-obfuscator path/to/your_project ``` - All Obfuscated code will be under the **obfuscated_code** directory that is created from the directory the tool was run. @@ -41,7 +52,7 @@ rust-obfuscator path/to/your_project - --var: Enables variable renaming source code obfuscation. ### Example usage with flag -``` +```sh rust-obfuscator path/to/your_project --no_flow ``` (disables flow obfuscation) diff --git a/cryptify/README.md b/cryptify/README.md index 2251add..59084a3 100644 --- a/cryptify/README.md +++ b/cryptify/README.md @@ -37,7 +37,7 @@ Hello World! ``` ## Example of expanded Flow_Stmt! -``` +```rs { let _is_dummy_145 = true; let _dummy_upper_bound = 100; From bb9265b63afd68fd1793b779bc2c36d54a5534c4 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 17:54:33 -0600 Subject: [PATCH 16/45] update docs --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7c0575c..c637b07 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ use cryptify; rust-obfuscator path/to/your_project ``` - All Obfuscated code will be under the **obfuscated_code** directory that is created from the directory the tool was run. +- **Recommended to use an auto-formatter with the obfuscated code as syn naturally modifies the structure and it will be written as one line** ## Option Flags - --no_string: Disables string obfuscation. @@ -58,4 +59,4 @@ rust-obfuscator path/to/your_project --no_flow (disables flow obfuscation) # License -labyrinth_macros is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. \ No newline at end of file +rust-obfuscator is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. \ No newline at end of file From 3e73ffee5ee80a2f295a573a33c8475fdd78afcb Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 17:55:40 -0600 Subject: [PATCH 17/45] update docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c637b07..3742135 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ## Features - **String Encryption**: Encrypts string literals in your code at compile time. -- **Control Flow Obfuscation**: Introduces compile-dummy dummy loops and random variables into control flows complicating the logic and structure of the code. +- **Control Flow Obfuscation**: Introduces compile-dummy dummy loops and random variables. - Note: for truly random control flow and variables, you can disable the insertion of the flow_macro using the **disable_macro** flag, but this will directly affect the source code. - **Customizable Obfuscation**: Offers flexibility to enable or disable specific obfuscation features based on your requirements. - **Variable Renaming**: Obfuscation of the source code directly, if you'd like to ship the code or just want to make your code look worse. From 457dea72fc43cbe82eaa0414c91b77d94c033fd2 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 19:34:21 -0600 Subject: [PATCH 18/45] cryptify docs typo --- cryptify/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cryptify/README.md b/cryptify/README.md index 59084a3..5413d5d 100644 --- a/cryptify/README.md +++ b/cryptify/README.md @@ -29,7 +29,7 @@ fn main(){ } ``` -Set the **CRYPTIFY_KEY** environment variable for custom encryption defaults to fixed key +Set the **CRYPTIFY_KEY** environment variable for custom encryption otherwise it defaults to defined fixed key ## Output ``` From e8f5f443cfd46ee5b58c06b23d21a728aeaf7277 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 19:58:27 -0600 Subject: [PATCH 19/45] fixed decrypt_string return --- cryptify/Cargo.toml | 2 +- cryptify/src/lib.rs | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/cryptify/Cargo.toml b/cryptify/Cargo.toml index 9568672..4db122a 100644 --- a/cryptify/Cargo.toml +++ b/cryptify/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptify" -version = "1.0.0" +version = "2.0.0" edition = "2021" authors = ["Pranav Dronavalli "] description = "A procedural macro library to obfuscate Rust code. Provides compile-time string encryption and random flow obfuscation." diff --git a/cryptify/src/lib.rs b/cryptify/src/lib.rs index 517343a..e2492e5 100644 --- a/cryptify/src/lib.rs +++ b/cryptify/src/lib.rs @@ -1,16 +1,19 @@ //re-export proc macro crate pub use labyrinth_macros::*; +use std::borrow::Cow; /// A helper decryption function meant to decrypt encrypted strings at runtime /// /// # Parameters /// - `input`: The encrypted string literal /// -pub fn decrypt_string(encrypted: &str) -> String { +pub fn decrypt_string(encrypted: &str) -> Cow<'static, str> { let key = std::env::var("CRYPTIFY_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); - encrypted + let decrypted: String = encrypted .chars() .zip(key.chars().cycle()) .map(|(encrypted_char, key_char)| ((encrypted_char as u8) ^ (key_char as u8)) as char) - .collect() + .collect(); + + Cow::Owned(decrypted) } From 39063aed9fb1ec3b57a9dc9669efa72984b6844e Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 19:59:43 -0600 Subject: [PATCH 20/45] bump cryptify version --- Cargo.lock | 8 ++++---- auto_obfuscate/Cargo.toml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7bf00a3..4ca3099 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -66,16 +66,16 @@ dependencies = [ [[package]] name = "cryptify" -version = "1.0.0" +version = "2.0.0" dependencies = [ "labyrinth_macros 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "cryptify" -version = "1.0.0" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9177f77c37d98aab6a95246f73a33333e124389dc9811d61d03a2abd0443f8ee" +checksum = "cf62efc903f2e3757ac352f080ae29a21f97cf5a254c4b8872ea6186c416f85d" dependencies = [ "labyrinth_macros 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -244,7 +244,7 @@ name = "rust-obfuscator" version = "1.0.0" dependencies = [ "clap", - "cryptify 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cryptify 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2", "quote", "rand", diff --git a/auto_obfuscate/Cargo.toml b/auto_obfuscate/Cargo.toml index 171e766..4221e7b 100644 --- a/auto_obfuscate/Cargo.toml +++ b/auto_obfuscate/Cargo.toml @@ -14,4 +14,4 @@ proc-macro2 = "1.0" rand = "0.8.0" regex = "1.5.4" clap = "3.0" -cryptify = "1.0.0" \ No newline at end of file +cryptify = "2.0.0" \ No newline at end of file From a0a137579792b4508a122d0f6be42228fe11c009 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 20:04:27 -0600 Subject: [PATCH 21/45] modify string to only replace expression literals' --- auto_obfuscate/src/string.rs | 18 +---------- auto_obfuscate/src/string/string_tests.rs | 38 ----------------------- 2 files changed, 1 insertion(+), 55 deletions(-) diff --git a/auto_obfuscate/src/string.rs b/auto_obfuscate/src/string.rs index 6a8ce17..72f0669 100644 --- a/auto_obfuscate/src/string.rs +++ b/auto_obfuscate/src/string.rs @@ -7,7 +7,6 @@ use syn::{ Expr, LitStr, Lit, - Macro, File, }; use quote::quote; @@ -47,6 +46,7 @@ impl StringObfuscator { strings_to_encrypt: 0, } } + #[allow(dead_code)] fn process_macro_tokens(&self, tokens: TokenStream) -> TokenStream { tokens .into_iter() @@ -122,22 +122,6 @@ impl VisitMut for StringObfuscator { syn::visit_mut::visit_expr_mut(self, expr); } - fn visit_macro_mut(&mut self, mac: &mut Macro) { - if self.enabled == false { - return; - } - //check to see if macro is not obfuscation macro - if - mac.path.segments.len() == 2 && - mac.path.segments[0].ident == "cryptify" && - mac.path.segments[1].ident == "encrypt_string" - { - return; - } - //encrypt string literal within macro - let new_tokens = self.process_macro_tokens(mac.tokens.clone()); - mac.tokens = new_tokens; - } } struct StringLiteralCounter { diff --git a/auto_obfuscate/src/string/string_tests.rs b/auto_obfuscate/src/string/string_tests.rs index 7499b1d..f30f143 100644 --- a/auto_obfuscate/src/string/string_tests.rs +++ b/auto_obfuscate/src/string/string_tests.rs @@ -1,23 +1,6 @@ #[cfg(test)] use super::*; -#[test] -fn test_replacement_in_macro() { - let code = r#" - fn main() { - println!("Hello, world!"); - } - "#; - let string_config = StringConfig::default(); - 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")); - - let parse_result = syn::parse_file(&obfuscated_code); - assert!(parse_result.is_ok(), "Modified code is not valid Rust code"); -} - #[test] fn test_replacement_in_expr() { let code = r#" @@ -34,24 +17,3 @@ fn test_replacement_in_expr() { let parse_result = syn::parse_file(&obfuscated_code); assert!(parse_result.is_ok(), "Modified code is not valid Rust code"); } - -#[test] -fn test_replacement_in_nested_macro() { - let code = - r#" - fn main() { - let num1 = 10; - let num2 = 20; - println!("Formatted: {}", format!("Num1: {}, Num2: {}", num1, num2)); - } -"#; - - let string_config = StringConfig::default(); - 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")); - - let parse_result = syn::parse_file(&obfuscated_code); - assert!(parse_result.is_ok(), "Modified code is not valid Rust code"); -} From ea02ce33e09186a54060b39de07b4a4d61a6871d Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 20:06:03 -0600 Subject: [PATCH 22/45] update readme --- cryptify/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cryptify/README.md b/cryptify/README.md index 5413d5d..70238be 100644 --- a/cryptify/README.md +++ b/cryptify/README.md @@ -21,11 +21,12 @@ cryptify = "1.0.0" # Usage ## Bring macro into scope -``` +```rs use cryptify; fn main(){ - println!(cryptify::encrypt_string("Hello, World!")); + let decrypted = cryptify::encrypt_string("Hello, World!"); + println!(decrypted); } ``` From d14939210dcd1c15f17a04dabd1dee1a340a1674 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 21:34:49 -0600 Subject: [PATCH 23/45] update encrypt_string macro --- labyrinth_macros/Cargo.toml | 2 +- labyrinth_macros/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/labyrinth_macros/Cargo.toml b/labyrinth_macros/Cargo.toml index 959e93a..1804eb2 100644 --- a/labyrinth_macros/Cargo.toml +++ b/labyrinth_macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "labyrinth_macros" -version = "2.0.0" +version = "3.0.0" edition = "2021" authors = ["Pranav Dronavalli "] description = "A procedural macro crate for the cryptify crate meant to provide obfuscation through compile time string encryption. not meant to be used standalone" diff --git a/labyrinth_macros/src/lib.rs b/labyrinth_macros/src/lib.rs index 15c80c9..1c9efd5 100644 --- a/labyrinth_macros/src/lib.rs +++ b/labyrinth_macros/src/lib.rs @@ -77,7 +77,7 @@ pub fn encrypt_string(input: TokenStream) -> TokenStream { let encrypted_string = xor_cipher(&string, &key); let output = quote! { - cryptify::decrypt_string(#encrypted_string) + cryptify::decrypt_string(#encrypted_string).as_ref() }; TokenStream::from(output) From f4d78106988923ec284523d849c9888b25a4e15b Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 21:38:39 -0600 Subject: [PATCH 24/45] bumped version of macro lib --- cryptify/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cryptify/Cargo.toml b/cryptify/Cargo.toml index 4db122a..40a2733 100644 --- a/cryptify/Cargo.toml +++ b/cryptify/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptify" -version = "2.0.0" +version = "3.0.0" edition = "2021" authors = ["Pranav Dronavalli "] description = "A procedural macro library to obfuscate Rust code. Provides compile-time string encryption and random flow obfuscation." @@ -10,4 +10,4 @@ repository = "https://github.com/dronavallipranav/rust-obfuscator/tree/main/laby # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -labyrinth_macros = "2.0.0" \ No newline at end of file +labyrinth_macros = "3.0.0" \ No newline at end of file From 8aedef0cbfea8d0ef0723ddc63e1638bcd6702bb Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 22:04:46 -0600 Subject: [PATCH 25/45] reset macro version --- cryptify/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cryptify/Cargo.toml b/cryptify/Cargo.toml index 40a2733..f549752 100644 --- a/cryptify/Cargo.toml +++ b/cryptify/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptify" -version = "3.0.0" +version = "3.0.4" edition = "2021" authors = ["Pranav Dronavalli "] description = "A procedural macro library to obfuscate Rust code. Provides compile-time string encryption and random flow obfuscation." @@ -10,4 +10,4 @@ repository = "https://github.com/dronavallipranav/rust-obfuscator/tree/main/laby # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -labyrinth_macros = "3.0.0" \ No newline at end of file +labyrinth_macros = "2.0.0" \ No newline at end of file From 520155254b7950e3430b5bcf6ff6197b9c432da3 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 22:05:08 -0600 Subject: [PATCH 26/45] revert to String --- cryptify/src/lib.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/cryptify/src/lib.rs b/cryptify/src/lib.rs index e2492e5..c84d92b 100644 --- a/cryptify/src/lib.rs +++ b/cryptify/src/lib.rs @@ -7,13 +7,11 @@ use std::borrow::Cow; /// # Parameters /// - `input`: The encrypted string literal /// -pub fn decrypt_string(encrypted: &str) -> Cow<'static, str> { +pub fn decrypt_string(encrypted: &str) -> String { let key = std::env::var("CRYPTIFY_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); - let decrypted: String = encrypted + encrypted .chars() .zip(key.chars().cycle()) .map(|(encrypted_char, key_char)| ((encrypted_char as u8) ^ (key_char as u8)) as char) - .collect(); - - Cow::Owned(decrypted) + .collect() } From fcb457e6e97d12af33dfc7a2a0f6c90c157f0f65 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 22:22:40 -0600 Subject: [PATCH 27/45] remove unused import --- cryptify/Cargo.toml | 2 +- cryptify/src/lib.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/cryptify/Cargo.toml b/cryptify/Cargo.toml index f549752..6705492 100644 --- a/cryptify/Cargo.toml +++ b/cryptify/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptify" -version = "3.0.4" +version = "3.0.6" edition = "2021" authors = ["Pranav Dronavalli "] description = "A procedural macro library to obfuscate Rust code. Provides compile-time string encryption and random flow obfuscation." diff --git a/cryptify/src/lib.rs b/cryptify/src/lib.rs index c84d92b..517343a 100644 --- a/cryptify/src/lib.rs +++ b/cryptify/src/lib.rs @@ -1,6 +1,5 @@ //re-export proc macro crate pub use labyrinth_macros::*; -use std::borrow::Cow; /// A helper decryption function meant to decrypt encrypted strings at runtime /// From 62b017659a060f95224011966254c0b80be6f05f Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 22:23:29 -0600 Subject: [PATCH 28/45] modify string to only replace literals in local var --- Cargo.lock | 18 +++++++------- auto_obfuscate/Cargo.toml | 2 +- auto_obfuscate/src/string.rs | 30 +++++++++-------------- auto_obfuscate/src/string/string_tests.rs | 17 +++++++++++++ 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4ca3099..c1bed4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -66,18 +66,18 @@ dependencies = [ [[package]] name = "cryptify" -version = "2.0.0" +version = "3.0.4" dependencies = [ - "labyrinth_macros 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "labyrinth_macros 2.0.0", ] [[package]] name = "cryptify" -version = "2.0.0" +version = "3.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf62efc903f2e3757ac352f080ae29a21f97cf5a254c4b8872ea6186c416f85d" +checksum = "7b6425aa5e7cb5b5254a68905755a2c974094bbc7859155cd9b281f1db346c56" dependencies = [ - "labyrinth_macros 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "labyrinth_macros 2.0.0", ] [[package]] @@ -119,6 +119,8 @@ dependencies = [ [[package]] name = "labyrinth_macros" version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a49a429784bf214985bd5e5c058a89889a2e5b9fc60ebd60ced2bf0429791fa4" dependencies = [ "proc-macro2", "quote", @@ -128,9 +130,7 @@ dependencies = [ [[package]] name = "labyrinth_macros" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a49a429784bf214985bd5e5c058a89889a2e5b9fc60ebd60ced2bf0429791fa4" +version = "3.0.0" dependencies = [ "proc-macro2", "quote", @@ -244,7 +244,7 @@ name = "rust-obfuscator" version = "1.0.0" dependencies = [ "clap", - "cryptify 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cryptify 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2", "quote", "rand", diff --git a/auto_obfuscate/Cargo.toml b/auto_obfuscate/Cargo.toml index 4221e7b..6cda64a 100644 --- a/auto_obfuscate/Cargo.toml +++ b/auto_obfuscate/Cargo.toml @@ -14,4 +14,4 @@ proc-macro2 = "1.0" rand = "0.8.0" regex = "1.5.4" clap = "3.0" -cryptify = "2.0.0" \ No newline at end of file +cryptify = "3.0.6" \ No newline at end of file diff --git a/auto_obfuscate/src/string.rs b/auto_obfuscate/src/string.rs index 72f0669..f81248d 100644 --- a/auto_obfuscate/src/string.rs +++ b/auto_obfuscate/src/string.rs @@ -2,12 +2,14 @@ use syn::{ visit_mut::VisitMut, visit::Visit, parse_file, - parse2, parse_str, Expr, + ExprLit, LitStr, Lit, File, + Local, + parse_quote, }; use quote::quote; use proc_macro2::{ TokenStream, TokenTree }; @@ -100,27 +102,17 @@ impl StringObfuscator { impl VisitMut for StringObfuscator { //replace all string literals with call to obfuscation macro - fn visit_expr_mut(&mut self, expr: &mut Expr) { - if - !self.enabled || - (self.encrypted_count >= self.strings_to_encrypt && self.percentage != 100) - { - return; - } - if let Expr::Lit(expr_lit) = expr { - if let Lit::Str(_) = &expr_lit.lit { - //replace string literal with macro call - let macro_call = - quote! { - cryptify::encrypt_string!(#expr_lit) - }; - self.encrypted_count += 1; - //replace expression to use macro call - *expr = parse2(macro_call).expect("Failed to parse macro call"); + fn visit_local_mut(&mut self, local: &mut Local) { + if let Some(local_init) = &mut local.init { + //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) }; + let new_expr: Expr = parse_quote!(#encrypted); + *local_init.expr = *Box::new(new_expr); } } - syn::visit_mut::visit_expr_mut(self, expr); + syn::visit_mut::visit_local_mut(self, local); } } diff --git a/auto_obfuscate/src/string/string_tests.rs b/auto_obfuscate/src/string/string_tests.rs index f30f143..b4c178b 100644 --- a/auto_obfuscate/src/string/string_tests.rs +++ b/auto_obfuscate/src/string/string_tests.rs @@ -17,3 +17,20 @@ fn test_replacement_in_expr() { let parse_result = syn::parse_file(&obfuscated_code); assert!(parse_result.is_ok(), "Modified code is not valid Rust code"); } + +#[test] +fn test_no_macro() { + let code = + r#" + fn main() { + println!("Hello, world!"); + let word_re = Regex::new(r"\b\w+\b").unwrap(); + } + "#; + let string_config = StringConfig::default(); + let mut string_obfuscator = StringObfuscator::new(string_config); + let obfuscated_code = string_obfuscator.obfuscate_strings(code); + assert!(!obfuscated_code.contains("encrypt_string")); + let parse_result = syn::parse_file(&obfuscated_code); + assert!(parse_result.is_ok(), "Modified code is not valid Rust code"); +} From e8d5ee94c5aa342ab42f8a15ce46141ba7f665bf Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 22:25:30 -0600 Subject: [PATCH 29/45] update readme --- cryptify/Cargo.toml | 2 +- cryptify/README.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cryptify/Cargo.toml b/cryptify/Cargo.toml index 6705492..1e6f160 100644 --- a/cryptify/Cargo.toml +++ b/cryptify/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptify" -version = "3.0.6" +version = "3.0.8" edition = "2021" authors = ["Pranav Dronavalli "] description = "A procedural macro library to obfuscate Rust code. Provides compile-time string encryption and random flow obfuscation." diff --git a/cryptify/README.md b/cryptify/README.md index 70238be..daa162f 100644 --- a/cryptify/README.md +++ b/cryptify/README.md @@ -27,6 +27,7 @@ use cryptify; fn main(){ let decrypted = cryptify::encrypt_string("Hello, World!"); println!(decrypted); + println!("{}", cryptify::encrypt_string!("formatted!")); } ``` @@ -35,6 +36,7 @@ Set the **CRYPTIFY_KEY** environment variable for custom encryption otherwise it ## Output ``` Hello World! +formatted! ``` ## Example of expanded Flow_Stmt! From 5263e49e309f6f26b09af0d4fe7c06eee97b955f Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 22:35:20 -0600 Subject: [PATCH 30/45] update docs, prep release --- Cargo.lock | 8 ++++---- README.md | 8 ++++++-- auto_obfuscate/Cargo.toml | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c1bed4e..5b97e71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -66,16 +66,16 @@ dependencies = [ [[package]] name = "cryptify" -version = "3.0.4" +version = "3.0.8" dependencies = [ "labyrinth_macros 2.0.0", ] [[package]] name = "cryptify" -version = "3.0.4" +version = "3.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b6425aa5e7cb5b5254a68905755a2c974094bbc7859155cd9b281f1db346c56" +checksum = "5d7875a5ccecb7451ad5542c0394e5fd7507c2be0ed2b88cbe96a35809f6f26b" dependencies = [ "labyrinth_macros 2.0.0", ] @@ -244,7 +244,7 @@ name = "rust-obfuscator" version = "1.0.0" dependencies = [ "clap", - "cryptify 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "cryptify 3.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2", "quote", "rand", diff --git a/README.md b/README.md index 3742135..154484a 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,11 @@ ## Features -- **String Encryption**: Encrypts string literals in your code at compile time. +- **String Encryption**: Automatically encrypts string literals assigned to local variables at compile time. + - Can also be used for formatted strings, but currently requires manual placement + ```rs + println!("{}", cryptify::encrypt_string!("hello!")); + ``` - **Control Flow Obfuscation**: Introduces compile-dummy dummy loops and random variables. - Note: for truly random control flow and variables, you can disable the insertion of the flow_macro using the **disable_macro** flag, but this will directly affect the source code. - **Customizable Obfuscation**: Offers flexibility to enable or disable specific obfuscation features based on your requirements. @@ -22,7 +26,7 @@ Add `cryptify` to your `Cargo.toml` as a dependency: ```toml [dependencies] -cryptify = "1.0.0" +cryptify = "3.0.8" ``` To install `rust-obfuscator`, clone the repository and build the tool using Cargo from the root: diff --git a/auto_obfuscate/Cargo.toml b/auto_obfuscate/Cargo.toml index 6cda64a..11ec39c 100644 --- a/auto_obfuscate/Cargo.toml +++ b/auto_obfuscate/Cargo.toml @@ -14,4 +14,4 @@ proc-macro2 = "1.0" rand = "0.8.0" regex = "1.5.4" clap = "3.0" -cryptify = "3.0.6" \ No newline at end of file +cryptify = "3.0.8" \ No newline at end of file From 4f81927f8ceacf30b6b0d7c92fb4f90160ba9444 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli <62522813+dronavallipranav@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:37:26 -0600 Subject: [PATCH 31/45] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 154484a..1239df2 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ println!("{}", cryptify::encrypt_string!("hello!")); ``` - **Control Flow Obfuscation**: Introduces compile-dummy dummy loops and random variables. -- Note: for truly random control flow and variables, you can disable the insertion of the flow_macro using the **disable_macro** flag, but this will directly affect the source code. + - Note: for truly random control flow and variables, you can disable the insertion of the flow_macro using the **disable_macro** flag, but this will directly affect the source code. - **Customizable Obfuscation**: Offers flexibility to enable or disable specific obfuscation features based on your requirements. - **Variable Renaming**: Obfuscation of the source code directly, if you'd like to ship the code or just want to make your code look worse. @@ -63,4 +63,4 @@ rust-obfuscator path/to/your_project --no_flow (disables flow obfuscation) # License -rust-obfuscator is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. \ No newline at end of file +rust-obfuscator is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. From 7f99f34b8ee7287cb8b6c74bbf0f26aa59891416 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli <62522813+dronavallipranav@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:49:24 -0600 Subject: [PATCH 32/45] Update README.md --- README.md | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1239df2..0bf9911 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ use cryptify; rust-obfuscator path/to/your_project ``` - All Obfuscated code will be under the **obfuscated_code** directory that is created from the directory the tool was run. -- **Recommended to use an auto-formatter with the obfuscated code as syn naturally modifies the structure and it will be written as one line** +- **Recommended to use a Rust Formatter with the obfuscated code as syn naturally modifies the structure and it will be written to the file as one line** ## Option Flags - --no_string: Disables string obfuscation. @@ -62,5 +62,86 @@ rust-obfuscator path/to/your_project --no_flow ``` (disables flow obfuscation) +#Input +-running the tool with no config +```rs +use cryptify; +mod word_counter; +use std::env; +use std::fs; +use word_counter::count_words; +fn main() { + let b = "Hello World"; + println!("{}", b); + let args: Vec = env::args().collect(); + if args.len() < 2 { + eprintln!("Usage: {} ", args[0]); + return; + } + let filename = &args[1]; + let content = fs::read_to_string(filename).expect("Could not read file"); + let word_counts = count_words(&content); + for (word, count) in word_counts.iter() { + println!("{}: {}", word, count); + } +} + +fn dummy() { + let a = 1; + let b = 2; + let c = a + b; + println!("{}", c); +} + +fn calc_sum(a: i32, b: i32) -> i32 { + cryptify::flow_stmt!(); + let c = a + b; + c +} + +fn helloooo(){ + println!("hi"); +} + +``` +#Output +```rs +use cryptify; +mod word_counter; +use std::env; +use std::fs; +use word_counter::count_words; +fn main() { + cryptify::flow_stmt!(); + let b = cryptify::encrypt_string!("Hello World"); + println!("{}", b); + let args: Vec = env::args().collect(); + if args.len() < 2 { + eprintln!("Usage: {} ", args[0]); + return; + } + let filename = &args[1]; + let content = fs::read_to_string(filename).expect("Could not read file"); + let word_counts = count_words(&content); + for (word, count) in word_counts.iter() { + println!("{}: {}", word, count); + } +} +fn dummy() { + cryptify::flow_stmt!(); + let a = 1; + let b = 2; + let c = a + b; + println!("{}", c); +} +fn calc_sum(a: i32, b: i32) -> i32 { + cryptify::flow_stmt!(); + let c = a + b; + c +} +fn helloooo() { + println!("hi"); +} +``` # License rust-obfuscator is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. From f4545b71968ae2016f6b145a51f6ac77a45c10bc Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli <62522813+dronavallipranav@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:57:53 -0600 Subject: [PATCH 33/45] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0bf9911..00ae715 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ The binary can be used on either a file or a directory. If provided with a direc use cryptify; ``` ```sh -rust-obfuscator path/to/your_project +./rust-obfuscator path/to/your_project ``` - All Obfuscated code will be under the **obfuscated_code** directory that is created from the directory the tool was run. - **Recommended to use a Rust Formatter with the obfuscated code as syn naturally modifies the structure and it will be written to the file as one line** From 654531fe77b1b3a3879dd85040bf2823535250b7 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli <62522813+dronavallipranav@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:59:37 -0600 Subject: [PATCH 34/45] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 00ae715..a8ce4a0 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,12 @@ cp ./target/release/rust-obfuscator . ``` # Usage -The binary can be used on either a file or a directory. If provided with a directory it will only modify rust source files within that directory not any subdirectories +Set the **CRYPTIFY_KEY** environment variable for custom encryption otherwise it defaults to defined fixed key - Add to source code you'd like to modify ```rs use cryptify; ``` +The binary can be used on either a file or a directory. If provided with a directory it will only modify rust source files within that directory not any subdirectories ```sh ./rust-obfuscator path/to/your_project ``` From e8ccf1edd5031e05cb82af41432db26221b00a4e Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 23:01:37 -0600 Subject: [PATCH 35/45] update cli help --- auto_obfuscate/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/auto_obfuscate/src/main.rs b/auto_obfuscate/src/main.rs index dff3342..05e6184 100644 --- a/auto_obfuscate/src/main.rs +++ b/auto_obfuscate/src/main.rs @@ -10,9 +10,9 @@ use std::fs; fn main() { //default config let mut config = Config::default(); - let matches = App::new("Rust Obfuscator") + let matches = App::new("Rust Automatic Obfuscator") .version("1.0") - .author("Your Name") + .author("Pranav Dronavalli") .about("Obfuscates Rust source code") .arg( Arg::with_name("path") From de6e40ea97e9ed844f6d52f7c7b933a710260eaa Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 23:02:54 -0600 Subject: [PATCH 36/45] update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a8ce4a0..f9440ab 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ rust-obfuscator path/to/your_project --no_flow ``` (disables flow obfuscation) -#Input +# Input -running the tool with no config ```rs use cryptify; @@ -105,7 +105,7 @@ fn helloooo(){ } ``` -#Output +# Output ```rs use cryptify; mod word_counter; From 04369eb6bccde09eef181505a5eb95b9bd87a198 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli <62522813+dronavallipranav@users.noreply.github.com> Date: Tue, 9 Jan 2024 23:03:53 -0600 Subject: [PATCH 37/45] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f9440ab..475bfb2 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ `rust-obfuscator` is a set of tools designed to automatically obfuscate Rust source code by automatically inserting procedural macros or by (optionally) providing the obfuscation in the source code directly. For more fine-grained obfuscation a procedural macro library [cryptify](https://crates.io/crates/cryptify) is also provided. ## Currently Supported -1. String literal encryption +1. string literal encryption 2. control-flow obfuscation 3. control-flow obfuscation (source code) 4. variable renaming (source code) From f18e83f03fafc72df79e8e37d234211de3095e13 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli <62522813+dronavallipranav@users.noreply.github.com> Date: Tue, 9 Jan 2024 23:25:51 -0600 Subject: [PATCH 38/45] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 475bfb2..aafdfc8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Rust-Obfuscator -`rust-obfuscator` is a set of tools designed to automatically obfuscate Rust source code by automatically inserting procedural macros or by (optionally) providing the obfuscation in the source code directly. For more fine-grained obfuscation a procedural macro library [cryptify](https://crates.io/crates/cryptify) is also provided. +`rust-obfuscator` is a set of tools designed to automatically obfuscate Rust source code by inserting procedural macros or by (optionally) providing the obfuscation in the source code directly. For more fine-grained obfuscation a procedural macro library [cryptify](https://crates.io/crates/cryptify) is also provided. ## Currently Supported 1. string literal encryption From 240a21a41d98875b9bcf077ab72b54da83ae6b98 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Tue, 9 Jan 2024 23:57:39 -0600 Subject: [PATCH 39/45] update cryptify doc --- cryptify/README.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/cryptify/README.md b/cryptify/README.md index daa162f..8cca009 100644 --- a/cryptify/README.md +++ b/cryptify/README.md @@ -9,15 +9,6 @@ - **String Obfuscation**: Automatically encrypts string literals in your code at compile time, making them harder to read and understand. - **Flow Obfuscation**: Introduces dummy loops and random variables into control flows, enhancing the overall obfuscation of the logic. -# Installation - -Add `cryptify` to your `Cargo.toml` as a dependency: - -```toml -[dependencies] -cryptify = "1.0.0" -``` - # Usage ## Bring macro into scope From 391211bda431284a8cb6caba14da9ccc097b33a5 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Wed, 10 Jan 2024 00:00:03 -0600 Subject: [PATCH 40/45] update cryptify version --- cryptify/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cryptify/Cargo.toml b/cryptify/Cargo.toml index 1e6f160..faf116e 100644 --- a/cryptify/Cargo.toml +++ b/cryptify/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptify" -version = "3.0.8" +version = "3.1.0" edition = "2021" authors = ["Pranav Dronavalli "] description = "A procedural macro library to obfuscate Rust code. Provides compile-time string encryption and random flow obfuscation." @@ -10,4 +10,4 @@ repository = "https://github.com/dronavallipranav/rust-obfuscator/tree/main/laby # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -labyrinth_macros = "2.0.0" \ No newline at end of file +labyrinth_macros = "2.0.0" From 3d7c30ce1aa3d79d16faa0a68f2a7968d016c348 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Wed, 10 Jan 2024 00:01:04 -0600 Subject: [PATCH 41/45] bumped cryptify version --- README.md | 2 +- auto_obfuscate/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aafdfc8..a3023b4 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Add `cryptify` to your `Cargo.toml` as a dependency: ```toml [dependencies] -cryptify = "3.0.8" +cryptify = "3.1.0" ``` To install `rust-obfuscator`, clone the repository and build the tool using Cargo from the root: diff --git a/auto_obfuscate/Cargo.toml b/auto_obfuscate/Cargo.toml index 11ec39c..d4d16f4 100644 --- a/auto_obfuscate/Cargo.toml +++ b/auto_obfuscate/Cargo.toml @@ -14,4 +14,4 @@ proc-macro2 = "1.0" rand = "0.8.0" regex = "1.5.4" clap = "3.0" -cryptify = "3.0.8" \ No newline at end of file +cryptify = "3.1.0" \ No newline at end of file From 214aa48d5a1adf3353f43c71f62d97abbcdc3577 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli <62522813+dronavallipranav@users.noreply.github.com> Date: Wed, 10 Jan 2024 16:49:16 -0600 Subject: [PATCH 42/45] Update README.md --- README.md | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a3023b4..6a0db97 100644 --- a/README.md +++ b/README.md @@ -107,11 +107,6 @@ fn helloooo(){ ``` # Output ```rs -use cryptify; -mod word_counter; -use std::env; -use std::fs; -use word_counter::count_words; fn main() { cryptify::flow_stmt!(); let b = cryptify::encrypt_string!("Hello World"); @@ -144,5 +139,99 @@ fn helloooo() { println!("hi"); } ``` +## Expanded Output +```rs +fn main() { + { + let _is_dummy_145 = true; + let _dummy_upper_bound = 100; + let _random_dummy_var = 1; + let mut _dummy_counter = 6i32; + let _dummy_increment = 2i32; + loop { + if _dummy_counter > _dummy_upper_bound { + break; + } + unsafe { + std::ptr::write_volatile( + &mut _dummy_counter, + _dummy_counter + _dummy_increment, + ); + } + } + }; + let b = cryptify::decrypt_string("0\u{b}\r\u{1f}\tFd\u{18}\u{11}\t\0"); + { + ::std::io::_print(format_args!("{0}\n", b)); + }; + let args: Vec = env::args().collect(); + if args.len() < 2 { + { + ::std::io::_eprint(format_args!("Usage: {0} \n", args[0])); + }; + return; + } + let filename = &args[1]; + let content = fs::read_to_string(filename).expect("Could not read file"); + let word_counts = count_words(&content); + for (word, count) in word_counts.iter() { + { + ::std::io::_print(format_args!("{0}: {1}\n", word, count)); + }; + } +} +fn dummy() { + { + let _is_dummy_145 = true; + let mut _dummy_counter = 4i32; + let _dummy_upper_bound = 100; + let _dummy_increment = 3i32; + loop { + if _dummy_counter > _dummy_upper_bound { + break; + } + unsafe { + std::ptr::write_volatile( + &mut _dummy_counter, + _dummy_counter + _dummy_increment, + ); + } + } + }; + let a = 1; + let b = 2; + let c = a + b; + { + ::std::io::_print(format_args!("{0}\n", c)); + }; +} +fn calc_sum(a: i32, b: i32) -> i32 { + { + let _is_dummy_145 = true; + let mut _dummy_counter = 8i32; + let _dummy_increment = 3i32; + let _extra_dummy_var = 4i32; + let _dummy_upper_bound = 100; + loop { + if _dummy_counter > _dummy_upper_bound { + break; + } + unsafe { + std::ptr::write_volatile( + &mut _dummy_counter, + _dummy_counter + _dummy_increment, + ); + } + } + }; + let c = a + b; + c +} +fn helloooo() { + { + ::std::io::_print(format_args!("hi\n")); + }; +} +``` # License rust-obfuscator is licensed under the MIT License - see the [LICENSE](https://github.com/dronavallipranav/rust-obfuscator/blob/main/LICENSE) file for details. From e5c474b722cfdb60ca3f24cafa9a69701f104691 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli <62522813+dronavallipranav@users.noreply.github.com> Date: Wed, 10 Jan 2024 16:49:44 -0600 Subject: [PATCH 43/45] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 6a0db97..4f5c98a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ println!("{}", cryptify::encrypt_string!("hello!")); ``` - **Control Flow Obfuscation**: Introduces compile-dummy dummy loops and random variables. - - Note: for truly random control flow and variables, you can disable the insertion of the flow_macro using the **disable_macro** flag, but this will directly affect the source code. - **Customizable Obfuscation**: Offers flexibility to enable or disable specific obfuscation features based on your requirements. - **Variable Renaming**: Obfuscation of the source code directly, if you'd like to ship the code or just want to make your code look worse. From 32aa4c9ef5ef8a3e56d3d3a37ed922e924b78ff7 Mon Sep 17 00:00:00 2001 From: Lucas Scharenbroch Date: Mon, 15 Jan 2024 11:51:17 -0600 Subject: [PATCH 44/45] 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 45/45] 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);