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
#![feature(type_ascription)]

pub mod internal;
pub mod licensing;
pub mod strings;

/// Check if currently running application is protected by vmprotect
///
/// Dead code will not be elminated
#[inline(always)]
pub fn is_protected() -> bool {
    unsafe { internal::VMProtectIsProtected() == 1 }
}

/// Check presence of debugger
#[inline(always)]
pub fn is_debugger_present(check_kernel_mode: bool) -> bool {
    unsafe { internal::VMProtectIsDebuggerPresent(if check_kernel_mode { 1 } else { 0 }) == 1 }
}

/// Returns true if running inside virtual machine
#[inline(always)]
pub fn is_virtual_machine() -> bool {
    unsafe { internal::VMProtectIsVirtualMachinePresent() == 1 }
}

/// Check if process memory is not damaged/edited
#[inline(always)]
pub fn is_valid_image_crc() -> bool {
    unsafe { internal::VMProtectIsValidImageCRC() == 1 }
}

#[macro_export]
macro_rules! protected {
    ($x: expr; mutate; $code: block) => {{
        let ret;
        unsafe { vmprotect::internal::VMProtectBeginMutation(real_c_string::real_c_string!($x)) };
        ret = $code;
        unsafe {
            vmprotect::internal::VMProtectEnd();
        };
        ret
    }};
    ($x: expr; virtualize false; $code: block) => {{
        let ret;
        unsafe {
            vmprotect::internal::VMProtectBeginVirtualization(real_c_string::real_c_string!($x))
        };
        ret = $code;
        unsafe {
            vmprotect::internal::VMProtectEnd();
        };
        ret
    }};
    ($x: expr; virtualize true; $code: block) => {{
        let ret;
        unsafe {
            vmprotect::internal::VMProtectBeginVirtualizationLockByKey(
                real_c_string::real_c_string!($x),
            )
        };
        ret = $code;
        unsafe {
            vmprotect::internal::VMProtectEnd();
        };
        ret
    }};
    ($x: expr; ultra false; $code: block) => {{
        let ret;
        unsafe { vmprotect::internal::VMProtectBeginUltra(real_c_string::real_c_string!($x)) };
        ret = $code;
        unsafe {
            vmprotect::internal::VMProtectEnd();
        };
        ret
    }};
    ($x: expr; ultra true; $code: block) => {{
        let ret;
        unsafe {
            vmprotect::internal::VMProtectBeginUltraLockByKey(real_c_string::real_c_string!($x))
        };
        ret = $code;
        unsafe {
            vmprotect::internal::VMProtectEnd();
        };
        ret
    }};
    (A; $x: expr) => {
        vmprotect::strings::encrypted_a::EncryptedStringA(unsafe {
            vmprotect::internal::VMProtectDecryptStringA(real_c_string::real_c_string!($x))
        }) as vmprotect::strings::encrypted_a::EncryptedStringA
    };
    (W; $x: expr) => {
        vmprotect::strings::encrypted_w::EncryptedStringW(unsafe {
            vmprotect::internal::VMProtectDecryptStringW(real_c_string::real_c_wstring!($x))
        }) as vmprotect::strings::encrypted_w::EncryptedStringW // To remove mut
    };
}