python-obfuscator 1.0.0

Python Obfuscator Web API interface can help you obfuscate and protect Python .py source code against analysis, reverse engineering and technology theft. Python Obfuscator provides advanced Python source code parsing based on AST trees, multiple advanced obfuscation, virtualization and protection strategies are available.
Documentation
/******************************************************************************
 * Python Obfuscator WebApi interface usage example.
 *
 * In this example we will obfuscate sample source with every public option
 * set, so you can see the full client surface and turn flags on or off.
 *
 * Version        : v1.0.0
 * Language       : Rust
 * Author         : Bartosz Wójcik
 * Web page       : https://www.pelock.com
 *
 *****************************************************************************/

use python_obfuscator::{
    CodeVirtualization, PythonObfuscator, PythonObfuscatorResponse, RenameStyle,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    //
    // include Python Obfuscator class
    //

    //
    // when developing from a repository clone without installing the package use:
    //
    // use python_obfuscator::{PythonObfuscator, PythonObfuscatorResponse};
    // (path dependency in Cargo.toml pointing to this crate)

    //
    // create Python Obfuscator class instance (we are using our activation key)
    //
    let mut my_python_obfuscator =
        PythonObfuscator::new(Some("ABCD-ABCD-ABCD-ABCD".to_string()));

    //
    // should the source code be compressed (both input & compressed)
    //
    my_python_obfuscator.enable_compression = false;

    //
    // globals
    //
    // fixed random seed for reproducible obfuscation output (optional)
    //
    my_python_obfuscator.seed = None;

    //
    // randomization density / intensity (0-100)
    //
    my_python_obfuscator.randomization_density = None;

    //
    // identifier renaming style: RenameStyle::Il, O0, Confusable, Hex, Homoglyph, or Mangled
    //
    my_python_obfuscator.rename_style = Some(RenameStyle::Il);

    //
    // code virtualization (exactly one): Vm, Fsa, or Flat
    // set to None to skip virtualization
    //
    my_python_obfuscator.code_virtualization = Some(CodeVirtualization::Vm);

    //
    // protection (opt-in on the service for --all; shown enabled here so every flag is visible)
    //
    // protection against tampering with protected code (integrity verification)
    //
    my_python_obfuscator.self_defending = true;

    //
    // protection linker (decoy call graph; requires self_defending)
    //
    my_python_obfuscator.protection_linker = true;

    //
    // insert anti-debugging detections
    //
    my_python_obfuscator.detect_debugger = true;

    //
    // insert virtual machine (anti-VM) detections
    //
    my_python_obfuscator.anti_vm = true;

    //
    // insert anti-sandbox detections
    //
    my_python_obfuscator.anti_sandbox = true;

    //
    // insert anti-emulators (CPU) detections
    //
    my_python_obfuscator.anti_emulator = true;

    //
    // renaming
    //
    // rename variable names to random string values
    //
    my_python_obfuscator.rename_variables = true;

    //
    // rename parameter names to random string values
    //
    my_python_obfuscator.rename_parameters = true;

    //
    // rename function names to random string values
    //
    my_python_obfuscator.rename_functions = true;

    //
    // rename function call references consistently with renamed functions
    //
    my_python_obfuscator.rename_function_calls = true;

    //
    // shuffle function order in the output source
    //
    my_python_obfuscator.shuffle_functions = true;

    //
    // fold/resolve constant expressions at obfuscation time
    //
    my_python_obfuscator.resolve_constants = true;

    //
    // strings
    //
    // split strings into concatenated chunks
    //
    my_python_obfuscator.split_strings = true;

    //
    // apply light transformations/mutations to string literals
    //
    my_python_obfuscator.modify_strings = true;

    //
    // encrypt strings using randomly generated polymorphic encryption algorithms
    //
    my_python_obfuscator.encrypt_strings = true;

    //
    // store string fragments in char-code array vaults
    //
    my_python_obfuscator.string_char_array_vault = true;

    //
    // numeric
    //
    // encrypt integers
    //
    my_python_obfuscator.encrypt_integers = true;

    //
    // encrypt floating point numbers
    //
    my_python_obfuscator.encrypt_floating = true;

    //
    // replace binary operators with mixed boolean-arithmetic (MBA) equivalents
    //
    my_python_obfuscator.mba_binops = true;

    //
    // represent integers via floating-point math
    //
    my_python_obfuscator.integers_to_floating = true;

    //
    // move integers to arrays
    //
    my_python_obfuscator.integers_to_arrays = true;

    //
    // move floats to arrays
    //
    my_python_obfuscator.floats_to_arrays = true;

    //
    // apply redundant xor / affine integer masks
    //
    my_python_obfuscator.affine_integer_mask = true;

    //
    // encrypted array literals
    //
    // encrypt integer array literals
    //
    my_python_obfuscator.array_int_crypt = true;

    //
    // encrypt character array literals
    //
    my_python_obfuscator.array_char_crypt = true;

    //
    // encrypt floating-point array literals
    //
    my_python_obfuscator.array_double_crypt = true;

    //
    // encrypt string array literals
    //
    my_python_obfuscator.array_string_crypt = true;

    //
    // decoy value pools
    //
    // insert a shared bucket of random noise values used by other strategies
    //
    my_python_obfuscator.insert_random_value_bucket = true;

    //
    // populate the random value bucket with decoy integers
    //
    my_python_obfuscator.random_bucket_integers = true;

    //
    // populate the random value bucket with decoy arrays
    //
    my_python_obfuscator.random_bucket_arrays = true;

    //
    // populate the random value bucket with decoy functions
    //
    my_python_obfuscator.random_bucket_functions = true;

    //
    // populate the random value bucket with decoy characters
    //
    my_python_obfuscator.random_bucket_characters = true;

    //
    // populate the random value bucket with anti-regex decoy noise
    //
    my_python_obfuscator.random_bucket_anti_regex = true;

    //
    // populate the random value bucket with autostart decoy stubs
    //
    my_python_obfuscator.random_bucket_autostart = true;

    //
    // opaque predicates & noise
    //
    // rewrite selected statements using ternary operators
    //
    my_python_obfuscator.insert_ternary_operators = true;

    //
    // replace boolean conditions with equivalent complex expressions
    //
    my_python_obfuscator.complexify_booleans = true;

    //
    // insert opaque predicate branches
    //
    my_python_obfuscator.opaque_branches = true;

    //
    // insert opaque mixer chains into control flow
    //
    my_python_obfuscator.opaque_mixer_chain = true;

    //
    // insert dead code
    //
    my_python_obfuscator.insert_dead_code = true;

    //
    // wrap code in try/finally blocks with dead noise
    //
    my_python_obfuscator.try_finally_noise = true;

    //
    // decoys
    //
    // insert decoy functions
    //
    my_python_obfuscator.decoy_functions = true;

    //
    // insert decoy lambda expressions
    //
    my_python_obfuscator.lambda_decoys = true;

    //
    // insert literal padding noise
    //
    my_python_obfuscator.literal_padding = true;

    //
    // insert fake import statement markers
    //
    my_python_obfuscator.fake_import_markers = true;

    //
    // use dynamic getattr()-based indirect calls
    //
    my_python_obfuscator.dynamic_getattr_calls = true;

    //
    // rewrite absolute imports into __import__ / getattr
    //
    my_python_obfuscator.obfuscate_imports = true;

    //
    // insert dead callback/event registration stubs
    //
    my_python_obfuscator.callback_registration_stubs = true;

    //
    // other
    //
    // strip comments from the output source
    //
    my_python_obfuscator.remove_comments = true;

    //
    // source code in Python format
    //
    let script_source_code = r#"label = 'SecretKey'
port = 443

def get_sum(a, b):
    return a + b

print(label, port)
r = get_sum(11, 31)
print(r)"#;

    //
    // obfuscate the source code with the options set above
    //
    let result = my_python_obfuscator
        .obfuscate_script_source(script_source_code, true)
        .await;

    //
    // it's also possible to pass a Python script file path instead of a string e.g.
    //
    // let result = my_python_obfuscator
    //     .obfuscate_script_file(std::path::Path::new("/path/to/project/script.py"), true)
    //     .await;

    //
    // result object holds the obfuscation results as well as other information
    //
    // result.error         - error code
    // result.output        - obfuscated code
    // result.demo          - demo mode (empty/invalid key); strategy flags ignored; always integers_to_arrays, mba_binops, encrypt_strings (no virt; 1000 char limit)
    // result.license_expiration - license end date (Y-m-d), empty if none
    // result.usages_total  - total obfuscations for this activation code
    //
    match result {
        Some(PythonObfuscatorResponse::Object(obj)) => {
            //
            // display obfuscated code
            //
            if obj.error == PythonObfuscator::ERROR_SUCCESS {
                println!("{}", obj.output.unwrap_or_default());
            } else {
                return Err(
                    format!("An error occurred, error code: {}", obj.error).into(),
                );
            }
        }
        Some(PythonObfuscatorResponse::Json(_)) => {}
        None => {
            return Err("Something unexpected happen while trying to obfuscate the code.".into());
        }
    }

    Ok(())
}