use base64::engine::general_purpose::STANDARD as B64_ENGINE;
use base64::Engine;
use flate2::read::ZlibDecoder;
use flate2::write::ZlibEncoder;
use flate2::Compression;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io::{Read, Write};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PowerShellProObfuscatorResult {
pub error: i64,
#[serde(default)]
pub output: Option<String>,
#[serde(default)]
pub demo: Option<bool>,
#[serde(default, rename = "credits_left")]
pub credits_left: Option<i64>,
#[serde(default, rename = "credits_total")]
pub credits_total: Option<i64>,
#[serde(default)]
pub expired: Option<bool>,
#[serde(default, rename = "string_limit")]
pub string_limit: Option<i64>,
}
#[derive(Debug, Clone)]
pub enum PowerShellProObfuscatorResponse {
Object(PowerShellProObfuscatorResult),
Json(String),
}
#[derive(Debug, Clone)]
pub struct PowerShellProObfuscator {
api_key: Option<String>,
client: reqwest::Client,
pub enable_compression: bool,
pub self_defending: bool,
pub protection_linker: bool,
pub rename_variables: bool,
pub rename_parameters: bool,
pub rename_functions: bool,
pub shuffle_functions: bool,
pub control_flow_flatten: bool,
pub state_machine: bool,
pub vm_strategy: bool,
pub encrypt_integers: bool,
pub split_strings: bool,
pub encrypt_strings: bool,
pub integers_to_arrays: bool,
pub floats_to_arrays: bool,
pub insert_dead_code: bool,
pub complexify_booleans: bool,
pub integers_to_floating: bool,
pub encrypt_floating: bool,
pub decoy_functions: bool,
pub detect_debugger: bool,
pub fake_dot_source_markers: bool,
pub opaque_branches: bool,
pub scriptblock_decoys: bool,
pub literal_padding: bool,
pub reflect_invoke_commands: bool,
pub string_char_array_vault: bool,
pub try_finally_noise: bool,
pub affine_integer_mask: bool,
pub event_stub: bool,
pub remove_comments: bool,
}
impl PowerShellProObfuscator {
pub const API_URL: &'static str = "https://www.pelock.com/api/powershell-pro-obfuscator/v1";
pub const ERROR_SUCCESS: i64 = 0;
pub const ERROR_INPUT_SIZE: i64 = 1;
pub const ERROR_INPUT: i64 = 2;
pub const ERROR_PARSING: i64 = 3;
pub const ERROR_OBFUSCATION: i64 = 4;
pub const ERROR_OUTPUT: i64 = 5;
pub fn new(api_key: Option<String>) -> Self {
Self {
api_key,
client: reqwest::Client::new(),
enable_compression: false,
self_defending: true,
protection_linker: true,
rename_variables: true,
rename_parameters: true,
rename_functions: true,
shuffle_functions: true,
control_flow_flatten: true,
state_machine: true,
vm_strategy: true,
encrypt_integers: true,
split_strings: true,
encrypt_strings: true,
integers_to_arrays: true,
floats_to_arrays: true,
insert_dead_code: true,
complexify_booleans: true,
integers_to_floating: true,
encrypt_floating: true,
decoy_functions: true,
detect_debugger: true,
fake_dot_source_markers: true,
opaque_branches: true,
scriptblock_decoys: true,
literal_padding: true,
reflect_invoke_commands: true,
string_char_array_vault: true,
try_finally_noise: true,
affine_integer_mask: true,
event_stub: true,
remove_comments: true,
}
}
pub async fn login(&self, return_as_object: bool) -> Option<PowerShellProObfuscatorResponse> {
let mut params = HashMap::new();
params.insert("command".to_string(), "login".to_string());
self.post_request(params, return_as_object).await
}
pub async fn obfuscate_script_file(
&self,
script_file_path: &std::path::Path,
return_as_object: bool,
) -> Option<PowerShellProObfuscatorResponse> {
let source = tokio::fs::read_to_string(script_file_path).await.ok()?;
if source.is_empty() {
return None;
}
self.obfuscate_script_source(&source, return_as_object).await
}
pub async fn obfuscate_script_source(
&self,
script_source: &str,
return_as_object: bool,
) -> Option<PowerShellProObfuscatorResponse> {
let mut params = HashMap::new();
params.insert("command".to_string(), "obfuscate".to_string());
params.insert("source".to_string(), script_source.to_string());
self.post_request(params, return_as_object).await
}
async fn post_request(
&self,
mut params: HashMap<String, String>,
return_as_object: bool,
) -> Option<PowerShellProObfuscatorResponse> {
if let Some(ref key) = self.api_key {
if !key.is_empty() {
params.insert("key".to_string(), key.clone());
}
}
if self.self_defending {
params.insert("self_defending".to_string(), "1".to_string());
}
if self.protection_linker {
params.insert("protection_linker".to_string(), "1".to_string());
}
if self.rename_variables {
params.insert("rename_variables".to_string(), "1".to_string());
}
if self.rename_parameters {
params.insert("rename_parameters".to_string(), "1".to_string());
}
if self.rename_functions {
params.insert("rename_functions".to_string(), "1".to_string());
}
if self.shuffle_functions {
params.insert("shuffle_functions".to_string(), "1".to_string());
}
if self.control_flow_flatten {
params.insert("control_flow_flatten".to_string(), "1".to_string());
}
if self.state_machine {
params.insert("state_machine".to_string(), "1".to_string());
}
if self.vm_strategy {
params.insert("vm_strategy".to_string(), "1".to_string());
}
if self.encrypt_integers {
params.insert("encrypt_integers".to_string(), "1".to_string());
}
if self.split_strings {
params.insert("split_strings".to_string(), "1".to_string());
}
if self.encrypt_strings {
params.insert("encrypt_strings".to_string(), "1".to_string());
}
if self.integers_to_arrays {
params.insert("integers_to_arrays".to_string(), "1".to_string());
}
if self.floats_to_arrays {
params.insert("floats_to_arrays".to_string(), "1".to_string());
}
if self.insert_dead_code {
params.insert("insert_dead_code".to_string(), "1".to_string());
}
if self.complexify_booleans {
params.insert("complexify_booleans".to_string(), "1".to_string());
}
if self.integers_to_floating {
params.insert("integers_to_floating".to_string(), "1".to_string());
}
if self.encrypt_floating {
params.insert("encrypt_floating".to_string(), "1".to_string());
}
if self.decoy_functions {
params.insert("decoy_functions".to_string(), "1".to_string());
}
if self.detect_debugger {
params.insert("detect_debugger".to_string(), "1".to_string());
}
if self.fake_dot_source_markers {
params.insert("fake_dot_source_markers".to_string(), "1".to_string());
}
if self.opaque_branches {
params.insert("opaque_branches".to_string(), "1".to_string());
}
if self.scriptblock_decoys {
params.insert("scriptblock_decoys".to_string(), "1".to_string());
}
if self.literal_padding {
params.insert("literal_padding".to_string(), "1".to_string());
}
if self.reflect_invoke_commands {
params.insert("reflect_invoke_commands".to_string(), "1".to_string());
}
if self.string_char_array_vault {
params.insert("string_char_array_vault".to_string(), "1".to_string());
}
if self.try_finally_noise {
params.insert("try_finally_noise".to_string(), "1".to_string());
}
if self.affine_integer_mask {
params.insert("affine_integer_mask".to_string(), "1".to_string());
}
if self.event_stub {
params.insert("event_stub".to_string(), "1".to_string());
}
if self.remove_comments {
params.insert("remove_comments".to_string(), "1".to_string());
}
if self.enable_compression {
if let Some(source) = params.get("source").cloned() {
let mut enc = ZlibEncoder::new(Vec::new(), Compression::best());
enc.write_all(source.as_bytes()).ok()?;
let compressed = enc.finish().ok()?;
params.insert("source".to_string(), B64_ENGINE.encode(compressed));
params.insert("compression".to_string(), "1".to_string());
}
}
let mut form = reqwest::multipart::Form::new();
for (k, v) in params {
form = form.text(k, v);
}
let response_text = self
.client
.post(Self::API_URL)
.header("User-Agent", "PELock PowerShell Pro Obfuscator")
.multipart(form)
.send()
.await
.ok()?
.text()
.await
.ok()?;
if response_text.is_empty() {
return None;
}
let mut result: PowerShellProObfuscatorResult =
serde_json::from_str(&response_text).ok()?;
let mut depacked = false;
if self.enable_compression
&& result.error == Self::ERROR_SUCCESS
&& result.output.as_ref().is_some_and(|s| !s.is_empty())
{
let b64 = result.output.as_ref()?;
let buf = B64_ENGINE.decode(b64.as_bytes()).ok()?;
let mut decoder = ZlibDecoder::new(&buf[..]);
let mut out = String::new();
decoder.read_to_string(&mut out).ok()?;
result.output = Some(out);
depacked = true;
}
if return_as_object {
return Some(PowerShellProObfuscatorResponse::Object(result));
}
if depacked {
let s = serde_json::to_string(&result).ok()?;
return Some(PowerShellProObfuscatorResponse::Json(s));
}
Some(PowerShellProObfuscatorResponse::Json(response_text))
}
}