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
//! `System.AppDomain` and `System.Reflection.Assembly` method hooks.
//!
//! This module provides hook implementations for application domain and assembly-related
//! methods that are commonly used in obfuscated .NET assemblies for dynamic assembly
//! loading, event handling, and resource access.
//!
//! # Overview
//!
//! Obfuscators frequently use dynamic assembly loading to hide payloads or decrypt
//! embedded code at runtime. This module's hooks intercept these operations and capture
//! the loaded assembly bytes for analysis, which is crucial for unpacking protected
//! executables.
//!
//! # Emulated .NET Methods
//!
//! ## AppDomain Methods
//!
//! | .NET Method | Hook Behavior |
//! |-------------|---------------|
//! | `AppDomain.CurrentDomain` | Returns a symbolic `AppDomain` object |
//! | `AppDomain.add_AssemblyResolve` | No-op (event registration ignored) |
//! | `AppDomain.remove_AssemblyResolve` | No-op (event unregistration ignored) |
//! | `AppDomain.GetAssemblies()` | Returns an empty `Assembly[]` array |
//!
//! ## Assembly Methods
//!
//! | .NET Method | Hook Behavior |
//! |-------------|---------------|
//! | `Assembly.Load(byte[])` | **Captures bytes**, returns symbolic `Assembly` |
//! | `Assembly.LoadFrom(string)` | Returns symbolic `Assembly` (path not loaded) |
//! | `Assembly.GetExecutingAssembly()` | Returns symbolic `Assembly` |
//! | `Assembly.GetCallingAssembly()` | Returns symbolic `Assembly` |
//! | `Assembly.GetEntryAssembly()` | Returns symbolic `Assembly` |
//! | `Assembly.GetManifestResourceStream()` | Returns stream with resource data |
//! | `Assembly.GetManifestResourceNames()` | Returns empty `string[]` |
//!
//! ## Delegate Methods
//!
//! | .NET Method | Hook Behavior |
//! |-------------|---------------|
//! | `Delegate..ctor` | Returns symbolic delegate object |
//! | `MulticastDelegate..ctor` | Returns symbolic delegate object |
//! | `ResolveEventHandler..ctor` | Returns symbolic delegate object |
//!
//! # Deobfuscation Use Cases
//!
//! ## Unpacking Embedded Assemblies
//!
//! Many packers store encrypted assemblies as resources or embedded data. At runtime,
//! they decrypt the bytes and call `Assembly.Load(byte[])`. This hook captures those
//! bytes, allowing the analyst to extract the unpacked assembly.

use crate::{
    emulation::{
        capture::{AssemblyLoadMethod, CaptureSource},
        runtime::hook::{Hook, HookContext, HookManager, PreHookResult},
        thread::EmulationThread,
        EmValue,
    },
    metadata::{token::Token, typesystem::CilFlavor},
};

/// Registers all AppDomain and Assembly method hooks with the given hook manager.
///
/// # Arguments
///
/// * `manager` - The [`HookManager`] to register hooks with
///
/// # Registered Hooks
///
/// - `AppDomain.get_CurrentDomain`
/// - `AppDomain.add_AssemblyResolve` / `remove_AssemblyResolve`
/// - `AppDomain.GetAssemblies()`
/// - `Assembly.Load(byte[])` - **captures assembly bytes**
/// - `Assembly.LoadFrom(string)`
/// - `Assembly.GetExecutingAssembly()` / `GetCallingAssembly()` / `GetEntryAssembly()`
/// - `Assembly.GetManifestResourceStream()` / `GetManifestResourceNames()`
/// - `Delegate..ctor` / `MulticastDelegate..ctor` / `ResolveEventHandler..ctor`
pub fn register(manager: &mut HookManager) {
    // AppDomain methods
    manager.register(
        Hook::new("System.AppDomain.get_CurrentDomain")
            .match_name("System", "AppDomain", "get_CurrentDomain")
            .pre(appdomain_get_current_domain_pre),
    );

    manager.register(
        Hook::new("System.AppDomain.add_AssemblyResolve")
            .match_name("System", "AppDomain", "add_AssemblyResolve")
            .pre(appdomain_add_assembly_resolve_pre),
    );

    manager.register(
        Hook::new("System.AppDomain.remove_AssemblyResolve")
            .match_name("System", "AppDomain", "remove_AssemblyResolve")
            .pre(appdomain_remove_assembly_resolve_pre),
    );

    manager.register(
        Hook::new("System.AppDomain.GetAssemblies")
            .match_name("System", "AppDomain", "GetAssemblies")
            .pre(appdomain_get_assemblies_pre),
    );

    // Assembly methods
    manager.register(
        Hook::new("System.Reflection.Assembly.Load")
            .match_name("System.Reflection", "Assembly", "Load")
            .pre(assembly_load_pre),
    );

    manager.register(
        Hook::new("System.Reflection.Assembly.LoadFrom")
            .match_name("System.Reflection", "Assembly", "LoadFrom")
            .pre(assembly_load_from_pre),
    );

    manager.register(
        Hook::new("System.Reflection.Assembly.GetExecutingAssembly")
            .match_name("System.Reflection", "Assembly", "GetExecutingAssembly")
            .pre(assembly_get_executing_assembly_pre),
    );

    manager.register(
        Hook::new("System.Reflection.Assembly.GetCallingAssembly")
            .match_name("System.Reflection", "Assembly", "GetCallingAssembly")
            .pre(assembly_get_calling_assembly_pre),
    );

    manager.register(
        Hook::new("System.Reflection.Assembly.GetEntryAssembly")
            .match_name("System.Reflection", "Assembly", "GetEntryAssembly")
            .pre(assembly_get_entry_assembly_pre),
    );

    manager.register(
        Hook::new("System.Reflection.Assembly.GetManifestResourceStream")
            .match_name("System.Reflection", "Assembly", "GetManifestResourceStream")
            .pre(assembly_get_manifest_resource_stream_pre),
    );

    manager.register(
        Hook::new("System.Reflection.Assembly.GetManifestResourceNames")
            .match_name("System.Reflection", "Assembly", "GetManifestResourceNames")
            .pre(assembly_get_manifest_resource_names_pre),
    );

    // Delegate methods
    manager.register(
        Hook::new("System.Delegate..ctor")
            .match_name("System", "Delegate", ".ctor")
            .pre(delegate_ctor_pre),
    );

    manager.register(
        Hook::new("System.MulticastDelegate..ctor")
            .match_name("System", "MulticastDelegate", ".ctor")
            .pre(delegate_ctor_pre),
    );

    manager.register(
        Hook::new("System.ResolveEventHandler..ctor")
            .match_name("System", "ResolveEventHandler", ".ctor")
            .pre(delegate_ctor_pre),
    );
}

/// Hook for `System.AppDomain.get_CurrentDomain` property.
///
/// # Handled Overloads
///
/// - `AppDomain.CurrentDomain -> AppDomain` (property getter)
///
/// # Parameters
///
/// None (static property).
///
/// # Returns
///
/// The cached `AppDomain` object reference for consistent equality checks.
fn appdomain_get_current_domain_pre(
    _ctx: &HookContext<'_>,
    thread: &mut EmulationThread,
) -> PreHookResult {
    // Return cached fake app domain for consistent equality checks
    if let Some(domain_ref) = thread.fake_objects().app_domain() {
        return PreHookResult::Bypass(Some(EmValue::ObjectRef(domain_ref)));
    }

    // Fallback: allocate new object if cache not initialized
    match thread.heap_mut().alloc_object(Token::new(0x0100_0011)) {
        Ok(domain_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(domain_ref))),
        Err(_) => PreHookResult::Bypass(Some(EmValue::Null)),
    }
}

/// Hook for `System.AppDomain.add_AssemblyResolve` event accessor.
///
/// # Handled Overloads
///
/// - `AppDomain.add_AssemblyResolve(ResolveEventHandler) -> void`
///
/// # Parameters
///
/// - `value`: The `ResolveEventHandler` delegate to add to the event.
///
/// # Returns
///
/// None. This hook is a no-op (event subscription is ignored during emulation).
fn appdomain_add_assembly_resolve_pre(
    _ctx: &HookContext<'_>,
    _thread: &mut EmulationThread,
) -> PreHookResult {
    PreHookResult::Bypass(None)
}

/// Hook for `System.AppDomain.remove_AssemblyResolve` event accessor.
///
/// # Handled Overloads
///
/// - `AppDomain.remove_AssemblyResolve(ResolveEventHandler) -> void`
///
/// # Parameters
///
/// - `value`: The `ResolveEventHandler` delegate to remove from the event.
///
/// # Returns
///
/// None. This hook is a no-op (event unsubscription is ignored during emulation).
fn appdomain_remove_assembly_resolve_pre(
    _ctx: &HookContext<'_>,
    _thread: &mut EmulationThread,
) -> PreHookResult {
    PreHookResult::Bypass(None)
}

/// Hook for `System.AppDomain.GetAssemblies` method.
///
/// # Handled Overloads
///
/// - `AppDomain.GetAssemblies() -> Assembly[]`
///
/// # Parameters
///
/// None (instance method, `this` is the AppDomain).
///
/// # Returns
///
/// An empty `Assembly[]` array.
fn appdomain_get_assemblies_pre(
    _ctx: &HookContext<'_>,
    thread: &mut EmulationThread,
) -> PreHookResult {
    match thread.heap_mut().alloc_array(CilFlavor::Object, 0) {
        Ok(array_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(array_ref))),
        Err(_) => PreHookResult::Bypass(Some(EmValue::Null)),
    }
}

/// Hook for `System.Reflection.Assembly.Load` method.
///
/// This is one of the most important hooks for deobfuscation. When obfuscated code
/// dynamically loads an assembly from a byte array, this hook captures the raw bytes
/// for later extraction and analysis.
///
/// # Handled Overloads
///
/// - `Assembly.Load(Byte[]) -> Assembly`
/// - `Assembly.Load(Byte[], Byte[]) -> Assembly` (with symbols)
/// - `Assembly.Load(String) -> Assembly` (by name, not captured)
/// - `Assembly.Load(AssemblyName) -> Assembly` (by AssemblyName, not captured)
///
/// # Parameters
///
/// - `rawAssembly`: The byte array containing the raw assembly data (PE file).
/// - `rawSymbolStore`: Optional byte array containing debugging symbols.
/// - `assemblyString`: Assembly display name (for string overload).
/// - `assemblyRef`: AssemblyName object (for AssemblyName overload).
///
/// # Returns
///
/// A symbolic `Assembly` object reference. The raw bytes are captured for analysis.
fn assembly_load_pre(ctx: &HookContext<'_>, thread: &mut EmulationThread) -> PreHookResult {
    // Assembly.Load(byte[]) - first arg is the byte array
    if let Some(EmValue::ObjectRef(array_ref)) = ctx.args.first() {
        if let Some(bytes) = thread.heap().get_byte_array(*array_ref) {
            // Capture the assembly bytes
            let source = CaptureSource::new(
                thread.current_method().unwrap_or(Token::new(0)),
                thread.id(),
                thread.current_offset().unwrap_or(0),
                0,
            );
            thread
                .capture()
                .capture_assembly(bytes, source, AssemblyLoadMethod::LoadBytes, None);
        }
    }

    // Return a fake Assembly object
    match thread.heap_mut().alloc_object(Token::new(0x0100_0010)) {
        Ok(assembly_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(assembly_ref))),
        Err(_) => PreHookResult::Bypass(Some(EmValue::Null)),
    }
}

/// Hook for `System.Reflection.Assembly.LoadFrom` method.
///
/// # Handled Overloads
///
/// - `Assembly.LoadFrom(String) -> Assembly`
/// - `Assembly.LoadFrom(String, Evidence) -> Assembly`
/// - `Assembly.LoadFrom(String, Byte[], AssemblyHashAlgorithm) -> Assembly`
///
/// # Parameters
///
/// - `assemblyFile`: The file path to the assembly to load.
/// - `securityEvidence`: Optional security evidence for the assembly.
/// - `hashValue`: Optional hash value for verification.
/// - `hashAlgorithm`: Hash algorithm used for the hash value.
///
/// # Returns
///
/// A symbolic `Assembly` object reference. The file is not actually loaded.
fn assembly_load_from_pre(_ctx: &HookContext<'_>, thread: &mut EmulationThread) -> PreHookResult {
    match thread.heap_mut().alloc_object(Token::new(0x0100_0010)) {
        Ok(assembly_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(assembly_ref))),
        Err(_) => PreHookResult::Bypass(Some(EmValue::Null)),
    }
}

/// Hook for `System.Reflection.Assembly.GetExecutingAssembly` method.
///
/// # Handled Overloads
///
/// - `Assembly.GetExecutingAssembly() -> Assembly`
///
/// # Parameters
///
/// None (static method).
///
/// # Returns
///
/// The cached `Assembly` object reference for consistent equality checks.
///
/// # Note
///
/// This returns the same reference as `GetCallingAssembly()` to ensure that
/// anti-tamper checks like `GetExecutingAssembly().Equals(GetCallingAssembly())`
/// pass during emulation.
fn assembly_get_executing_assembly_pre(
    _ctx: &HookContext<'_>,
    thread: &mut EmulationThread,
) -> PreHookResult {
    // Return cached fake assembly for consistent equality checks
    if let Some(assembly_ref) = thread.fake_objects().assembly() {
        return PreHookResult::Bypass(Some(EmValue::ObjectRef(assembly_ref)));
    }

    // Fallback: allocate new object if cache not initialized
    match thread.heap_mut().alloc_object(Token::new(0x0100_0010)) {
        Ok(assembly_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(assembly_ref))),
        Err(_) => PreHookResult::Bypass(Some(EmValue::Null)),
    }
}

/// Hook for `System.Reflection.Assembly.GetCallingAssembly` method.
///
/// # Handled Overloads
///
/// - `Assembly.GetCallingAssembly() -> Assembly`
///
/// # Parameters
///
/// None (static method).
///
/// # Returns
///
/// The cached `Assembly` object reference for consistent equality checks.
///
/// # Note
///
/// This returns the same reference as `GetExecutingAssembly()` to ensure that
/// anti-tamper checks like `GetExecutingAssembly().Equals(GetCallingAssembly())`
/// pass during emulation.
fn assembly_get_calling_assembly_pre(
    _ctx: &HookContext<'_>,
    thread: &mut EmulationThread,
) -> PreHookResult {
    // Return cached fake assembly for consistent equality checks
    if let Some(assembly_ref) = thread.fake_objects().assembly() {
        return PreHookResult::Bypass(Some(EmValue::ObjectRef(assembly_ref)));
    }

    // Fallback: allocate new object if cache not initialized
    match thread.heap_mut().alloc_object(Token::new(0x0100_0010)) {
        Ok(assembly_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(assembly_ref))),
        Err(_) => PreHookResult::Bypass(Some(EmValue::Null)),
    }
}

/// Hook for `System.Reflection.Assembly.GetEntryAssembly` method.
///
/// # Handled Overloads
///
/// - `Assembly.GetEntryAssembly() -> Assembly`
///
/// # Parameters
///
/// None (static method).
///
/// # Returns
///
/// The cached `Assembly` object reference for consistent equality checks.
///
/// # Note
///
/// This returns the same reference as `GetExecutingAssembly()` and `GetCallingAssembly()`
/// to ensure that any equality checks between these methods pass during emulation.
fn assembly_get_entry_assembly_pre(
    _ctx: &HookContext<'_>,
    thread: &mut EmulationThread,
) -> PreHookResult {
    // Return cached fake assembly for consistent equality checks
    if let Some(assembly_ref) = thread.fake_objects().assembly() {
        return PreHookResult::Bypass(Some(EmValue::ObjectRef(assembly_ref)));
    }

    // Fallback: allocate new object if cache not initialized
    match thread.heap_mut().alloc_object(Token::new(0x0100_0010)) {
        Ok(assembly_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(assembly_ref))),
        Err(_) => PreHookResult::Bypass(Some(EmValue::Null)),
    }
}

/// Hook for `System.Reflection.Assembly.GetManifestResourceStream` method.
///
/// # Handled Overloads
///
/// - `Assembly.GetManifestResourceStream(String) -> Stream`
/// - `Assembly.GetManifestResourceStream(Type, String) -> Stream`
///
/// # Parameters
///
/// - `name`: The case-sensitive name of the manifest resource.
/// - `type`: The type whose namespace is used to scope the resource name.
///
/// # Returns
///
/// A `Stream` object containing the resource data, or `null` if not found.
fn assembly_get_manifest_resource_stream_pre(
    ctx: &HookContext<'_>,
    thread: &mut EmulationThread,
) -> PreHookResult {
    // Get the resource name from the first argument
    let resource_name = match ctx.args.first() {
        Some(EmValue::ObjectRef(href)) => thread
            .heap()
            .get_string(*href)
            .ok()
            .map(|arc| arc.to_string()),
        _ => None,
    };

    let Some(resource_name) = resource_name else {
        return PreHookResult::Bypass(Some(EmValue::Null));
    };

    // Get the assembly from the thread
    let Some(assembly) = thread.assembly() else {
        return PreHookResult::Bypass(Some(EmValue::Null));
    };

    // Look up the resource
    let resources = assembly.resources();
    let resource = resources.get(&resource_name);

    let Some(resource) = resource else {
        return PreHookResult::Bypass(Some(EmValue::Null));
    };

    // Get the resource data
    let Some(data) = resources.get_data(&resource) else {
        return PreHookResult::Bypass(Some(EmValue::Null));
    };

    // Allocate a stream with the resource data
    match thread.heap_mut().alloc_stream(data.to_vec()) {
        Ok(stream_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(stream_ref))),
        Err(_) => PreHookResult::Bypass(Some(EmValue::Null)),
    }
}

/// Hook for `System.Reflection.Assembly.GetManifestResourceNames` method.
///
/// # Handled Overloads
///
/// - `Assembly.GetManifestResourceNames() -> String[]`
///
/// # Parameters
///
/// None (instance method, `this` is the Assembly).
///
/// # Returns
///
/// An empty `String[]` array (resource enumeration not implemented).
fn assembly_get_manifest_resource_names_pre(
    _ctx: &HookContext<'_>,
    thread: &mut EmulationThread,
) -> PreHookResult {
    match thread.heap_mut().alloc_array(CilFlavor::String, 0) {
        Ok(array_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(array_ref))),
        Err(_) => PreHookResult::Bypass(Some(EmValue::Null)),
    }
}

/// Hook for delegate constructor methods.
///
/// # Handled Overloads
///
/// - `Delegate..ctor(Object, IntPtr) -> void`
/// - `MulticastDelegate..ctor(Object, IntPtr) -> void`
/// - `ResolveEventHandler..ctor(Object, IntPtr) -> void`
///
/// # Parameters
///
/// - `target`: The object on which the delegate invokes the instance method.
/// - `method`: A pointer to the method to be invoked.
///
/// # Returns
///
/// A symbolic delegate object reference.
fn delegate_ctor_pre(_ctx: &HookContext<'_>, thread: &mut EmulationThread) -> PreHookResult {
    match thread.heap_mut().alloc_object(Token::new(0x0100_0012)) {
        Ok(delegate_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(delegate_ref))),
        Err(_) => PreHookResult::Bypass(Some(EmValue::Null)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        emulation::runtime::hook::HookManager, metadata::typesystem::PointerSize,
        test::emulation::create_test_thread,
    };

    #[test]
    fn test_register_hooks() {
        let mut manager = HookManager::new();
        register(&mut manager);
        assert_eq!(manager.len(), 14);
    }

    #[test]
    fn test_get_current_domain() {
        let ctx = HookContext::new(
            Token::new(0x0A000001),
            "System",
            "AppDomain",
            "get_CurrentDomain",
            PointerSize::Bit64,
        );

        let mut thread = create_test_thread();
        let result = appdomain_get_current_domain_pre(&ctx, &mut thread);

        match result {
            PreHookResult::Bypass(Some(EmValue::ObjectRef(_))) => {}
            _ => panic!("Expected Bypass with ObjectRef"),
        }
    }

    #[test]
    fn test_assembly_load_captures_bytes() {
        let ctx = HookContext::new(
            Token::new(0x0A000001),
            "System.Reflection",
            "Assembly",
            "Load",
            PointerSize::Bit64,
        );

        let mut thread = create_test_thread();

        // Allocate a byte array on the heap
        let test_data = vec![0x4D, 0x5A, 0x90, 0x00]; // MZ header start
        let array_ref = thread.heap_mut().alloc_byte_array(&test_data).unwrap();

        let args = [EmValue::ObjectRef(array_ref)];
        let ctx = ctx.with_args(&args);

        let result = assembly_load_pre(&ctx, &mut thread);

        // Should return an Assembly object
        match result {
            PreHookResult::Bypass(Some(EmValue::ObjectRef(_))) => {}
            _ => panic!("Expected Bypass with ObjectRef"),
        }

        // Check that assembly was captured
        let captured = thread.capture().assemblies();
        assert_eq!(captured.len(), 1);
        assert_eq!(captured[0].data, test_data);
    }
}