Rust Obfuscator
Compile-safe Rust source transformation: renames local bindings with verified reference updates, and encodes strings only in type-safe String contexts. Plus real Cargo.toml, linker, and build-script configuration that produces genuinely obfuscated release binaries.
This tool performs only transformations that cannot break compilation on valid Rust input. Identifier renaming targets local bindings, parameters, and loop variables. String encoding targets only let-bound String values (via type annotation, .to_string(), or String::from). It never touches format!, println!, const, static, &str parameters, struct fields, macro arguments, or match patterns.
rustc. The transform performs a structural consistency check — it confirms every renamed identifier's references have been updated. Only the Local Verification tab can confirm compilation. Always run it before shipping.
[profile.release] # Aggressive release optimizations — real binary obfuscation for Rust opt-level = "z" lto = "fat" codegen-units = 1 panic = "abort" strip = "symbols" incremental = false debug = false debug-assertions = false overflow-checks = false rpath = false
[profile.release] section into your existing Cargo.toml. It does not add dependencies or change source. After adding it, run cargo build --release — the resulting binary will have no symbols, no unwinding tables, and no debug info.
# Place this file at the root of your project as .cargo/config.toml
[target.'cfg(all(target_os = "linux"))']
rustflags = [
"-C", "link-arg=-s",
"-C", "link-arg=-Wl,--strip-all",
"-C", "link-arg=-Wl,--build-id=none",
]
[target.'cfg(all(target_os = "macos"))']
rustflags = [
"-C", "link-arg=-Wl,-x",
"-C", "link-arg=-Wl,-S",
]
[target.'cfg(all(target_os = "windows", target_env = "msvc"))']
rustflags = [
"-C", "link-arg=/DEBUG:NONE",
"-C", "link-arg=/OPT:REF",
"-C", "link-arg=/OPT:ICF",
]
// build.rs — emit per-target linker arguments for stripping.
// Add to project root and set `build = "build.rs"` in Cargo.toml [package].
use std::env;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
let profile = env::var("PROFILE").unwrap_or_default();
if profile == "release" {
match target_os.as_str() {
"linux" | "android" => {
println!("cargo:rustc-link-arg=-s");
println!("cargo:rustc-link-arg=-Wl,--strip-all");
println!("cargo:rustc-link-arg=-Wl,--build-id=none");
}
"macos" | "ios" => {
println!("cargo:rustc-link-arg=-Wl,-x");
println!("cargo:rustc-link-arg=-Wl,-S");
}
"windows" => {
println!("cargo:rustc-link-arg=/DEBUG:NONE");
println!("cargo:rustc-link-arg=/OPT:REF");
println!("cargo:rustc-link-arg=/OPT:ICF");
}
_ => {}
}
}
}
#!/usr/bin/env bash
# strip-binary.sh — manual post-build stripping for extra symbol removal.
# Run AFTER `cargo build --release`.
set -euo pipefail
BIN="${1:-target/release/myapp}"
if [ ! -f "$BIN" ]; then
echo "Binary not found: $BIN" >&2
exit 1
fi
echo "Original size: $(wc -c < "$BIN") bytes"
if command -v strip >/dev/null 2>&1; then
strip --strip-all --remove-section=.comment --remove-section=.note "$BIN" || true
fi
if command -v llvm-strip >/dev/null 2>&1; then
llvm-strip --strip-all "$BIN" || true
fi
if command -v objcopy >/dev/null 2>&1; then
objcopy --remove-section=.comment --remove-section=.note.gnu.build-id "$BIN" || true
fi
echo "Final size: $(wc -c < "$BIN") bytes"
echo "Stripped: $BIN"
#!/usr/bin/env bash
# verify-obfuscation.sh
#
# Confirms two things:
# 1. The transformed Rust source compiles successfully.
# 2. The compiled program produces identical output to the original.
#
# Place both project copies side by side:
# ./original-project/ (unmodified source)
# ./obfuscated-project/ (source with the obfuscated file applied)
#
# Then run: bash verify-obfuscation.sh ./original-project ./obfuscated-project
set -euo pipefail
ORIG_DIR="${1:-./original-project}"
OBF_DIR="${2:-./obfuscated-project}"
BIN_NAME="${3:-myapp}"
if [ ! -d "$ORIG_DIR" ] || [ ! -d "$OBF_DIR" ]; then
echo "Usage: $0 [binary-name]" >&2
exit 1
fi
echo "=== Building original ==="
(cd "$ORIG_DIR" && cargo build --release --quiet)
echo "=== Building obfuscated ==="
(cd "$OBF_DIR" && cargo build --release --quiet)
ORIG_BIN="$ORIG_DIR/target/release/$BIN_NAME"
OBF_BIN="$OBF_DIR/target/release/$BIN_NAME"
if [ ! -x "$ORIG_BIN" ]; then echo "Original binary not found: $ORIG_BIN" >&2; exit 1; fi
if [ ! -x "$OBF_BIN" ]; then echo "Obfuscated binary not found: $OBF_BIN" >&2; exit 1; fi
echo "=== Running both programs ==="
"$ORIG_BIN" > /tmp/orig.out 2>&1 || true
"$OBF_BIN" > /tmp/obf.out 2>&1 || true
if diff -u /tmp/orig.out /tmp/obf.out; then
echo ""
echo "PASS: outputs are identical."
echo ""
echo "Binary sizes:"
echo " original: $(wc -c < "$ORIG_BIN") bytes"
echo " obfuscated: $(wc -c < "$OBF_BIN") bytes"
exit 0
else
echo ""
echo "FAIL: outputs differ."
exit 2
fi
Rust obfuscation is surrounded by misinformation because Rust compiles to native machine code through LLVM. There is no bytecode, no interpreter, and no source shipped to end users. This means most "Rust obfuscator" tools perform transformations on source files that users never see — with zero effect on shipped binaries, and frequently with compile-breaking bugs.
This page explains what genuinely protects a Rust binary, what compile-safe source transformation does, and how to verify every transformation locally with a script this tool generates.
Table of Contents
The Reality of Rust Obfuscation
Rust's toolchain: you write .rs source, rustc compiles it to LLVM IR, LLVM emits native machine code, the linker combines everything into a single executable, and you ship only the executable. Transformations applied to source disappear completely during compilation — LLVM does not preserve local variable names, comment text, or string literal formatting in the final binary.
Real Binary Obfuscation for Rust
The techniques that actually change the shipped artifact are all build-time and linker-time:
| Technique | What It Does | Effect on Binary |
|---|---|---|
strip = "symbols" | Removes symbol names | Eliminates function and type names |
lto = "fat" | Cross-crate LTO | Inlines and merges code across crates |
codegen-units = 1 | Single codegen unit | Maximum inlining; smaller, more merged code |
panic = "abort" | Immediate abort on panic | Removes stack unwinding tables |
Linker -s / --strip-all | Strips at link time | Removes symbols Cargo's own stripping might miss |
| O-LLVM | Bogus control flow, substitution, flattening | Substantially harder to reverse |
Compile-Safe Source Transformation
This tool performs the following transformations, all of which cannot produce a compile error on valid input:
Local Binding Renaming (with verified reference updates)
The tokenizer identifies let and let mut bindings, function parameters, closure parameters, and for-loop patterns. Each renamed name has every reference in the file updated consistently. A post-transform pass scans the output for any occurrence of the original name — if one is found as a bare identifier, the output is rejected.
Context-Aware String Encoding (New)
String literals are XOR-encoded with a per-build key only when they appear in a let binding whose value is unambiguously a String. Every other context is left untouched. See the next section for the exact rules.
Non-Doc Comment Removal
Line (//) and block (/* */) comments are removed. Documentation comments (/// and //!) attached to public items are preserved.
String Encoding Rules
Rust validates format strings, requires const-evaluable initializers in const and static, and infers types from context. Encoding a string without knowing its type is what breaks compilation. The tool therefore encodes strings only in these specific patterns:
| Source pattern | Encoded? | Why |
|---|---|---|
let x = "...".to_string(); | ✅ Yes | Result is String |
let x = String::from("..."); | ✅ Yes | String::from(&str) accepts either type |
let x: String = "..."; | ✅ Yes | Explicit type annotation proves String |
let x = "..."; (no annotation, no .to_string()) | ❌ No | Could be &str or String depending on context — cannot determine |
foo("...") | ❌ No | Callee parameter type unknown |
foo(&"...") | ❌ No | Type is &str |
format!("...", ...) | ❌ No | Rust validates format strings at compile time |
println!("...", ...) and friends | ❌ No | Same |
const X: &str = "..."; | ❌ No | Must be const-evaluable |
static X: &str = "..."; | ❌ No | Same |
return "..."; | ❌ No | Return type unknown |
Foo { name: "..." } | ❌ No | Field type unknown |
match x { "..." => ... } | ❌ No | Pattern, not runtime value |
#[doc = "..."] / include_str!("...") | ❌ No | Macro / attribute input |
const API_KEY: &str = "..."; cannot be encoded by any tool that produces valid Rust, because const requires const-evaluable expressions and the decoder is a runtime function. If you need to hide such secrets, do not store them in the source — use environment variables, a config file, or a server-side secret store.Local Verification Workflow
The browser cannot run rustc. The tool generates a shell script that does the real verification. Copy it from the Local Verification tab and run it:
bash verify-obfuscation.sh ./original-project ./obfuscated-project myapp
The script builds both projects with cargo build --release, runs both binaries, and diffs their output. If the build fails, the transformation broke compilation. If the output differs, it broke behavior. Either way, do not ship the output.
Limitations & Legal
- Source transformation is a mild deterrent. Not encryption, not security. Do not embed secrets in source.
- Const and static strings remain readable. They cannot be encoded without breaking const-evaluability.
- Symbol stripping breaks panic messages. Keep an unstripped build for debugging.
- License compliance. Some licenses require shipping source; obfuscation does not satisfy that.
- O-LLVM requires a locally built custom rustc. Not possible in a browser.
Related Obfuscators
Frequently Asked Questions
Will the obfuscated Rust source compile?
Which strings get encoded?
Why aren't format! and println! strings encoded?
Will a const API_KEY still be readable?
Is the string encoding encryption?
Can a browser truly obfuscate a Rust binary?
What is the local verification script for?
Does the tool rename struct fields or enum variants?
Does the tool rename public functions or types?
Will renaming break macros?
Does the tool upload my Rust code?
What actually obfuscates a Rust binary?
Is the generated Cargo.toml config safe to add to my project?
What Rust editions are supported?
What is obfuscator-llvm (O-LLVM)?
Does stripping symbols break debugging?
Can I use this tool for WebAssembly Rust projects?
Is the source transformation reversible?
Do I need Rust installed to use this tool?
What does panic = "abort" do?
Is this obfuscation compliant with open-source licenses?
How do I test that the obfuscated code behaves identically?
Conclusion
Real Rust obfuscation happens at the build configuration and linker level. This tool gives you the actual Cargo.toml, .cargo/config.toml, and build.rs snippets that produce genuinely obfuscated binaries, plus a compile-safe source transformation that renames locals and encodes strings only in type-safe contexts. It never claims compilation success on its own — instead it gives you a script that runs cargo build --release and diffs the output. Verify locally, then ship.