litcrypt

repository·master·Indexed 42 days ago

https://github.com/anvie/litcrypt.rs

A Rust procedural macro for encrypting string literals at compile time using an XOR method. It protects sensitive strings from static analysis tools by keeping them encrypted in the binary on disk and in memory during execution. Version 0.4.0 provides the lc! macro for string encryption and the use_litcrypt!() macro for initialization, with support for the LITCRYPT_ENCRYPT_KEY environment variable.

Tokens
831
Snippets
5
Records
6
Agent score
4%

What's inside litcrypt

  1. Encrypt strings with the lc! macro

    master

    To protect strings from static analysis, use the lc! macro. This encrypts strings at compile time using a basic XOR method, keeping them encrypted in both disk and memory during runtime.

    Setup Steps:

    1. Set the encryption key: Before compiling, you must set the LITCRYPT_ENCRYPT_KEY environment variable. This key is used to encrypt your literal strings and is itself encrypted to remain hidden from static analyzers.
    2. Initialize the macro: Call the use_litcrypt!() macro at the top of your crate to initialize the encryption logic.
    3. Use the macro: Wrap your plain text strings in the lc! macro.

    Example Usage:

    #[macro_use]
    extern crate litcrypt;
    
    use_litcrypt!();
    
    fn main(){
        // The string "Voldemort" will be encrypted at compile time
        println!("his name is: {}", lc!("Voldemort"));
    }
    #[macro_use]
    extern crate litcrypt;
    
    use_litcrypt!();
    
    fn main(){
        println!("his name is: {}", lc!("Voldemort"));
    }
  2. Verify string encryption with strings

    master

    You can verify that your strings are successfully hidden from static analysis tools (like strings or Hexeditors) by searching for the plain text in your compiled binary. If the encryption worked, the plain text should not appear in the output.

    # Replace target/debug/my_valuable_app with your actual binary path
    strings target/debug/my_valuable_app | grep Voldemort
  3. Override the encryption key using LITCRYPT_ENCRYPT_KEY

    master

    Instead of passing a key directly to use_litcrypt!, you can set the LITCRYPT_ENCRYPT_KEY environment variable. This is useful for CI/CD pipelines or keeping secrets out of the source code.

    Example:

    export LITCRYPT_ENCRYPT_KEY="myverysuperdupermegaultrasecretkey"
  4. Initialize LitCrypt with use_litcrypt!

    master

    The use_litcrypt! macro must be called once to initialize the encryption system. It accepts a secret key as a string literal. This key is used to encrypt all subsequent strings wrapped in the lc! macro. The key itself is also encrypted at compile time to prevent it from being visible to static analysis tools.

    Note: You can also provide the key via the LITCRYPT_ENCRYPT_KEY environment variable to avoid hardcoding it in your source code.

    #[macro_use]
    extern crate litcrypt;
    
    // Initialize with a secret key
    use_litcrypt!("MY-SECRET-SPELL");
  5. Encrypt string literals with lc!

    master

    The lc! macro encrypts the provided string literal at compile time using the key established by use_litcrypt!. The resulting string remains encrypted in the binary and in memory, and is only decrypted to plain text at runtime when the macro is invoked.

    #[macro_use]
    extern crate litcrypt;
    
    use_litcrypt!("MY-SECRET-SPELL");
    
    fn main() {
        // The string "Voldemort" is encrypted at compile time
        println!("his name is: {}", lc!("Voldemort"));
    }