Crates.io | karen |
lib.rs | karen |
version | 0.1.2 |
source | src |
created_at | 2024-04-28 17:46:20.04232 |
updated_at | 2024-06-23 04:53:01.009623 |
description | Detect if you are running as root, restart self with sudo or any other wrapper if needed or setup uid zero when running with the SUID flag set. |
homepage | |
repository | https://github.com/FyraLabs/karen |
max_upload_size | |
id | 1223407 |
size | 32,441 |
Escalate to your manager and get root access!
This is an extended fork of the sudo
and elevate
crates, which is a simple library to restart your process with sudo to escalate privileges.
This fork is a refactor of the original version, with the following changes:
Elevate
structpkexec
or polkit
as an alternative to sudo
by setting the wrapper from the builderThe API is a superset of the original sudo
crate, so you can use it as a drop-in replacement, but you can also use the new builder pattern to set your own options (currently only wrapper is supported)
The original sudo
crate can be found on GitLab (crates.io).
Detect if you are running as root, restart self with sudo
if needed or setup uid zero when running with the SUID flag set.
sudo
.doas
instead of sudo
on OpenBSD using the new builder pattern.First, add karen to your Cargo.toml
:
[dependencies]
karen = "0.6.1"
In your main.rs
:
fn main() -> Result<(), Box<dyn Error>> {
karen::escalate_if_needed()?;
println!("Hello, Root-World!");
Ok( () )
}
If you are using logging based on the log infrastructure you will get timestamped and formatted output.
The crate will automatically keep the setting of RUST_BACKTRACE
intact if it is set to one of the following values:
1
or true
<- standard tracefull
<- full trace$ RUST_BACKTRACE=full cargo run --example backtrace
2020-07-05 18:10:31,544 TRACE [karen] Running as User
2020-07-05 18:10:31,544 DEBUG [karen] Escalating privileges
2020-07-05 18:10:31,544 TRACE [karen] relaying RUST_BACKTRACE=full
[karen] Passwort für user:
2020-07-05 18:10:39,238 TRACE [karen] Running as Root
2020-07-05 18:10:39,238 TRACE [karen] already running as Root
2020-07-05 18:10:39,238 INFO [backtrace] entering failing_function
thread 'main' panicked at 'now you see me fail', examples/backtrace.rs:16:5
You can keep parts of your environment across the sudo barrier. This enables more configuration options often used in daemons or cloud environments:
// keeping all environment variables starting with "EXAMPLE_" or "CARGO"
karen::with_env(&["EXAMPLE_", "CARGO"]).expect("sudo failed");
Warning: This may introduce security problems to your application if untrusted users are able to set these variables.
$ EXAMPLE_EXEC='$(ls)' EXAMPLE_BTICKS='`ls`' cargo run --example environment
2020-07-07 16:32:11,261 INFO [environment] ① uid: 1000; euid: 1000;
...
declare -x EXAMPLE_BTICKS="\`ls\`"
declare -x EXAMPLE_EXEC="\$(ls)"
...
[karen] password for user:
2020-07-07 16:32:11,285 TRACE [karen] Running as Root
2020-07-07 16:32:11,285 TRACE [karen] already running as Root
2020-07-07 16:32:11,285 INFO [environment] ② uid: 0; euid: 0;
...
declare -x EXAMPLE_BTICKS="\`ls\`"
declare -x EXAMPLE_EXEC="\$(ls)"
$ cargo run --example suid
2020-04-17 15:13:49,450 INFO [suid] ① uid: 1000; euid: 1000;
uid=1000(user) gid=1000(user) groups=1000(user),4(adm),27(sudo)
2020-04-17 15:13:49,453 TRACE [karen] Running as User
2020-04-17 15:13:49,453 DEBUG [karen] Escalating privileges
[karen] password for user:
2020-04-17 15:13:53,529 INFO [suid] ① uid: 0; euid: 0;
uid=0(root) gid=0(root) groups=0(root)
2020-04-17 15:13:53,532 TRACE [karen] Running as Root
2020-04-17 15:13:53,532 TRACE [karen] already running as Root
2020-04-17 15:13:53,532 INFO [suid] ② uid: 0; euid: 0;
uid=0(root) gid=0(root) groups=0(root)
Then give the file to root
and add the suid flag.
$ sudo chown root target/debug/examples/suid
$ sudo chmod 4755 target/debug/examples/suid
Now run the program again:
$ target/debug/examples/suid
2020-04-17 15:14:37,199 INFO [suid] ① uid: 1000; euid: 0;
uid=1000(user) gid=1000(user) euid=0(root) groups=1000(user),4(adm),27(sudo)
2020-04-17 15:14:37,202 TRACE [karen] Running as Suid
2020-04-17 15:14:37,202 TRACE [karen] setuid(0)
2020-04-17 15:14:37,202 INFO [suid] ② uid: 0; euid: 0;
uid=0(root) gid=1000(user) groups=1000(user),4(adm),27(sudo)