use std::collections::{BTreeMap, BTreeSet};
use serde::Serialize;
use super::model::{DetectionClassification, EvidenceCategory, EvidenceKind, PolicyMetadata};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct ScoreCap {
pub min: i16,
pub max: i16,
}
impl ScoreCap {
pub const fn new(min: i16, max: i16) -> Self {
Self { min, max }
}
pub fn clamp(&self, value: i16) -> i16 {
value.clamp(self.min, self.max)
}
}
#[derive(Debug, Clone, Serialize)]
pub struct DnsRuleSet {
pub min_successful_ratio: f32,
pub min_cloudflare_ratio: f32,
pub negative_when_no_cloudflare: bool,
}
impl Default for DnsRuleSet {
fn default() -> Self {
Self {
min_successful_ratio: 0.50,
min_cloudflare_ratio: 0.50,
negative_when_no_cloudflare: true,
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct ClassificationRuleSet {
pub cloudflare_threshold: i16,
pub not_cloudflare_threshold: i16,
pub require_host_specific_positive: bool,
pub host_specific_positive_kinds: BTreeSet<EvidenceKind>,
pub allow_ip_only_classification: bool,
}
impl Default for ClassificationRuleSet {
fn default() -> Self {
let mut kinds = BTreeSet::new();
kinds.insert(EvidenceKind::DnsResolvesToCloudflare);
kinds.insert(EvidenceKind::DnsCnameToCloudflare);
kinds.insert(EvidenceKind::TlsCertificateHostnameMatch);
kinds.insert(EvidenceKind::HttpCfRay);
kinds.insert(EvidenceKind::HttpCfCacheStatus);
kinds.insert(EvidenceKind::HttpServerCloudflare);
kinds.insert(EvidenceKind::HttpCfConnectingIp);
kinds.insert(EvidenceKind::HttpCfMitigated);
Self {
cloudflare_threshold: 65,
not_cloudflare_threshold: -50,
require_host_specific_positive: true,
host_specific_positive_kinds: kinds,
allow_ip_only_classification: true,
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct ConfidenceRuleSet {
pub positive_base: f32,
pub positive_divisor: f32,
pub negative_base: f32,
pub negative_divisor: f32,
pub max_confidence: f32,
pub very_high_threshold: f32,
pub high_threshold: f32,
pub medium_threshold: f32,
}
impl Default for ConfidenceRuleSet {
fn default() -> Self {
Self {
positive_base: 0.50,
positive_divisor: 200.0,
negative_base: 0.50,
negative_divisor: 200.0,
max_confidence: 0.99,
very_high_threshold: 0.95,
high_threshold: 0.85,
medium_threshold: 0.70,
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct RuleSet {
pub weights: BTreeMap<EvidenceKind, i16>,
pub category_caps: BTreeMap<EvidenceCategory, ScoreCap>,
pub dns: DnsRuleSet,
pub classification: ClassificationRuleSet,
pub confidence: ConfidenceRuleSet,
pub overall_cap: ScoreCap,
}
impl RuleSet {
pub fn weight(&self, kind: EvidenceKind) -> i16 {
self.weights.get(&kind).copied().unwrap_or(0)
}
pub fn category_cap(&self, category: EvidenceCategory) -> ScoreCap {
self.category_caps
.get(&category)
.copied()
.unwrap_or(ScoreCap::new(-100, 100))
}
pub fn cloudflare_web_proxy_v1() -> Self {
let mut weights = BTreeMap::new();
weights.insert(EvidenceKind::CloudflareIpRange, 80);
weights.insert(EvidenceKind::IpOutsideCloudflareRange, -100);
weights.insert(EvidenceKind::DnsResolvesToCloudflare, 25);
weights.insert(EvidenceKind::DnsResolverConsensus, 10);
weights.insert(EvidenceKind::DnsNoCloudflareResolution, -10);
weights.insert(EvidenceKind::DnsCnameToCloudflare, 20);
weights.insert(EvidenceKind::DnsCnameChain, 5);
weights.insert(EvidenceKind::TlsHandshakeSucceeded, 5);
weights.insert(EvidenceKind::TlsCertificateHostnameMatch, 10);
weights.insert(EvidenceKind::TlsCertificateHostnameMismatch, -5);
weights.insert(EvidenceKind::TlsCertificateVerified, 5);
weights.insert(EvidenceKind::TlsCertificateVerificationUnavailable, 0);
weights.insert(EvidenceKind::HttpCfRay, 35);
weights.insert(EvidenceKind::HttpCfCacheStatus, 5);
weights.insert(EvidenceKind::HttpServerCloudflare, 5);
weights.insert(EvidenceKind::HttpCfConnectingIp, 1);
weights.insert(EvidenceKind::HttpCfIpCountry, 1);
weights.insert(EvidenceKind::HttpCfMitigated, 2);
weights.insert(EvidenceKind::HttpNoCloudflareSignals, 0);
let mut caps = BTreeMap::new();
caps.insert(EvidenceCategory::Network, ScoreCap::new(-100, 80));
caps.insert(EvidenceCategory::Dns, ScoreCap::new(-35, 60));
caps.insert(EvidenceCategory::Tls, ScoreCap::new(-20, 20));
caps.insert(EvidenceCategory::Http, ScoreCap::new(0, 45));
Self {
weights,
category_caps: caps,
dns: DnsRuleSet {
min_successful_ratio: 0.50,
min_cloudflare_ratio: 0.50,
negative_when_no_cloudflare: true,
},
classification: ClassificationRuleSet::default(),
confidence: ConfidenceRuleSet::default(),
overall_cap: ScoreCap::new(-100, 100),
}
}
}
pub trait DetectionPolicy: Send + Sync {
fn metadata(&self) -> &PolicyMetadata;
fn rules(&self) -> &RuleSet;
fn positive_classification(&self) -> DetectionClassification {
DetectionClassification::Cloudflare
}
fn negative_classification(&self) -> DetectionClassification {
DetectionClassification::NotCloudflare
}
}
#[derive(Debug, Clone)]
pub struct CloudflareWebProxyV1 {
metadata: PolicyMetadata,
rules: RuleSet,
}
impl Default for CloudflareWebProxyV1 {
fn default() -> Self {
Self {
metadata:
PolicyMetadata {
id:
"cloudflare-web-proxy"
.to_string(),
version:
1,
name:
"Cloudflare Web Proxy V1"
.to_string(),
description:
"Evidence policy for identifying web traffic served through Cloudflare's published edge network"
.to_string(),
},
rules:
RuleSet::
cloudflare_web_proxy_v1(),
}
}
}
impl DetectionPolicy for CloudflareWebProxyV1 {
fn metadata(&self) -> &PolicyMetadata {
&self.metadata
}
fn rules(&self) -> &RuleSet {
&self.rules
}
}
impl CloudflareWebProxyV1 {
pub fn rules_mut(&mut self) -> &mut RuleSet {
&mut self.rules
}
pub fn metadata(&self) -> &PolicyMetadata {
&self.metadata
}
}