Skip to main content

powershell_pro_obfuscator_example_simple/
powershell-pro-obfuscator-example-simple.rs

1/******************************************************************************
2 * PowerShell Pro Obfuscator WebApi interface usage example.
3 *
4 * In this example we will obfuscate sample source with default 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 my_powershell_pro_obfuscator =
31        PowerShellProObfuscator::new(Some("ABCD-ABCD-ABCD-ABCD".to_string()));
32
33    //
34    // source code in PowerShell format
35    //
36    let script_source_code = r#"function Get-Greeting {
37    param([string]$Name)
38    Write-Host "Hello World from $Name!"
39}
40Get-Greeting "PowerShell Pro Obfuscator""#;
41
42    //
43    // by default all obfuscation options are enabled, so we can just simply call:
44    //
45    let result = my_powershell_pro_obfuscator
46        .obfuscate_script_source(script_source_code, true)
47        .await;
48
49    //
50    // it's also possible to pass a PowerShell script file path instead of a string e.g.
51    //
52    // let result = my_powershell_pro_obfuscator
53    //     .obfuscate_script_file(std::path::Path::new("/path/to/project/script.ps1"), true)
54    //     .await;
55
56    //
57    // result object holds the obfuscation results as well as other information
58    //
59    // result.error         - error code
60    // result.output        - obfuscated code
61    // result.demo          - was it used in demo mode (invalid or empty activation key was used)
62    // result.credits_left  - usage credits left after this operation
63    // result.credits_total - total number of credits for this activation code
64    // result.expired       - if this was the last usage credit for the activation key it will be set to true
65    //
66    match result {
67        Some(PowerShellProObfuscatorResponse::Object(obj)) => {
68            //
69            // display obfuscated code
70            //
71            if obj.error == PowerShellProObfuscator::ERROR_SUCCESS {
72                println!("{}", obj.output.unwrap_or_default());
73            } else {
74                return Err(
75                    format!("An error occurred, error code: {}", obj.error).into(),
76                );
77            }
78        }
79        Some(PowerShellProObfuscatorResponse::Json(_)) => {}
80        None => {
81            return Err("Something unexpected happen while trying to obfuscate the code.".into());
82        }
83    }
84
85    Ok(())
86}