Skip to content

Commit eb359d0

Browse files
authored
UNIX: highlight files with elevated permissions (#33)
Highlight the filenames of files with SETUID or SETGID flags set. Also highlight files with Linux file capabilities.
1 parent a87fbec commit eb359d0

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ windows = {version = "0.42.0", features = [
4949
"Win32_System_Threading",
5050
]}
5151

52+
[target.'cfg(target_os="linux")'.dependencies]
53+
xattr = {version = "1.0.0", optional = true}
54+
5255
[lib]
5356
name = "checksec"
5457
path = "src/lib.rs"
@@ -58,7 +61,7 @@ name = "checksec"
5861
path = "src/main.rs"
5962

6063
[features]
61-
color = ["colored", "colored_json"]
64+
color = ["colored", "colored_json", "xattr"]
6265
default = ["elf", "macho", "pe", "color", "maps"]
6366
elf = ["shared"]
6467
macho = ["shared"]

src/binary.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#[cfg(feature = "color")]
22
use colored::Colorize;
33
use serde::{Deserialize, Serialize};
4+
#[cfg(all(feature = "color", not(target_os = "windows")))]
5+
use std::os::unix::fs::PermissionsExt;
6+
#[cfg(all(feature = "color", not(target_os = "windows")))]
7+
use std::path::Path;
48
use std::path::PathBuf;
59
use std::{fmt, usize};
610

@@ -108,13 +112,43 @@ impl fmt::Display for Binary {
108112
#[cfg(feature = "color")]
109113
impl fmt::Display for Binary {
110114
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115+
#[cfg(target_os = "windows")]
116+
let filefmt = self.file.display().to_string().bright_blue();
117+
#[cfg(not(target_os = "windows"))]
118+
let filefmt = match std::fs::metadata(&self.file) {
119+
Ok(md) => {
120+
#[cfg(target_os = "linux")]
121+
fn has_filecaps(file: &Path) -> bool {
122+
xattr::get(file, "security.capability")
123+
.unwrap_or(None)
124+
.is_some()
125+
}
126+
#[cfg(not(target_os = "linux"))]
127+
fn has_filecaps(_file: &Path) -> bool {
128+
false
129+
}
130+
131+
let mode = md.permissions().mode();
132+
if mode & 0o4000 == 0o4000 {
133+
self.file.display().to_string().white().on_red()
134+
} else if mode & 0o2000 == 0o2000 {
135+
self.file.display().to_string().black().on_yellow()
136+
} else if has_filecaps(&self.file) {
137+
self.file.display().to_string().black().on_blue()
138+
} else {
139+
self.file.display().to_string().bright_blue()
140+
}
141+
}
142+
Err(_) => self.file.display().to_string().bright_blue(),
143+
};
144+
111145
write!(
112146
f,
113147
"{}: | {} | {} {}",
114148
self.binarytype,
115149
self.properties,
116150
"File:".bold().underline(),
117-
self.file.display().to_string().bright_blue()
151+
filefmt
118152
)
119153
}
120154
}

0 commit comments

Comments
 (0)