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)]
183#[serde(tag = "type")]
184pub enum ObfuscationPass {
185    LoopEncodeSemantics(LoopEncodeSemantics),
186    MixedBooleanArithmetic(MixedBooleanArithmetic),
187    MutationEngine(MutationEngine),
188    IDADecompilerCrasher,
189    ObscureConstants,
190    ObscureReferences,
191    ObscureControlFlow,
192}
193
194/// Profile definition used to apply passes to symbols.
195#[derive(Debug, Serialize, Deserialize)]
196pub struct CDProfile {
197    /// Name of the profile.
198    pub name: String,
199    /// Obfuscation passes for this profile.
200    pub passes: Vec<ObfuscationPass>,
201    /// Compiler settings for this profile.
202    pub compiler_settings: CDCompilerSettings,
203    /// List of symbol RVAs this profile targets.
204    pub symbols: Vec<u64>,
205}
206
207/// Top-level config file structure.
208#[derive(Debug, Serialize, Deserialize)]
209pub struct CDConfig {
210    /// Module-wide settings.
211    pub module_settings: CDModuleSettings,
212    /// All profiles to apply during obfuscation.
213    pub profiles: Vec<CDProfile>,
214}
215
216/// Information about a single function found during analysis.
217#[derive(Deserialize, Serialize, Clone, Debug)]
218pub struct AnalysisFunction {
219    /// RVA of the function.
220    pub rva: u64,
221    /// Function name.
222    pub symbol: String,
223    /// Number of references to this function.
224    pub ref_count: usize,
225}
226
227/// Reason why a function was rejected from analysis.
228#[derive(Deserialize, Serialize, Clone, Debug)]
229pub struct AnalysisReject {
230    /// RVA of the rejected function.
231    pub rva: u64,
232    /// Symbol name.
233    pub symbol: String,
234    /// Mnemonic reason string (e.g., internal enum).
235    pub ty: String,
236    /// Stringified reason (human-readable).
237    pub reason: String,
238}
239
240/// Grouping of functions under a named macro profile.
241#[derive(Deserialize, Serialize, Clone, Debug)]
242pub struct AnalysisMacroProfile {
243    /// Name of the macro profile.
244    pub name: String,
245    /// List of function RVAs in this macro.
246    pub rvas: Vec<u64>,
247}
248
249/// Results from binary analysis, returned to the frontend.
250#[derive(Deserialize, Serialize, Clone, Debug)]
251pub struct AnalysisResult {
252    /// Environment type (UserMode, KernelMode, UEFI).
253    pub environment: PeEnvironment,
254    /// Functions found during analysis.
255    pub functions: Vec<AnalysisFunction>,
256    /// Rejected functions and reasons.
257    pub rejects: Vec<AnalysisReject>,
258    /// Macro profiles generated from analysis.
259    pub macros: Vec<AnalysisMacroProfile>,
260}
261
262/// Symbol representation used in YAML: either name or RVA.
263#[derive(Debug, Serialize, Deserialize)]
264pub enum YamlSymbol {
265    /// Symbol name
266    Name(String),
267    /// Symbol RVA.
268    Rva(u64),
269}
270
271/// Obfuscation profile for YAML configuration.
272#[derive(Debug, Serialize, Deserialize)]
273pub struct YamlProfile {
274    /// Profile name (referenced by source macros).
275    pub name: String,
276    /// Passes to apply to this profile.
277    pub passes: Vec<ObfuscationPass>,
278    /// Compiler configuration for this profile.
279    pub compiler_settings: CDCompilerSettings,
280    /// Symbols targeted by this profile.
281    pub symbols: Vec<YamlSymbol>,
282    /// Only used by the SaaS UI. Not used by the CLI.
283    pub color: Option<String>,
284}
285
286/// Root YAML config structure.
287#[derive(Debug, Serialize, Deserialize)]
288pub struct YamlConfig {
289    /// Version of the config file format.
290    pub version: String,
291    /// API key provided by the CodeDefender web service.
292    pub api_key: String,
293    /// Poll timeout for downloading the obfuscated program.
294    /// Do not go below 500(ms) otherwise you will be timed out.
295    pub timeout: u64,
296    /// Input binary to process.
297    pub input_file: PathBuf,
298    /// Output path for the obfuscated binary.
299    pub output_file: PathBuf,
300    /// Optional debug symbol (PDB) file.
301    pub pdb_file: Option<PathBuf>,
302    /// Global module-wide obfuscation settings.
303    pub module_settings: CDModuleSettings,
304    /// Obfuscation profiles to apply.
305    pub profiles: Vec<YamlProfile>,
306}