如何执行与 LLVM 前端可执行文件兼容的机器外壳代码的原始字节



我正在尝试执行一些直接从 Rust 执行jmp $(xebxfe(的 x86 机器代码,但可执行文件只是崩溃。

我用 GCC 编译器在 C 中成功做到了这一点,但它也因 Clang 而崩溃:

#include <stdio.h>
char code[] = "xebxfe";
int main(int argc, char **argv){
int (*func)();
func = (int (*)()) code;
(int)(*func)();
}

为了在纯 Rust 中做到这一点,我将我的字节数组转换为一个空指针(*const ()(,然后将其转换为带有std::mem::transmuteunsafe extern "C" fn () -> !

static shellcode: [u8; 73] = *b"xebxfe";
fn main() -> std::io::Result<()> {
let raw: unsafe extern "C" fn() -> ! =
unsafe { std::mem::transmute(&shellcode.as_ptr() as *const _ as *const ()) };
unsafe { raw() };
return Ok(());
}

我已经阅读了如何在 Rust 中的内存缓冲区执行原始指令? 答案基本上是我所做的,所以我有点困惑......

我已经为 x86/x64 编译了 Rust 代码,两者都崩溃了,我认为"shellcode"与 LLVM 不兼容。

我认为是因为缓冲区没有执行标志,我不确定,但您可以尝试使用 mProtect 将可执行标志添加到缓冲区。

最新更新