-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "sample_privileges_sys" | ||
version = "0.0.0" | ||
edition = "2018" | ||
|
||
[dependencies.windows-sys] | ||
path = "../../../libs/sys" | ||
features = [ | ||
"Win32_Foundation", | ||
"Win32_Security", | ||
"Win32_System_Threading", | ||
"Win32_System_Memory", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use windows_sys::{Win32::Security::*, Win32::System::Memory::*, Win32::System::Threading::*}; | ||
|
||
fn main() { | ||
unsafe { | ||
let mut token = 0; | ||
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token); | ||
let mut bytes_required = 0; | ||
|
||
GetTokenInformation( | ||
token, | ||
TokenPrivileges, | ||
std::ptr::null_mut(), | ||
0, | ||
&mut bytes_required, | ||
); | ||
|
||
let buffer = LocalAlloc(LPTR, bytes_required as _); | ||
|
||
GetTokenInformation( | ||
token, | ||
TokenPrivileges, | ||
buffer as *mut _, | ||
bytes_required, | ||
&mut bytes_required, | ||
); | ||
|
||
let header = &*(buffer as *const TOKEN_PRIVILEGES); | ||
|
||
let privileges = | ||
std::slice::from_raw_parts(header.Privileges.as_ptr(), header.PrivilegeCount as _); | ||
|
||
for privilege in privileges { | ||
let mut name_len = 0; | ||
|
||
LookupPrivilegeNameW( | ||
std::ptr::null(), | ||
&privilege.Luid, | ||
std::ptr::null_mut(), | ||
&mut name_len, | ||
); | ||
|
||
let mut name = vec![0u16; (name_len + 1) as usize]; | ||
|
||
LookupPrivilegeNameW( | ||
std::ptr::null(), | ||
&privilege.Luid, | ||
name.as_mut_ptr(), | ||
&mut name_len, | ||
); | ||
|
||
println!("{}", String::from_utf16_lossy(&name)); | ||
} | ||
|
||
LocalFree(buffer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "sample_privileges" | ||
version = "0.0.0" | ||
edition = "2018" | ||
|
||
[dependencies.windows] | ||
path = "../../../libs/windows" | ||
features = [ | ||
"Win32_Foundation", | ||
"Win32_Security", | ||
"Win32_System_Threading", | ||
"Win32_System_Memory", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use windows::{ | ||
core::*, Win32::Foundation::*, Win32::Security::*, Win32::System::Memory::*, | ||
Win32::System::Threading::*, | ||
}; | ||
|
||
fn main() -> Result<()> { | ||
unsafe { | ||
let mut token = HANDLE::default(); | ||
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token).ok()?; | ||
|
||
let mut bytes_required = 0; | ||
GetTokenInformation(token, TokenPrivileges, None, 0, &mut bytes_required); | ||
|
||
let buffer = LocalAlloc(LPTR, bytes_required as _)?; | ||
|
||
GetTokenInformation( | ||
token, | ||
TokenPrivileges, | ||
Some(buffer.0 as *mut _), | ||
bytes_required, | ||
&mut bytes_required, | ||
) | ||
.ok()?; | ||
|
||
let header = &*(buffer.0 as *const TOKEN_PRIVILEGES); | ||
|
||
let privileges = | ||
std::slice::from_raw_parts(header.Privileges.as_ptr(), header.PrivilegeCount as _); | ||
|
||
for privilege in privileges { | ||
let mut name_len = 0; | ||
LookupPrivilegeNameW(None, &privilege.Luid, PWSTR::null(), &mut name_len); | ||
|
||
let mut name = vec![0u16; (name_len + 1) as usize]; | ||
let name = PWSTR(name.as_mut_ptr()); | ||
LookupPrivilegeNameW(None, &privilege.Luid, name, &mut name_len).ok()?; | ||
|
||
println!("{}", name.display()) | ||
} | ||
|
||
_ = LocalFree(buffer); | ||
Ok(()) | ||
} | ||
} |