cipherrun 0.3.0

A fast, modular, and scalable TLS/SSL security scanner written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// Advanced HTTP Security Headers Analysis
// HSTS, HPKP, Cookie flags, Date/Time, Banners, Reverse Proxy detection

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// HSTS (HTTP Strict Transport Security) detailed analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HstsAnalysis {
    pub enabled: bool,
    pub max_age: Option<u64>,
    pub include_subdomains: bool,
    pub preload: bool,
    pub details: String,
    pub grade: Grade,
}

/// HPKP (HTTP Public Key Pinning) analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HpkpAnalysis {
    pub enabled: bool,
    pub max_age: Option<u64>,
    pub include_subdomains: bool,
    pub report_uri: Option<String>,
    pub pins: Vec<String>,
    pub backup_pins: Vec<String>,
    pub details: String,
}

/// Cookie security flags analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CookieAnalysis {
    pub cookies: Vec<CookieInfo>,
    pub secure_count: usize,
    pub httponly_count: usize,
    pub samesite_count: usize,
    pub insecure_count: usize,
    pub details: String,
    pub grade: Grade,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CookieInfo {
    pub name: String,
    pub secure: bool,
    pub httponly: bool,
    pub samesite: Option<String>,
    pub domain: Option<String>,
    pub path: Option<String>,
    pub expires: Option<String>,
}

/// HTTP Date/Time check
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DateTimeCheck {
    pub server_date: Option<String>,
    pub skew_seconds: Option<i64>,
    pub synchronized: bool,
    pub details: String,
}

/// Application banner detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BannerDetection {
    pub server: Option<String>,
    pub powered_by: Option<String>,
    pub application: Option<String>,
    pub framework: Option<String>,
    pub version_exposed: bool,
    pub details: String,
    pub grade: Grade,
}

/// Reverse proxy detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReverseProxyDetection {
    pub detected: bool,
    pub via_header: Option<String>,
    pub x_forwarded_for: bool,
    pub x_real_ip: bool,
    pub x_forwarded_proto: bool,
    pub proxy_type: Option<String>,
    pub details: String,
}

/// Security grade
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Grade {
    A,
    B,
    C,
    D,
    F,
}

impl Grade {
    pub fn as_str(&self) -> &'static str {
        match self {
            Grade::A => "A",
            Grade::B => "B",
            Grade::C => "C",
            Grade::D => "D",
            Grade::F => "F",
        }
    }
}

/// Parse HSTS header
pub fn parse_hsts(headers: &HashMap<String, String>) -> HstsAnalysis {
    if let Some(hsts) = headers.get("strict-transport-security") {
        let mut max_age = None;
        let mut include_subdomains = false;
        let mut preload = false;

        // Parse directives
        for part in hsts.split(';') {
            let part = part.trim();
            if let Some(value) = part.strip_prefix("max-age=") {
                if let Ok(age) = value.parse::<u64>() {
                    max_age = Some(age);
                }
            } else if part.eq_ignore_ascii_case("includesubdomains") {
                include_subdomains = true;
            } else if part.eq_ignore_ascii_case("preload") {
                preload = true;
            }
        }

        let grade = grade_hsts(max_age, include_subdomains, preload);
        let details = format_hsts_details(max_age, include_subdomains, preload);

        HstsAnalysis {
            enabled: true,
            max_age,
            include_subdomains,
            preload,
            details,
            grade,
        }
    } else {
        HstsAnalysis {
            enabled: false,
            max_age: None,
            include_subdomains: false,
            preload: false,
            details: "HSTS not enabled - connections can be downgraded to HTTP".to_string(),
            grade: Grade::F,
        }
    }
}

fn grade_hsts(max_age: Option<u64>, include_subdomains: bool, preload: bool) -> Grade {
    match max_age {
        Some(age) if age >= 31536000 && include_subdomains && preload => Grade::A,
        Some(age) if age >= 31536000 && include_subdomains => Grade::B,
        Some(age) if age >= 15768000 => Grade::C,
        Some(_) => Grade::D,
        None => Grade::F,
    }
}

fn format_hsts_details(max_age: Option<u64>, include_subdomains: bool, preload: bool) -> String {
    let mut parts = Vec::new();

    if let Some(age) = max_age {
        let days = age / 86400;
        parts.push(format!("max-age={} ({} days)", age, days));
    }

    if include_subdomains {
        parts.push("includeSubDomains".to_string());
    }

    if preload {
        parts.push("preload".to_string());
    }

    if parts.is_empty() {
        "HSTS enabled but misconfigured".to_string()
    } else {
        format!("HSTS: {}", parts.join("; "))
    }
}

/// Parse HPKP header (deprecated but still checked)
pub fn parse_hpkp(headers: &HashMap<String, String>) -> HpkpAnalysis {
    if let Some(hpkp) = headers
        .get("public-key-pins")
        .or_else(|| headers.get("public-key-pins-report-only"))
    {
        let mut max_age = None;
        let mut include_subdomains = false;
        let mut report_uri = None;
        let mut pins = Vec::new();
        let mut backup_pins = Vec::new();

        for part in hpkp.split(';') {
            let part = part.trim();
            if let Some(value) = part.strip_prefix("max-age=") {
                if let Ok(age) = value.parse::<u64>() {
                    max_age = Some(age);
                }
            } else if part.eq_ignore_ascii_case("includesubdomains") {
                include_subdomains = true;
            } else if let Some(value) = part.strip_prefix("report-uri=") {
                report_uri = Some(value.trim_matches('"').to_string());
            } else if let Some(value) = part.strip_prefix("pin-sha256=") {
                pins.push(value.trim_matches('"').to_string());
            }
        }

        // Separate backup pins (just informational)
        if pins.len() > 1 {
            backup_pins = pins[1..].to_vec();
        }

        let details =
            "HPKP enabled (DEPRECATED - use Certificate Transparency instead)".to_string();

        HpkpAnalysis {
            enabled: true,
            max_age,
            include_subdomains,
            report_uri,
            pins: if pins.is_empty() {
                Vec::new()
            } else {
                vec![pins[0].clone()]
            },
            backup_pins,
            details,
        }
    } else {
        HpkpAnalysis {
            enabled: false,
            max_age: None,
            include_subdomains: false,
            report_uri: None,
            pins: Vec::new(),
            backup_pins: Vec::new(),
            details: "HPKP not enabled (good - it's deprecated)".to_string(),
        }
    }
}

/// Parse Set-Cookie headers for security flags
pub fn parse_cookies(set_cookie_headers: &[String]) -> CookieAnalysis {
    let mut cookies = Vec::new();
    let mut secure_count = 0;
    let mut httponly_count = 0;
    let mut samesite_count = 0;
    let mut insecure_count = 0;

    for cookie_str in set_cookie_headers {
        let cookie = parse_single_cookie(cookie_str);

        if cookie.secure {
            secure_count += 1;
        }
        if cookie.httponly {
            httponly_count += 1;
        }
        if cookie.samesite.is_some() {
            samesite_count += 1;
        }
        if !cookie.secure || !cookie.httponly || cookie.samesite.is_none() {
            insecure_count += 1;
        }

        cookies.push(cookie);
    }

    let grade = if insecure_count == 0 && !cookies.is_empty() {
        Grade::A
    } else if insecure_count < cookies.len() / 2 {
        Grade::B
    } else if insecure_count < cookies.len() {
        Grade::C
    } else {
        Grade::F
    };

    let details = format!(
        "{} cookie(s): {} Secure, {} HttpOnly, {} SameSite, {} insecure",
        cookies.len(),
        secure_count,
        httponly_count,
        samesite_count,
        insecure_count
    );

    CookieAnalysis {
        cookies,
        secure_count,
        httponly_count,
        samesite_count,
        insecure_count,
        details,
        grade,
    }
}

fn parse_single_cookie(cookie_str: &str) -> CookieInfo {
    let parts: Vec<&str> = cookie_str.split(';').collect();

    let name = if let Some(name_value) = parts.first() {
        name_value
            .split('=')
            .next()
            .unwrap_or("")
            .trim()
            .to_string()
    } else {
        "unknown".to_string()
    };

    let mut secure = false;
    let mut httponly = false;
    let mut samesite = None;
    let mut domain = None;
    let mut path = None;
    let mut expires = None;

    for part in parts.iter().skip(1) {
        let part = part.trim();
        if part.eq_ignore_ascii_case("Secure") {
            secure = true;
        } else if part.eq_ignore_ascii_case("HttpOnly") {
            httponly = true;
        } else if let Some(value) = part.strip_prefix("SameSite=") {
            samesite = Some(value.to_string());
        } else if let Some(value) = part.strip_prefix("Domain=") {
            domain = Some(value.to_string());
        } else if let Some(value) = part.strip_prefix("Path=") {
            path = Some(value.to_string());
        } else if let Some(value) = part.strip_prefix("Expires=") {
            expires = Some(value.to_string());
        }
    }

    CookieInfo {
        name,
        secure,
        httponly,
        samesite,
        domain,
        path,
        expires,
    }
}

/// Check HTTP Date/Time
pub fn check_datetime(headers: &HashMap<String, String>) -> DateTimeCheck {
    if let Some(date_str) = headers.get("date") {
        // Parse HTTP date format
        if let Ok(server_time) = chrono::DateTime::parse_from_rfc2822(date_str) {
            let now = chrono::Utc::now();
            let skew = (server_time.timestamp() - now.timestamp()).abs();

            let synchronized = skew < 300; // Within 5 minutes

            let details = if synchronized {
                format!("Server time synchronized (skew: {} seconds)", skew)
            } else {
                format!(
                    "Server time NOT synchronized (skew: {} seconds, {} minutes)",
                    skew,
                    skew / 60
                )
            };

            return DateTimeCheck {
                server_date: Some(date_str.clone()),
                skew_seconds: Some(skew),
                synchronized,
                details,
            };
        }
    }

    DateTimeCheck {
        server_date: None,
        skew_seconds: None,
        synchronized: false,
        details: "No Date header found".to_string(),
    }
}

/// Detect application banners
pub fn detect_banners(headers: &HashMap<String, String>) -> BannerDetection {
    let server = headers.get("server").cloned();
    let powered_by = headers.get("x-powered-by").cloned();
    let application = headers
        .get("x-aspnet-version")
        .or_else(|| headers.get("x-aspnetmvc-version"))
        .cloned();
    let framework = headers.get("x-framework").cloned();

    let version_exposed = server.as_ref().is_some_and(|s| s.contains('/'))
        || powered_by.as_ref().is_some_and(|p| p.contains('/'))
        || application.is_some();

    let grade = if !version_exposed && server.is_none() && powered_by.is_none() {
        Grade::A
    } else if !version_exposed {
        Grade::B
    } else {
        Grade::F
    };

    let details = format!(
        "Server: {}, X-Powered-By: {}, Version exposed: {}",
        server.as_deref().unwrap_or("not disclosed"),
        powered_by.as_deref().unwrap_or("not disclosed"),
        version_exposed
    );

    BannerDetection {
        server,
        powered_by,
        application,
        framework,
        version_exposed,
        details,
        grade,
    }
}

/// Detect reverse proxy
pub fn detect_reverse_proxy(headers: &HashMap<String, String>) -> ReverseProxyDetection {
    let via_header = headers.get("via").cloned();
    let x_forwarded_for = headers.contains_key("x-forwarded-for");
    let x_real_ip = headers.contains_key("x-real-ip");
    let x_forwarded_proto = headers.contains_key("x-forwarded-proto");

    let detected = via_header.is_some() || x_forwarded_for || x_real_ip || x_forwarded_proto;

    let proxy_type = if let Some(ref via) = via_header {
        if via.contains("nginx") {
            Some("nginx".to_string())
        } else if via.contains("Apache") {
            Some("Apache".to_string())
        } else if via.contains("HAProxy") {
            Some("HAProxy".to_string())
        } else if via.contains("Varnish") {
            Some("Varnish".to_string())
        } else {
            Some("Unknown".to_string())
        }
    } else if x_forwarded_for {
        Some("Generic reverse proxy".to_string())
    } else {
        None
    };

    let details = if detected {
        let mut parts = Vec::new();
        if let Some(ref via) = via_header {
            parts.push(format!("Via: {}", via));
        }
        if x_forwarded_for {
            parts.push("X-Forwarded-For".to_string());
        }
        if x_real_ip {
            parts.push("X-Real-IP".to_string());
        }
        if x_forwarded_proto {
            parts.push("X-Forwarded-Proto".to_string());
        }
        format!("Reverse proxy detected: {}", parts.join(", "))
    } else {
        "No reverse proxy detected".to_string()
    };

    ReverseProxyDetection {
        detected,
        via_header,
        x_forwarded_for,
        x_real_ip,
        x_forwarded_proto,
        proxy_type,
        details,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_hsts_full() {
        let mut headers = HashMap::new();
        headers.insert(
            "strict-transport-security".to_string(),
            "max-age=31536000; includeSubDomains; preload".to_string(),
        );

        let hsts = parse_hsts(&headers);
        assert!(hsts.enabled);
        assert_eq!(hsts.max_age, Some(31536000));
        assert!(hsts.include_subdomains);
        assert!(hsts.preload);
        assert_eq!(hsts.grade, Grade::A);
    }

    #[test]
    fn test_parse_cookie_secure() {
        let cookies = vec!["session=abc123; Secure; HttpOnly; SameSite=Strict".to_string()];

        let analysis = parse_cookies(&cookies);
        assert_eq!(analysis.secure_count, 1);
        assert_eq!(analysis.httponly_count, 1);
        assert_eq!(analysis.samesite_count, 1);
        assert_eq!(analysis.insecure_count, 0);
    }

    #[test]
    fn test_detect_reverse_proxy() {
        let mut headers = HashMap::new();
        headers.insert("via".to_string(), "1.1 nginx".to_string());
        headers.insert("x-forwarded-for".to_string(), "192.168.1.1".to_string());

        let detection = detect_reverse_proxy(&headers);
        assert!(detection.detected);
        assert_eq!(detection.proxy_type, Some("nginx".to_string()));
    }
}