fix(ui): generate the sabia ui token from the OS CSPRNG
The token was the startup time in nanoseconds, guessable by another local user or process within a small window, which would hand over the API (captures, deauth, Evil Twin). It is now 32 random bytes from getrandom, checked in constant time. Release 1.0.6. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0ff945ef65
commit
bcc4178e89
9 files changed
+61
-16
No files matched your search
@@ -10,7 +10,6 @@ use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
const INDEX_HTML: &str = include_str!("../../../../apps/gui/ui/index.html");
|
||||
const APP_CSS: &str = include_str!("../../../../apps/gui/ui/app.css");
|
||||
@@ -54,12 +53,27 @@ pub fn serve(port: u16, audit_path: std::path::PathBuf, open: bool) -> sabia_cor
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 32 bytes from the operating system's CSPRNG, hex-encoded. The token used to
|
||||
/// be the startup time in nanoseconds, which anyone on the machine could guess
|
||||
/// within a small window and use to drive the API (captures, deauth, Evil Twin).
|
||||
fn gen_token() -> String {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_nanos();
|
||||
format!("{nanos:x}")
|
||||
let mut bytes = [0u8; 32];
|
||||
getrandom::fill(&mut bytes).expect("operating system random number generator");
|
||||
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
||||
/// Constant-time check, so response timing does not reveal how much of a
|
||||
/// guess was right.
|
||||
fn token_ok(candidate: Option<&str>, expected: &str) -> bool {
|
||||
let Some(candidate) = candidate else {
|
||||
return false;
|
||||
};
|
||||
let (candidate, expected) = (candidate.as_bytes(), expected.as_bytes());
|
||||
let mut diff = candidate.len() ^ expected.len();
|
||||
for (index, byte) in expected.iter().enumerate() {
|
||||
diff |= usize::from(byte ^ candidate.get(index).copied().unwrap_or(0));
|
||||
}
|
||||
diff == 0
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
@@ -230,7 +244,7 @@ fn handle(
|
||||
let req = parse_request(&mut stream)?;
|
||||
|
||||
if req.path == "/" {
|
||||
if req.token.as_deref() != Some(expected_token) {
|
||||
if !token_ok(req.token.as_deref(), expected_token) {
|
||||
return respond(&mut stream, 401, "text/plain", b"token invalido");
|
||||
}
|
||||
return respond(
|
||||
@@ -242,7 +256,7 @@ fn handle(
|
||||
}
|
||||
|
||||
if req.path == "/api/report.html" {
|
||||
if req.token.as_deref() != Some(expected_token) {
|
||||
if !token_ok(req.token.as_deref(), expected_token) {
|
||||
return respond(&mut stream, 401, "text/plain", b"token invalido");
|
||||
}
|
||||
let Some(iface) = req.text("iface") else {
|
||||
@@ -315,7 +329,7 @@ fn handle(
|
||||
if !req.path.starts_with("/api/") {
|
||||
return respond(&mut stream, 404, "text/plain", b"not found");
|
||||
}
|
||||
if req.token.as_deref() != Some(expected_token) {
|
||||
if !token_ok(req.token.as_deref(), expected_token) {
|
||||
return respond(
|
||||
&mut stream,
|
||||
401,
|
||||
@@ -834,6 +848,25 @@ fn api(engine: &Arc<Engine>, req: &Request) -> (u16, String) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[test]
|
||||
fn tokens_are_random_and_long() {
|
||||
let (a, b) = (gen_token(), gen_token());
|
||||
assert_eq!(a.len(), 64);
|
||||
assert!(a.bytes().all(|c| c.is_ascii_hexdigit()));
|
||||
assert_ne!(a, b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_check_needs_the_exact_value() {
|
||||
assert!(token_ok(Some("abc123"), "abc123"));
|
||||
assert!(!token_ok(Some("abc12"), "abc123"));
|
||||
assert!(!token_ok(Some("abc1234"), "abc123"));
|
||||
assert!(!token_ok(Some("abc124"), "abc123"));
|
||||
assert!(!token_ok(None, "abc123"));
|
||||
assert!(!token_ok(Some(""), "abc123"));
|
||||
}
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user