Skip to content

Commit 041342f

Browse files
committed
feat: enable SeDebugPrivilege for SYSTEM processes
1 parent ddef3a2 commit 041342f

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ tokio = { version = "1.44.2", default-features = false, features = [
1616
] }
1717
win32-ecoqos = "0.5.0"
1818
windows = { version = "0.61", features = [
19-
"Win32_UI_WindowsAndMessaging",
19+
# UI event hook
2020
"Win32_UI_Accessibility",
21+
# foreground event id, enter event loop
22+
"Win32_UI_WindowsAndMessaging",
23+
# enable SeDebugPrivilege for SYSTEM processes
24+
"Win32_Security",
2125
] }
2226

2327
[build-dependencies]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ EnergyStar alernative in Rust.
1414
- [x] Ctrl-C handle
1515
- [ ] windows terminate handle
1616
- [x] Support UWP applications
17+
- [x] Support `SYSTEM` privileged processes
1718
- [ ] Configurable whitelist and blacklist
1819

1920
## Non-goals (for now)

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use kanal::Sender;
66
pub mod bypass;
77
pub mod events;
88
pub mod logging;
9+
pub mod privilege;
910
pub mod utils;
1011

1112
pub static PID_SENDER: OnceLock<Sender<u32>> = OnceLock::new();

src/main.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::error::Error;
22
use std::ffi::OsString;
33

44
use ahash::AHashSet;
5-
use spdlog::{Level, LevelFilter, debug, info};
5+
use rustystar::privilege::try_enable_se_debug_privilege;
6+
use spdlog::{Level, LevelFilter, debug, info, warn};
67
use win32_ecoqos::process::toggle_efficiency_mode;
78

89
use rustystar::bypass::should_bypass;
@@ -17,20 +18,44 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
1718

1819
let _ = WHITELIST.set(AHashSet::from_iter(
1920
[
21+
// ourself
22+
"RustyStar.exe",
2023
// System processes
2124
"explorer.exe",
25+
// Windows Manager of Windows
2226
"dwm.exe",
27+
// CSRSS core process
2328
"csrss.exe",
29+
// Windows services process
2430
"svchost.exe",
31+
// Task Manager
2532
"Taskmgr.exe",
33+
// Session Manager Subsystem
2634
"smss.exe",
35+
// Chinese input method
2736
"ChsIME.exe",
37+
// Speech-To-Text, Screen keyboard, handwrite input, e.g.
2838
"ctfmon.exe",
39+
// Windows User Mode Driver Framework
2940
"WUDFRd.exe",
41+
"WUDFHost.exe",
3042
// Edge is energy aware
3143
"msedge.exe",
3244
// UWP special handle
3345
"ApplicationFrameHost.exe",
46+
// system itself
47+
"[System Process]",
48+
"System",
49+
"Registry",
50+
// parent of "services.exe"
51+
"wininit.exe",
52+
// parent of "svchost.exe", "wudfhost.exe", e.g.
53+
"services.exe",
54+
// Local Security Authority Subsystem Service
55+
"lsass.exe",
56+
// part of the Windows Security Center,
57+
// responsible for monitoring and reporting the security status of your system
58+
"SecurityHealthService.exe",
3459
]
3560
.map(OsString::from),
3661
));
@@ -41,6 +66,15 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
4166
std::process::exit(0);
4267
})?;
4368

69+
match try_enable_se_debug_privilege() {
70+
Ok(_) => {
71+
info!("SeDebugPriviledge enabled!");
72+
}
73+
Err(e) => {
74+
warn!("SeDebugPriviledge enable failed: {e}");
75+
}
76+
}
77+
4478
info!("throtting all processes...");
4579
tokio::task::spawn_blocking(|| toggle_all(Some(true))).await??;
4680

src/privilege/mod.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use spdlog::warn;
2+
use win32_ecoqos::windows_result;
3+
use windows::Win32::{
4+
Foundation::{HANDLE, LUID},
5+
Security::{
6+
AdjustTokenPrivileges, LUID_AND_ATTRIBUTES, LookupPrivilegeValueW, SE_DEBUG_NAME,
7+
SE_PRIVILEGE_ENABLED, TOKEN_ADJUST_PRIVILEGES, TOKEN_PRIVILEGES,
8+
},
9+
System::Threading::{GetCurrentProcess, OpenProcessToken},
10+
};
11+
12+
pub fn try_enable_se_debug_privilege() -> windows_result::Result<()> {
13+
unsafe {
14+
let processhandle = GetCurrentProcess();
15+
let mut tokenhandle = HANDLE(std::ptr::null_mut());
16+
let desiredaccess = TOKEN_ADJUST_PRIVILEGES;
17+
OpenProcessToken(processhandle, desiredaccess, &mut tokenhandle as _)?;
18+
19+
if tokenhandle.is_invalid() {
20+
warn!("failed to open access token!");
21+
}
22+
23+
let mut luid = LUID::default();
24+
let lpname = SE_DEBUG_NAME;
25+
LookupPrivilegeValueW(None, Some(&lpname), &mut luid)?;
26+
27+
let attributes = SE_PRIVILEGE_ENABLED;
28+
let privilege = LUID_AND_ATTRIBUTES {
29+
Luid: luid,
30+
Attributes: attributes,
31+
};
32+
33+
let disableallprivileges = false;
34+
let newstate = TOKEN_PRIVILEGES {
35+
PrivilegeCount: 1,
36+
Privileges: [privilege],
37+
};
38+
AdjustTokenPrivileges(
39+
tokenhandle,
40+
disableallprivileges,
41+
Some(&newstate),
42+
0,
43+
None,
44+
None,
45+
)?;
46+
}
47+
Ok(())
48+
}

0 commit comments

Comments
 (0)