Skip to main content

powershell_pro_obfuscator_example/
powershell-pro-obfuscator-example.rs

1/******************************************************************************
2 * PowerShell Pro Obfuscator WebApi interface usage example.
3 *
4 * In this example we will obfuscate sample source with custom options.
5 *
6 * Version        : v1.0.0
7 * Language       : Rust
8 * Author         : Bartosz Wójcik
9 * Web page       : https://www.pelock.com
10 *
11 *****************************************************************************/
12
13use powershell_pro_obfuscator::{PowerShellProObfuscator, PowerShellProObfuscatorResponse};
14
15#[tokio::main]
16async fn main() -> Result<(), Box<dyn std::error::Error>> {
17    //
18    // include PowerShell Pro Obfuscator class
19    //
20
21    //
22    // when developing from a repository clone without installing the package use:
23    //
24    // use powershell_pro_obfuscator::{PowerShellProObfuscator, PowerShellProObfuscatorResponse};
25    // (path dependency in Cargo.toml pointing to this crate)
26
27    //
28    // create PowerShell Pro Obfuscator class instance (we are using our activation key)
29    //
30    let mut my_powershell_pro_obfuscator =
31        PowerShellProObfuscator::new(Some("ABCD-ABCD-ABCD-ABCD".to_string()));
32
33    //
34    // should the source code be compressed (both input & compressed)
35    //
36    my_powershell_pro_obfuscator.enable_compression = false;
37
38    //
39    // global obfuscation options
40    //
41    // you can disable a particular obfuscation strategy globally if it
42    // fails or you don't want to use it without modifying the source codes
43    //
44    // by default all obfuscation strategies are enabled
45    //
46
47    //
48    // protection against tampering with protected code (integrity verification)
49    //
50    my_powershell_pro_obfuscator.self_defending = true;
51
52    //
53    // protection linker (decoy call graph)
54    //
55    my_powershell_pro_obfuscator.protection_linker = true;
56
57    //
58    // rename variable names to random string values
59    //
60    my_powershell_pro_obfuscator.rename_variables = true;
61
62    //
63    // rename parameter names to random string values
64    //
65    my_powershell_pro_obfuscator.rename_parameters = true;
66
67    //
68    // rename function names to random string values
69    //
70    my_powershell_pro_obfuscator.rename_functions = true;
71
72    //
73    // shuffle function order in the output source
74    //
75    my_powershell_pro_obfuscator.shuffle_functions = true;
76
77    //
78    // change linear code execution flow via control-flow flattening
79    //
80    my_powershell_pro_obfuscator.control_flow_flatten = true;
81
82    //
83    // rewrite statement blocks into finite-state automata (state-machine obfuscation)
84    //
85    my_powershell_pro_obfuscator.state_machine = true;
86
87    //
88    // lift selected statements into a VM engine (virtualized statements)
89    //
90    my_powershell_pro_obfuscator.vm_strategy = true;
91
92    //
93    // encrypt integers
94    //
95    my_powershell_pro_obfuscator.encrypt_integers = true;
96
97    //
98    // split strings into concatenated chunks
99    //
100    my_powershell_pro_obfuscator.split_strings = true;
101
102    //
103    // encrypt strings using randomly generated polymorphic encryption algorithms
104    //
105    my_powershell_pro_obfuscator.encrypt_strings = true;
106
107    //
108    // move integers to arrays
109    //
110    my_powershell_pro_obfuscator.integers_to_arrays = true;
111
112    //
113    // move floats to arrays
114    //
115    my_powershell_pro_obfuscator.floats_to_arrays = true;
116
117    //
118    // insert dead code
119    //
120    my_powershell_pro_obfuscator.insert_dead_code = true;
121
122    //
123    // replace boolean conditions with equivalent complex expressions
124    //
125    my_powershell_pro_obfuscator.complexify_booleans = true;
126
127    //
128    // represent integers via floating-point math
129    //
130    my_powershell_pro_obfuscator.integers_to_floating = true;
131
132    //
133    // encrypt floating point numbers
134    //
135    my_powershell_pro_obfuscator.encrypt_floating = true;
136
137    //
138    // insert decoy functions
139    //
140    my_powershell_pro_obfuscator.decoy_functions = true;
141
142    //
143    // insert anti-debugging detections
144    //
145    my_powershell_pro_obfuscator.detect_debugger = true;
146
147    //
148    // insert fake dot-source comment markers
149    //
150    my_powershell_pro_obfuscator.fake_dot_source_markers = true;
151
152    //
153    // insert opaque predicate branches
154    //
155    my_powershell_pro_obfuscator.opaque_branches = true;
156
157    //
158    // insert scriptblock decoys
159    //
160    my_powershell_pro_obfuscator.scriptblock_decoys = true;
161
162    //
163    // insert here-string padding
164    //
165    my_powershell_pro_obfuscator.literal_padding = true;
166
167    //
168    // use indirect command invocation
169    //
170    my_powershell_pro_obfuscator.reflect_invoke_commands = true;
171
172    //
173    // store string fragments in char-code array vaults
174    //
175    my_powershell_pro_obfuscator.string_char_array_vault = true;
176
177    //
178    // wrap code in try/finally blocks with dead noise
179    //
180    my_powershell_pro_obfuscator.try_finally_noise = true;
181
182    //
183    // apply redundant xor / affine integer masks
184    //
185    my_powershell_pro_obfuscator.affine_integer_mask = true;
186
187    //
188    // insert dead event/timer stubs
189    //
190    my_powershell_pro_obfuscator.event_stub = true;
191
192    //
193    // strip comments from the output source
194    //
195    my_powershell_pro_obfuscator.remove_comments = true;
196
197    //
198    // source code in PowerShell format
199    //
200    let script_source_code = r#"function Get-Greeting {
201    param([string]$Name)
202    Write-Host "Hello World from $Name!"
203}
204Get-Greeting "PowerShell Pro Obfuscator""#;
205
206    //
207    // by default all obfuscation options are enabled, so we can just simply call:
208    //
209    let result = my_powershell_pro_obfuscator
210        .obfuscate_script_source(script_source_code, true)
211        .await;
212
213    //
214    // it's also possible to pass a PowerShell script file path instead of a string e.g.
215    //
216    // let result = my_powershell_pro_obfuscator
217    //     .obfuscate_script_file(std::path::Path::new("/path/to/project/script.ps1"), true)
218    //     .await;
219
220    //
221    // result object holds the obfuscation results as well as other information
222    //
223    // result.error         - error code
224    // result.output        - obfuscated code
225    // result.demo          - was it used in demo mode (invalid or empty activation key was used)
226    // result.credits_left  - usage credits left after this operation
227    // result.credits_total - total number of credits for this activation code
228    // result.expired       - if this was the last usage credit for the activation key it will be set to true
229    //
230    match result {
231        Some(PowerShellProObfuscatorResponse::Object(obj)) => {
232            //
233            // display obfuscated code
234            //
235            if obj.error == PowerShellProObfuscator::ERROR_SUCCESS {
236                println!("{}", obj.output.unwrap_or_default());
237            } else {
238                return Err(
239                    format!("An error occurred, error code: {}", obj.error).into(),
240                );
241            }
242        }
243        Some(PowerShellProObfuscatorResponse::Json(_)) => {}
244        None => {
245            return Err("Something unexpected happen while trying to obfuscate the code.".into());
246        }
247    }
248
249    Ok(())
250}