codedefender_config/
lib.rs

1//! `codedefender-config` provides the Rust data structures used for serializing and deserializing
2//! CodeDefender YAML configuration files and analysis results. These structures are used by both
3//! the CodeDefender CLI and its backend services.
4//!
5//! This crate is intended to be consumed by tools that integrate with or generate CodeDefender config files.
6
7use serde::{Deserialize, Serialize};
8use std::path::PathBuf;
9
10/// Current supported YAML config version.
11pub const YAML_CONFIG_VERSION: &str = "1.0.0";
12
13/// Available SIMD extension types used by mutation engines.
14#[derive(Debug, Serialize, Deserialize, Clone)]
15pub enum MutationEngineExtension {
16    /// Generic (no special SIMD usage)
17    Generic = 0,
18    /// SSE-enabled
19    SSE = 1,
20}
21
22/// Supported PE environments.
23#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
24pub enum PeEnvironment {
25    /// User-mode PE (exe, dll)
26    UserMode,
27    /// Kernel-mode PE (sys)
28    KernelMode,
29    /// UEFI firmware image
30    UEFI,
31}
32
33/// Configuration settings for lifting x86 instructions into IR.
34#[derive(Debug, Serialize, Deserialize, Clone)]
35pub struct LifterSettings {
36    /// Whether to lift calls into IR.
37    pub lift_calls: bool,
38    /// Max stack copy size in bytes when lifting.
39    pub max_stack_copy_size: u32,
40    /// Fallback: split on calls if lifting fails.
41    pub split_on_calls_fallback: bool,
42}
43
44/// IR optimization settings.
45#[derive(Debug, Serialize, Deserialize, Clone)]
46pub struct OptimizationSettings {
47    /// Enable constant propagation.
48    pub constant_propagation: bool,
49    /// Enable instruction combining.
50    pub instruction_combine: bool,
51    /// Enable dead code elimination.
52    pub dead_code_elim: bool,
53    /// Enable pruning of unused block parameters.
54    pub prune_useless_block_params: bool,
55    /// Number of optimization iterations to run.
56    pub iterations: u32,
57}
58
59/// Assembler-level codegen settings.
60#[derive(Debug, Serialize, Deserialize, Clone)]
61pub struct AssemblerSettings {
62    /// Whether to shuffle basic blocks.
63    pub shuffle_basic_blocks: bool,
64    /// Instruction prefix to prepend to emitted instructions.
65    pub instruction_prefix: String,
66    /// Chance of randomly applying the prefix.
67    pub random_prefix_chance: f64,
68}
69
70/// Compiler configuration (IR + codegen) for a profile.
71#[derive(Debug, Serialize, Deserialize, Clone)]
72pub struct CDCompilerSettings {
73    /// Assembler settings.
74    pub assembler_settings: AssemblerSettings,
75    /// Optimization settings.
76    pub optimization_settings: OptimizationSettings,
77    /// IR lifter settings.
78    pub lifter_settings: LifterSettings,
79}
80
81/// Fake PDB string settings to confuse debuggers.
82#[derive(Debug, Serialize, Deserialize)]
83pub struct FakePdbString {
84    /// Whether the fake PDB string is enabled.
85    pub enabled: bool,
86    /// Value to emit as the fake PDB string.
87    pub value: String,
88}
89
90/// Custom `.text` section name override.
91#[derive(Debug, Serialize, Deserialize)]
92pub struct CustomSectionName {
93    /// Whether this feature is enabled.
94    pub enabled: bool,
95    /// Custom section name value.
96    pub value: String,
97}
98
99/// Global obfuscation settings for the module.
100#[derive(Debug, Serialize, Deserialize)]
101pub struct CDModuleSettings {
102    /// Whether to crash the IDA decompiler intentionally.
103    pub ida_crasher: bool,
104    /// Whether to enable IAT/Import protection.
105    pub import_protection: bool,
106    /// Fake PDB string settings.
107    pub fake_pdb_string: FakePdbString,
108    /// Custom PE section name settings.
109    pub custom_section_name: CustomSectionName,
110}
111
112/// Instruction-level semantics used in transformations.
113#[derive(Debug, Serialize, Deserialize, Clone)]
114pub struct Semantics {
115    pub add: bool,
116    pub sub: bool,
117    pub and: bool,
118    pub xor: bool,
119    pub or: bool,
120    pub not: bool,
121    pub neg: bool,
122}
123
124/// Bit widths to apply transformations to.
125#[derive(Debug, Serialize, Deserialize, Clone)]
126pub struct BitWidths {
127    pub bit8: bool,
128    pub bit16: bool,
129    pub bit32: bool,
130    pub bit64: bool,
131}
132
133/// Configuration for the Loop Encode Semantics pass.
134#[derive(Debug, Serialize, Deserialize, Clone)]
135pub struct LoopEncodeSemantics {
136    /// Number of times to attempt transformation.
137    pub iterations: u32,
138    /// Percent chance to apply transformation (0–100).
139    pub probability: u32,
140    /// Instruction semantics to consider.
141    pub semantics: Semantics,
142    /// Bit widths to target.
143    pub bitwidths: BitWidths,
144}
145
146/// Configuration for Mixed Boolean Arithmetic pass.
147#[derive(Debug, Serialize, Deserialize, Clone)]
148pub struct MixedBooleanArithmetic {
149    pub iterations: u32,
150    pub probability: u32,
151    pub semantics: Semantics,
152    pub bitwidths: BitWidths,
153}
154
155/// Configuration for Mutation Engine pass.
156#[derive(Debug, Serialize, Deserialize, Clone)]
157pub struct MutationEngine {
158    pub iterations: u32,
159    pub probability: u32,
160    pub extension: MutationEngineExtension,
161    pub semantics: Semantics,
162    pub bitwidths: BitWidths,
163}
164
165/// Pass that crashes IDA’s decompiler.
166#[derive(Debug, Serialize, Deserialize, Clone)]
167pub struct IDADecompilerCrasher;
168
169/// Constant obfuscation pass.
170#[derive(Debug, Serialize, Deserialize, Clone)]
171pub struct ObscureConstants;
172
173/// Memory reference obfuscation pass.
174#[derive(Debug, Serialize, Deserialize, Clone)]
175pub struct ObscureReferences;
176
177/// Control-flow obfuscation pass.
178#[derive(Debug, Serialize, Deserialize, Clone)]
179pub struct ObscureControlFlow;
180
181/// All possible obfuscation passes.
182#[derive(Debug, Serialize, Deserialize, Clone)]
183pub enum ObfuscationPass {
184    LoopEncodeSemantics(LoopEncodeSemantics),
185    MixedBooleanArithmetic(MixedBooleanArithmetic),
186    MutationEngine(MutationEngine),
187    IDADecompilerCrasher(IDADecompilerCrasher),
188    ObscureConstants(ObscureConstants),
189    ObscureReferences(ObscureReferences),
190    ObscureControlFlow(ObscureControlFlow),
191}
192
193/// Profile definition used to apply passes to symbols.
194#[derive(Debug, Serialize, Deserialize)]
195pub struct CDProfile {
196    /// Name of the profile.
197    pub name: String,
198    /// Obfuscation passes for this profile.
199    pub passes: Vec<ObfuscationPass>,
200    /// Compiler settings for this profile.
201    pub compiler_settings: CDCompilerSettings,
202    /// List of symbol RVAs this profile targets.
203    pub symbols: Vec<u64>,
204}
205
206/// Top-level config file structure.
207#[derive(Debug, Serialize, Deserialize)]
208pub struct CDConfig {
209    /// Module-wide settings.
210    pub module_settings: CDModuleSettings,
211    /// All profiles to apply during obfuscation.
212    pub profiles: Vec<CDProfile>,
213}
214
215/// Information about a single function found during analysis.
216#[derive(Deserialize, Serialize, Clone, Debug)]
217pub struct AnalysisFunction {
218    /// RVA of the function.
219    pub rva: u64,
220    /// Function name.
221    pub symbol: String,
222    /// Number of references to this function.
223    pub ref_count: usize,
224}
225
226/// Reason why a function was rejected from analysis.
227#[derive(Deserialize, Serialize, Clone, Debug)]
228pub struct AnalysisReject {
229    /// RVA of the rejected function.
230    pub rva: u64,
231    /// Symbol name.
232    pub symbol: String,
233    /// Mnemonic reason string (e.g., internal enum).
234    pub ty: String,
235    /// Stringified reason (human-readable).
236    pub reason: String,
237}
238
239/// Grouping of functions under a named macro profile.
240#[derive(Deserialize, Serialize, Clone, Debug)]
241pub struct AnalysisMacroProfile {
242    /// Name of the macro profile.
243    pub name: String,
244    /// List of function RVAs in this macro.
245    pub rvas: Vec<u64>,
246}
247
248/// Results from binary analysis, returned to the frontend.
249#[derive(Deserialize, Serialize, Clone, Debug)]
250pub struct AnalysisResult {
251    /// Environment type (UserMode, KernelMode, UEFI).
252    pub environment: PeEnvironment,
253    /// Functions found during analysis.
254    pub functions: Vec<AnalysisFunction>,
255    /// Rejected functions and reasons.
256    pub rejects: Vec<AnalysisReject>,
257    /// Macro profiles generated from analysis.
258    pub macros: Vec<AnalysisMacroProfile>,
259}
260
261/// Symbol representation used in YAML: either name or RVA.
262#[derive(Debug, Serialize, Deserialize)]
263pub enum YamlSymbol {
264    /// Symbol name
265    Name(String),
266    /// Symbol RVA.
267    Rva(u64),
268}
269
270/// Obfuscation profile for YAML configuration.
271#[derive(Debug, Serialize, Deserialize)]
272pub struct YamlProfile {
273    /// Profile name (referenced by source macros).
274    pub name: String,
275    /// Passes to apply to this profile.
276    pub passes: Vec<ObfuscationPass>,
277    /// Compiler configuration for this profile.
278    pub compiler_settings: CDCompilerSettings,
279    /// Symbols targeted by this profile.
280    pub symbols: Vec<YamlSymbol>,
281    /// Only used by the SaaS UI. Not used by the CLI.
282    pub color: String,
283}
284
285/// Root YAML config structure.
286#[derive(Debug, Serialize, Deserialize)]
287pub struct YamlConfig {
288    /// Version of the config file format.
289    pub version: String,
290    /// API key provided by the CodeDefender web service.
291    pub api_key: String,
292    /// Poll timeout for downloading the obfuscated program.
293    /// Do not go below 500(ms) otherwise you will be timed out.
294    pub timeout: u64,
295    /// Input binary to process.
296    pub input_file: PathBuf,
297    /// Output path for the obfuscated binary.
298    pub output_file: PathBuf,
299    /// Optional debug symbol (PDB) file.
300    pub pdb_file: Option<PathBuf>,
301    /// Global module-wide obfuscation settings.
302    pub module_settings: CDModuleSettings,
303    /// Obfuscation profiles to apply.
304    pub profiles: Vec<YamlProfile>,
305}