pub struct PrivilegedCommand { /* private fields */ }Expand description
Builder for executing a program with elevated privileges.
§Example
use privesc::Command;
let output = Command::new("/usr/bin/cat")
.arg("/etc/shadow")
.run()?;§Platform Behavior
- macOS: Uses
osascriptwith AppleScript for GUI,sudofor CLI. - Linux: Uses
pkexecfor GUI,sudofor CLI. - Windows: Uses
ShellExecuteExWwith “runas” verb (UAC). Output capture is not available.
Implementations§
Source§impl PrivilegedCommand
impl PrivilegedCommand
Sourcepub fn new(program: impl Into<String>) -> Self
pub fn new(program: impl Into<String>) -> Self
Creates a new Command for the given program.
§Arguments
program- The path to the program to execute with elevated privileges.
Examples found in repository?
3fn main() {
4 let output = PrivilegedCommand::new("cat")
5 .arg("/etc/shadow")
6 .gui(true)
7 .prompt("Administrator privileges required to read the test file")
8 .run()
9 .unwrap();
10
11 println!("Exit status: {}", output.status);
12
13 match output.stdout_str() {
14 Some(stdout) => println!("Out: {stdout}"),
15 None => println!("Out: <not available on this platform>"),
16 }
17
18 match output.stderr_str() {
19 Some(stderr) => println!("Err: {stderr}"),
20 None => println!("Err: <not available on this platform>"),
21 }
22}More examples
5fn main() {
6 // Spawn a privileged process without blocking
7 let mut child = PrivilegedCommand::new("sleep").arg("2").spawn().unwrap();
8
9 if let Some(id) = child.id() {
10 println!("Spawned process with ID: {id}");
11 }
12
13 // Do some work while the process runs
14 println!("Doing other work while process runs...");
15
16 sleep(Duration::from_secs(1));
17
18 // Check if the process has finished (non-blocking)
19 match child.try_wait().unwrap() {
20 Some(status) => println!("Process already finished with: {status}"),
21 None => println!("Process still running..."),
22 }
23
24 // Wait for the process to complete
25 println!("Waiting for process to finish...");
26 let output = child.wait().unwrap();
27
28 println!("Exit status: {}", output.status);
29}Sourcepub fn arg(self, arg: impl Into<String>) -> Self
pub fn arg(self, arg: impl Into<String>) -> Self
Adds a single argument to pass to the program.
Examples found in repository?
3fn main() {
4 let output = PrivilegedCommand::new("cat")
5 .arg("/etc/shadow")
6 .gui(true)
7 .prompt("Administrator privileges required to read the test file")
8 .run()
9 .unwrap();
10
11 println!("Exit status: {}", output.status);
12
13 match output.stdout_str() {
14 Some(stdout) => println!("Out: {stdout}"),
15 None => println!("Out: <not available on this platform>"),
16 }
17
18 match output.stderr_str() {
19 Some(stderr) => println!("Err: {stderr}"),
20 None => println!("Err: <not available on this platform>"),
21 }
22}More examples
5fn main() {
6 // Spawn a privileged process without blocking
7 let mut child = PrivilegedCommand::new("sleep").arg("2").spawn().unwrap();
8
9 if let Some(id) = child.id() {
10 println!("Spawned process with ID: {id}");
11 }
12
13 // Do some work while the process runs
14 println!("Doing other work while process runs...");
15
16 sleep(Duration::from_secs(1));
17
18 // Check if the process has finished (non-blocking)
19 match child.try_wait().unwrap() {
20 Some(status) => println!("Process already finished with: {status}"),
21 None => println!("Process still running..."),
22 }
23
24 // Wait for the process to complete
25 println!("Waiting for process to finish...");
26 let output = child.wait().unwrap();
27
28 println!("Exit status: {}", output.status);
29}Sourcepub fn gui(self, gui: bool) -> Self
pub fn gui(self, gui: bool) -> Self
Sets whether to use a GUI prompt for authentication.
true: Use GUI prompt (AppleScript on macOS, pkexec on Linux, UAC on Windows)false: Use terminal-based sudo (default)
On Windows, this parameter is ignored as only UAC elevation is available.
Examples found in repository?
3fn main() {
4 let output = PrivilegedCommand::new("cat")
5 .arg("/etc/shadow")
6 .gui(true)
7 .prompt("Administrator privileges required to read the test file")
8 .run()
9 .unwrap();
10
11 println!("Exit status: {}", output.status);
12
13 match output.stdout_str() {
14 Some(stdout) => println!("Out: {stdout}"),
15 None => println!("Out: <not available on this platform>"),
16 }
17
18 match output.stderr_str() {
19 Some(stderr) => println!("Err: {stderr}"),
20 None => println!("Err: <not available on this platform>"),
21 }
22}Sourcepub fn prompt(self, prompt: impl Into<String>) -> Self
pub fn prompt(self, prompt: impl Into<String>) -> Self
Sets a custom prompt message for authentication.
On Windows, this is ignored as UAC displays its own prompt. On Linux with GUI mode, this is ignored due to pkexec limitations.
Examples found in repository?
3fn main() {
4 let output = PrivilegedCommand::new("cat")
5 .arg("/etc/shadow")
6 .gui(true)
7 .prompt("Administrator privileges required to read the test file")
8 .run()
9 .unwrap();
10
11 println!("Exit status: {}", output.status);
12
13 match output.stdout_str() {
14 Some(stdout) => println!("Out: {stdout}"),
15 None => println!("Out: <not available on this platform>"),
16 }
17
18 match output.stderr_str() {
19 Some(stderr) => println!("Err: {stderr}"),
20 None => println!("Err: <not available on this platform>"),
21 }
22}Sourcepub fn run(&self) -> Result<PrivilegedOutput>
pub fn run(&self) -> Result<PrivilegedOutput>
Executes the command with elevated privileges.
§Returns
A PrivilegedOutput containing the exit status and optionally captured
stdout/stderr. Note that stdout/stderr are None on Windows.
Examples found in repository?
3fn main() {
4 let output = PrivilegedCommand::new("cat")
5 .arg("/etc/shadow")
6 .gui(true)
7 .prompt("Administrator privileges required to read the test file")
8 .run()
9 .unwrap();
10
11 println!("Exit status: {}", output.status);
12
13 match output.stdout_str() {
14 Some(stdout) => println!("Out: {stdout}"),
15 None => println!("Out: <not available on this platform>"),
16 }
17
18 match output.stderr_str() {
19 Some(stderr) => println!("Err: {stderr}"),
20 None => println!("Err: <not available on this platform>"),
21 }
22}Sourcepub fn spawn(&self) -> Result<PrivilegedChild>
pub fn spawn(&self) -> Result<PrivilegedChild>
Spawns the command with elevated privileges, returning a handle to the process.
Unlike run, this method returns immediately after spawning,
allowing you to perform other work while the privileged process runs.
§Returns
A PrivilegedChild handle that can be used to wait for the process to finish.
§Platform Behavior
- macOS/Linux: Returns a handle wrapping the underlying process.
- Windows: Returns a handle wrapping the Windows process
HANDLE.
§Example
use privesc::PrivilegedCommand;
let child = PrivilegedCommand::new("/usr/bin/long-running-task")
.spawn()?;
// Do other work...
let output = child.wait()?;Examples found in repository?
5fn main() {
6 // Spawn a privileged process without blocking
7 let mut child = PrivilegedCommand::new("sleep").arg("2").spawn().unwrap();
8
9 if let Some(id) = child.id() {
10 println!("Spawned process with ID: {id}");
11 }
12
13 // Do some work while the process runs
14 println!("Doing other work while process runs...");
15
16 sleep(Duration::from_secs(1));
17
18 // Check if the process has finished (non-blocking)
19 match child.try_wait().unwrap() {
20 Some(status) => println!("Process already finished with: {status}"),
21 None => println!("Process still running..."),
22 }
23
24 // Wait for the process to complete
25 println!("Waiting for process to finish...");
26 let output = child.wait().unwrap();
27
28 println!("Exit status: {}", output.status);
29}Trait Implementations§
Source§impl Clone for PrivilegedCommand
impl Clone for PrivilegedCommand
Source§fn clone(&self) -> PrivilegedCommand
fn clone(&self) -> PrivilegedCommand
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more