libmwemu 0.24.4

x86 32/64bits and system internals emulator, for securely emulating malware and other stuff.
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
use crate::emu;
use crate::maps::mem64::Permission;
use crate::windows::peb::peb64::ldr::create_ldr_entry;
use crate::windows::structures::PebLdrData64;
use crate::windows::structures::PEB64;
use crate::windows::structures::RtlUserProcessParameters64;
use crate::windows::structures::TEB64;

/// `PEB+0x90` (`system_dependent_07`): ntdll reads this as a pointer in loader / API-set helpers.
/// Real CSRSS/kernel fills it; under emulation `LdrInitializeThunk` (and related code) often leaves
/// this NULL, which makes loader code fault. If the slot is NULL or points at unmapped memory,
/// install or reuse a dedicated zero-filled page and write its base to `PEB+0x90`.
pub fn ensure_peb_system_dependent_07(emu: &mut emu::Emu) {
    if !emu.cfg.arch.is_64bits() {
        return;
    }
    let peb_base = match emu.maps.get_map_by_name("peb") {
        Some(m) => m.get_base(),
        None => return,
    };
    let cur = emu.maps.read_qword(peb_base + 0x90).unwrap_or(0);
    if cur != 0 && emu.maps.is_mapped(cur) {
        return;
    }

    const SD07_NAME: &str = "peb_system_dependent_07";
    const SD07_SZ: u64 = 0x1000;
    let sd07 = if let Some(m) = emu.maps.get_map_by_name(SD07_NAME) {
        m.get_base()
    } else {
        let base = emu.maps.map(SD07_NAME, SD07_SZ, Permission::READ_WRITE);
        emu.maps.memset(base, 0, SD07_SZ as usize);
        base
    };
    emu.maps.write_qword(peb_base + 0x90, sd07);
}

/// `TEB+0x2C8` points at the thread activation-context stack.
fn ensure_teb_activation_context_stack(emu: &mut emu::Emu) {
    if !emu.cfg.arch.is_64bits() {
        return;
    }
    let teb_base = match emu.maps.get_map_by_name("teb") {
        Some(m) => m.get_base(),
        None => return,
    };
    let cur = emu.maps.read_qword(teb_base + 0x2c8).unwrap_or(0);
    if cur != 0 && emu.maps.is_mapped(cur) {
        return;
    }

    const ACTCTX_NAME: &str = "teb_activation_context_stack";
    const ACTCTX_SZ: u64 = 0x1000;
    let actctx = if let Some(m) = emu.maps.get_map_by_name(ACTCTX_NAME) {
        m.get_base()
    } else {
        let base = emu.maps.map(ACTCTX_NAME, ACTCTX_SZ, Permission::READ_WRITE);
        emu.maps.memset(base, 0, ACTCTX_SZ as usize);
        emu.maps.write_qword(base + 0x08, base + 0x08);
        emu.maps.write_qword(base + 0x10, base + 0x08);
        base
    };
    emu.maps.write_qword(teb_base + 0x2c8, actctx);
}

/// Compressed Win11 (24H2) `API_SET_NAMESPACE` blob, sourced from sogen's
/// `default_apiset.hpp`. Decompresses to ~128 KB containing the full
/// virtual-DLL → host-DLL redirect schema for hundreds of `api-ms-*` and
/// `ext-ms-*` names. Embedding this avoids shipping the ~115 individual
/// stub DLLs that would otherwise be needed to satisfy ntdll's loader.
const APISET_W11_ZLIB: &[u8] = include_bytes!("../../../../data/apiset_w11.zlib");

/// `PEB+0x68` = `ApiSetMap` on x64 Windows 10+ ntdll. The loader reads it
/// during `LdrpInitializeProcess` to redirect virtual API-set names
/// (`api-ms-*`, `ext-ms-*`) to real DLLs. Without a real schema every
/// dependency lookup fails: KnownDll has no entry, disk lookup fails, and
/// the loader terminates with `STATUS_DLL_NOT_FOUND`.
///
/// Decompress the embedded Win11 schema and map it at a stable virtual
/// address; the schema's `EntryOffset`, `HashOffset`, and per-entry name
/// offsets are all *relative* to the namespace base, so we can map the raw
/// blob unchanged and ntdll's resolver walks it correctly.
fn ensure_peb_system_dependent_06(emu: &mut emu::Emu) {
    if !emu.cfg.arch.is_64bits() {
        return;
    }
    let peb_base = match emu.maps.get_map_by_name("peb") {
        Some(m) => m.get_base(),
        None => return,
    };
    let cur = emu.maps.read_qword(peb_base + 0x68).unwrap_or(0);
    if cur != 0 && emu.maps.is_mapped(cur) {
        return;
    }

    const SD06_NAME: &str = "peb_apiset_map";
    let sd06 = if let Some(m) = emu.maps.get_map_by_name(SD06_NAME) {
        m.get_base()
    } else {
        // Decompress the zlib-compressed schema from the embedded blob.
        use flate2::read::ZlibDecoder;
        use std::io::Read;
        let mut decoder = ZlibDecoder::new(APISET_W11_ZLIB);
        let mut decompressed = Vec::with_capacity(0x20000);
        if decoder.read_to_end(&mut decompressed).is_err() {
            log::warn!(
                "ensure_peb_system_dependent_06: failed to decompress embedded apiset, falling back to empty schema"
            );
            return;
        }

        let sz = ((decompressed.len() + 0xFFF) & !0xFFF) as u64;
        let base = emu.maps.map(SD06_NAME, sz, Permission::READ_WRITE);
        emu.maps.memset(base, 0, sz as usize);
        if let Some(mem) = emu.maps.get_mem_by_addr_mut(base) {
            mem.memcpy(&decompressed, decompressed.len());
            mem.set_permission(Permission::READ);
        }
        log::trace!(
            "ensure_peb_system_dependent_06: ApiSetMap (Win11 schema, {} bytes) mapped at 0x{:x}",
            decompressed.len(),
            base
        );
        base
    };
    emu.maps.write_qword(peb_base + 0x68, sd06);
}

/// Map the three NLS code-page sections that `RtlInitNlsTables` reads via
/// `PEB.AnsiCodePageData` / `OemCodePageData` / `UnicodeCaseTableData`. In
/// real Windows the kernel hands ntdll already-populated section views from
/// `C:\Windows\System32\C_1252.NLS`, `C_437.NLS` and `locale.nls`; we mirror
/// that by reading those files from `cfg.maps_folder`. Without real NLS
/// content every byte→wide conversion in the loader produces zeros and DLL
/// dependency lookups fail with `STATUS_DLL_NOT_FOUND`.
pub fn ensure_peb_nls_tables(emu: &mut emu::Emu) {
    if !emu.cfg.arch.is_64bits() {
        return;
    }
    let peb_base = match emu.maps.get_map_by_name("peb") {
        Some(m) => m.get_base(),
        None => return,
    };

    // Fallback size if the file is missing — ntdll then sees a zero buffer
    // (same as before this fix), but at least it's mapped so reads don't crash.
    const FALLBACK_SZ: u64 = 0x1000;
    // (PEB slot, map name, NLS filename)
    let slots: &[(&str, u64, &str)] = &[
        ("peb_nls_ansi",    peb_base + 0xA0, "C_1252.NLS"),
        ("peb_nls_oem",     peb_base + 0xA8, "C_437.NLS"),
        ("peb_nls_unicode", peb_base + 0xB0, "locale.nls"),
    ];
    for (name, slot_addr, nls_filename) in slots {
        let cur = emu.maps.read_qword(*slot_addr).unwrap_or(0);
        let page = cur & !0xFFF;
        if cur != 0 && page != 0 && emu.maps.is_mapped(page) {
            continue;
        }
        let base = if let Some(m) = emu.maps.get_map_by_name(name) {
            m.get_base()
        } else {
            let path = emu.cfg.get_maps_folder(nls_filename);
            match std::fs::read(&path) {
                Ok(bytes) => {
                    let sz = ((bytes.len() + 0xFFF) & !0xFFF) as u64;
                    let b = emu.maps.map(name, sz, Permission::READ_WRITE);
                    emu.maps.memset(b, 0, sz as usize);
                    if let Some(mem) = emu.maps.get_mem_by_addr_mut(b) {
                        mem.memcpy(&bytes, bytes.len());
                    }
                    log::trace!(
                        "ensure_peb_nls_tables: loaded {} ({} bytes) at 0x{:x}",
                        nls_filename, bytes.len(), b
                    );
                    b
                }
                Err(_) => {
                    log::warn!(
                        "ensure_peb_nls_tables: missing {} — leaving {} zeroed",
                        path, name
                    );
                    let b = emu.maps.map(name, FALLBACK_SZ, Permission::READ_WRITE);
                    emu.maps.memset(b, 0, FALLBACK_SZ as usize);
                    b
                }
            }
        };
        emu.maps.write_qword(*slot_addr, base);
    }
}

pub fn init_ldr(emu: &mut emu::Emu) -> u64 {
    let ldr_sz = PebLdrData64::size() + 100;
    let ldr_addr = emu
        .maps
        .lib64_alloc(ldr_sz as u64)
        .expect("cannot alloc the LDR");
    emu.maps
        .create_map("ldr", ldr_addr, ldr_sz as u64, Permission::READ_WRITE)
        .expect("cannot create ldr map");
    let exe_name = emu.cfg.exe_name.clone();
    let module_entry = create_ldr_entry(emu, 0, 0, &exe_name, 0, 0);
    let mut ldr = PebLdrData64::new();
    ldr.initializated = 1;
    ldr.in_load_order_module_list.flink = module_entry;
    ldr.in_load_order_module_list.blink = module_entry;
    ldr.in_memory_order_module_list.flink = module_entry + 0x10;
    ldr.in_memory_order_module_list.blink = module_entry + 0x10;
    ldr.in_initialization_order_module_list.flink = module_entry + 0x20;
    ldr.in_initialization_order_module_list.blink = module_entry + 0x20;
    ldr.entry_in_progress.flink = module_entry;
    ldr.entry_in_progress.blink = module_entry;
    ldr.save(ldr_addr, &mut emu.maps);

    ldr_addr
}

pub fn init_arguments(emu: &mut emu::Emu) -> u64 {
    let addr = emu.maps.map(
        "RtlUserProcessParameters64",
        RtlUserProcessParameters64::size() as u64,
        Permission::READ_WRITE_EXECUTE,
    );
    let mut params_struct = RtlUserProcessParameters64::new();

    let filename_len = emu.cfg.filename.len() as u64 * 2 + 2;
    let cmdline_len = filename_len + emu.cfg.arguments.len() as u64 * 2 + 2;

    let filename = emu
        .maps
        .map("file_name", filename_len, Permission::READ_WRITE);
    let cmdline = emu
        .maps
        .map("command_line", cmdline_len, Permission::READ_WRITE);

    let dll_path_buf = emu.maps.map("dll_path", 4, Permission::READ_WRITE);
    emu.maps.write_wide_string(dll_path_buf, "");

    params_struct.image_path_name.length = filename_len as u16;
    params_struct.image_path_name.maximum_length = filename_len as u16;
    params_struct.image_path_name.buffer = filename;

    params_struct.command_line.length = cmdline_len as u16;
    params_struct.command_line.maximum_length = cmdline_len as u16;
    params_struct.command_line.buffer = cmdline;

    params_struct.dll_path.length = 0;
    params_struct.dll_path.maximum_length = 4;
    params_struct.dll_path.buffer = dll_path_buf;

    let mut params = emu.cfg.filename.clone();
    params.push_str(&emu.cfg.arguments);

    emu.maps.write_wide_string(filename, &emu.cfg.filename);
    emu.maps.write_wide_string(cmdline, &params);

    params_struct.save(addr, &mut emu.maps);

    addr
}

pub fn init_peb(emu: &mut emu::Emu) {
    let ldr = init_ldr(emu);
    let params_addr = init_arguments(emu);

    let peb_addr = emu
        .maps
        .lib64_alloc(PEB64::size() as u64)
        .expect("cannot alloc the PEB64");
    let peb_map = emu
        .maps
        .create_map(
            "peb",
            peb_addr,
            PEB64::size() as u64,
            Permission::READ_WRITE,
        )
        .expect("cannot create peb map");
    let peb = PEB64::new(0, ldr, params_addr);
    peb.save(peb_map);
    emu.maps.write_byte(peb_addr + 2, 0);

    let teb_addr = emu
        .maps
        .lib64_alloc(TEB64::size() as u64)
        .expect("cannot alloc the TEB64");
    let teb_map = emu
        .maps
        .create_map(
            "teb",
            teb_addr,
            TEB64::size() as u64,
            Permission::READ_WRITE,
        )
        .expect("cannot create teb map");
    let teb = TEB64::new(peb_addr);
    teb.save(teb_map);

    ensure_teb_activation_context_stack(emu);
    ensure_peb_system_dependent_07(emu);
}

/// Allocate and initialize a minimal Windows `_HEAP` (x64) structure.
fn init_process_heap(emu: &mut emu::Emu) -> u64 {
    let heap_sz: u64 = 0x1000;
    let h = emu
        .maps
        .lib64_alloc(heap_sz)
        .expect("cannot alloc fake ProcessHeap");
    let _heap_map = emu
        .maps
        .create_map("process_heap", h, heap_sz, Permission::READ_WRITE)
        .expect("cannot create process_heap map");
    emu.maps.memset(h, 0, heap_sz as usize);

    let self_list = |maps: &mut crate::maps::Maps, addr: u64| {
        maps.write_qword(addr, addr);
        maps.write_qword(addr + 8, addr);
    };

    emu.maps.write_dword(h + 0x010, 0xFEED_FEED);
    self_list(&mut emu.maps, h + 0x018);
    emu.maps.write_qword(h + 0x028, h);
    emu.maps.write_qword(h + 0x030, h);
    emu.maps.write_dword(h + 0x038, (heap_sz >> 12) as u32);
    emu.maps.write_qword(h + 0x040, h + 0x200);
    emu.maps.write_qword(h + 0x048, h + heap_sz);
    self_list(&mut emu.maps, h + 0x060);

    emu.maps.write_dword(h + 0x070, 0x0000_0002);
    emu.maps.write_dword(h + 0x098, 0xEEFF_EEFF);
    emu.maps.write_qword(h + 0x0C8, 0x7FFF_EFFF_FFFF);

    self_list(&mut emu.maps, h + 0x0E8);
    self_list(&mut emu.maps, h + 0x108);
    self_list(&mut emu.maps, h + 0x118);
    self_list(&mut emu.maps, h + 0x148);

    let ucr_stub = h + 0x300;
    self_list(&mut emu.maps, ucr_stub);
    emu.maps.write_qword(h + 0x138, ucr_stub);

    emu.maps.write_word(h + 0x1B0, 0);

    let lock_addr = h + 0x400;
    emu.maps.write_dword(lock_addr + 0x08, 0xFFFF_FFFF);
    emu.maps.write_qword(h + 0x158, lock_addr);
    emu.maps.write_qword(h + 0x160, lock_addr);

    h
}

/// Minimal PEB/TEB for `--init` / `ssdt_use_ldr_initialize_thunk`.
pub fn init_peb_teb_empty(emu: &mut emu::Emu) {
    let ldr_addr = init_ldr(emu);
    let params_addr = init_arguments(emu);

    let peb_addr = emu
        .maps
        .lib64_alloc(PEB64::size() as u64)
        .expect("cannot alloc the PEB64");
    let _peb_map = emu
        .maps
        .create_map(
            "peb",
            peb_addr,
            PEB64::size() as u64,
            Permission::READ_WRITE,
        )
        .expect("cannot create peb map");
    let heap_addr = init_process_heap(emu);
    let peb = PEB64::new(0, ldr_addr, params_addr);
    peb.save(emu.maps.get_mem_mut("peb"));
    emu.maps.write_byte(peb_addr + 2, 0);
    emu.maps.write_qword(peb_addr + 0x30, heap_addr);

    let teb_addr = emu
        .maps
        .lib64_alloc(TEB64::size() as u64)
        .expect("cannot alloc the TEB64");
    let _teb_map = emu
        .maps
        .create_map(
            "teb",
            teb_addr,
            TEB64::size() as u64,
            Permission::READ_WRITE,
        )
        .expect("cannot create teb map");
    let teb = TEB64::new(peb_addr);
    teb.save(emu.maps.get_mem_mut("teb"));

    ensure_teb_activation_context_stack(emu);
    ensure_peb_system_dependent_06(emu);
    ensure_peb_system_dependent_07(emu);
    ensure_peb_nls_tables(emu);
}

pub fn update_peb_image_base(emu: &mut emu::Emu, base: u64) {
    let peb = emu.maps.get_mem("peb");
    let peb_base = peb.get_base();
    emu.maps.write_qword(peb_base + 0x10, base);
}