python_obfuscator_example/python-obfuscator-example.rs
1/******************************************************************************
2 * Python Obfuscator WebApi interface usage example.
3 *
4 * In this example we will obfuscate sample source with every public option
5 * set, so you can see the full client surface and turn flags on or off.
6 *
7 * Version : v1.0.0
8 * Language : Rust
9 * Author : Bartosz Wójcik
10 * Web page : https://www.pelock.com
11 *
12 *****************************************************************************/
13
14use python_obfuscator::{
15 CodeVirtualization, PythonObfuscator, PythonObfuscatorResponse, RenameStyle,
16};
17
18#[tokio::main]
19async fn main() -> Result<(), Box<dyn std::error::Error>> {
20 //
21 // include Python Obfuscator class
22 //
23
24 //
25 // when developing from a repository clone without installing the package use:
26 //
27 // use python_obfuscator::{PythonObfuscator, PythonObfuscatorResponse};
28 // (path dependency in Cargo.toml pointing to this crate)
29
30 //
31 // create Python Obfuscator class instance (we are using our activation key)
32 //
33 let mut my_python_obfuscator =
34 PythonObfuscator::new(Some("ABCD-ABCD-ABCD-ABCD".to_string()));
35
36 //
37 // should the source code be compressed (both input & compressed)
38 //
39 my_python_obfuscator.enable_compression = false;
40
41 //
42 // globals
43 //
44 // fixed random seed for reproducible obfuscation output (optional)
45 //
46 my_python_obfuscator.seed = None;
47
48 //
49 // randomization density / intensity (0-100)
50 //
51 my_python_obfuscator.randomization_density = None;
52
53 //
54 // identifier renaming style: RenameStyle::Il, O0, Confusable, Hex, Homoglyph, or Mangled
55 //
56 my_python_obfuscator.rename_style = Some(RenameStyle::Il);
57
58 //
59 // code virtualization (exactly one): Vm, Fsa, or Flat
60 // set to None to skip virtualization
61 //
62 my_python_obfuscator.code_virtualization = Some(CodeVirtualization::Vm);
63
64 //
65 // protection (opt-in on the service for --all; shown enabled here so every flag is visible)
66 //
67 // protection against tampering with protected code (integrity verification)
68 //
69 my_python_obfuscator.self_defending = true;
70
71 //
72 // protection linker (decoy call graph; requires self_defending)
73 //
74 my_python_obfuscator.protection_linker = true;
75
76 //
77 // insert anti-debugging detections
78 //
79 my_python_obfuscator.detect_debugger = true;
80
81 //
82 // insert virtual machine (anti-VM) detections
83 //
84 my_python_obfuscator.anti_vm = true;
85
86 //
87 // insert anti-sandbox detections
88 //
89 my_python_obfuscator.anti_sandbox = true;
90
91 //
92 // insert anti-emulators (CPU) detections
93 //
94 my_python_obfuscator.anti_emulator = true;
95
96 //
97 // renaming
98 //
99 // rename variable names to random string values
100 //
101 my_python_obfuscator.rename_variables = true;
102
103 //
104 // rename parameter names to random string values
105 //
106 my_python_obfuscator.rename_parameters = true;
107
108 //
109 // rename function names to random string values
110 //
111 my_python_obfuscator.rename_functions = true;
112
113 //
114 // rename function call references consistently with renamed functions
115 //
116 my_python_obfuscator.rename_function_calls = true;
117
118 //
119 // shuffle function order in the output source
120 //
121 my_python_obfuscator.shuffle_functions = true;
122
123 //
124 // fold/resolve constant expressions at obfuscation time
125 //
126 my_python_obfuscator.resolve_constants = true;
127
128 //
129 // strings
130 //
131 // split strings into concatenated chunks
132 //
133 my_python_obfuscator.split_strings = true;
134
135 //
136 // apply light transformations/mutations to string literals
137 //
138 my_python_obfuscator.modify_strings = true;
139
140 //
141 // encrypt strings using randomly generated polymorphic encryption algorithms
142 //
143 my_python_obfuscator.encrypt_strings = true;
144
145 //
146 // store string fragments in char-code array vaults
147 //
148 my_python_obfuscator.string_char_array_vault = true;
149
150 //
151 // numeric
152 //
153 // encrypt integers
154 //
155 my_python_obfuscator.encrypt_integers = true;
156
157 //
158 // encrypt floating point numbers
159 //
160 my_python_obfuscator.encrypt_floating = true;
161
162 //
163 // replace binary operators with mixed boolean-arithmetic (MBA) equivalents
164 //
165 my_python_obfuscator.mba_binops = true;
166
167 //
168 // represent integers via floating-point math
169 //
170 my_python_obfuscator.integers_to_floating = true;
171
172 //
173 // move integers to arrays
174 //
175 my_python_obfuscator.integers_to_arrays = true;
176
177 //
178 // move floats to arrays
179 //
180 my_python_obfuscator.floats_to_arrays = true;
181
182 //
183 // apply redundant xor / affine integer masks
184 //
185 my_python_obfuscator.affine_integer_mask = true;
186
187 //
188 // encrypted array literals
189 //
190 // encrypt integer array literals
191 //
192 my_python_obfuscator.array_int_crypt = true;
193
194 //
195 // encrypt character array literals
196 //
197 my_python_obfuscator.array_char_crypt = true;
198
199 //
200 // encrypt floating-point array literals
201 //
202 my_python_obfuscator.array_double_crypt = true;
203
204 //
205 // encrypt string array literals
206 //
207 my_python_obfuscator.array_string_crypt = true;
208
209 //
210 // decoy value pools
211 //
212 // insert a shared bucket of random noise values used by other strategies
213 //
214 my_python_obfuscator.insert_random_value_bucket = true;
215
216 //
217 // populate the random value bucket with decoy integers
218 //
219 my_python_obfuscator.random_bucket_integers = true;
220
221 //
222 // populate the random value bucket with decoy arrays
223 //
224 my_python_obfuscator.random_bucket_arrays = true;
225
226 //
227 // populate the random value bucket with decoy functions
228 //
229 my_python_obfuscator.random_bucket_functions = true;
230
231 //
232 // populate the random value bucket with decoy characters
233 //
234 my_python_obfuscator.random_bucket_characters = true;
235
236 //
237 // populate the random value bucket with anti-regex decoy noise
238 //
239 my_python_obfuscator.random_bucket_anti_regex = true;
240
241 //
242 // populate the random value bucket with autostart decoy stubs
243 //
244 my_python_obfuscator.random_bucket_autostart = true;
245
246 //
247 // opaque predicates & noise
248 //
249 // rewrite selected statements using ternary operators
250 //
251 my_python_obfuscator.insert_ternary_operators = true;
252
253 //
254 // replace boolean conditions with equivalent complex expressions
255 //
256 my_python_obfuscator.complexify_booleans = true;
257
258 //
259 // insert opaque predicate branches
260 //
261 my_python_obfuscator.opaque_branches = true;
262
263 //
264 // insert opaque mixer chains into control flow
265 //
266 my_python_obfuscator.opaque_mixer_chain = true;
267
268 //
269 // insert dead code
270 //
271 my_python_obfuscator.insert_dead_code = true;
272
273 //
274 // wrap code in try/finally blocks with dead noise
275 //
276 my_python_obfuscator.try_finally_noise = true;
277
278 //
279 // decoys
280 //
281 // insert decoy functions
282 //
283 my_python_obfuscator.decoy_functions = true;
284
285 //
286 // insert decoy lambda expressions
287 //
288 my_python_obfuscator.lambda_decoys = true;
289
290 //
291 // insert literal padding noise
292 //
293 my_python_obfuscator.literal_padding = true;
294
295 //
296 // insert fake import statement markers
297 //
298 my_python_obfuscator.fake_import_markers = true;
299
300 //
301 // use dynamic getattr()-based indirect calls
302 //
303 my_python_obfuscator.dynamic_getattr_calls = true;
304
305 //
306 // rewrite absolute imports into __import__ / getattr
307 //
308 my_python_obfuscator.obfuscate_imports = true;
309
310 //
311 // insert dead callback/event registration stubs
312 //
313 my_python_obfuscator.callback_registration_stubs = true;
314
315 //
316 // other
317 //
318 // strip comments from the output source
319 //
320 my_python_obfuscator.remove_comments = true;
321
322 //
323 // source code in Python format
324 //
325 let script_source_code = r#"label = 'SecretKey'
326port = 443
327
328def get_sum(a, b):
329 return a + b
330
331print(label, port)
332r = get_sum(11, 31)
333print(r)"#;
334
335 //
336 // obfuscate the source code with the options set above
337 //
338 let result = my_python_obfuscator
339 .obfuscate_script_source(script_source_code, true)
340 .await;
341
342 //
343 // it's also possible to pass a Python script file path instead of a string e.g.
344 //
345 // let result = my_python_obfuscator
346 // .obfuscate_script_file(std::path::Path::new("/path/to/project/script.py"), true)
347 // .await;
348
349 //
350 // result object holds the obfuscation results as well as other information
351 //
352 // result.error - error code
353 // result.output - obfuscated code
354 // result.demo - demo mode (empty/invalid key); strategy flags ignored; always integers_to_arrays, mba_binops, encrypt_strings (no virt; 1000 char limit)
355 // result.license_expiration - license end date (Y-m-d), empty if none
356 // result.usages_total - total obfuscations for this activation code
357 //
358 match result {
359 Some(PythonObfuscatorResponse::Object(obj)) => {
360 //
361 // display obfuscated code
362 //
363 if obj.error == PythonObfuscator::ERROR_SUCCESS {
364 println!("{}", obj.output.unwrap_or_default());
365 } else {
366 return Err(
367 format!("An error occurred, error code: {}", obj.error).into(),
368 );
369 }
370 }
371 Some(PythonObfuscatorResponse::Json(_)) => {}
372 None => {
373 return Err("Something unexpected happen while trying to obfuscate the code.".into());
374 }
375 }
376
377 Ok(())
378}