Skip to content

Obfuscate a Rust program ​

Requires Codefend 3.3.0 or later for the recommended m_ preset.

Codefend can rename selected application identifiers in Rust source. This single-file example totals a slice of integers while keeping the entry point and standard macro names unchanged.

1. Create the source ​

Install Rust and Codefend. Codefend requires Node.js 18 or later; it runs before Rust compilation.

sh
npm install --global codefend

Create source/main.rs:

rust
fn m_total(m_values: &[i32]) -> i32 {
    let mut m_sum = 0;
    for m_value in m_values {
        m_sum += *m_value;
    }
    m_sum
}

fn main() {
    let m_values = [12, 18, 12];
    println!("{}", m_total(&m_values));
}

Expected obfuscated source ​

With the configuration in the next step, Codefend produces:

rust
fn ox0(ox1: &[i32]) -> i32 {
    let mut ox2 = 0;
    for ox3 in ox1 {
        ox2 += *ox3;
    }
    ox2
}

fn main() {
    let ox1 = [12, 18, 12];
    println!("{}", ox0(&ox1));
}

The program's expected output remains 42. Generated identifier numbers can change when the source or Codefend version changes.

Only our helper, parameters, and locals use m_. Keep main, Rust keywords/types, and println! unchanged.

2. Configure selection ​

Create .codefendrc.json beside source:

json
{
  "generation": {
    "inputDir": "source",
    "outputDir": "release"
  },
  "transformation": {
    "prefix": "ox"
  },
  "parser": {
    "name": "startWithMAndUnderscore"
  }
}

The startWithMAndUnderscore preset selects this example's m_ names, leaving unrelated underscore names alone. See parser presets for the exact rule and why we recommend this convention.

The lowercase ox prefix produces names such as ox0, compatible with the naming style used for the example's functions and locals. The default empty pool uses generated names without a limited replacement dictionary.

release is a disposable sibling directory. Codefend replaces existing output, so keep original source and valuable files outside it. Keep build artifacts out of the processed input tree.

3. Compile both versions ​

From the project root:

sh
rustc --edition=2021 source/main.rs -o original
./original
codefend --obfuscate
rustc --edition=2021 release/main.rs -o generated
./generated

On Windows, use .\original.exe and .\generated.exe if the compiler appends the executable extension. Both programs should print:

text
42

See the Rust book's compile-and-run example and the compiler's edition option.

Applying this to Cargo projects ​

This example uses rustc directly and has no external crates. For a Cargo application, preserve Cargo.toml, lockfiles, module paths, build scripts, and dependency configuration while arranging for Cargo to build the generated source. Keep target and downloaded dependencies outside Codefend's input. Test the generated package through its normal Cargo workflow.

Do not mark public APIs, trait-required method names, externally linked symbols, procedural-macro contracts, or serialization fields unless you control their consumers. Renaming text does not understand Rust scopes, borrowing, macro expansion, or foreign-function interfaces.

Compile and run tests for all generated modules. The default empty pool avoids current cross-call pool issues, but text replacement still requires checking case variants, embedded names, and string contents; see current limitations.

Add Codefend to your workflow ​

Run Codefend before compiling the generated Rust source and testing its behavior. For a Cargo project, keep its manifests and dependency configuration and arrange for the intended generated sources to be built and tested.

sh
codefend --obfuscate
rustc --edition=2021 release/main.rs -o generated
./generated

In CI, stop on a failed step and package only validated generated output. See build and deployment for the shared workflow.