dotscope 0.6.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//! ConfuserEx resource decryption via emulation.
//!
//! This module provides resource protection decryption by:
//! 1. Finding the resource initializer method via candidate detection
//! 2. Emulating it with stubs for Assembly.Load to capture decrypted assemblies
//! 3. Extracting resources from the captured hidden assemblies
//! 4. Inserting those resources back into the original assembly
//!
//! # ConfuserEx Resource Protection
//!
//! ConfuserEx resource protection works as follows:
//! 1. All `EmbeddedResource` entries are moved to a hidden temporary assembly
//! 2. The hidden assembly is compressed with LZMA
//! 3. Encrypted with a block cipher (XOR in Normal mode)
//! 4. Stored in a FieldRVA (static field with raw data)
//! 5. A runtime stub registers an AssemblyResolve handler that:
//!    - Reads the encrypted data from the field
//!    - Decrypts using XOR with a key derived from xorshift
//!    - Decompresses with LZMA
//!    - Returns the hidden assembly via Assembly.Load
//!
//! By emulating this runtime stub and capturing Assembly.Load calls,
//! we can extract the hidden assemblies and recover the original resources.
//!
//! # Deobfuscation Process
//!
//! 1. Emulate the resource decryptor to capture hidden assembly bytes
//! 2. Load hidden assemblies as CilObjects
//! 3. Extract ManifestResource entries from hidden assemblies
//! 4. Insert resources back into the original assembly using CilAssembly
//! 5. Return the modified assembly with resources restored

use std::sync::Arc;

use crate::{
    assembly::Operand,
    cilassembly::{CilAssembly, GeneratorConfig},
    compiler::EventLog,
    deobfuscation::{
        detection::{DetectionEvidence, DetectionScore},
        findings::DeobfuscationFindings,
        obfuscators::confuserex::{
            candidates::{find_candidates, ProtectionType},
            hooks::create_lzma_hook,
        },
    },
    emulation::{EmulationOutcome, ProcessBuilder},
    error::Error,
    metadata::{
        tables::{
            ManifestResourceAttributes, ManifestResourceBuilder, ManifestResourceRaw, TableId,
        },
        token::Token,
    },
    prelude::FlowType,
    CilObject, Result, ValidationConfig,
};

/// Information about an extracted embedded assembly.
#[derive(Debug, Clone)]
pub struct ExtractedAssembly {
    /// Raw bytes of the extracted assembly.
    pub data: Vec<u8>,
    /// Suggested filename for the assembly (if determinable).
    pub suggested_name: Option<String>,
    /// Whether this appears to be a valid PE/COFF file.
    pub is_valid_pe: bool,
}

impl ExtractedAssembly {
    /// Creates a new `ExtractedAssembly` from raw bytes.
    #[must_use]
    pub fn new(data: Vec<u8>) -> Self {
        let is_valid_pe = data.len() >= 2 && data[0] == b'M' && data[1] == b'Z';

        // Try to extract assembly name from the PE if valid
        let suggested_name = if is_valid_pe {
            Self::try_extract_name(&data)
        } else {
            None
        };

        Self {
            data,
            suggested_name,
            is_valid_pe,
        }
    }

    /// Attempts to extract the assembly name from PE metadata.
    fn try_extract_name(data: &[u8]) -> Option<String> {
        // Try to load as CilObject to get the assembly name
        let assembly =
            CilObject::from_mem_with_validation(data.to_vec(), ValidationConfig::disabled())
                .ok()?;

        assembly.assembly().map(|a| format!("{}.dll", a.name))
    }

    /// Returns the size of the assembly in bytes.
    #[must_use]
    pub fn size(&self) -> usize {
        self.data.len()
    }
}

/// A resource extracted from a hidden ConfuserEx assembly.
///
/// This represents a single ManifestResource entry that was hidden
/// in an encrypted assembly by ConfuserEx resource protection.
#[derive(Debug, Clone)]
pub struct ExtractedResource {
    /// Resource name (e.g., "MyApp.Resources.strings.resources").
    pub name: String,
    /// Resource visibility flags (as raw bits).
    pub flags: u32,
    /// Raw resource data bytes.
    pub data: Vec<u8>,
}

impl ExtractedResource {
    /// Returns the size of the resource data in bytes.
    #[must_use]
    pub fn size(&self) -> usize {
        self.data.len()
    }

    /// Returns true if the resource is public.
    #[must_use]
    pub fn is_public(&self) -> bool {
        (self.flags & ManifestResourceAttributes::PUBLIC.bits()) != 0
    }
}

/// Result of resource decryption.
#[derive(Debug)]
pub struct ResourceDecryptionResult {
    /// Extracted embedded assemblies with metadata.
    pub assemblies: Vec<ExtractedAssembly>,
    /// Resources extracted from hidden assemblies.
    pub resources: Vec<ExtractedResource>,
    /// The method token that performed the decryption.
    pub decryptor_method: Option<Token>,
    /// Number of instructions executed during emulation.
    pub instructions_executed: u64,
    /// Tokens of methods/types that are ConfuserEx protection stubs.
    pub protection_stubs: Vec<Token>,
}

impl ResourceDecryptionResult {
    /// Returns true if any assemblies were extracted.
    #[must_use]
    pub fn has_assemblies(&self) -> bool {
        !self.assemblies.is_empty()
    }

    /// Returns true if any resources were extracted.
    #[must_use]
    pub fn has_resources(&self) -> bool {
        !self.resources.is_empty()
    }

    /// Returns the number of extracted assemblies.
    #[must_use]
    pub fn assembly_count(&self) -> usize {
        self.assemblies.len()
    }

    /// Returns the number of extracted resources.
    #[must_use]
    pub fn resource_count(&self) -> usize {
        self.resources.len()
    }

    /// Returns the total size of all extracted assemblies.
    #[must_use]
    pub fn total_size(&self) -> usize {
        self.assemblies.iter().map(ExtractedAssembly::size).sum()
    }

    /// Returns the total size of all extracted resources.
    #[must_use]
    pub fn total_resource_size(&self) -> usize {
        self.resources.iter().map(ExtractedResource::size).sum()
    }
}

/// Detects resource protection patterns and populates findings.
///
/// This is called by the orchestrator in detection.rs to detect
/// ConfuserEx resource protection.
///
/// Resource protection is detected by looking for:
/// - AppDomain.CurrentDomain.add_AssemblyResolve handler registration
/// - AppDomain.CurrentDomain.add_ResourceResolve handler registration
/// - Assembly.Load calls (for loading hidden assemblies)
/// - Delegate creation for event handlers
/// - Decompression calls (LZMA, Inflate, etc.)
pub fn detect(assembly: &CilObject, score: &DetectionScore, findings: &mut DeobfuscationFindings) {
    for method_entry in assembly.methods() {
        let method = method_entry.value();

        let mut has_assembly_resolve = false;
        let mut has_resource_resolve = false;
        let mut has_assembly_load = false;
        let mut has_decompress = false;
        let mut has_current_domain = false;

        for instr in method.instructions() {
            if instr.flow_type != FlowType::Call {
                continue;
            }

            let Operand::Token(token) = instr.operand else {
                continue;
            };

            let Some(type_name) = get_call_target_name(assembly, token) else {
                continue;
            };

            // AppDomain.CurrentDomain access
            if type_name.contains("AppDomain") && type_name.contains("CurrentDomain") {
                has_current_domain = true;
            }

            // AssemblyResolve event handler registration
            if type_name.contains("add_AssemblyResolve") || type_name.contains("AssemblyResolve") {
                has_assembly_resolve = true;
            }

            // ResourceResolve event handler registration
            if type_name.contains("add_ResourceResolve") || type_name.contains("ResourceResolve") {
                has_resource_resolve = true;
            }

            // Assembly.Load calls
            if type_name.contains("Assembly") && type_name.contains("Load") {
                has_assembly_load = true;
            }

            // Decompression calls
            if type_name.contains("Decompress")
                || type_name.contains("Lzma")
                || type_name.contains("Inflate")
                || type_name.contains("GZipStream")
                || type_name.contains("DeflateStream")
            {
                has_decompress = true;
            }
        }

        // Resource handler pattern: registers event handler + has assembly load
        let is_resource_handler = (has_assembly_resolve || has_resource_resolve)
            && has_current_domain
            && (has_assembly_load || has_decompress);

        if is_resource_handler {
            findings.resource_handler_methods.push(method.token);
            let locations = boxcar::Vec::new();
            locations.push(method.token);
            score.add(DetectionEvidence::BytecodePattern {
                name: format!(
                    "Resource handler (resolve={} load={} decompress={})",
                    has_assembly_resolve || has_resource_resolve,
                    has_assembly_load,
                    has_decompress
                ),
                locations,
                confidence: 5,
            });
        }
    }
}

/// Gets the fully qualified name of a call target from a token.
fn get_call_target_name(assembly: &CilObject, token: Token) -> Option<String> {
    let table_id = token.table();

    match table_id {
        // MethodDef
        0x06 => {
            let method = assembly.method(&token)?;
            Some(method.name.clone())
        }
        // MemberRef
        0x0A => {
            if let Some(member_ref) = assembly.member_ref(&token) {
                let type_name = member_ref
                    .declaredby
                    .fullname()
                    .unwrap_or_else(|| "Unknown".to_string());
                return Some(format!("{}::{}", type_name, member_ref.name));
            }
            None
        }
        // MethodSpec - generic method instantiation, check the underlying method
        0x2B => {
            // MethodSpec wraps a MethodDef or MemberRef with generic args
            // For detection purposes, we can skip these as the underlying method
            // will be checked separately
            None
        }
        _ => None,
    }
}

/// Extracts ManifestResource entries from a hidden assembly.
///
/// This function loads the raw assembly bytes and extracts all embedded resources.
/// If the hidden assembly is a netmodule (no Assembly table), it falls back to
/// returning an empty list since netmodules typically don't contain the actual
/// resource data - they just define the manifest entries.
///
/// # Arguments
///
/// * `data` - Raw bytes of the hidden assembly or netmodule.
///
/// # Returns
///
/// A vector of extracted resources, or an empty vector if resources can't be extracted.
fn extract_resources_from_assembly(data: &[u8]) -> Vec<ExtractedResource> {
    // Try loading as a full CilObject (for assemblies with Assembly table)
    match CilObject::from_mem_with_validation(data.to_vec(), ValidationConfig::disabled()) {
        Ok(hidden_assembly) => {
            // Standard path: use the Resources container
            let resources = hidden_assembly.resources();
            let mut extracted = Vec::new();

            for entry in resources {
                let manifest = entry.value();

                // Only extract embedded resources (source == None means embedded)
                if manifest.source.is_some() {
                    continue;
                }

                // Get actual resource data
                if let Some(resource_data) = resources.get_data(manifest) {
                    extracted.push(ExtractedResource {
                        name: manifest.name.clone(),
                        flags: manifest.flags.bits(),
                        data: resource_data.to_vec(),
                    });
                }
            }

            extracted
        }
        Err(_) => {
            // Failed to load as CilObject (likely a netmodule without Assembly table)
            // For now, return empty - netmodules need special handling
            // TODO: Implement direct parsing of ManifestResource table for netmodules
            Vec::new()
        }
    }
}

/// Attempts to decrypt resource-protected embedded assemblies.
///
/// This function uses emulation to run the resource initializer and capture
/// any Assembly.Load calls, extracting the decrypted assembly bytes.
///
/// # Arguments
///
/// * `assembly` - The resource-protected assembly.
/// * `events` - Event log for recording activity.
///
/// # Returns
///
/// A new `CilObject` with resource protection removed, along with any extracted
/// embedded assemblies in the `ResourceDecryptionResult`.
///
/// # Extracted Assemblies
///
/// The extracted assemblies are complete .NET assemblies that were hidden by ConfuserEx.
/// They should be saved as separate files alongside the deobfuscated main assembly.
/// The `ExtractedAssembly::suggested_name` field provides a filename suggestion based
/// on the assembly's metadata.
///
/// # Errors
///
/// Returns an error if emulation fails catastrophically.
pub fn decrypt_resources(
    assembly: CilObject,
    events: &mut EventLog,
) -> Result<(CilObject, ResourceDecryptionResult)> {
    // Keep original bytes for rebuilding
    let pe_bytes = assembly.file().data().to_vec();

    // Wrap in Arc for emulation (emulation API requires Arc)
    let assembly_arc = Arc::new(assembly);

    // Find resource protection candidates
    let candidates = find_candidates(&assembly_arc, ProtectionType::Resources);
    let result = if candidates.is_empty() {
        ResourceDecryptionResult {
            assemblies: Vec::new(),
            resources: Vec::new(),
            decryptor_method: None,
            instructions_executed: 0,
            protection_stubs: Vec::new(),
        }
    } else {
        // Try each candidate until we successfully extract assemblies
        let mut final_result = None;
        for candidate in candidates.iter() {
            match try_emulate_resource_decryptor(&assembly_arc, candidate.token, events) {
                Ok(result) if result.has_assemblies() || result.has_resources() => {
                    // Log extraction details
                    events.info(format!(
                        "Extracted {} hidden assemblies ({} bytes) and {} resources ({} bytes) via method 0x{:08x}",
                        result.assembly_count(),
                        result.total_size(),
                        result.resource_count(),
                        result.total_resource_size(),
                        candidate.token.value()
                    ));

                    // Log individual assembly names
                    for (i, asm) in result.assemblies.iter().enumerate() {
                        let name = asm.suggested_name.as_deref().unwrap_or("<unknown>");
                        events.info(format!(
                            "  Hidden assembly {}: {} ({} bytes, valid PE: {})",
                            i + 1,
                            name,
                            asm.size(),
                            asm.is_valid_pe
                        ));
                    }

                    // Log individual resource names
                    for (i, res) in result.resources.iter().enumerate() {
                        let visibility = if res.is_public() { "public" } else { "private" };
                        events.info(format!(
                            "  Resource {}: {} ({} bytes, {})",
                            i + 1,
                            res.name,
                            res.size(),
                            visibility
                        ));
                    }

                    final_result = Some(result);
                    break;
                }
                Ok(_) => {}
                Err(e) => {
                    events.warn(format!(
                        "Resource emulation failed for 0x{:08x}: {}",
                        candidate.token.value(),
                        e
                    ));
                }
            }
        }
        final_result.unwrap_or(ResourceDecryptionResult {
            assemblies: Vec::new(),
            resources: Vec::new(),
            decryptor_method: None,
            instructions_executed: 0,
            protection_stubs: Vec::new(),
        })
    };

    // Drop the Arc - we'll return a new CilObject
    drop(assembly_arc);

    // Early exit: no resources to insert, just reload unchanged
    if !result.has_resources() {
        let new_assembly =
            CilObject::from_mem_with_validation(pe_bytes, ValidationConfig::analysis())?;
        return Ok((new_assembly, result));
    }

    // Create CilAssembly for modifications
    let mut cil_assembly =
        CilAssembly::from_bytes_with_validation(pe_bytes.clone(), ValidationConfig::analysis())?;

    // Remove existing ManifestResource rows with matching names to avoid duplicates.
    // We replace ANY existing resource with the same name (embedded or external)
    // to ensure clean replacement with the decrypted version.
    let names_to_insert: std::collections::HashSet<_> =
        result.resources.iter().map(|r| r.name.as_str()).collect();

    let rids_to_remove = find_manifest_resources_by_name(&cil_assembly, &names_to_insert);
    for rid in rids_to_remove {
        if let Err(e) = cil_assembly.table_row_remove(TableId::ManifestResource, rid) {
            events.warn(format!(
                "Failed to remove existing ManifestResource row {rid}: {e}"
            ));
        }
    }

    // Insert each extracted resource
    for resource in &result.resources {
        let builder = ManifestResourceBuilder::new()
            .name(&resource.name)
            .flags(resource.flags)
            .resource_data(&resource.data);

        match builder.build(&mut cil_assembly) {
            Ok(_) => events.info(format!(
                "Inserted resource: {} ({} bytes)",
                resource.name,
                resource.size()
            )),
            Err(e) => events.warn(format!(
                "Failed to insert resource '{}': {}",
                resource.name, e
            )),
        }
    }

    // Build and return the modified assembly
    let new_assembly = cil_assembly
        .into_cilobject_with(ValidationConfig::analysis(), GeneratorConfig::default())?;
    Ok((new_assembly, result))
}

/// Finds ManifestResource row IDs that match any of the given names.
fn find_manifest_resources_by_name(
    cil_assembly: &CilAssembly,
    names: &std::collections::HashSet<&str>,
) -> Vec<u32> {
    let view = cil_assembly.view();
    let Some(strings) = view.strings() else {
        return Vec::new();
    };
    let Some(tables) = view.tables() else {
        return Vec::new();
    };
    let Some(manifest_table) = tables.table::<ManifestResourceRaw>() else {
        return Vec::new();
    };

    manifest_table
        .iter()
        .filter_map(|row| {
            strings
                .get(row.name as usize)
                .ok()
                .filter(|name| names.contains(*name))
                .map(|_| row.rid)
        })
        .collect()
}

/// Attempts to emulate a single resource decryptor candidate.
fn try_emulate_resource_decryptor(
    assembly: &Arc<CilObject>,
    method_token: Token,
    _events: &mut EventLog,
) -> Result<ResourceDecryptionResult> {
    // BCL stream hooks track state via HeapObject::Stream, so no custom
    // registration needed - the default BCL hooks handle MemoryStream operations.
    // We add the LZMA hook to handle ConfuserEx's inline LZMA decompressor natively.
    let process = ProcessBuilder::new()
        .assembly_arc(Arc::clone(assembly))
        .with_max_instructions(2_000_000)
        .with_max_call_depth(100)
        .capture_assemblies() // Enable assembly capture for embedded assembly extraction
        .hook(create_lzma_hook()) // Native LZMA decompression for ConfuserEx
        .build()?;

    let outcome = process.execute_method(method_token, vec![])?;
    let instructions_executed = match outcome {
        EmulationOutcome::Completed { instructions, .. }
        | EmulationOutcome::Breakpoint { instructions, .. } => instructions,
        EmulationOutcome::LimitReached { limit, .. } => {
            return Err(Error::Deobfuscation(format!(
                "Resource emulation exceeded limit: {limit:?}"
            )));
        }
        EmulationOutcome::Stopped { reason, .. } => {
            return Err(Error::Deobfuscation(format!(
                "Resource emulation stopped: {reason}"
            )));
        }
        EmulationOutcome::UnhandledException { exception, .. } => {
            return Err(Error::Deobfuscation(format!(
                "Resource emulation threw exception: {exception:?}"
            )));
        }
        EmulationOutcome::RequiresSymbolic { reason, .. } => {
            return Err(Error::Deobfuscation(format!(
                "Resource emulation requires symbolic execution: {reason}"
            )));
        }
    };

    // Convert captured assembly bytes to ExtractedAssembly with metadata
    let assemblies: Vec<ExtractedAssembly> = process
        .capture()
        .assemblies()
        .iter()
        .map(|a| ExtractedAssembly::new(a.data.clone()))
        .collect();

    // Extract resources from each hidden assembly
    let mut resources = Vec::new();
    for asm in &assemblies {
        if asm.is_valid_pe {
            let extracted = extract_resources_from_assembly(&asm.data);
            resources.extend(extracted);
        }
    }

    // The decryptor method itself is a protection stub that should be removed
    let protection_stubs = vec![method_token];

    Ok(ResourceDecryptionResult {
        assemblies,
        resources,
        decryptor_method: Some(method_token),
        instructions_executed,
        protection_stubs,
    })
}

#[cfg(test)]
mod tests {
    use crate::{
        compiler::EventLog,
        deobfuscation::obfuscators::confuserex::{
            candidates::{find_candidates, ProtectionType},
            resources::decrypt_resources,
        },
        CilObject, ValidationConfig,
    };

    const MAXIMUM_PATH: &str = "tests/samples/packers/confuserex/mkaring_maximum.exe";
    const RESOURCES_PATH: &str = "tests/samples/packers/confuserex/mkaring_resources.exe";

    #[test]
    fn test_find_resource_candidates() {
        let assembly =
            CilObject::from_path_with_validation(MAXIMUM_PATH, ValidationConfig::analysis())
                .expect("Failed to load assembly");

        let candidates = find_candidates(&assembly, ProtectionType::Resources);

        println!("Resource candidates found: {}", candidates.candidates.len());
        for (i, c) in candidates.iter().enumerate() {
            println!(
                "  {}. 0x{:08x} score={} reasons={:?}",
                i + 1,
                c.token.value(),
                c.score,
                c.reasons
            );
        }
    }

    #[test]
    fn test_find_resource_candidates_in_resources_sample() {
        let assembly =
            CilObject::from_path_with_validation(RESOURCES_PATH, ValidationConfig::analysis())
                .expect("Failed to load assembly");

        let candidates = find_candidates(&assembly, ProtectionType::Resources);

        println!(
            "Resource candidates in resources sample: {}",
            candidates.candidates.len()
        );
        for (i, c) in candidates.iter().enumerate() {
            println!(
                "  {}. 0x{:08x} score={} reasons={:?}",
                i + 1,
                c.token.value(),
                c.score,
                c.reasons
            );
        }

        // Resources sample should have candidates with resource-specific patterns
        assert!(
            !candidates.is_empty(),
            "Resources sample should have resource protection candidates"
        );
    }

    #[test]
    fn test_decrypt_resources() {
        let assembly =
            CilObject::from_path_with_validation(RESOURCES_PATH, ValidationConfig::analysis())
                .expect("Failed to load assembly");

        // First show candidates
        let candidates = find_candidates(&assembly, ProtectionType::Resources);
        println!("Resource candidates: {}", candidates.candidates.len());
        for (i, c) in candidates.iter().enumerate() {
            println!(
                "  {}. 0x{:08x} score={} reasons={:?}",
                i + 1,
                c.token.value(),
                c.score,
                c.reasons
            );
        }

        let mut events = EventLog::new();
        let result = decrypt_resources(assembly, &mut events);

        // Show events
        println!("Events logged: {}", events.len());
        for event in events.iter() {
            println!("  Event: {:?}", event);
        }

        match result {
            Ok((deobfuscated_assembly, decryption_result)) => {
                println!(
                    "Resource decryption result: {} assemblies extracted",
                    decryption_result.assembly_count()
                );
                if let Some(method) = decryption_result.decryptor_method {
                    println!("Decryptor method: 0x{:08x}", method.value());
                }
                println!(
                    "Instructions executed: {}",
                    decryption_result.instructions_executed
                );

                for (i, asm) in decryption_result.assemblies.iter().enumerate() {
                    println!("  Assembly {}: {} bytes", i + 1, asm.size());
                }

                // Also print extracted resources
                for (i, res) in decryption_result.resources.iter().enumerate() {
                    println!("  Resource {}: {} ({} bytes)", i + 1, res.name, res.size());
                }

                // Verify resources were extracted from the assemblies
                assert!(
                    decryption_result.has_resources(),
                    "Should have extracted at least one resource"
                );
                assert_eq!(
                    decryption_result.resource_count(),
                    1,
                    "Should have extracted exactly one resource (duplicates are filtered)"
                );
                assert_eq!(
                    decryption_result.resources[0].name, "ResourceTestApp.testimage.bmp",
                    "Resource name should match expected"
                );

                // Verify resources are present in the final CilObject
                println!("\nVerifying resources in deobfuscated assembly:");
                let final_resources = deobfuscated_assembly.resources();
                println!(
                    "  Resource count in final assembly: {}",
                    final_resources.len()
                );

                // Find our inserted resource and verify it's accessible
                let mut found_resource = false;
                for entry in final_resources.iter() {
                    let manifest = entry.value();
                    println!(
                        "    - {} ({} bytes, embedded: {})",
                        manifest.name,
                        manifest.data_size,
                        manifest.source.is_none()
                    );
                    if manifest.name == "ResourceTestApp.testimage.bmp" {
                        found_resource = true;

                        // Verify resource data is accessible
                        let data = final_resources
                            .get_data(manifest)
                            .expect("Resource data should be accessible");

                        // The original testimage.bmp is 246 bytes
                        assert_eq!(
                            data.len(),
                            246,
                            "Resource data size should match original (246 bytes)"
                        );
                    }
                }
                assert!(
                    found_resource,
                    "Inserted resource should be present in final assembly"
                );
            }
            Err(e) => {
                panic!("Resource decryption failed: {}", e);
            }
        }
    }
}