pub struct PythonObfuscator {Show 53 fields
pub enable_compression: bool,
pub seed: Option<i64>,
pub randomization_density: Option<i64>,
pub rename_style: Option<RenameStyle>,
pub code_virtualization: Option<CodeVirtualization>,
pub self_defending: bool,
pub protection_linker: bool,
pub rename_variables: bool,
pub rename_parameters: bool,
pub rename_functions: bool,
pub rename_function_calls: bool,
pub shuffle_functions: bool,
pub resolve_constants: bool,
pub split_strings: bool,
pub modify_strings: bool,
pub encrypt_strings: bool,
pub string_char_array_vault: bool,
pub encrypt_integers: bool,
pub encrypt_floating: bool,
pub mba_binops: bool,
pub integers_to_floating: bool,
pub integers_to_arrays: bool,
pub floats_to_arrays: bool,
pub affine_integer_mask: bool,
pub array_int_crypt: bool,
pub array_char_crypt: bool,
pub array_double_crypt: bool,
pub array_string_crypt: bool,
pub insert_random_value_bucket: bool,
pub random_bucket_integers: bool,
pub random_bucket_arrays: bool,
pub random_bucket_functions: bool,
pub random_bucket_characters: bool,
pub random_bucket_anti_regex: bool,
pub random_bucket_autostart: bool,
pub insert_ternary_operators: bool,
pub complexify_booleans: bool,
pub opaque_branches: bool,
pub opaque_mixer_chain: bool,
pub insert_dead_code: bool,
pub try_finally_noise: bool,
pub decoy_functions: bool,
pub lambda_decoys: bool,
pub literal_padding: bool,
pub fake_import_markers: bool,
pub dynamic_getattr_calls: bool,
pub obfuscate_imports: bool,
pub callback_registration_stubs: bool,
pub detect_debugger: bool,
pub anti_vm: bool,
pub anti_sandbox: bool,
pub anti_emulator: bool,
pub remove_comments: bool,
/* private fields */
}Expand description
Python Obfuscator WebApi client.
Fields§
§enable_compression: boolShould the source code be compressed.
seed: Option<i64>Fixed random seed for reproducible obfuscation output (optional).
randomization_density: Option<i64>Randomization density / intensity (0-100).
rename_style: Option<RenameStyle>Identifier renaming style (il, o0, confusable, hex, homoglyph, mangled).
code_virtualization: Option<CodeVirtualization>Public virtualization mode (vm, fsa, or flat). Set to None to skip virtualization.
self_defending: boolProtection against tampering with protected code (integrity verification).
protection_linker: boolProtection linker (decoy call graph).
rename_variables: boolRename variable names to random string values.
rename_parameters: boolRename parameter names to random string values.
rename_functions: boolRename function names to random string values.
rename_function_calls: boolRename function call references consistently with renamed functions.
shuffle_functions: boolShuffle function order in the output source.
resolve_constants: boolFold/resolve constant expressions at obfuscation time.
split_strings: boolSplit strings into concatenated chunks.
modify_strings: boolApply light transformations/mutations to string literals.
encrypt_strings: boolEncrypt strings using randomly generated polymorphic encryption algorithms.
string_char_array_vault: boolStore string fragments in char-code array vaults.
encrypt_integers: boolEncrypt integers.
encrypt_floating: boolEncrypt floating point numbers.
mba_binops: boolReplace binary operators with mixed boolean-arithmetic (MBA) equivalents.
integers_to_floating: boolRepresent integers via floating-point math.
integers_to_arrays: boolMove integers to arrays.
floats_to_arrays: boolMove floats to arrays.
affine_integer_mask: boolApply redundant xor / affine integer masks.
array_int_crypt: boolEncrypt integer array literals.
array_char_crypt: boolEncrypt character array literals.
array_double_crypt: boolEncrypt floating-point array literals.
array_string_crypt: boolEncrypt string array literals.
insert_random_value_bucket: boolInsert a shared bucket of random noise values used by other strategies.
random_bucket_integers: boolPopulate the random value bucket with decoy integers.
random_bucket_arrays: boolPopulate the random value bucket with decoy arrays.
random_bucket_functions: boolPopulate the random value bucket with decoy functions.
random_bucket_characters: boolPopulate the random value bucket with decoy characters.
random_bucket_anti_regex: boolPopulate the random value bucket with anti-regex decoy noise.
random_bucket_autostart: boolPopulate the random value bucket with autostart decoy stubs.
insert_ternary_operators: boolRewrite selected statements using ternary operators.
complexify_booleans: boolReplace boolean conditions with equivalent complex expressions.
opaque_branches: boolInsert opaque predicate branches.
opaque_mixer_chain: boolInsert opaque mixer chains into control flow.
insert_dead_code: boolInsert dead code.
try_finally_noise: boolWrap code in try/finally blocks with dead noise.
decoy_functions: boolInsert decoy functions.
lambda_decoys: boolInsert decoy lambda expressions.
literal_padding: boolInsert literal padding noise.
fake_import_markers: boolInsert fake import statement markers.
dynamic_getattr_calls: boolUse dynamic getattr()-based indirect calls.
obfuscate_imports: boolRewrite absolute imports into import / getattr.
callback_registration_stubs: boolInsert dead callback/event registration stubs.
detect_debugger: boolInsert anti-debugging detections.
anti_vm: boolInsert virtual machine (anti-VM) detections.
anti_sandbox: boolInsert anti-sandbox detections.
anti_emulator: boolInsert anti-emulators (CPU) detections.
remove_comments: boolStrip comments from the output source.
Implementations§
Source§impl PythonObfuscator
impl PythonObfuscator
Sourcepub const API_URL: &'static str = "https://www.pelock.com/api/python-obfuscator/v1"
pub const API_URL: &'static str = "https://www.pelock.com/api/python-obfuscator/v1"
Default Python Obfuscator WebApi endpoint.
Sourcepub const ERROR_SUCCESS: i64 = 0
pub const ERROR_SUCCESS: i64 = 0
Success.
Sourcepub const ERROR_INPUT_SIZE: i64 = 1
pub const ERROR_INPUT_SIZE: i64 = 1
Invalid size for source code (it’s 1000 bytes max. for demo version).
Sourcepub const ERROR_INPUT: i64 = 2
pub const ERROR_INPUT: i64 = 2
Input source is empty.
Sourcepub const ERROR_PARSING: i64 = 3
pub const ERROR_PARSING: i64 = 3
Python source code parsing error.
Sourcepub const ERROR_OBFUSCATION: i64 = 4
pub const ERROR_OBFUSCATION: i64 = 4
Python parsed code obfuscation error.
Sourcepub const ERROR_OUTPUT: i64 = 5
pub const ERROR_OUTPUT: i64 = 5
An error occurred while generating output code.
Sourcepub fn new(api_key: Option<String>) -> Self
pub fn new(api_key: Option<String>) -> Self
Initialize Python Obfuscator client.
api_key — activation key. Empty or invalid keys run demo mode, which ignores strategy flags and always applies integers_to_arrays, mba_binops, and encrypt_strings (no virtualization; 1000 character limit).
Examples found in repository?
16async fn main() -> Result<(), Box<dyn std::error::Error>> {
17 //
18 // include Python Obfuscator class
19 //
20
21 //
22 // if you don't want to use crates.io use a path dependency to this crate
23 //
24 // [dependencies]
25 // python-obfuscator = { path = ".." }
26
27 //
28 // create Python Obfuscator class instance (we are using our activation key)
29 //
30 let my_python_obfuscator =
31 PythonObfuscator::new(Some("ABCD-ABCD-ABCD-ABCD".to_string()));
32
33 //
34 // login to the service
35 //
36 let result = my_python_obfuscator.login(true).await;
37
38 //
39 // result object holds the information about the license
40 //
41 // result.demo - demo mode (empty/invalid key); strategy flags ignored; always integers_to_arrays, mba_binops, encrypt_strings (no virt; 1000 char limit)
42 // result.license_expiration - license end date (Y-m-d), empty if none
43 // result.usages_total - total obfuscations for this activation code
44 // result.string_limit - max. source code size allowed (it's 1000 bytes for demo mode)
45 //
46 match result {
47 Some(PythonObfuscatorResponse::Object(obj)) => {
48 println!(
49 "Demo version status - {}",
50 if obj.demo.unwrap_or(false) {
51 "true"
52 } else {
53 "false"
54 }
55 );
56 println!(
57 "License expiration - {:?}",
58 obj.license_expiration
59 );
60 println!(
61 "Total obfuscations - {}",
62 obj.usages_total.unwrap_or(0)
63 );
64 println!(
65 "Max. source code size - {}",
66 obj.string_limit.unwrap_or(0)
67 );
68 }
69 Some(PythonObfuscatorResponse::Json(_)) => {}
70 None => {
71 return Err("Something unexpected happen while trying to login to the service.".into());
72 }
73 }
74
75 Ok(())
76}More examples
16async fn main() -> Result<(), Box<dyn std::error::Error>> {
17 //
18 // include Python Obfuscator class
19 //
20
21 //
22 // when developing from a repository clone without installing the package use:
23 //
24 // use python_obfuscator::{PythonObfuscator, PythonObfuscatorResponse};
25 // (path dependency in Cargo.toml pointing to this crate)
26
27 //
28 // create Python Obfuscator class instance (we are using our activation key)
29 //
30 let my_python_obfuscator =
31 PythonObfuscator::new(Some("ABCD-ABCD-ABCD-ABCD".to_string()));
32
33 //
34 // source code in Python format
35 //
36 let script_source_code = r#"label = 'SecretKey'
37port = 443
38
39def get_sum(a, b):
40 return a + b
41
42print(label, port)
43r = get_sum(11, 31)
44print(r)"#;
45
46 //
47 // by default all obfuscation options are enabled, so we can just simply call:
48 //
49 let result = my_python_obfuscator
50 .obfuscate_script_source(script_source_code, true)
51 .await;
52
53 //
54 // it's also possible to pass a Python script file path instead of a string e.g.
55 //
56 // let result = my_python_obfuscator
57 // .obfuscate_script_file(std::path::Path::new("/path/to/project/script.py"), true)
58 // .await;
59
60 //
61 // result object holds the obfuscation results as well as other information
62 //
63 // result.error - error code
64 // result.output - obfuscated code
65 // result.demo - demo mode (empty/invalid key); strategy flags ignored; always integers_to_arrays, mba_binops, encrypt_strings (no virt; 1000 char limit)
66 // result.license_expiration - license end date (Y-m-d), empty if none
67 // result.usages_total - total obfuscations for this activation code
68 //
69 match result {
70 Some(PythonObfuscatorResponse::Object(obj)) => {
71 //
72 // display obfuscated code
73 //
74 if obj.error == PythonObfuscator::ERROR_SUCCESS {
75 println!("{}", obj.output.unwrap_or_default());
76 } else {
77 return Err(
78 format!("An error occurred, error code: {}", obj.error).into(),
79 );
80 }
81 }
82 Some(PythonObfuscatorResponse::Json(_)) => {}
83 None => {
84 return Err("Something unexpected happen while trying to obfuscate the code.".into());
85 }
86 }
87
88 Ok(())
89}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}Sourcepub async fn login(
&self,
return_as_object: bool,
) -> Option<PythonObfuscatorResponse>
pub async fn login( &self, return_as_object: bool, ) -> Option<PythonObfuscatorResponse>
Login to the service and get the information about the current license limits.
return_as_object — return result as an object or JSON encoded string.
Examples found in repository?
16async fn main() -> Result<(), Box<dyn std::error::Error>> {
17 //
18 // include Python Obfuscator class
19 //
20
21 //
22 // if you don't want to use crates.io use a path dependency to this crate
23 //
24 // [dependencies]
25 // python-obfuscator = { path = ".." }
26
27 //
28 // create Python Obfuscator class instance (we are using our activation key)
29 //
30 let my_python_obfuscator =
31 PythonObfuscator::new(Some("ABCD-ABCD-ABCD-ABCD".to_string()));
32
33 //
34 // login to the service
35 //
36 let result = my_python_obfuscator.login(true).await;
37
38 //
39 // result object holds the information about the license
40 //
41 // result.demo - demo mode (empty/invalid key); strategy flags ignored; always integers_to_arrays, mba_binops, encrypt_strings (no virt; 1000 char limit)
42 // result.license_expiration - license end date (Y-m-d), empty if none
43 // result.usages_total - total obfuscations for this activation code
44 // result.string_limit - max. source code size allowed (it's 1000 bytes for demo mode)
45 //
46 match result {
47 Some(PythonObfuscatorResponse::Object(obj)) => {
48 println!(
49 "Demo version status - {}",
50 if obj.demo.unwrap_or(false) {
51 "true"
52 } else {
53 "false"
54 }
55 );
56 println!(
57 "License expiration - {:?}",
58 obj.license_expiration
59 );
60 println!(
61 "Total obfuscations - {}",
62 obj.usages_total.unwrap_or(0)
63 );
64 println!(
65 "Max. source code size - {}",
66 obj.string_limit.unwrap_or(0)
67 );
68 }
69 Some(PythonObfuscatorResponse::Json(_)) => {}
70 None => {
71 return Err("Something unexpected happen while trying to login to the service.".into());
72 }
73 }
74
75 Ok(())
76}Sourcepub async fn obfuscate_script_file(
&self,
script_file_path: &Path,
return_as_object: bool,
) -> Option<PythonObfuscatorResponse>
pub async fn obfuscate_script_file( &self, script_file_path: &Path, return_as_object: bool, ) -> Option<PythonObfuscatorResponse>
Obfuscate Python script source code file using provided parameters.
return_as_object — return result as an object or JSON encoded string.
Sourcepub async fn obfuscate_script_source(
&self,
script_source: &str,
return_as_object: bool,
) -> Option<PythonObfuscatorResponse>
pub async fn obfuscate_script_source( &self, script_source: &str, return_as_object: bool, ) -> Option<PythonObfuscatorResponse>
Obfuscate Python script source code (string) using provided parameters.
return_as_object — return result as an object or JSON encoded string.
Examples found in repository?
16async fn main() -> Result<(), Box<dyn std::error::Error>> {
17 //
18 // include Python Obfuscator class
19 //
20
21 //
22 // when developing from a repository clone without installing the package use:
23 //
24 // use python_obfuscator::{PythonObfuscator, PythonObfuscatorResponse};
25 // (path dependency in Cargo.toml pointing to this crate)
26
27 //
28 // create Python Obfuscator class instance (we are using our activation key)
29 //
30 let my_python_obfuscator =
31 PythonObfuscator::new(Some("ABCD-ABCD-ABCD-ABCD".to_string()));
32
33 //
34 // source code in Python format
35 //
36 let script_source_code = r#"label = 'SecretKey'
37port = 443
38
39def get_sum(a, b):
40 return a + b
41
42print(label, port)
43r = get_sum(11, 31)
44print(r)"#;
45
46 //
47 // by default all obfuscation options are enabled, so we can just simply call:
48 //
49 let result = my_python_obfuscator
50 .obfuscate_script_source(script_source_code, true)
51 .await;
52
53 //
54 // it's also possible to pass a Python script file path instead of a string e.g.
55 //
56 // let result = my_python_obfuscator
57 // .obfuscate_script_file(std::path::Path::new("/path/to/project/script.py"), true)
58 // .await;
59
60 //
61 // result object holds the obfuscation results as well as other information
62 //
63 // result.error - error code
64 // result.output - obfuscated code
65 // result.demo - demo mode (empty/invalid key); strategy flags ignored; always integers_to_arrays, mba_binops, encrypt_strings (no virt; 1000 char limit)
66 // result.license_expiration - license end date (Y-m-d), empty if none
67 // result.usages_total - total obfuscations for this activation code
68 //
69 match result {
70 Some(PythonObfuscatorResponse::Object(obj)) => {
71 //
72 // display obfuscated code
73 //
74 if obj.error == PythonObfuscator::ERROR_SUCCESS {
75 println!("{}", obj.output.unwrap_or_default());
76 } else {
77 return Err(
78 format!("An error occurred, error code: {}", obj.error).into(),
79 );
80 }
81 }
82 Some(PythonObfuscatorResponse::Json(_)) => {}
83 None => {
84 return Err("Something unexpected happen while trying to obfuscate the code.".into());
85 }
86 }
87
88 Ok(())
89}More examples
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}