Skip to main content

autoit_obfuscator/
lib.rs

1/******************************************************************************
2 * AutoIt Obfuscator WebApi interface
3 *
4 * Version        : v1.5.0
5 * Language       : Rust
6 * Author         : Bartosz Wójcik
7 * Web page       : https://www.pelock.com
8 *
9 *****************************************************************************/
10
11use base64::engine::general_purpose::STANDARD as B64_ENGINE;
12use base64::Engine;
13use flate2::read::ZlibDecoder;
14use flate2::write::ZlibEncoder;
15use flate2::Compression;
16use serde::{Deserialize, Serialize};
17use std::collections::HashMap;
18use std::io::{Read, Write};
19
20#[derive(Debug, Clone, Deserialize, Serialize)]
21pub struct AutoItObfuscatorResult {
22    pub error: i64,
23    #[serde(default)]
24    pub output: Option<String>,
25    #[serde(default)]
26    pub demo: Option<bool>,
27    #[serde(default, rename = "credits_left")]
28    pub credits_left: Option<i64>,
29    #[serde(default, rename = "credits_total")]
30    pub credits_total: Option<i64>,
31    #[serde(default)]
32    pub expired: Option<bool>,
33    #[serde(default, rename = "string_limit")]
34    pub string_limit: Option<i64>,
35}
36
37#[derive(Debug, Clone)]
38pub enum AutoItObfuscatorResponse {
39    Object(AutoItObfuscatorResult),
40    Json(String),
41}
42
43#[derive(Debug, Clone)]
44pub struct AutoItObfuscator {
45    api_key: Option<String>,
46    client: reqwest::Client,
47    pub enable_compression: bool,
48    pub anti_debug: bool,
49    pub anti_vm: bool,
50    pub anti_sandbox: bool,
51    pub anti_emulator: bool,
52    pub random_integers: bool,
53    pub random_characters: bool,
54    pub random_anti_regex: bool,
55    pub random_arrays: bool,
56    pub random_arrays_multidimensional: bool,
57    pub random_functions: bool,
58    pub random_autostarted: bool,
59    pub mix_code_flow: bool,
60    pub rename_variables: bool,
61    pub rename_functions: bool,
62    pub rename_function_calls: bool,
63    pub shuffle_functions: bool,
64    pub resolve_constants: bool,
65    pub crypt_numbers: bool,
66    pub split_strings: bool,
67    pub modify_strings: bool,
68    pub crypt_strings: bool,
69    pub insert_ternary_operators: bool,
70}
71
72impl AutoItObfuscator {
73    pub const API_URL: &'static str = "https://www.pelock.com/api/autoit-obfuscator/v1";
74    pub const ERROR_SUCCESS: i64 = 0;
75    pub const ERROR_INPUT_SIZE: i64 = 1;
76    pub const ERROR_INPUT: i64 = 2;
77    pub const ERROR_PARSING: i64 = 3;
78    pub const ERROR_OBFUSCATION: i64 = 4;
79    pub const ERROR_OUTPUT: i64 = 5;
80
81    pub fn new(api_key: Option<String>) -> Self {
82        Self {
83            api_key,
84            client: reqwest::Client::new(),
85            enable_compression: false,
86            anti_debug: false,
87            anti_vm: false,
88            anti_sandbox: false,
89            anti_emulator: false,
90            random_integers: false,
91            random_characters: false,
92            random_anti_regex: false,
93            random_arrays: false,
94            random_arrays_multidimensional: false,
95            random_functions: false,
96            random_autostarted: false,
97            mix_code_flow: false,
98            rename_variables: false,
99            rename_functions: false,
100            rename_function_calls: false,
101            shuffle_functions: false,
102            resolve_constants: false,
103            crypt_numbers: false,
104            split_strings: false,
105            modify_strings: false,
106            crypt_strings: false,
107            insert_ternary_operators: false,
108        }
109    }
110
111    pub async fn login(&self, return_as_object: bool) -> Option<AutoItObfuscatorResponse> {
112        let mut params = HashMap::new();
113        params.insert("command".to_string(), "login".to_string());
114        self.post_request(params, return_as_object).await
115    }
116
117    pub async fn obfuscate_script_file(
118        &self,
119        script_file_path: &std::path::Path,
120        return_as_object: bool,
121    ) -> Option<AutoItObfuscatorResponse> {
122        let source = tokio::fs::read_to_string(script_file_path).await.ok()?;
123        if source.is_empty() {
124            return None;
125        }
126        self.obfuscate_script_source(&source, return_as_object).await
127    }
128
129    pub async fn obfuscate_script_source(
130        &self,
131        script_source: &str,
132        return_as_object: bool,
133    ) -> Option<AutoItObfuscatorResponse> {
134        let mut params = HashMap::new();
135        params.insert("command".to_string(), "obfuscate".to_string());
136        params.insert("source".to_string(), script_source.to_string());
137        self.post_request(params, return_as_object).await
138    }
139
140    async fn post_request(
141        &self,
142        mut params: HashMap<String, String>,
143        return_as_object: bool,
144    ) -> Option<AutoItObfuscatorResponse> {
145        if let Some(ref key) = self.api_key {
146            if !key.is_empty() {
147                params.insert("key".to_string(), key.clone());
148            }
149        }
150
151        if self.anti_debug {
152            params.insert("anti_debug".to_string(), "1".to_string());
153        }
154        if self.anti_vm {
155            params.insert("anti_vm".to_string(), "1".to_string());
156        }
157        if self.anti_sandbox {
158            params.insert("anti_sandbox".to_string(), "1".to_string());
159        }
160        if self.anti_emulator {
161            params.insert("anti_emulator".to_string(), "1".to_string());
162        }
163        if self.random_integers {
164            params.insert("random_bucket_integers".to_string(), "1".to_string());
165        }
166        if self.random_characters {
167            params.insert("random_bucket_characters".to_string(), "1".to_string());
168        }
169        if self.random_anti_regex {
170            params.insert("random_bucket_anti_regex".to_string(), "1".to_string());
171        }
172        if self.random_arrays {
173            params.insert("random_bucket_arrays".to_string(), "1".to_string());
174        }
175        if self.random_arrays_multidimensional {
176            params.insert("random_bucket_arrays_multidimensional".to_string(), "1".to_string());
177        }
178        if self.random_functions {
179            params.insert("random_bucket_functions".to_string(), "1".to_string());
180        }
181        if self.random_autostarted {
182            params.insert("random_bucket_autostart".to_string(), "1".to_string());
183        }
184        if self.mix_code_flow {
185            params.insert("mix_code_flow".to_string(), "1".to_string());
186        }
187        if self.rename_variables {
188            params.insert("rename_variables".to_string(), "1".to_string());
189        }
190        if self.rename_functions {
191            params.insert("rename_functions".to_string(), "1".to_string());
192        }
193        if self.rename_function_calls {
194            params.insert("rename_function_calls".to_string(), "1".to_string());
195        }
196        if self.shuffle_functions {
197            params.insert("shuffle_functions".to_string(), "1".to_string());
198        }
199        if self.resolve_constants {
200            params.insert("resolve_const".to_string(), "1".to_string());
201        }
202        if self.crypt_numbers {
203            params.insert("crypt_numbers".to_string(), "1".to_string());
204        }
205        if self.split_strings {
206            params.insert("split_strings".to_string(), "1".to_string());
207        }
208        if self.modify_strings {
209            params.insert("modify_strings".to_string(), "1".to_string());
210        }
211        if self.crypt_strings {
212            params.insert("crypt_strings".to_string(), "1".to_string());
213        }
214        if self.insert_ternary_operators {
215            params.insert("insert_ternary_operators".to_string(), "1".to_string());
216        }
217
218        if self.enable_compression {
219            if let Some(source) = params.get("source").cloned() {
220                let mut enc = ZlibEncoder::new(Vec::new(), Compression::best());
221                enc.write_all(source.as_bytes()).ok()?;
222                let compressed = enc.finish().ok()?;
223                params.insert("source".to_string(), B64_ENGINE.encode(compressed));
224                params.insert("compression".to_string(), "1".to_string());
225            }
226        }
227
228        let mut form = reqwest::multipart::Form::new();
229        for (k, v) in params {
230            form = form.text(k, v);
231        }
232
233        let response_text = self
234            .client
235            .post(Self::API_URL)
236            .header("User-Agent", "PELock AutoIt Obfuscator")
237            .multipart(form)
238            .send()
239            .await
240            .ok()?
241            .text()
242            .await
243            .ok()?;
244
245        if response_text.is_empty() {
246            return None;
247        }
248
249        let mut result: AutoItObfuscatorResult = serde_json::from_str(&response_text).ok()?;
250        let mut depacked = false;
251
252        if self.enable_compression
253            && result.error == Self::ERROR_SUCCESS
254            && result.output.as_ref().is_some_and(|s| !s.is_empty())
255        {
256            let b64 = result.output.as_ref()?;
257            let buf = B64_ENGINE.decode(b64.as_bytes()).ok()?;
258            let mut decoder = ZlibDecoder::new(&buf[..]);
259            let mut out = String::new();
260            decoder.read_to_string(&mut out).ok()?;
261            result.output = Some(out);
262            depacked = true;
263        }
264
265        if return_as_object {
266            return Some(AutoItObfuscatorResponse::Object(result));
267        }
268
269        if depacked {
270            let s = serde_json::to_string(&result).ok()?;
271            return Some(AutoItObfuscatorResponse::Json(s));
272        }
273
274        Some(AutoItObfuscatorResponse::Json(response_text))
275    }
276}