rinzler-core 0.1.11-alpha

Core library for Rinzler - API scanner data models and database
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// Fuzzing module for forced browsing / directory enumeration

use colored::Colorize;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use reqwest::Client;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Mutex, Semaphore};
use url::Url;

/// Result of a fuzz attempt
#[derive(Debug, Clone)]
pub struct FuzzResult {
    pub url: String,
    pub status_code: u16,
    pub content_length: Option<u64>,
    pub content_type: Option<String>,
    pub source: FuzzSource,
}

/// Source of the fuzz target
#[derive(Debug, Clone, PartialEq)]
pub enum FuzzSource {
    Initial,    // From command line
    Database,   // From previous crawl
    Discovered, // Found during fuzzing
}

/// Options for configuring a fuzz operation
pub struct FuzzOptions {
    pub base_urls: Vec<String>,
    pub wordlist: Vec<String>,
    pub threads: usize,
    pub show_progress_bars: bool,
    pub use_head_requests: bool,
    pub timeout_secs: u64,
    pub db_path: Option<std::path::PathBuf>,
}

/// Execute fuzzing with given options
pub async fn execute_fuzz(options: FuzzOptions) -> Result<Vec<FuzzResult>, String> {
    let FuzzOptions {
        base_urls,
        wordlist,
        threads,
        show_progress_bars,
        use_head_requests,
        timeout_secs,
        db_path,
    } = options;

    if base_urls.is_empty() {
        return Err("No base URLs provided".to_string());
    }

    if wordlist.is_empty() {
        return Err("Wordlist is empty".to_string());
    }

    // Query database for known endpoints from previous crawls
    let mut db_endpoints = Vec::new();
    if let Some(ref db_path) = db_path {
        if let Ok(db_urls) = query_database_endpoints(db_path, &base_urls) {
            db_endpoints = db_urls;
            if !db_endpoints.is_empty() {
                println!(
                    "✓ Found {} endpoints from previous crawls in database",
                    db_endpoints.len()
                );
            }
        }
    }

    // Build initial base URLs with sources
    // Note: We add database URLs first, then command-line URLs
    // Since workers use pop() which takes from the end, this ensures
    // root routes (command-line) are tested BEFORE database endpoints
    let mut base_urls_with_source = Vec::new();

    // Add database URLs first (will be tested last)
    for url in &db_endpoints {
        base_urls_with_source.push((url.clone(), FuzzSource::Database));
    }

    // Add command-line URLs last (will be tested first due to pop())
    for url in &base_urls {
        base_urls_with_source.push((url.clone(), FuzzSource::Initial));
    }

    // Build full URLs to test
    let mut urls_to_test = Vec::new();
    for (base_url, source) in &base_urls_with_source {
        for word in &wordlist {
            let test_url = build_test_url(base_url, word)?;
            urls_to_test.push((test_url, source.clone()));
        }
    }

    let initial_count = urls_to_test.len();
    println!(
        "Testing {} initial URLs with {} workers",
        initial_count, threads
    );
    if !db_endpoints.is_empty() {
        println!(
            "  {} from command line, {} from database",
            base_urls.len() * wordlist.len(),
            db_endpoints.len() * wordlist.len()
        );
    }
    println!();

    // Set up multi-progress for worker tracking
    let m = if show_progress_bars {
        Some(Arc::new(MultiProgress::new()))
    } else {
        None
    };

    // Create shared results vector and hits display
    let results: Arc<Mutex<Vec<FuzzResult>>> = Arc::new(Mutex::new(Vec::new()));
    let hits_display: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));

    // Create dynamic work queue and tested URLs set
    let work_queue: Arc<Mutex<Vec<(String, FuzzSource)>>> = Arc::new(Mutex::new(urls_to_test));
    let tested_urls: Arc<Mutex<std::collections::HashSet<String>>> =
        Arc::new(Mutex::new(std::collections::HashSet::new()));
    let wordlist_arc = Arc::new(wordlist);

    // Create hits display progress bar (sticky at top)
    let hits_pb = if show_progress_bars && let Some(ref multi_progress) = m {
        let pb = multi_progress.add(ProgressBar::new(0));
        pb.set_style(ProgressStyle::default_bar().template("{msg}").unwrap());
        pb.set_message("Hits: 0".to_string());
        Some(Arc::new(pb))
    } else {
        None
    };

    // Create optimized HTTP client with HTTP/2 and connection pooling
    let client = Arc::new(
        Client::builder()
            .timeout(Duration::from_secs(timeout_secs))
            .connect_timeout(Duration::from_secs(timeout_secs / 2))
            .pool_max_idle_per_host(threads) // Connection pooling
            .pool_idle_timeout(Duration::from_secs(90))
            .http2_adaptive_window(true) // Enable HTTP/2 with adaptive flow control
            .tcp_keepalive(Duration::from_secs(60))
            .redirect(reqwest::redirect::Policy::limited(3))
            .user_agent("Rinzler/0.1 (https://github.com/trapdoorsec/rinzler)")
            .build()
            .map_err(|e| format!("Failed to create HTTP client: {}", e))?,
    );

    // Create semaphore to limit concurrent requests
    let semaphore = Arc::new(Semaphore::new(threads));

    // Spawn workers that pull from shared queue
    let mut worker_tasks = Vec::new();

    for worker_id in 0..threads {
        // Create progress bar for this worker
        let pb = if show_progress_bars && let Some(ref multi_progress) = m {
            let progress_bar = multi_progress.add(ProgressBar::new(initial_count as u64));
            progress_bar.set_style(
                ProgressStyle::default_bar()
                    .template("[{bar:40.cyan/blue}] Worker {msg}")
                    .unwrap()
                    .progress_chars("=>-"),
            );
            progress_bar.set_message(format!("{} idle", worker_id));
            Some(progress_bar)
        } else {
            None
        };

        let client_clone = client.clone();
        let results_clone = results.clone();
        let hits_display_clone = hits_display.clone();
        let hits_pb_clone = hits_pb.clone();
        let semaphore_clone = semaphore.clone();
        let work_queue_clone = work_queue.clone();
        let tested_urls_clone = tested_urls.clone();
        let wordlist_clone = wordlist_arc.clone();

        let task = tokio::spawn(async move {
            let mut processed = 0;

            loop {
                // Try to get work from queue
                let work_item = {
                    let mut queue = work_queue_clone.lock().await;
                    queue.pop()
                };

                let Some((url, source)) = work_item else {
                    break; // No more work
                };

                // Extract path for display
                let url_path = extract_path(&url);

                // Update progress bar with current URL in orange
                if let Some(ref pb) = pb {
                    processed += 1;
                    let msg = format!(
                        "{} {} {}",
                        worker_id,
                        processed,
                        format!("[{}]", url_path).truecolor(255, 165, 0) // Orange
                    );
                    pb.set_message(msg);
                }

                // Acquire semaphore permit
                let _permit = semaphore_clone.acquire().await.unwrap();

                // Make request
                if let Ok(mut result) =
                    make_fuzz_request(&client_clone, &url, use_head_requests).await
                {
                    result.source = source.clone();

                    // Save all responses < 500 to results for final report
                    if result.status_code < 500 {
                        results_clone.lock().await.push(result.clone());
                    }

                    // If we found a new endpoint (200-399), add it to queue for fuzzing
                    if (200..400).contains(&result.status_code) {
                        // Display the hit
                        let hit_display = format_hit(&result);
                        hits_display_clone.lock().await.push(hit_display);

                        // Update hits display area
                        if let Some(ref hits_pb) = hits_pb_clone {
                            let hits = hits_display_clone.lock().await;
                            let formatted = format_hits_display(&hits);
                            hits_pb.set_message(formatted);
                        }

                        // Extract base path for this discovered endpoint
                        if let Ok(base_url) = extract_base_url(&result.url) {
                            let mut tested = tested_urls_clone.lock().await;

                            // Only add if we haven't tested this base yet
                            if !tested.contains(&base_url) {
                                tested.insert(base_url.clone());

                                // Generate new fuzz targets from this discovered endpoint
                                let mut queue = work_queue_clone.lock().await;
                                for word in wordlist_clone.iter() {
                                    if let Ok(new_url) = build_test_url(&base_url, word) {
                                        queue.push((new_url, FuzzSource::Discovered));
                                    }
                                }
                            }
                        }
                    }
                }

                if let Some(ref pb) = pb {
                    pb.inc(1);
                }
            }

            if let Some(ref pb) = pb {
                pb.finish_with_message(format!("{}: done", worker_id));
            }
        });

        worker_tasks.push(task);
    }

    // Wait for all workers to complete
    for task in worker_tasks {
        task.await
            .map_err(|e| format!("Worker task failed: {}", e))?;
    }

    // Finalize hits display
    if let Some(ref hits_pb) = hits_pb {
        hits_pb.finish();
    }

    // Extract results
    let final_results = results.lock().await.clone();

    Ok(final_results)
}

/// Extract path component from URL for display
fn extract_path(url: &str) -> String {
    if let Ok(parsed) = Url::parse(url) {
        let path = parsed.path();
        if path.len() > 40 {
            format!("...{}", &path[path.len() - 37..])
        } else {
            path.to_string()
        }
    } else {
        url.to_string()
    }
}

/// Format a single hit for display
fn format_hit(result: &FuzzResult) -> String {
    let path = extract_path(&result.url);
    let status_colored = colorize_status_code(result.status_code);
    format!("{} {}", path, status_colored)
}

/// Colorize status code based on value
fn colorize_status_code(status: u16) -> String {
    let status_str = format!("[{}]", status);
    match status {
        200..=299 => status_str.green().to_string(),
        300..=399 => status_str.yellow().to_string(),
        400..=499 => status_str.red().to_string(),
        _ => status_str.white().to_string(),
    }
}

/// Format hits display with multi-column layout (max 10 per column)
fn format_hits_display(hits: &[String]) -> String {
    if hits.is_empty() {
        return "Hits: 0".to_string();
    }

    let total_hits = hits.len();
    let max_per_column = 10;

    if total_hits <= max_per_column {
        // Single column
        let mut display = format!("Hits: {}\n", total_hits);
        for hit in hits {
            display.push_str(&format!("  {}\n", hit));
        }
        display
    } else {
        // Multiple columns
        let num_columns = (total_hits + max_per_column - 1) / max_per_column;
        let mut display = format!(
            "Hits: {} (showing in {} columns)\n",
            total_hits, num_columns
        );

        // Build column layout
        let mut columns: Vec<Vec<&str>> = vec![Vec::new(); num_columns];
        for (idx, hit) in hits.iter().enumerate() {
            let col = idx / max_per_column;
            if col < num_columns {
                columns[col].push(hit);
            }
        }

        // Display columns side by side
        let max_rows = columns.iter().map(|c| c.len()).max().unwrap_or(0);
        for row in 0..max_rows {
            for (col_idx, column) in columns.iter().enumerate() {
                if let Some(hit) = column.get(row) {
                    display.push_str(&format!("  {:50}", hit));
                } else {
                    display.push_str(&format!("{:52}", ""));
                }
                if col_idx < columns.len() - 1 {
                    display.push_str("  ");
                }
            }
            display.push('\n');
        }

        display
    }
}

/// Make a single fuzz request
async fn make_fuzz_request(
    client: &Client,
    url: &str,
    use_head: bool,
) -> Result<FuzzResult, String> {
    let response = if use_head {
        // Use HEAD request to skip body download
        client
            .head(url)
            .send()
            .await
            .map_err(|e| format!("Request failed: {}", e))?
    } else {
        // Use GET request
        client
            .get(url)
            .send()
            .await
            .map_err(|e| format!("Request failed: {}", e))?
    };

    let status_code = response.status().as_u16();
    let content_length = response.content_length();
    let content_type = response
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .map(String::from);

    Ok(FuzzResult {
        url: url.to_string(),
        status_code,
        content_length,
        content_type,
        source: FuzzSource::Initial, // Will be overwritten by caller
    })
}

/// Extract base URL from a full URL (removes query params and fragments)
fn extract_base_url(url: &str) -> Result<String, String> {
    let parsed = Url::parse(url).map_err(|e| format!("Invalid URL: {}", e))?;
    let mut base = parsed.clone();
    base.set_query(None);
    base.set_fragment(None);
    Ok(base.to_string())
}

/// Query database for known endpoints from previous crawls
fn query_database_endpoints(
    db_path: &std::path::Path,
    target_urls: &[String],
) -> Result<Vec<String>, String> {
    use crate::data::Database;

    // Open database
    let db = Database::new(db_path).map_err(|e| format!("Failed to open database: {}", e))?;

    let mut endpoints = Vec::new();

    // Extract domains from target URLs
    let mut target_domains = Vec::new();
    for url in target_urls {
        if let Ok(parsed) = Url::parse(url) {
            if let Some(host) = parsed.host_str() {
                target_domains.push(host.to_string());
            }
        }
    }

    // Query database for nodes matching these domains
    for domain in &target_domains {
        // Simple query - get all nodes for this domain
        let query = "SELECT url FROM nodes WHERE domain = ?";

        if let Ok(mut stmt) = db.get_connection().prepare(query) {
            if let Ok(rows) = stmt.query_map([domain], |row| row.get::<_, String>(0)) {
                for url_result in rows.flatten() {
                    // Only include if it's a valid URL for the target
                    if let Ok(parsed) = Url::parse(&url_result) {
                        if let Some(host) = parsed.host_str() {
                            if host == domain || host.ends_with(&format!(".{}", domain)) {
                                endpoints.push(url_result);
                            }
                        }
                    }
                }
            }
        }
    }

    Ok(endpoints)
}

/// Build a test URL from base URL and wordlist entry
fn build_test_url(base_url: &str, word: &str) -> Result<String, String> {
    let mut url =
        Url::parse(base_url).map_err(|e| format!("Invalid base URL '{}': {}", base_url, e))?;

    // Get current path
    let current_path = url.path().to_string();

    // Ensure path ends with /
    let path_base = if current_path.ends_with('/') {
        current_path
    } else {
        format!("{}/", current_path)
    };

    // Build new path with word
    let new_path = format!("{}{}", path_base, word.trim_start_matches('/'));

    url.set_path(&new_path);

    Ok(url.to_string())
}

/// Load wordlist from file
pub fn load_wordlist(path: &Path) -> Result<Vec<String>, String> {
    let content = fs::read_to_string(path)
        .map_err(|e| format!("Failed to read wordlist {}: {}", path.display(), e))?;

    let words: Vec<String> = content
        .lines()
        .filter(|line| !line.trim().is_empty())
        .filter(|line| !line.trim().starts_with('#'))
        .map(|line| line.trim().to_string())
        .collect();

    if words.is_empty() {
        return Err(format!(
            "Wordlist {} is empty or contains only comments",
            path.display()
        ));
    }

    Ok(words)
}

/// Generate a simple fuzz report
pub fn generate_fuzz_report(results: &[FuzzResult]) -> String {
    let mut report = String::new();

    // Count by source
    let initial_count = results
        .iter()
        .filter(|r| r.source == FuzzSource::Initial)
        .count();
    let db_count = results
        .iter()
        .filter(|r| r.source == FuzzSource::Database)
        .count();
    let discovered_count = results
        .iter()
        .filter(|r| r.source == FuzzSource::Discovered)
        .count();

    // Group by status code
    let mut by_status: HashMap<u16, Vec<&FuzzResult>> = HashMap::new();
    for result in results {
        by_status
            .entry(result.status_code)
            .or_default()
            .push(result);
    }

    // Sort status codes
    let mut status_codes: Vec<u16> = by_status.keys().copied().collect();
    status_codes.sort();

    report.push_str(
        "\n═══════════════════════════════════════════════════════════════════════════════\n",
    );
    report.push_str("                            FUZZ RESULTS\n");
    report.push_str(
        "═══════════════════════════════════════════════════════════════════════════════\n\n",
    );

    report.push_str(&format!("Total findings: {}\n", results.len()));
    if db_count > 0 || discovered_count > 0 {
        report.push_str(&format!("  {} from initial targets\n", initial_count));
        if db_count > 0 {
            report.push_str(&format!("  {} from database endpoints\n", db_count));
        }
        if discovered_count > 0 {
            report.push_str(&format!(
                "  {} from discovered endpoints\n",
                discovered_count
            ));
        }
    }
    report.push('\n');

    for status_code in status_codes {
        if let Some(status_results) = by_status.get(&status_code) {
            let status_label = match status_code {
                200..=299 => format!("[{}] Success", status_code),
                300..=399 => format!("[{}] Redirect", status_code),
                400..=499 => format!("[{}] Client Error", status_code),
                _ => format!("[{}]", status_code),
            };

            report.push_str(&format!(
                "{} ({} findings)\n",
                status_label,
                status_results.len()
            ));
            report.push_str(
                "───────────────────────────────────────────────────────────────────────────────\n",
            );

            for result in status_results {
                // Add source indicator
                let source_marker = match result.source {
                    FuzzSource::Initial => "",
                    FuzzSource::Database => " [DB]",
                    FuzzSource::Discovered => " [DISC]",
                };

                report.push_str(&format!("  {}{}", result.url, source_marker));
                if let Some(length) = result.content_length {
                    report.push_str(&format!(" ({} bytes)", length));
                }
                if let Some(ref ct) = result.content_type {
                    let short_ct = ct.split(';').next().unwrap_or(ct);
                    report.push_str(&format!(" [{}]", short_ct));
                }
                report.push('\n');
            }
            report.push('\n');
        }
    }

    report.push_str(
        "═══════════════════════════════════════════════════════════════════════════════\n",
    );
    if discovered_count > 0 {
        report.push_str("  [DB] = From database  [DISC] = Discovered during fuzzing\n");
        report.push_str(
            "═══════════════════════════════════════════════════════════════════════════════\n",
        );
    }
    report.push_str("                            End of Report\n");
    report.push_str(
        "═══════════════════════════════════════════════════════════════════════════════\n",
    );

    report
}