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
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
//! Practical wrappers for functions/structs in ntdll

use super::util::BufferType;
pub use ntapi::ntexapi::*;
pub use ntapi::ntldr::*;
pub use ntapi::ntmmapi::*;
use ntapi::ntobapi::*;
pub use ntapi::ntpebteb::*;
pub use ntapi::ntpsapi::*;
pub use ntapi::ntzwapi::*;

// use ::windows::Win32::Foundation::UNICODE_STRING;
use alloc::string::*;
use alloc::vec::Vec;
use core::marker::PhantomData;
use core::mem::{size_of, size_of_val, transmute, zeroed};
use core::ptr::{null, null_mut};
use core::slice::from_raw_parts;
use failed_result::*;
use winapi::shared::ntdef::*;
use winapi::shared::{minwindef::LPVOID, ntstatus::STATUS_INFO_LENGTH_MISMATCH};

pub enum ProcessInfoClass {
    BasicInformation = 0,
    DebugPort = 7,
    Wow64Information = 26,
    ImageFileName = 27,
    BreakOnTermination = 29,
    SubsystemInformation = 75,
}

pub enum ThreadInfoClass {
    BasicInformation = 0,
    Times = 1,
    Priority = 2,
    BasePriority = 3,
    AffinityMask = 4,
    ImpersonationToken = 5,
    DescriptorTableEntry = 6,
    EnableAlignmentFaultFixup = 7,
    EventPair = 8,
    QuerySetWin32StartAddress = 9,
    ZeroTlsCell = 10,
    PerformanceCount = 11,
    AmILastThread = 12,
    IdealProcessor = 13,
    PriorityBoost = 14,
    SetTlsArrayAddress = 15,
    IsIoPending = 16,
    HideFromDebugger = 17,
}

pub fn query_thread<T>(
    handle: HANDLE,
    info: ThreadInfoClass,
    out_len: Option<&mut usize>,
) -> Option<T> {
    let mut len: ULONG = 0;
    unsafe {
        // https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntqueryinformationthread
        let mut result: T = zeroed();
        let r = NtQueryInformationThread(
            handle,
            info as u32,
            transmute(&mut result),
            size_of::<T>() as u32,
            &mut len,
        );
        if let Some(out_len) = out_len {
            *out_len = len as usize;
        }
        if NT_SUCCESS(r) {
            Some(result)
        } else {
            None
        }
    }
}

pub fn query_process<T>(
    handle: HANDLE,
    info: ProcessInfoClass,
    out_len: Option<&mut usize>,
) -> Option<T> {
    let mut len: ULONG = 0;
    unsafe {
        // https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntqueryinformationprocess
        let mut result: T = zeroed();
        let r = ZwQueryInformationProcess(
            handle,
            info as u32,
            transmute(&mut result),
            size_of::<T>() as u32,
            &mut len,
        );
        if let Some(out_len) = out_len {
            *out_len = len as usize;
        }
        if NT_SUCCESS(r) {
            Some(result)
        } else {
            None
        }
    }
}

pub fn read_object_info(handle: HANDLE, info: u32, extra_size: usize) -> WindowsResult<Vec<u8>> {
    let mut size = 0u32;
    unsafe {
        let mut result = vec![0u8; 1024];
        let err = loop {
            let err = NtQueryObject(
                handle,
                info,
                result.as_mut_ptr().cast(),
                result.len() as u32,
                &mut size,
            );
            if err != STATUS_INFO_LENGTH_MISMATCH {
                break err;
            }
            result.resize(size as usize + extra_size, 0u8);
        };
        err.ntstatus_result()?;
        Ok(result)
    }
}

// maybe block, you can call this function within `call_with_timeout`
pub fn query_object_name(handle: HANDLE) -> WindowsResult<BufferType<UNICODE_STRING>> {
    read_object_info(handle, ObjectNameInformation, size_of::<u16>() * 16)
        .map(|r| BufferType::from_vec(r))
}

pub fn query_object_type(handle: HANDLE) -> WindowsResult<BufferType<OBJECT_TYPE_INFORMATION>> {
    read_object_info(handle, ObjectTypeInformation, 0).map(|r| BufferType::from_vec(r))
}

// https://docs.rs/ntapi/0.3.3/ntapi/ntexapi/struct.SYSTEM_PROCESS_INFORMATION.html
pub struct SystemProcessInfoIter<'a> {
    data: Vec<u8>,
    ptr: PSYSTEM_PROCESS_INFORMATION,
    _phan: core::marker::PhantomData<&'a SYSTEM_PROCESS_INFORMATION>,
}

impl<'a> SystemProcessInfoIter<'a> {
    pub fn new() -> SystemProcessInfoIter<'a> {
        SystemProcessInfoIter {
            data: vec![],
            ptr: core::ptr::null_mut(),
            _phan: PhantomData,
        }
    }

    pub fn from(mut data: Vec<u8>) -> SystemProcessInfoIter<'a> {
        let ptr = data.as_mut_ptr() as PSYSTEM_PROCESS_INFORMATION;
        SystemProcessInfoIter {
            data,
            ptr,
            _phan: PhantomData,
        }
    }
}

impl<'a> Iterator for SystemProcessInfoIter<'a> {
    type Item = &'a SYSTEM_PROCESS_INFORMATION;
    fn next(&mut self) -> Option<Self::Item> {
        if self.ptr.is_null() {
            return None;
        }
        unsafe {
            let next = (*self.ptr).NextEntryOffset as usize;
            if next == 0 {
                return None;
            }
            let result: &'static SYSTEM_PROCESS_INFORMATION = transmute(self.ptr);
            self.ptr = transmute(self.ptr as usize + next);
            Some(result)
        }
    }
}

pub trait SystemProcessInfo {
    fn threads(&self) -> &[SYSTEM_THREAD_INFORMATION];
}

impl SystemProcessInfo for SYSTEM_PROCESS_INFORMATION {
    fn threads(&self) -> &[SYSTEM_THREAD_INFORMATION] {
        unsafe { core::slice::from_raw_parts(self.Threads.as_ptr(), self.NumberOfThreads as usize) }
    }
}

pub fn system_process_information<'a>() -> WindowsResult<SystemProcessInfoIter<'a>> {
    let v = read_system_information(SystemProcessInformation, 0)?;
    Ok(SystemProcessInfoIter::from(v))
}

pub struct SystemHandleInfoIter<'a> {
    data: Vec<u8>,
    ptr: PSYSTEM_HANDLE_INFORMATION,
    i: usize,
    _phan: core::marker::PhantomData<&'a SYSTEM_HANDLE_INFORMATION>,
}

impl<'a> SystemHandleInfoIter<'a> {
    pub fn new() -> SystemHandleInfoIter<'a> {
        SystemHandleInfoIter {
            data: vec![],
            ptr: core::ptr::null_mut(),
            _phan: PhantomData,
            i: 0,
        }
    }

    pub fn from(mut data: Vec<u8>) -> SystemHandleInfoIter<'a> {
        let ptr = data.as_mut_ptr() as PSYSTEM_HANDLE_INFORMATION;
        SystemHandleInfoIter {
            data,
            ptr,
            i: 0,
            _phan: PhantomData,
        }
    }
}

impl<'a> Iterator for SystemHandleInfoIter<'a> {
    type Item = &'a SYSTEM_HANDLE_TABLE_ENTRY_INFO;
    fn next(&mut self) -> Option<Self::Item> {
        if self.ptr.is_null() {
            return None;
        }
        unsafe {
            let this = &*self.ptr;
            let r = from_raw_parts(this.Handles.as_ptr(), this.NumberOfHandles as usize);
            let result = r.get(self.i)?;
            self.i += 1;
            Some(result)
        }
    }
}

pub fn read_system_information(
    si: SYSTEM_INFORMATION_CLASS,
    extra_size: usize,
) -> WindowsResult<Vec<u8>> {
    let mut size = 0u32;
    unsafe {
        let mut result = vec![0u8; 1024];
        let err = loop {
            let err = ZwQuerySystemInformation(
                si,
                result.as_mut_ptr().cast(),
                result.len() as u32,
                &mut size,
            );
            if err != STATUS_INFO_LENGTH_MISMATCH {
                break err;
            }
            result.resize(size as usize + extra_size, 0u8);
        };
        err.ntstatus_result()?;
        Ok(result)
    }
}

pub fn system_handle_information<'a>() -> SystemHandleInfoIter<'a> {
    let mut size: ULONG = 0;
    unsafe {
        let mut result = vec![0u8; 4 * 1024];
        let err = loop {
            let err = ZwQuerySystemInformation(
                SystemHandleInformation,
                result.as_mut_ptr().cast(),
                result.len() as u32,
                &mut size,
            );
            if err != STATUS_INFO_LENGTH_MISMATCH {
                break err;
            }
            result.resize(result.len() * 2, 0u8);
        };
        if NT_SUCCESS(err) {
            SystemHandleInfoIter::from(result)
        } else {
            SystemHandleInfoIter::new()
        }
    }
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct SYSTEM_MODULE_INFORMATION_ENTRY {
    pub Section: usize,
    pub MappedBase: usize,
    pub ImageBase: PVOID,
    pub ImageSize: ULONG,
    pub Flags: ULONG,
    pub LoadOrderIndex: USHORT,
    pub InitOrderIndex: USHORT,
    pub LoadCount: USHORT,
    pub OffsetToFileName: USHORT,
    pub FullPathName: [u8; 256],
}

#[repr(C)]
pub struct SYSTEM_MODULE_INFORMATION {
    Count: u32,
    Module: [SYSTEM_MODULE_INFORMATION_ENTRY; 1],
}

impl SYSTEM_MODULE_INFORMATION_ENTRY {
    pub fn full_path(&self) -> &[u8] {
        let len = self.FullPathName.len();
        let len = self
            .FullPathName
            .iter()
            .position(|&x| x == 0)
            .unwrap_or(len);
        &self.FullPathName[..len]
    }

    #[inline(always)]
    pub fn full_path_str(&self) -> &str {
        unsafe { core::str::from_utf8_unchecked(self.full_path()) }
    }
}

pub fn system_module_list<'a>(
) -> WindowsResult<impl Iterator<Item = &'a SYSTEM_MODULE_INFORMATION_ENTRY>> {
    let v = read_system_information(SystemModuleInformation, 0)?;

    let smi = BufferType::<SYSTEM_MODULE_INFORMATION>::from_vec(v);
    let p = smi.Module.as_ptr();
    let mut i = 0;
    Ok(core::iter::from_fn(move || unsafe {
        if i >= smi.Count {
            return None;
        }
        let m = &*p.offset(i as isize);
        i += 1;
        Some(m)
    }))
}

pub trait SystemThreadInformation {
    fn state(&self) -> &'static str;
    fn wait_reason(&self) -> &'static str;
    fn priority(&self) -> &'static str;
    fn status(&self) -> String;
}

pub fn priority_str(p: u32) -> &'static str {
    use winapi::um::winbase::*;
    match p {
        THREAD_PRIORITY_LOWEST => "Lowest",
        THREAD_PRIORITY_BELOW_NORMAL => "BelowNormal",
        THREAD_PRIORITY_NORMAL => "Normal",
        THREAD_PRIORITY_HIGHEST => "Highest",
        THREAD_PRIORITY_ABOVE_NORMAL => "AboveNormal",
        THREAD_PRIORITY_ERROR_RETURN => "ErrorReturn",
        THREAD_PRIORITY_TIME_CRITICAL => "TimeCritical",
        THREAD_PRIORITY_IDLE => "Idle",
        _ => "-",
    }
}

impl SystemThreadInformation for SYSTEM_THREAD_INFORMATION {
    fn state(&self) -> &'static str {
        use ntapi::ntkeapi::*;

        match self.ThreadState {
            Initialized => "Initialized",
            Ready => "Ready",
            Running => "Running",
            Standby => "Standby",
            Terminated => "Terminated",
            Waiting => "Waiting",
            Transition => "Transition",
            DeferredReady => "DeferredReady",
            GateWaitObsolete => "GateWaitObsolete",
            WaitingForProcessInSwap => "WaitingForProcessInSwap",
            // MaximumThreadState => "MaximumThreadState",
            _ => "-",
        }
    }

    fn wait_reason(&self) -> &'static str {
        use ntapi::ntkeapi::*;

        match self.WaitReason {
            Executive => "Executive",
            FreePage => "FreePage",
            PageIn => "PageIn",
            PoolAllocation => "PoolAllocation",
            DelayExecution => "DelayExecution",
            Suspended => "Suspended",
            UserRequest => "UserRequest",
            WrExecutive => "WrExecutive",
            WrFreePage => "WrFreePage",
            WrPageIn => "WrPageIn",
            WrPoolAllocation => "WrPoolAllocation",
            WrDelayExecution => "WrDelayExecution",
            WrSuspended => "WrSuspended",
            WrUserRequest => "WrUserRequest",
            WrEventPair => "WrEventPair",
            WrQueue => "WrQueue",
            WrLpcReceive => "WrLpcReceive",
            WrLpcReply => "WrLpcReply",
            WrVirtualMemory => "WrVirtualMemory",
            WrPageOut => "WrPageOut",
            WrRendezvous => "WrRendezvous",
            // Spare2 => "Spare2",
            // Spare3 => "Spare3",
            // Spare4 => "Spare4",
            // Spare5 => "Spare5",
            WrCalloutStack => "WrCalloutStack",
            WrKernel => "WrKernel",
            WrResource => "WrResource",
            WrPushLock => "WrPushLock",
            WrMutex => "WrMutex",
            WrQuantumEnd => "WrQuantumEnd",
            WrDispatchInt => "WrDispatchInt",
            WrPreempted => "WrPreempted",
            WrYieldExecution => "WrYieldExecution",
            WrFastMutex => "WrFastMutex",
            WrGuardedMutex => "WrGuardedMutex",
            WrRundown => "WrRundown",
            WrAlertByThreadId => "WrAlertByThreadId",
            WrDeferredPreempt => "WrDeferredPreempt",
            // MaximumWaitReason => "MaximumWaitReason",
            _ => "-",
        }
    }

    fn priority(&self) -> &'static str {
        priority_str(self.Priority as u32)
    }

    fn status(&self) -> String {
        let st = self.state();
        if st == "Waiting" {
            format!("Waiting: {}", self.wait_reason())
        } else {
            st.to_string()
        }
    }
}

pub trait SystemHandleInformation {
    fn pid(&self) -> u32;
    fn type_name(&self) -> &'static str;
}

impl SystemHandleInformation for SYSTEM_HANDLE_TABLE_ENTRY_INFO {
    #[inline]
    fn pid(&self) -> u32 {
        self.UniqueProcessId as u32
    }

    fn type_name(&self) -> &'static str {
        ""
    }
}

pub fn get_mapped_file_name(handle: HANDLE, base: usize) -> Option<String> {
    struct MEMORY_MAPPED_FILE_NAME_INFORMATION {
        name: UNICODE_STRING,
        buffer: [WCHAR; 512],
    }

    let mut buffer: MEMORY_MAPPED_FILE_NAME_INFORMATION = unsafe { zeroed() };
    buffer.name.Length = 0;
    buffer.name.Buffer = buffer.buffer.as_mut_ptr();
    let mut len: usize = 0;
    unsafe {
        use super::string::UnicodeUtil;

        let r = ZwQueryVirtualMemory(
            handle,
            transmute(base),
            MemoryMappedFilenameInformation as u32,
            transmute(&mut buffer),
            size_of_val(&buffer),
            &mut len,
        );
        if NT_SUCCESS(r) {
            Some(buffer.name.to_string())
        } else {
            None
        }
    }
}

pub const PAGE_SIZE: usize = 0x1000;
pub const PAGE_SHIFT: usize = 12;

pub fn query_working_set_ex(
    handle: HANDLE,
    base: usize,
    size: usize,
) -> WindowsResult<Vec<MEMORY_WORKING_SET_EX_INFORMATION>> {
    let count = size / PAGE_SIZE;
    let mut len: usize = 0;
    unsafe {
        let mut infos = vec![zeroed::<MEMORY_WORKING_SET_EX_INFORMATION>(); count];
        for i in 0..count {
            infos[i].VirtualAddress = transmute(base + PAGE_SIZE * i);
        }
        ZwQueryVirtualMemory(
            handle,
            null_mut(),
            MemoryWorkingSetExInformation as u32,
            infos.as_mut_ptr().cast(),
            infos.len() * size_of::<MEMORY_WORKING_SET_EX_INFORMATION>(),
            &mut len,
        )
        .ntstatus_result()?;
        Ok(infos)
    }
}

// https://docs.microsoft.com/en-us/windows/win32/devnotes/ldrregisterdllnotification
pub type FnLdrRegisterDllNotification = unsafe extern "system" fn(
    flags: ULONG,
    callback: PLDR_DLL_NOTIFICATION_FUNCTION,
    context: PVOID,
    cookie: *mut usize,
) -> NTSTATUS;

// ----------------- utils -----------------

pub fn find_handle<'a>(
    type_index: u32,
    access: u32,
) -> impl Iterator<Item = &'a SYSTEM_HANDLE_TABLE_ENTRY_INFO> {
    system_handle_information().filter(move |h| {
        type_index == h.ObjectTypeIndex as u32 && h.GrantedAccess & access == access
    })
}

#[inline]
pub fn traverse_list<F: FnMut(usize) -> bool>(list: &LIST_ENTRY, mut callback: F) {
    unsafe {
        let first: *const LIST_ENTRY = transmute(list.Flink);
        let mut current = first;
        while current != null() && (*current).Flink as *const LIST_ENTRY != first {
            if !callback(current as usize) {
                break;
            }
            current = (*current).Flink;
        }
    }
}

pub fn foreach_in_initorder<F>(ldr: &PEB_LDR_DATA, mut callback: F)
where
    F: FnMut(&mut LDR_DATA_TABLE_ENTRY) -> bool,
{
    traverse_list(&ldr.InInitializationOrderModuleList, |p| {
        let p: *mut LDR_DATA_TABLE_ENTRY = unsafe { transmute(p - size_of::<LPVOID>() * 4) };
        callback(unsafe { transmute(p) })
    });
}

pub fn foreach_in_loadorder<F>(ldr: &PEB_LDR_DATA, mut callback: F)
where
    F: FnMut(&mut LDR_DATA_TABLE_ENTRY) -> bool,
{
    traverse_list(&ldr.InLoadOrderModuleList, |p| {
        let p: *mut LDR_DATA_TABLE_ENTRY = unsafe { transmute(p - size_of::<LPVOID>() * 0) };
        callback(unsafe { transmute(p) })
    });
}

pub fn foreach_in_memoryorder<F>(ldr: &PEB_LDR_DATA, mut callback: F)
where
    F: FnMut(&mut LDR_DATA_TABLE_ENTRY) -> bool,
{
    traverse_list(&ldr.InMemoryOrderModuleList, |p| {
        let p: *mut LDR_DATA_TABLE_ENTRY = unsafe { transmute(p - size_of::<LPVOID>() * 2) };
        callback(unsafe { transmute(p) })
    });
}

pub fn get_pbi() -> WindowsResult<PROCESS_BASIC_INFORMATION> {
    let mut pbi: PROCESS_BASIC_INFORMATION = unsafe { zeroed() };
    unsafe {
        ZwQueryInformationProcess(
            ZwCurrentProcess,
            ProcessBasicInformation,
            transmute(&mut pbi),
            size_of_val(&pbi) as u32,
            null_mut(),
        )
        .ntstatus_result()?;
        Ok(pbi)
    }
}

pub fn get_peb() -> WindowsResult<*mut PEB> {
    Ok(get_pbi()?.PebBaseAddress)
}