I'm trying to execute some x86 machine code that execute jmp $
(\xeb\xfe
) directly from Rust but the executable just crashes.
I successfully did it in C with the GCC compiler but it also crashes with Clang:
#include <stdio.h>
char code[] = "\xeb\xfe";
int main(int argc, char **argv){
int (*func)();
func = (int (*)()) code;
(int)(*func)();
}
To do it in pure Rust, I converted my byte array into a void pointer (*const ()
) then converted it into a unsafe extern "C" fn () -> !
with std::mem::transmute
:
static shellcode: [u8; 73] = *b"\xeb\xfe";
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(());
}
I already read How to execute raw instructions from a memory buffer in Rust? and the answer is basically what I did so I'm kind of confused...
I have compiled the Rust code for x86/x64 and both crashed, I think that the "shellcode" isn't compatible with LLVM.