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
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(overflowing_literals)]
#![allow(non_upper_case_globals)]

pub mod error;
pub mod peloader;
pub mod peparser;

use error::Result;
use peloader::{DllLoader, ExeLoader};
use peparser::PE;
use std::os::raw::c_void;

#[cfg(feature = "hook")]
pub use peloader::hook;
#[cfg(feature = "hook")]
use peloader::hook::ProcDesc;
#[cfg(feature = "hook")]
use std::collections::HashMap;

pub unsafe fn memexec_exe(bs: &[u8]) -> Result<()> {
    let pe = PE::new(bs)?;
    #[cfg(feature = "hook")]
    let loader = ExeLoader::new(&pe, None)?;
    #[cfg(not(feature = "hook"))]
    let loader = ExeLoader::new(&pe)?;
    Ok(loader.invoke_entry_point())
}

#[cfg(feature = "hook")]
pub unsafe fn memexec_exe_with_hooks(
    bs: &[u8],
    hooks: &HashMap<ProcDesc, *const c_void>,
) -> Result<()> {
    let pe = PE::new(bs)?;
    let loader = ExeLoader::new(&pe, Some(hooks))?;
    Ok(loader.invoke_entry_point())
}

pub unsafe fn memexec_dll(
    bs: &[u8],
    hmod: *const c_void,
    reason_for_call: u32,
    lp_reserved: *const c_void,
) -> Result<bool> {
    let pe = PE::new(bs)?;
    #[cfg(feature = "hook")]
    let loader = DllLoader::new(&pe, None)?;
    #[cfg(not(feature = "hook"))]
    let loader = DllLoader::new(&pe)?;
    Ok(loader.invoke_entry_point(hmod, reason_for_call, lp_reserved))
}

#[cfg(feature = "hook")]
pub unsafe fn memexec_dll_with_hooks(
    bs: &[u8],
    hmod: *const c_void,
    reason_for_call: u32,
    lp_reserved: *const c_void,
    hooks: &HashMap<ProcDesc, *const c_void>,
) -> Result<bool> {
    let pe = PE::new(bs)?;
    let loader = DllLoader::new(&pe, Some(hooks))?;
    Ok(loader.invoke_entry_point(hmod, reason_for_call, lp_reserved))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Read;

    #[test]
    #[cfg(not(feature = "hook"))]
    fn test_dll() {
        let mut buf = Vec::new();
        #[cfg(all(target_arch = "x86_64", target_os = "windows"))]
        File::open("./test.x64.dll")
            .unwrap()
            .read_to_end(&mut buf)
            .unwrap();
        #[cfg(all(target_arch = "x86", target_os = "windows"))]
        File::open("./test.x86.dll")
            .unwrap()
            .read_to_end(&mut buf)
            .unwrap();

        unsafe {
            memexec_dll(&buf, 0 as _, peloader::def::DLL_PROCESS_ATTACH, 0 as _).unwrap();
        }
    }

    #[test]
    #[cfg(not(feature = "hook"))]
    fn test_exe() {
        let mut buf = Vec::new();
        #[cfg(all(target_arch = "x86_64", target_os = "windows"))]
        File::open("./test.x64.exe")
            .unwrap()
            .read_to_end(&mut buf)
            .unwrap();
        #[cfg(all(target_arch = "x86", target_os = "windows"))]
        File::open("./test.x86.exe")
            .unwrap()
            .read_to_end(&mut buf)
            .unwrap();

        unsafe {
            memexec_exe(&buf).unwrap();
        }
    }

    #[cfg(feature = "hook")]
    use std::mem;

    #[cfg(feature = "hook")]
    #[cfg(all(target_arch = "x86", target_os = "windows"))]
    extern "cdecl" fn __wgetmainargs(
        _Argc: *mut i32,
        _Argv: *mut *const *const u16,
        _Env: *const c_void,
        _DoWildCard: i32,
        _StartInfo: *const c_void,
    ) -> i32 {
        unsafe {
            *_Argc = 2;
            let a0: Vec<_> = "program_name\0"
                .chars()
                .map(|c| (c as u16).to_le())
                .collect();
            let a1: Vec<_> = "token::whoami\0"
                .chars()
                .map(|c| (c as u16).to_le())
                .collect();
            *_Argv = [a0.as_ptr(), a1.as_ptr()].as_ptr();

            mem::forget(a0);
            mem::forget(a1);
        }

        0
    }

    #[test]
    #[cfg(feature = "hook")]
    #[cfg(all(target_arch = "x86", target_os = "windows"))]
    fn hook_x86() {
        let mut buf = Vec::new();
        File::open("./test.x86.exe")
            .unwrap()
            .read_to_end(&mut buf)
            .unwrap();

        let mut hooks = HashMap::new();

        unsafe {
            hooks.insert(
                "msvcrt.dll!__wgetmainargs".into(),
                mem::transmute::<
                    extern "cdecl" fn(
                        *mut i32,
                        *mut *const *const u16,
                        *const c_void,
                        i32,
                        *const c_void,
                    ) -> i32,
                    *const c_void,
                >(__wgetmainargs),
            );
            memexec_exe_with_hooks(&buf, &hooks).unwrap();
        }
    }

    #[cfg(feature = "hook")]
    #[cfg(all(target_arch = "x86_64", target_os = "windows"))]
    extern "win64" fn __wgetmainargs(
        _Argc: *mut i32,
        _Argv: *mut *const *const u16,
        _Env: *const c_void,
        _DoWildCard: i32,
        _StartInfo: *const c_void,
    ) -> i32 {
        unsafe {
            *_Argc = 2;

            let a0: Vec<_> = "program_name\0"
                .chars()
                .map(|c| (c as u16).to_le())
                .collect();
            let a1: Vec<_> = "token::whoami\0"
                .chars()
                .map(|c| (c as u16).to_le())
                .collect();
            *_Argv = [a0.as_ptr(), a1.as_ptr()].as_ptr();

            mem::forget(a0);
            mem::forget(a1);
        }

        0
    }

    #[test]
    #[cfg(feature = "hook")]
    #[cfg(all(target_arch = "x86_64", target_os = "windows"))]
    fn hook_x64() {
        let mut buf = Vec::new();
        File::open("./test.x64.exe")
            .unwrap()
            .read_to_end(&mut buf)
            .unwrap();

        let mut hooks = HashMap::new();

        unsafe {
            hooks.insert(
                "msvcrt.dll!__wgetmainargs".into(),
                mem::transmute::<
                    extern "win64" fn(
                        *mut i32,
                        *mut *const *const u16,
                        *const c_void,
                        i32,
                        *const c_void,
                    ) -> i32,
                    *const c_void,
                >(__wgetmainargs),
            );
            memexec_exe_with_hooks(&buf, &hooks).unwrap();
        }
    }
}