gossan-hidden 0.3.1

Hidden endpoint and misconfiguration scanner for gossan (CORS, SSRF, JWT, Swagger, cache deception) — part of the security research ecosystem
Documentation
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#![forbid(unsafe_code)]
// pedantic moved to workspace [lints.clippy] in root Cargo.toml
#![cfg_attr(
    not(test),
    deny(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::todo,
        clippy::unimplemented,
        clippy::panic
    )
)]
#![allow(
    clippy::module_name_repetitions,
    clippy::must_use_candidate,
    clippy::missing_errors_doc
)]

//! Hidden endpoint/file probe scanner.
//!
//! Probes each web asset for:
//!   .git/.env/backup exposure · GraphQL introspection + batching · Swagger/OpenAPI ·
//!   CORS misconfiguration · WAF fingerprint · favicon hash ·
//!   robots.txt/sitemap harvest · OAuth/OIDC misconfiguration · API version enumeration ·
//!   rate limit absence · cookie security flags · tech-specific CMS probes ·
//!   dependency confusion · 403 bypass · directory brute-force

mod api_versions;
pub mod backup_files;
mod bypass403;
mod cookies;
pub mod cors;
pub mod csp;
mod debug_endpoints;
pub mod dependency_confusion;
pub mod directory_brute;
mod error_disclosure;
mod favicon;
pub mod git_env;
pub mod graphql;
mod methods;
mod oauth;
mod path_sanitize;
mod rate_limit;
pub mod robots;
mod security_headers;
mod sitemap;
mod soft404;
pub mod swagger;
mod tech_probes;
mod waf;

use async_trait::async_trait;
use futures::StreamExt;
use gossan_core::{Config, ScanClient, ScanInput, Scanner, Target};
use secfinding::{Evidence, Finding, FindingBuilder, Severity};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{RwLock, Semaphore};

/// Maximum concurrent in-flight requests to a single host.
const PER_HOST_CONCURRENCY: usize = 4;

/// Maximum response body size to read into memory (10 MiB).
pub const MAX_BODY_BYTES: usize = 10 * 1024 * 1024;

/// Per-host rate limiter that enforces a minimum delay between requests
/// and exponential backoff on 429/503.
pub struct HostRateLimiter {
    /// Map of hostname to last request time.
    last_request: RwLock<HashMap<String, Instant>>,
    /// Minimum delay between requests to the same host.
    delay: Duration,
    /// Current backoff multiplier per host.
    backoff: RwLock<HashMap<String, Duration>>,
}

impl HostRateLimiter {
    /// Create a new rate limiter with the specified delay between requests.
    #[must_use]
    pub fn new(delay_ms: u64) -> Self {
        Self {
            last_request: RwLock::new(HashMap::new()),
            delay: Duration::from_millis(delay_ms),
            backoff: RwLock::new(HashMap::new()),
        }
    }

    /// Wait until it's safe to send a request to the given host.
    pub async fn wait_for_host(&self, host: &str) {
        if self.delay.is_zero() {
            return;
        }

        let now = Instant::now();
        let should_sleep = {
            let read_guard = self.last_request.read().await;
            let backoff_guard = self.backoff.read().await;
            let effective_delay = self.delay + backoff_guard.get(host).copied().unwrap_or_default();
            if let Some(last) = read_guard.get(host) {
                let elapsed = now.duration_since(*last);
                if elapsed < effective_delay {
                    Some(effective_delay - elapsed)
                } else {
                    None
                }
            } else {
                None
            }
        };

        if let Some(sleep_duration) = should_sleep {
            tokio::time::sleep(sleep_duration).await;
        }

        let mut write_guard = self.last_request.write().await;
        write_guard.insert(host.to_string(), Instant::now());
    }

    /// Increase backoff on 429/503 responses.
    pub async fn observe_status(&self, host: &str, status: u16) {
        if status == 429 || status == 503 {
            let mut backoff_guard = self.backoff.write().await;
            let current = backoff_guard.get(host).copied().unwrap_or_default();
            let next = if current.is_zero() {
                self.delay
            } else {
                current * 2
            };
            // Cap at 60 seconds
            let capped = next.min(Duration::from_secs(60));
            backoff_guard.insert(host.to_string(), capped);
        }
    }

    /// Decay backoff after a successful request.
    pub async fn decay_backoff(&self, host: &str) {
        let mut backoff_guard = self.backoff.write().await;
        if let Some(current) = backoff_guard.get(host).copied() {
            let next = current / 2;
            if next < self.delay {
                backoff_guard.remove(host);
            } else {
                backoff_guard.insert(host.to_string(), next);
            }
        }
    }

    /// Get the configured base delay.
    #[must_use]
    pub fn delay(&self) -> Duration {
        self.delay
    }
}

/// `HiddenScanner`.
pub struct HiddenScanner;

pub(crate) fn finding_builder(
    target: &Target,
    severity: Severity,
    title: impl Into<String>,
    detail: impl Into<String>,
) -> FindingBuilder {
    Finding::builder("hidden", target.domain().unwrap_or("?"), severity)
        .title(title)
        .detail(detail)
}

/// Build a finding with an explicit [`FindingKind`].
pub(crate) fn finding_builder_typed(
    target: &Target,
    severity: Severity,
    kind: secfinding::FindingKind,
    title: impl Into<String>,
    detail: impl Into<String>,
) -> FindingBuilder {
    Finding::builder("hidden", target.domain().unwrap_or("?"), severity)
        .title(title)
        .detail(detail)
        .kind(kind)
}

/// Vulnerability finding (confirmed exploit path).
pub(crate) fn vulnerability_finding(
    target: &Target,
    severity: Severity,
    title: impl Into<String>,
    detail: impl Into<String>,
) -> FindingBuilder {
    finding_builder_typed(
        target,
        severity,
        secfinding::FindingKind::Vulnerability,
        title,
        detail,
    )
}

/// Misconfiguration finding (CORS, CSP, headers).
pub(crate) fn misconfig_finding(
    target: &Target,
    severity: Severity,
    title: impl Into<String>,
    detail: impl Into<String>,
) -> FindingBuilder {
    finding_builder_typed(
        target,
        severity,
        secfinding::FindingKind::Misconfiguration,
        title,
        detail,
    )
}

/// Exposure finding (open endpoints, admin panels).
pub(crate) fn exposure_finding(
    target: &Target,
    severity: Severity,
    title: impl Into<String>,
    detail: impl Into<String>,
) -> FindingBuilder {
    finding_builder_typed(
        target,
        severity,
        secfinding::FindingKind::Exposure,
        title,
        detail,
    )
}

/// File discovery finding (backup files, .git, .env).
pub(crate) fn file_finding(
    target: &Target,
    severity: Severity,
    title: impl Into<String>,
    detail: impl Into<String>,
) -> FindingBuilder {
    finding_builder_typed(
        target,
        severity,
        secfinding::FindingKind::FileDiscovery,
        title,
        detail,
    )
}

/// Info disclosure finding (stack traces, server versions).
pub(crate) fn info_finding(
    target: &Target,
    severity: Severity,
    title: impl Into<String>,
    detail: impl Into<String>,
) -> FindingBuilder {
    finding_builder_typed(
        target,
        severity,
        secfinding::FindingKind::InfoDisclosure,
        title,
        detail,
    )
}

/// Tech detect finding (framework/language fingerprint).
pub(crate) fn tech_finding(
    target: &Target,
    title: impl Into<String>,
    detail: impl Into<String>,
) -> FindingBuilder {
    finding_builder_typed(
        target,
        Severity::Info,
        secfinding::FindingKind::TechDetect,
        title,
        detail,
    )
}

/// Supply chain finding (dependency confusion, typosquat).
pub(crate) fn supply_chain_finding(
    target: &Target,
    severity: Severity,
    title: impl Into<String>,
    detail: impl Into<String>,
) -> FindingBuilder {
    finding_builder_typed(
        target,
        severity,
        secfinding::FindingKind::SupplyChain,
        title,
        detail,
    )
}

/// Secret leak finding (exposed API keys, tokens).
pub(crate) fn secret_finding(
    target: &Target,
    severity: Severity,
    title: impl Into<String>,
    detail: impl Into<String>,
) -> FindingBuilder {
    finding_builder_typed(
        target,
        severity,
        secfinding::FindingKind::SecretLeak,
        title,
        detail,
    )
}

/// Build a finding from a FindingBuilder and push it into the provided findings
/// vector. Any build error is logged and the finding is skipped instead of
/// panicking; probes should be resilient to builder failures.
pub(crate) fn try_push_finding(builder: FindingBuilder, findings: &mut Vec<Finding>) {
    match builder.build() {
        Ok(f) => findings.push(f),
        Err(e) => tracing::warn!(error = %e, "finding builder failed; skipping finding"),
    }
}

#[async_trait]
impl Scanner for HiddenScanner {
    fn name(&self) -> &'static str {
        "hidden"
    }
    fn tags(&self) -> &[&'static str] {
        &["active", "web", "hidden"]
    }
    fn accepts(&self, target: &Target) -> bool {
        matches!(target, Target::Web(_))
    }

    async fn run(&self, input: ScanInput, config: &Config) -> anyhow::Result<()> {
        let client_no_redir =
            ScanClient::from_config_no_redirect(config, Arc::clone(&input.resolver))?;
        let client_follow = ScanClient::from_config(config, Arc::clone(&input.resolver))?;
        let rate_limiter = Arc::new(HostRateLimiter::new(config.host_delay_ms));

        // Drain the streaming target receiver into a Vec. Hidden's per-host
        // probe layout (futures::stream::iter + buffer_unordered + per-host
        // semaphore) is batch-shaped, not incremental — collect first, then
        // fan out probes per accepted Web target.
        let owned: Vec<Target> = {
            let mut rx = input.target_rx.lock().await;
            let mut buf = Vec::new();
            while let Ok(t) = rx.try_recv() {
                if self.accepts(&t) {
                    buf.push(t);
                }
            }
            buf
        };

        let findings: Vec<Vec<Finding>> = futures::stream::iter(owned)
            .map(|target| {
                let cn = client_no_redir.clone();
                let cf = client_follow.clone();
                let rl = Arc::clone(&rate_limiter);
                async move {
                    let mut f = Vec::new();
                    let host = target.domain().unwrap_or("").to_string();
                    let semaphore = Arc::new(Semaphore::new(PER_HOST_CONCURRENCY));

                    // Establish a shared soft-404 baseline for this target
                    let baseline = if let Target::Web(asset) = &target {
                        let base = asset.url.as_str().trim_end_matches('/');
                        soft404::establish(&cn, base).await
                    } else {
                        None
                    };
                    let baseline = Arc::new(baseline);

                    let mut probes = futures::stream::FuturesUnordered::new();

                    macro_rules! spawn_probe {
                        ($name:expr, $client:expr, $target:expr) => {
                            {
                                let rl_inner = Arc::clone(&rl);
                                let host_inner = host.clone();
                                let target_inner = $target.clone();
                                let client_inner = $client.clone();
                                let sem_inner = Arc::clone(&semaphore);
                                let baseline_inner = Arc::clone(&baseline);
                                probes.push(tokio::spawn(async move {
                                    let _permit = sem_inner.acquire().await;
                                    rl_inner.wait_for_host(&host_inner).await;
                                    let result = match $name {
                                        "git_env" => git_env::probe(&client_inner, &target_inner).await,
                                        "swagger" => swagger::probe(&client_inner, &target_inner, baseline_inner.as_ref().as_ref()).await,
                                        "cookies" => cookies::probe(&client_inner, &target_inner).await,
                                        "graphql" => graphql::probe(&client_inner, &target_inner, baseline_inner.as_ref().as_ref()).await,
                                        "cors" => cors::probe(&client_inner, &target_inner).await,
                                        "csp" => csp::probe(&client_inner, &target_inner).await,
                                        "api_versions" => api_versions::probe(&client_inner, &target_inner).await,
                                        "methods" => methods::probe(&client_inner, &target_inner).await,
                                        "rate_limit" => rate_limit::probe(&client_inner, &target_inner).await,
                                        "security_headers" => security_headers::probe(&client_inner, &target_inner).await,
                                        "debug_endpoints" => debug_endpoints::probe(&client_inner, &target_inner).await,
                                        "error_disclosure" => error_disclosure::probe(&client_inner, &target_inner).await,
                                        "robots" => robots::probe(&client_inner, &target_inner).await,
                                        "sitemap" => sitemap::probe(&client_inner, &target_inner).await,
                                        "favicon" => favicon::probe(&client_inner, &target_inner).await,
                                        "waf" => waf::probe(&client_inner, &target_inner).await,
                                        "tech_probes" => {
                                            // tech_probes::probe returns
                                            // Vec<Finding> directly (no Result),
                                            // unlike the other probes. Wrap so
                                            // the match arms agree on
                                            // anyhow::Result<Vec<Finding>>.
                                            if let Target::Web(asset) = &target_inner {
                                                Ok(tech_probes::probe(&client_inner, asset, &target_inner).await)
                                            } else {
                                                Ok::<Vec<Finding>, anyhow::Error>(Vec::new())
                                            }
                                        }
                                        "debug_endpoints_follow" => debug_endpoints::probe(&client_inner, &target_inner).await,
                                        "directory_brute" => {
                                            let words = directory_brute::load_wordlist(None);
                                            let exts = directory_brute::extensions(&[]);
                                            let codes = directory_brute::status_codes(&[]);
                                            Ok(directory_brute::probe(&client_inner, &target_inner, &words, &exts, &codes, baseline_inner.as_ref().as_ref()).await)
                                        }
                                        "bypass403" => bypass403::probe(&client_inner, &target_inner).await,
                                        "oauth" => oauth::probe(&client_inner, &target_inner).await,
                                        "dependency_confusion" => dependency_confusion::probe(&client_inner, &target_inner).await,
                                        "backup_files" => backup_files::probe(&client_inner, &target_inner).await,
                                        _ => Ok(Vec::new()),
                                    };
                                    // Update rate-limiter state based on response evidence
                                    if let Ok(ref findings) = result {
                                        for finding in findings {
                                            for ev in finding.evidence() {
                                                if let Evidence::HttpResponse { status, .. } = ev {
                                                    rl_inner.observe_status(&host_inner, *status).await;
                                                }
                                            }
                                        }
                                    }
                                    result.unwrap_or_else(|e| {
                                        tracing::warn!(probe = $name, err = %e, "probe error");
                                        Vec::new()
                                    })
                                }));
                            }
                        };
                    }

                    spawn_probe!("git_env", cn, target);
                    spawn_probe!("swagger", cn, target);
                    spawn_probe!("cookies", cn, target);
                    spawn_probe!("graphql", cn, target);
                    spawn_probe!("cors", cn, target);
                    spawn_probe!("csp", cn, target);
                    spawn_probe!("api_versions", cn, target);
                    spawn_probe!("methods", cn, target);
                    spawn_probe!("rate_limit", cn, target);
                    spawn_probe!("security_headers", cn, target);
                    spawn_probe!("debug_endpoints", cn, target);
                    spawn_probe!("error_disclosure", cn, target);
                    spawn_probe!("robots", cf, target);
                    spawn_probe!("sitemap", cf, target);
                    spawn_probe!("favicon", cf, target);
                    spawn_probe!("waf", cf, target);
                    spawn_probe!("tech_probes", cf, target);
                    spawn_probe!("debug_endpoints_follow", cf, target);
                    spawn_probe!("directory_brute", cf, target);
                    spawn_probe!("bypass403", cn, target);
                    spawn_probe!("oauth", cn, target);
                    spawn_probe!("dependency_confusion", cn, target);
                    spawn_probe!("backup_files", cn, target);

                    while let Some(res) = probes.next().await {
                        if let Ok(mut probe_findings) = res {
                            f.append(&mut probe_findings);
                        }
                    }

                    f
                }
            })
            .buffer_unordered(config.concurrency)
            .collect()
            .await;

        for batch in findings {
            for finding in batch {
                input.emit(finding);
            }
        }
        Ok(())
    }
}

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

    #[tokio::test]
    async fn host_rate_limiter_respects_delay() {
        let limiter = HostRateLimiter::new(100); // 100ms delay
        let _start = Instant::now();
        limiter.wait_for_host("example.com").await;
        let first = Instant::now();
        limiter.wait_for_host("example.com").await;
        let second = Instant::now();
        let elapsed = second.duration_since(first);
        assert!(
            elapsed >= Duration::from_millis(100),
            "Rate limiter did not enforce delay"
        );

        let third_start = Instant::now();
        limiter.wait_for_host("other.com").await;
        let third_elapsed = Instant::now().duration_since(third_start);
        assert!(
            third_elapsed < Duration::from_millis(50),
            "Different host was incorrectly delayed"
        );
    }

    #[tokio::test]
    async fn host_rate_limiter_zero_delay_no_wait() {
        let limiter = HostRateLimiter::new(0);
        let start = Instant::now();
        limiter.wait_for_host("example.com").await;
        limiter.wait_for_host("example.com").await;
        limiter.wait_for_host("example.com").await;
        let elapsed = Instant::now().duration_since(start);
        assert!(
            elapsed < Duration::from_millis(50),
            "Zero delay should not wait"
        );
    }

    #[tokio::test]
    async fn host_rate_limiter_exponential_backoff() {
        let limiter = HostRateLimiter::new(100);
        limiter.observe_status("example.com", 429).await;
        let start = Instant::now();
        limiter.wait_for_host("example.com").await;
        limiter.wait_for_host("example.com").await;
        let elapsed = Instant::now().duration_since(start);
        // base 100ms + backoff 100ms = 200ms effective, so two requests should be >= 200ms apart
        assert!(elapsed >= Duration::from_millis(200), "Backoff not applied");
    }
}