udbg 0.3.1

cross-platform library for binary debugging and memory hacking
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
use std::{
    mem::{size_of, size_of_val},
    ptr::null_mut,
};

use anyhow::Context;
use ntapi::ntpsapi::PROCESS_BASIC_INFORMATION;
use windows::Win32::{
    Foundation::{
        DuplicateHandle, DUPLICATE_SAME_ACCESS, HANDLE, HMODULE, MAX_PATH, UNICODE_STRING,
    },
    System::{
        Diagnostics::{
            Debug::*,
            ToolHelp::{MODULEENTRY32W, THREADENTRY32},
        },
        Memory::*,
        ProcessStatus::*,
        Threading::*,
    },
};

use super::{
    ntdll::{ProcessInfoClass, SystemHandleInformation},
    toolhelp::*,
    util, Handle,
};
use crate::{
    error::*,
    memory::*,
    shell::{HandleInfo, ProcessInfo},
    string::ToMbstr,
};

#[derive(Debug, Clone)]
pub struct Process {
    pub handle: Handle,
}

impl ReadMemory for Process {
    fn read_memory<'a>(&self, addr: usize, data: &'a mut [u8]) -> Option<&'a mut [u8]> {
        let r = read_process_memory(*self.handle, addr, data).ok()?;
        if r > 0 {
            Some(&mut data[..r])
        } else {
            None
        }
    }
}

impl WriteMemory for Process {
    fn write_memory(&self, address: usize, data: &[u8]) -> Option<usize> {
        let r = self.write_memory(address, data);
        if r > 0 {
            Some(r)
        } else {
            None
        }
    }

    fn flush_cache(&self, address: usize, len: usize) -> std::io::Result<()> {
        if unsafe { FlushInstructionCache(*self.handle, Some(address as _), len).is_ok() } {
            Ok(())
        } else {
            Err(std::io::Error::last_os_error())
        }
    }
}

impl Process {
    pub fn open(pid: u32, access: Option<PROCESS_ACCESS_RIGHTS>) -> anyhow::Result<Process> {
        unsafe {
            let handle = OpenProcess(access.unwrap_or(PROCESS_ALL_ACCESS), false, pid)?;
            Ok(Self {
                handle: Handle::from_raw_handle(handle),
            })
        }
    }

    #[inline]
    pub unsafe fn borrow_raw(handle: &HANDLE) -> &Self {
        Self::borrow_handle(Handle::borrow(handle))
    }

    #[inline]
    pub unsafe fn borrow_handle(handle: &Handle) -> &Self {
        &*(handle as *const _ as *const Self)
    }

    pub fn duplicate_from_other_process(pid: u32, access: u32) -> anyhow::Result<Process> {
        let handle = super::duplicate_process(pid, access)
            .next()
            .context("dup not found")?;
        Self::from_handle(handle).context("from handle")
    }

    pub fn from_name(name: &str, access: Option<PROCESS_ACCESS_RIGHTS>) -> anyhow::Result<Process> {
        let pid = enum_process()?
            .filter(move |p| p.name().eq_ignore_ascii_case(name))
            .next()
            .context("not found")?
            .pid();
        Self::open(pid, access).context("open")
    }

    pub fn from_handle(handle: Handle) -> Option<Process> {
        unsafe {
            let pid = GetProcessId(*handle);
            if pid == 0 {
                return None;
            }

            return Some(Process { handle });
        }
    }

    pub fn current() -> Process {
        unsafe { Self::from_handle(Handle::from_raw_handle(GetCurrentProcess())).unwrap() }
    }

    pub fn basic_information(&self) -> Option<PROCESS_BASIC_INFORMATION> {
        super::ntdll::query_process(
            self.handle.as_winapi(),
            ProcessInfoClass::BasicInformation,
            None,
        )
    }

    pub fn pid(&self) -> u32 {
        unsafe { GetProcessId(*self.handle) }
    }

    pub fn peb(&self) -> Option<usize> {
        self.basic_information().map(|i| i.PebBaseAddress as usize)
    }

    // https://docs.microsoft.com/en-us/windows/win32/api/wow64apiset/nf-wow64apiset-iswow64process
    pub fn is_wow64(&self) -> bool {
        let mut result = Default::default();
        unsafe {
            IsWow64Process(*self.handle, &mut result).ok();
        }
        result.as_bool()
    }

    pub fn get_module_name(&self, module: u64) -> UDbgResult<String> {
        unsafe {
            let mut name = [0 as u16; MAX_PATH as usize];
            if GetModuleBaseNameW(*self.handle, HMODULE(module as _), &mut name) > 0 {
                Ok(name.as_ref().to_utf8())
            } else {
                Err(UDbgError::system())
            }
        }
    }

    // TODO: [bug] wow64进程下32位dll取到的是64位的路径
    #[deprecated]
    pub fn get_module_path(&self, module: usize) -> Option<String> {
        unsafe {
            let mut path = [0 as u16; MAX_PATH as usize];
            if GetModuleFileNameExW(*self.handle, HMODULE(module as _), &mut path) > 0 {
                Some(path.as_ref().to_utf8())
            } else {
                None
            }
        }
    }

    /// use EnumProcessModulesEx
    pub fn get_module_list(&self, flag: ENUM_PROCESS_MODULES_EX_FLAGS) -> Option<Vec<usize>> {
        unsafe {
            let mut len = 0u32;
            EnumProcessModulesEx(*self.handle, null_mut(), 0, &mut len, flag);
            let mut result = vec![0usize; len as usize];
            if len > 0 {
                if EnumProcessModulesEx(
                    *self.handle,
                    result.as_mut_ptr() as _,
                    result.len() as u32,
                    &mut len,
                    flag,
                )
                .is_ok()
                {
                    return Some(result.into_iter().filter(|&m| m > 0).collect());
                }
            }
            None
        }
    }

    /// use GetModuleInformation
    pub fn get_module_info(&self, base: usize) -> Option<MODULEINFO> {
        unsafe {
            let mut result: MODULEINFO = core::mem::zeroed();
            if GetModuleInformation(
                *self.handle,
                HMODULE(base as _),
                &mut result,
                size_of::<MODULEINFO>() as u32,
            )
            .is_ok()
            {
                return Some(result);
            }
            None
        }
    }

    pub fn duplicate_handle_to_current(&self, src: HANDLE) -> windows::core::Result<Handle> {
        self.duplicate_handle(src, unsafe { GetCurrentProcess() })
            .map(|x| unsafe { Handle::from_raw_handle(x) })
    }

    pub fn duplicate_handle(&self, src: HANDLE, dst_ps: HANDLE) -> windows::core::Result<HANDLE> {
        let mut handle = HANDLE::default();
        unsafe {
            DuplicateHandle(
                *self.handle,
                src,
                dst_ps,
                &mut handle,
                0,
                false,
                DUPLICATE_SAME_ACCESS,
            )?;
            Ok(handle)
        }
    }

    #[inline]
    pub fn enum_thread<'a>(
        &'a self,
    ) -> windows::core::Result<impl Iterator<Item = THREADENTRY32> + 'a> {
        let pid = self.pid();
        Ok(enum_thread()?.filter(move |x| x.pid() == pid))
    }

    #[inline]
    pub fn enum_module(&self) -> windows::core::Result<ToolHelperIter<MODULEENTRY32W>> {
        enum_module(self.pid())
    }

    /// Wrapper of QueryFullProcessImageNameW
    pub fn image_path(&self) -> windows::core::Result<String> {
        unsafe {
            let mut path = [0 as u16; MAX_PATH as usize];
            let mut size = path.len() as u32;
            QueryFullProcessImageNameW(
                *self.handle,
                PROCESS_NAME_WIN32,
                windows::core::PWSTR(path.as_mut_ptr()),
                &mut size,
            )?;
            Ok(path.as_ref().to_utf8())
        }
    }

    pub fn cmdline(&self) -> Option<String> {
        use ntapi::ntrtl::RTL_USER_PROCESS_PARAMETERS;
        use ntapi::FIELD_OFFSET;

        self.peb()
            .and_then(|peb| {
                self.read_value::<usize>(peb as usize + FIELD_OFFSET!(PEB, ProcessParameters))
            })
            .and_then(|p| {
                self.read_value::<UNICODE_STRING>(
                    p + FIELD_OFFSET!(RTL_USER_PROCESS_PARAMETERS, CommandLine),
                )
            })
    }

    pub fn protect_memory(
        &self,
        address: usize,
        size: usize,
        attr: PAGE_PROTECTION_FLAGS,
    ) -> windows::core::Result<PAGE_PROTECTION_FLAGS> {
        unsafe {
            let mut oldattr = Default::default();
            VirtualProtectEx(*self.handle, address as _, size, attr, &mut oldattr)?;
            Ok(oldattr)
        }
    }

    #[inline]
    pub fn write_memory(&self, address: usize, data: &[u8]) -> usize {
        write_process_memory(*self.handle, address, data).unwrap_or_default()
    }

    pub fn enum_memory(&self, address: usize) -> impl Iterator<Item = MemoryPage> + '_ {
        pub struct MemoryIter<'p> {
            pub process: &'p Process,
            pub address: usize,
        }

        impl MemoryIter<'_> {
            pub fn next_commit(&mut self) -> Option<MemoryPage> {
                while let Some(m) = self.next() {
                    if m.is_commit() {
                        return Some(m);
                    }
                }
                return None;
            }
        }

        impl Iterator for MemoryIter<'_> {
            type Item = MemoryPage;

            fn next(&mut self) -> Option<Self::Item> {
                let result = self.process.virtual_query(self.address);
                if let Some(m) = result.as_ref() {
                    self.address += m.size;
                }
                return result;
            }
        }

        MemoryIter {
            process: self,
            address,
        }
    }

    pub fn virtual_alloc(
        &self,
        address: usize,
        size: usize,
        mem_type: VIRTUAL_ALLOCATION_TYPE,
        protect: PAGE_PROTECTION_FLAGS,
    ) -> usize {
        unsafe {
            VirtualAllocEx(*self.handle, Some(address as _), size, mem_type, protect) as usize
        }
    }

    pub fn virtual_free(&self, address: usize) -> bool {
        unsafe { VirtualFreeEx(*self.handle, address as _, 0, MEM_RELEASE).is_ok() }
    }

    pub fn virtual_query(&self, address: usize) -> Option<MemoryPage> {
        unsafe {
            let mut mbi: MEMORY_BASIC_INFORMATION = core::mem::zeroed();
            match VirtualQueryEx(
                *self.handle,
                Some(address as _),
                &mut mbi,
                size_of_val(&mbi),
            ) {
                0 => None,
                _ => Some(MemoryPage::from(&mbi)),
            }
        }
    }

    #[inline]
    pub fn terminate(&self) -> windows::core::Result<()> {
        unsafe { TerminateProcess(*self.handle, 0) }
    }

    pub fn get_exit_code(&self) -> Option<u32> {
        let mut code = 0u32;
        unsafe {
            GetExitCodeProcess(*self.handle, &mut code)
                .is_ok()
                .then_some(code)
        }
    }

    // https://docs.microsoft.com/zh-cn/windows/win32/memory/obtaining-a-file-name-from-a-file-handle
    pub fn get_mapped_file_name(&self, address: usize) -> Option<String> {
        unsafe {
            let mut buf = [0u16; 300];
            let len = GetMappedFileNameW(*self.handle, address as _, &mut buf);
            if len > 0 {
                util::to_dos_path(&mut buf)
                    .map(|s| s.to_utf8())
                    .or_else(|| Some(buf.as_ref().to_utf8()))
            } else {
                None
            }
        }
    }

    pub fn enum_handle(&self, pid: Option<u32>) -> impl Iterator<Item = HandleInfo> + '_ {
        let pid = pid.unwrap_or_else(|| self.pid());
        let mut type_cache = util::HandleTypeCache::default();
        super::ntdll::system_handle_information().filter_map(move |h| {
            if h.pid() != pid {
                return None;
            }
            type_cache.cache_get(self, h)
        })
    }
}

pub fn read_process_memory(
    handle: HANDLE,
    address: usize,
    data: &mut [u8],
) -> anyhow::Result<usize> {
    let mut readed = 0usize;
    unsafe {
        ReadProcessMemory(
            handle,
            address as _,
            data.as_mut_ptr().cast(),
            data.len(),
            Some(&mut readed),
        )?;
    }
    Ok(readed)
}

pub fn write_process_memory(handle: HANDLE, address: usize, data: &[u8]) -> anyhow::Result<usize> {
    let mut written = 0usize;
    let mut old_protect = Default::default();
    let mut new_protect = Default::default();
    unsafe {
        VirtualProtectEx(
            handle,
            address as _,
            data.len(),
            PAGE_EXECUTE_READWRITE,
            &mut old_protect,
        )
        .context("protect")?;
        let result = WriteProcessMemory(
            handle,
            address as _,
            data.as_ptr() as _,
            data.len(),
            Some(&mut written),
        )
        .context("write")?;
        VirtualProtectEx(
            handle,
            address as _,
            data.len(),
            old_protect,
            &mut new_protect,
        )
        .context("protect2")?;
    }
    Ok(written)
}

impl ProcessInfo {
    pub fn enumerate() -> UDbgResult<impl Iterator<Item = ProcessInfo>> {
        Ok(enum_process()?.map(|p| {
            let pid = p.pid();
            let mut result = ProcessInfo {
                pid,
                name: p.name(),
                wow64: false,
                path: String::new(),
                cmdline: String::new(),
            };
            Process::open(pid, Some(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ))
                .map(|p| {
                    result.wow64 = p.is_wow64();
                    p.image_path().map(|path| result.path = path);
                    p.cmdline().map(|cmd| result.cmdline = cmd);
                })
                .ok();
            result
        }))
    }
}

impl From<&MEMORY_BASIC_INFORMATION> for MemoryPage {
    fn from(mbi: &MEMORY_BASIC_INFORMATION) -> Self {
        MemoryPage {
            base: mbi.BaseAddress as usize,
            alloc_base: mbi.AllocationBase as usize,
            size: mbi.RegionSize,
            type_: mbi.Type.0,
            state: mbi.State.0,
            protect: mbi.Protect.0,
            alloc_protect: mbi.AllocationProtect.0,
            ..Default::default()
        }
    }
}