libmem 4.4.0

Advanced Game Hacking Library (Windows/Linux/FreeBSD)
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
use libmem::*;
use std::fmt;
use std::mem;

fn separator() {
    println!("====================");
}

#[no_mangle]
pub extern "C" fn some_function() {
    println!("[*] Some Function Called!");
}

static mut TRAMPOLINE: (lm_address_t, lm_size_t) = (0, 0);

#[no_mangle]
pub extern "C" fn hk_some_function() {
    println!("[*] Some Function Hooked!");

    // OBS: Calling the trampoline is crashing the program,
    // although the trampoline is correct and so is the hook
    /*
    let orig_func = unsafe {
        mem::transmute::<*const (), extern "C" fn()>(TRAMPOLINE.0 as *const ())
    };
    orig_func();
    */
}

fn print_n<T>(vec: Vec<T>, n: usize)
where
    T: fmt::Display,
{
    for i in 0..n {
        if i >= vec.len() {
            break;
        }

        println!("{}", vec[i]);
    }
}

// Simple memory representation of a C++ class
#[repr(C)]
struct SomeClassVMT {
    pfn_some_function: extern "C" fn(),
}

#[repr(C)]
struct SomeClass {
    vtable: SomeClassVMT,
}

impl SomeClass {
    fn new() -> Self {
        Self {
            vtable: SomeClassVMT {
                pfn_some_function: some_function,
            },
        }
    }

    fn some_function(&self) {
        (self.vtable.pfn_some_function)();
    }
}

unsafe fn test() {
    println!("[*] libmem-rs tests");

    separator();

    println!("[*] Process Enumeration");
    print_n(LM_EnumProcesses().unwrap(), 5);

    separator();

    let cur_proc = LM_GetProcess().unwrap();
    println!("[*] Current Process: {}", cur_proc);

    separator();

    let parent_proc = LM_GetProcessEx(cur_proc.get_ppid()).unwrap();
    println!("[*] Parent Process Of Current Process: {}", parent_proc);

    separator();

    let proc = LM_FindProcess("test1").unwrap();
    println!("[*] Remote Process: {}", proc);

    separator();

    let is_alive = LM_IsProcessAlive(&proc);
    println!(
        "[*] Is the Remote Process Alive? {}",
        if is_alive { "Yes" } else { "No" }
    );

    separator();

    let sysbits = LM_GetSystemBits();
    println!("[*] System Bits: {}", sysbits);

    separator();

    println!(
        "[*] Current Process Threads: {:?}",
        LM_EnumThreads().unwrap()
    );

    separator();

    println!(
        "[*] Remote Process Threads: {:?}",
        LM_EnumThreadsEx(&proc).unwrap()
    );

    separator();

    let thread = LM_GetThread().unwrap();
    println!("[*] Current Thread ID: {}", thread);
    println!(
        "[*] Remote Process Thread ID: {}",
        LM_GetThreadEx(&proc).unwrap()
    );

    separator();

    println!(
        "[*] Process From Thread '{}': {}",
        thread,
        LM_GetThreadProcess(&thread).unwrap()
    );

    separator();

    println!("[*] Current Process - Module Enumeration");
    print_n(LM_EnumModules().unwrap(), 5);

    separator();

    println!("[*] Remote Process - Module Enumeration");
    print_n(LM_EnumModulesEx(&proc).unwrap(), 5);

    separator();

    let cur_module = LM_FindModule(&cur_proc.get_name()).unwrap();
    println!("[*] Current Process Module: {}", cur_module);

    separator();

    let module = LM_FindModuleEx(&proc, &proc.get_name()).unwrap();
    println!("[*] Remote Process Module: {}", module);

    separator();

    let libmodule = LM_LoadModule("/usr/local/lib/libtest.so").unwrap();
    println!("[*] Module Loaded into Current Process: {}", libmodule);

    separator();

    // TODO: Add tests for LM_LoadModuleEx

    // separator();

    // Needs internal fixing
    // LM_UnloadModule(&module).unwrap();
    // println!("[*] Unloaded Module from Current Process: {}", module);

    // separator();

    // TODO: Add tests for LM_UnloadModuleEx

    // separator();

    // TODO: Fix empty symbol list
    /*
    println!("[*] Current Process - Symbol Enumeration");
    println!("[*] Module: {}", cur_module.get_name());
    print_n(LM_EnumSymbols(&cur_module).unwrap(), 5);

    separator();

    let some_function_addr = LM_FindSymbolAddress(&cur_module, "some_function").unwrap();
    println!(
        "[*] Address of 'some_function': {:p}",
        some_function as *const ()
    );
    println!("[*] Symbol Address Lookup:      {:#x}", some_function_addr);

    separator();
    */
    let some_function_addr = some_function as *const () as usize;

    let mangled = "_ZN5tests9separator17h9f5c7cd256d1d06aE";
    let demangled = LM_DemangleSymbol(&mangled).unwrap();
    println!("[*] Demangled '{}': {}", mangled, demangled);

    separator();

    println!("[*] Current Process - Page Enumeration");
    print_n(LM_EnumPages().unwrap(), 5);

    separator();

    println!("[*] Remote Process - Page Enumeration");
    print_n(LM_EnumPagesEx(&proc).unwrap(), 5);

    separator();

    println!(
        "[*] Current Process - Page at: {:#x}",
        cur_module.get_base()
    );
    println!("{}", LM_GetPage(cur_module.get_base()).unwrap());

    separator();

    println!("[*] Remote Process - Page at: {:#x}", module.get_base());
    println!("{}", LM_GetPageEx(&proc, module.get_base()).unwrap());

    separator();

    let number: i32 = 1337;
    let number_addr = &number as *const i32 as usize;
    let read_number: i32 = LM_ReadMemory(number_addr).unwrap();
    println!("[*] Number Value: {}", number);
    println!("[*] Read Number Value: {}", read_number);

    separator();

    // TODO: Add tests for LM_ReadMemoryEx

    // separator();

    let value: i32 = 69;
    LM_WriteMemory(number_addr, &value).unwrap();
    println!("[*] Value to write: {}", value);
    println!("[*] Number Value: {}", number);

    separator();

    // TODO: Add tests for LM_WriteMemoryEx

    // separator();

    let buffer: [u8; 10] = [0; 10];
    println!("[*] Buffer Original: {:?}", buffer);
    LM_SetMemory(
        buffer.as_ptr() as usize,
        255,
        buffer.len() * mem::size_of::<u8>(),
    )
    .unwrap();
    println!("[*] Buffer After LM_SetMemory: {:?}", buffer);

    separator();

    // TODO: Add tests for LM_SetMemoryEx

    // separator();

    let old_prot = LM_ProtMemory(some_function_addr, 0x1000, LM_PROT_XRW).unwrap();
    println!(
        "[*] Original Protection of '{:#x}': {}",
        some_function_addr, old_prot
    );
    let prot = LM_ProtMemory(some_function_addr, 0x1000, old_prot).unwrap();
    println!(
        "[*] Reverted Protection (from '{}' back to '{}')",
        prot, old_prot
    );

    separator();

    let old_prot = LM_ProtMemoryEx(&proc, module.get_base(), 0x1000, LM_PROT_XRW).unwrap();
    println!(
        "[*] Remote - Original Protection of '{:#x}': {}",
        module.get_base(),
        old_prot
    );
    let prot = LM_ProtMemoryEx(&proc, module.get_base(), 0x1000, old_prot).unwrap();
    println!(
        "[*] Remote - Reverted Protection (from '{}' back to '{}')",
        prot, old_prot
    );

    separator();

    let alloc = LM_AllocMemory(0x1000, LM_PROT_XRW).unwrap();
    println!("[*] Allocated Memory: {:#x}", alloc);
    LM_FreeMemory(alloc, 0x1000).unwrap();
    println!("[*] Freed Memory");

    separator();

    let alloc = LM_AllocMemoryEx(&proc, 0x1000, LM_PROT_XRW).unwrap();
    println!("[*] Remote - Allocated Memory: {:#x}", alloc);
    LM_FreeMemoryEx(&proc, alloc, 0x1000).unwrap();
    println!("[*] Remote - Freed Memory");

    separator();

    let scan_me: [u8; 10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    let scan_me_addr = scan_me.as_ptr() as lm_address_t;
    let scan_start = scan_me_addr - 0x10; // start the scan before the 'scan_me' address
    let scan_size = 0xFF;
    let data_scan = LM_DataScan(&scan_me, scan_start, scan_size).unwrap();
    let pattern_scan = LM_PatternScan(&scan_me, "xxxx?xxxx?", scan_start, scan_size).unwrap();
    let sig_scan = LM_SigScan("01 02 03 04 ?? 06 07 08 09 ??", scan_start, scan_size).unwrap();
    println!("[*] Real ScanMe Address: {:#x}", scan_me_addr);
    println!("[*] Data Scan Address: {:#x}", data_scan);
    println!("[*] Pattern Scan Address: {:#x}", pattern_scan);
    println!("[*] Signature Scan Address: {:#x}", sig_scan);

    separator();

    // TODO: Add tests LM_DataScanEx, LM_PatternScanEx, LM_SigScanEx

    // separator();

    println!("[*] Hooking 'some_function'");
    println!("[*] Original Address: {:#x}", some_function_addr);

    TRAMPOLINE = LM_HookCode(
        some_function_addr,
        hk_some_function as *const () as lm_address_t,
    )
    .unwrap();
    println!("[*] Trampoline: {:#x?}", TRAMPOLINE);

    some_function();

    LM_UnhookCode(some_function_addr, TRAMPOLINE).unwrap();

    println!("[*] Unhooked 'some_function'");
    some_function();

    separator();

    // TODO: Add tests for LM_HookCodeEx and LM_UnhookCodeEx

    // separator();

    let inst = LM_Assemble("mov eax, ebx").unwrap();
    println!("[*] Assembled Instruction: {}", inst);

    separator();

    let bytes =
        LM_AssembleEx("push ebp ; mov ebp, esp; mov esp, ebp; pop ebp; ret", 32, 0).unwrap();
    println!("[*] Assembled Instructions: {:#x?}", bytes);

    separator();

    let inst = LM_Disassemble(some_function_addr).unwrap();
    println!("[*] Disassembled Instruction: {}", inst);

    separator();

    let insts =
        LM_DisassembleEx(some_function_addr, LM_BITS, 0x100, 5, some_function_addr).unwrap();
    println!("[*] Disassembled Instructions:");
    for inst in insts {
        println!("{}", inst);
    }

    separator();

    let minsize = 0x5;
    let alignedsize = LM_CodeLength(some_function_addr, minsize).unwrap();
    println!(
        "[*] Aligned Size (minimum: {:#x}): {:#x}",
        minsize, alignedsize
    );

    separator();

    // TODO: Add tests for LM_CodeLengthEx

    // separator();

    println!("[*] VMT Hooking");

    let some_object = SomeClass::new();
    let mut some_object_vmt = lm_vmt_t::new(&some_object as *const SomeClass as *mut lm_address_t);
    some_object_vmt.hook(0, hk_some_function as lm_address_t);

    println!(
        "[*] Original VMT Function: {:#x}",
        some_object_vmt.get_original(0).unwrap()
    );

    some_object.some_function();
    some_object_vmt.unhook(0);

    // let consume_vmt = |_vmt : lm_vmt_t| {};
    // consume_vmt(some_object_vmt);

    println!();
    println!("[*] Unhooked");
    println!();

    some_object.some_function();

    separator();
}

fn main() {
    unsafe {
        test();
    }
}