repositories

percival

Follow the trail a handle leaves across the web: username search across 5,700+ sites, in Rust

clonegit clone https://git.daemon-sec.xyz/percival.git
result.rsRust213 lines5.7 KiBraw download
//! Result types produced by the scanner.

use serde::{Deserialize, Serialize};

use crate::archive::ArchiveHit;
use crate::db::Source;
use crate::extract::Profile;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Status {
    Found,
    NotFound,
    Blocked,
    Error,
    Unknown,
    /// The account cannot exist on this site (fails its username rules).
    Illegal,
}

impl Status {
    pub fn label(self) -> &'static str {
        match self {
            Status::Found => "found",
            Status::NotFound => "not found",
            Status::Blocked => "blocked",
            Status::Error => "error",
            Status::Unknown => "unknown",
            Status::Illegal => "invalid",
        }
    }

    pub fn glyph(self) -> &'static str {
        match self {
            Status::Found => "✔",
            Status::NotFound => "✘",
            Status::Blocked => "⛔",
            Status::Error => "✖",
            Status::Unknown => "?",
            Status::Illegal => "∅",
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Confidence {
    Low,
    Medium,
    High,
}

impl Confidence {
    pub fn label(self) -> &'static str {
        match self {
            Confidence::Low => "low",
            Confidence::Medium => "medium",
            Confidence::High => "high",
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TargetKind {
    #[default]
    Username,
    Email,
}

impl TargetKind {
    pub fn label(self) -> &'static str {
        match self {
            TargetKind::Username => "username",
            TargetKind::Email => "email",
        }
    }
}

/// Outcome of checking one target against one site.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ScanResult {
    pub target: String,
    pub kind: TargetKind,
    pub depth: u8,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub via: Option<String>,
    pub site: String,
    pub category: String,
    pub tags: Vec<String>,
    pub source: Source,
    pub url: String,
    pub probe_url: String,
    pub status: Status,
    pub confidence: Confidence,
    /// Canary verification: `Some(true)` passed, `Some(false)` the canary was
    /// also "found" (detector unreliable), `None` not verified.
    pub verified: Option<bool>,
    pub reason: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub http_status: Option<u16>,
    pub elapsed_ms: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub profile: Option<Profile>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub archive: Option<ArchiveHit>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

impl ScanResult {
    /// A confirmed hit (found and not contradicted by the canary).
    pub fn is_hit(&self) -> bool {
        self.status == Status::Found && self.verified != Some(false)
    }

    /// Found, but the canary account was found too — likely a false positive.
    pub fn is_suspicious(&self) -> bool {
        self.status == Status::Found && self.verified == Some(false)
    }

    /// Bucket used for counting and colouring.
    pub fn bucket(&self) -> Bucket {
        if self.is_suspicious() {
            return Bucket::Suspicious;
        }
        match self.status {
            Status::Found => Bucket::Found,
            Status::NotFound => Bucket::NotFound,
            Status::Blocked => Bucket::Blocked,
            Status::Error => Bucket::Error,
            Status::Unknown => Bucket::Unknown,
            Status::Illegal => Bucket::Illegal,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Bucket {
    Found,
    Suspicious,
    NotFound,
    Blocked,
    Error,
    Unknown,
    Illegal,
}

impl Bucket {
    pub fn label(self) -> &'static str {
        match self {
            Bucket::Found => "found",
            Bucket::Suspicious => "suspicious",
            Bucket::NotFound => "not found",
            Bucket::Blocked => "blocked",
            Bucket::Error => "error",
            Bucket::Unknown => "unknown",
            Bucket::Illegal => "invalid",
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Stats {
    pub total: usize,
    pub found: usize,
    pub suspicious: usize,
    pub not_found: usize,
    pub blocked: usize,
    pub errors: usize,
    pub unknown: usize,
    pub illegal: usize,
}

impl Stats {
    pub fn from_results(results: &[ScanResult]) -> Stats {
        let mut s = Stats::default();
        for r in results {
            s.add(r);
        }
        s
    }

    pub fn add(&mut self, r: &ScanResult) {
        self.total += 1;
        match r.bucket() {
            Bucket::Found => self.found += 1,
            Bucket::Suspicious => self.suspicious += 1,
            Bucket::NotFound => self.not_found += 1,
            Bucket::Blocked => self.blocked += 1,
            Bucket::Error => self.errors += 1,
            Bucket::Unknown => self.unknown += 1,
            Bucket::Illegal => self.illegal += 1,
        }
    }

    pub fn merge(&mut self, o: &Stats) {
        self.total += o.total;
        self.found += o.found;
        self.suspicious += o.suspicious;
        self.not_found += o.not_found;
        self.blocked += o.blocked;
        self.errors += o.errors;
        self.unknown += o.unknown;
        self.illegal += o.illegal;
    }

    /// Everything that produced a usable answer.
    pub fn answered(&self) -> usize {
        self.found + self.suspicious + self.not_found
    }
}