rwalk 0.9.0

A blazing fast web directory scanner
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
use std::{
    collections::HashMap,
    io,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
    time::Duration,
};

use crate::{
    cli::{helpers::KeyVal, opts::Opts},
    runner::{
        wordlists::{compute_checksum, ParsedWordlist},
        Runner,
    },
    utils::{
        constants::{DEFAULT_FUZZ_KEY, DEFAULT_MODE, DEFAULT_STATUS_CODES},
        table::build_opts_table,
    },
};
use color_eyre::eyre::{bail, eyre, Result};
use colored::Colorize;
use futures::{future::abortable, FutureExt};
use indicatif::HumanDuration;
use itertools::Itertools;
use log::{debug, error, info, warn};
use merge::Merge;
use parking_lot::Mutex;
use ptree::print_tree;
use tokio::{io::AsyncWriteExt, task::JoinHandle, time::timeout};
use url::Url;
use utils::{structs::FuzzMatch, tree::UrlType};

use crate::utils::{
    constants::SUCCESS,
    structs::{Mode, Save},
    tree::{Tree, TreeData},
};

pub mod cli;
pub mod runner;
pub mod utils;

pub async fn _main(opts: Opts) -> Result<Tree<TreeData>> {
    if opts.url.is_none() && !opts.resume {
        bail!("Missing URL");
    }
    if opts.wordlists.is_empty() && !opts.resume {
        bail!("Missing wordlists");
    }

    let saved = if opts.resume {
        let res = tokio::fs::read_to_string(opts.save_file.clone().unwrap()).await;
        if res.is_err() {
            bail!("No save file: {}", opts.save_file.clone().unwrap().dimmed());
        }
        res
    } else {
        Err(io::Error::new(io::ErrorKind::NotFound, "No save file"))
    };
    let saved_json = if let Ok(saved) = saved {
        Some(serde_json::from_str::<Save>(&saved))
    } else {
        None
    };

    // Merge saved options with the ones passed as arguments
    let mut opts = if let Some(ref save) = saved_json {
        if let Ok(mut saved_opts) = save.as_ref().map(|x| x.opts.clone()) {
            saved_opts.merge(opts.clone());
            saved_opts
        } else {
            let err = save.as_ref().unwrap_err();
            error!(
                "Error while parsing save file: {}, using passed options",
                err.to_string().bold().red()
            );
            opts.clone()
        }
    } else {
        opts.clone()
    };

    // Default status filters
    if !opts.filter.iter().any(|e| e.0 == "status") {
        let mut filters = opts.filter.clone();
        filters.push(KeyVal(
            "status".to_string(),
            DEFAULT_STATUS_CODES.to_string(),
        ));
        opts.filter = filters;
    }

    // Parse wordlists into a HashMap associating each wordlist key to its contents
    let mut words = runner::wordlists::parse(&opts.wordlists).await?;

    let mut url = opts.url.clone().unwrap();

    // Check if the URL contains any of the replace keywords
    let mut fuzz_matches = words
        .keys()
        .flat_map(|x| {
            url.match_indices(x).map(|(i, e)| FuzzMatch {
                content: e.to_string(),
                start: i,
                end: i + e.len(),
            })
        })
        .collect::<Vec<_>>();
    // Set the mode based on the options and the URL
    let mode: Mode = if opts.mode.is_some() {
        opts.mode.as_deref().unwrap().into()
    } else if opts.depth.is_some() {
        Mode::Recursive
    } else if !fuzz_matches.is_empty() {
        Mode::Classic
    } else {
        DEFAULT_MODE.into()
    };

    match mode {
        Mode::Recursive => {
            if !fuzz_matches.is_empty() {
                warn!(
                    "URL contains the replace keyword{}: {}, this is supported with {}",
                    if fuzz_matches.len() > 1 { "s" } else { "" },
                    fuzz_matches
                        .iter()
                        .map(|e| e.content.clone())
                        .join(", ")
                        .bold()
                        .blue(),
                    format!("{} {}", "--mode".dimmed(), "classic".bold())
                );
            }
        }
        Mode::Classic => {
            if fuzz_matches.is_empty() {
                url = url.trim_end_matches('/').to_string() + "/" + DEFAULT_FUZZ_KEY;
                fuzz_matches.push(FuzzMatch {
                    content: DEFAULT_FUZZ_KEY.to_string(),
                    start: url.len() - 1,
                    end: url.len() - 1 + DEFAULT_FUZZ_KEY.len(),
                });
                warn!(
                    "URL does not contain the replace keyword: {}, it will be treated as: {}",
                    DEFAULT_FUZZ_KEY.bold(),
                    url.bold()
                );
            }
            // Remove unused wordlists keys
            for k in words.keys().cloned().collect::<Vec<_>>() {
                if !fuzz_matches.iter().any(|e| e.content == k) {
                    warn!(
                        "Wordlist {} is not used in the URL, removing it",
                        k.bold().blue()
                    );
                    words.remove(&k);
                }
            }
        }
        Mode::Spider => {
            if !fuzz_matches.is_empty() {
                warn!(
                    "URL contains the replace keyword{}: {}, this is supported with {}",
                    if fuzz_matches.len() > 1 { "s" } else { "" },
                    fuzz_matches
                        .iter()
                        .map(|e| e.content.clone())
                        .join(", ")
                        .bold()
                        .blue(),
                    format!("{} {}", "--mode".dimmed(), "classic".bold())
                );
            }
        }
    }

    let before = words.values().fold(0, |acc, x| acc + x.words.len());

    // Apply filters and transformations to the wordlists (if any)
    runner::wordlists::filters(&opts, &mut words)?;
    runner::wordlists::transformations(&opts, &mut words);

    runner::wordlists::deduplicate(&mut words);

    // Get the number of threads to use, default to 10 times the number of cores
    let threads = opts
        .threads
        .unwrap_or(num_cpus::get() * 10)
        .max(1)
        .min(words.iter().fold(0, |acc, (_, v)| acc + v.words.len()));

    if !opts.quiet {
        println!(
            "{}",
            build_opts_table(&opts, &words, &mode, threads, url.clone(), &fuzz_matches)
        );
    }

    let after = words.values().fold(0, |acc, x| acc + x.words.len());
    if before != after && !opts.quiet {
        info!(
            "{} words loaded, {} after deduplication and filters (-{}%)",
            before.to_string().bold().blue(),
            after.to_string().bold().blue(),
            ((before - after) as f64 / before as f64 * 100.0)
                .trunc()
                .to_string()
                .bold()
                .green()
        );
    }

    if words.values().all(|x| x.words.is_empty()) {
        bail!("No words found in wordlists");
    }

    // These will be used to keep track of the current state of the tree across threads
    let current_depth = Arc::new(Mutex::new(0));
    let current_indexes: Arc<Mutex<HashMap<String, Vec<usize>>>> =
        Arc::new(Mutex::new(HashMap::new()));

    let saved_tree = if opts.resume {
        match saved_json {
            Some(json) if json.is_ok() => Some(utils::tree::from_save(
                &opts,
                &json.unwrap(),
                current_depth.clone(),
                current_indexes.clone(),
                words.clone(),
            )?),
            _ => None,
        }
    } else {
        None
    };
    // We need to define this here for later use
    let has_saved = saved_tree.is_some();

    // Create the tree
    let tree = if let Some(saved_tree) = saved_tree {
        // Resume from the saved state
        saved_tree
    } else {
        // Create the tree with the root URL
        let t = Arc::new(Mutex::new(Tree::new()));
        let cleaned_url = match mode {
            Mode::Recursive | Mode::Spider => url.clone(),
            Mode::Classic => {
                // Get the first part of the url, before the first occurence of a fuzz key from fuzz_matches
                let mut smallest_index = url.len();
                for match_ in &fuzz_matches {
                    if let Some(index) = url.find(&match_.content) {
                        if index < smallest_index {
                            smallest_index = index;
                        }
                    }
                }
                url[..smallest_index].to_string()
            }
        };
        t.lock().insert(
            TreeData {
                url: cleaned_url.clone(),
                depth: 0,
                path: Url::parse(&cleaned_url.clone())?
                    .path()
                    .to_string()
                    .trim_end_matches('/')
                    .to_string(),
                status_code: 0,
                extra: serde_json::Value::Null,
                url_type: UrlType::Directory,
                response: None,
            },
            None,
        );
        t
    };

    // Check if the root URL is up
    let root_url = tree.lock().root.clone().unwrap().lock().data.url.clone();
    let root_url = Url::parse(&root_url)?;

    let tmp_client = runner::client::build(&opts)?;

    let res = tmp_client.get(root_url.clone()).send().await;
    if let Err(e) = res {
        error!("Error while connecting to {}: {}", root_url, e);
        // Exit if the root URL is down and the user didn't specify to force the execution
        if !opts.force {
            bail!("Root URL is down, use --force to continue");
        }
    } else {
        tree.lock().root.clone().unwrap().lock().data.status_code = res?.status().as_u16();
    }

    let start_time = std::time::Instant::now();

    if !opts.quiet {
        info!(
            "Press {} to {}exit",
            "Ctrl+C".bold(),
            if opts.no_save { "" } else { "save state and " }
        );
    }

    // Define the main function to run based on the mode
    let main_fun = match mode {
        Mode::Recursive => runner::recursive::Recursive::new(
            opts.clone(),
            current_depth.clone(),
            tree.clone(),
            current_indexes.clone(),
            // Split the words into chunks of equal size for each thread
            Arc::new(
                words
                    .iter()
                    .fold(
                        Vec::new(),
                        |mut acc, (_, ParsedWordlist { words: w, .. })| {
                            acc.extend(w.clone());
                            acc
                        },
                    )
                    .chunks(words.iter().fold(0, |acc, (_, v)| acc + v.words.len()) / threads)
                    .map(|x| x.to_vec())
                    .collect::<Vec<_>>(),
            ),
        )
        .run()
        .boxed(),
        Mode::Classic => runner::classic::Classic::new(
            url.clone(),
            opts.clone(),
            tree.clone(),
            // We do not need to chunk the words here as it is chunked in the Classic struct
            words.clone(),
            threads,
        )
        .run()
        .boxed(),
        Mode::Spider => {
            runner::spider::Spider::new(url.clone(), opts.clone(), tree.clone(), threads)
                .run()
                .boxed()
        }
    };
    // Run the main function with a timeout if specified
    let (task, handle) = if let Some(max_time) = opts.max_time {
        log::debug!("Setting timeout to {}s", max_time);
        abortable(timeout(Duration::from_secs(max_time as u64), main_fun))
    } else {
        // NOTE: This is clearly not the best way to handle this because it adds a useless poll but whatever
        // u64::MAX = 18_446_744_073_709_551_615 secs = 584'942'417'355 years (hopefully enough time to finish the scan)
        abortable(timeout(Duration::from_secs(u64::MAX), main_fun))
    };

    let main_thread = tokio::spawn(task);
    let aborted = Arc::new(AtomicBool::new(false));
    // Create a channel to receive the abort signal
    let (tx, mut rx) = tokio::sync::oneshot::channel::<()>();

    // We need to clone the variables to be used in the signal handler
    let ctrlc_tree = tree.clone();
    let ctrlc_depth = current_depth.clone();
    let ctrlc_words = words.clone();
    let ctrlc_opts = opts.clone();
    let ctrlc_aborted = aborted.clone();
    let ctrlc_save_file = opts.save_file.clone();

    let (ctrlc_task, ctrlc_handle) = abortable(async move {
        tokio::signal::ctrl_c()
            .await
            .expect("Failed to listen to Ctrl-C");
        println!();
        info!("Aborting...");

        ctrlc_aborted.store(true, Ordering::Relaxed);

        handle.abort();
        if !opts.no_save {
            let content = serde_json::to_string(&Save {
                tree: ctrlc_tree.clone(),
                depth: ctrlc_depth.clone(),
                wordlist_checksum: compute_checksum(&ctrlc_words),
                indexes: current_indexes.lock().clone(),
                opts: ctrlc_opts.clone(),
            });
            if let Ok(content) = content {
                let mut file = tokio::fs::File::create(
                    &ctrlc_save_file
                        .clone()
                        .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "No save file"))?,
                )
                .await?;

                file.write_all(content.as_bytes()).await?;
                file.flush().await?;
                print!("\x1B[2K\r");
                info!(
                    "Saved state to {}",
                    ctrlc_save_file
                        .clone()
                        .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "No save file"),)?
                        .to_string()
                        .bold()
                );
            }
        }
        tx.send(())
            .map_err(|_| eyre!(format!("Error while sending abort signal")))?;
        Ok(())
    });

    let signals_task: JoinHandle<Result<Result<()>, futures::future::Aborted>> =
        tokio::spawn(ctrlc_task);
    let abort_res = main_thread.await?;

    let timeout_res = match abort_res {
        Ok(res) => Some(res),
        Err(e) => {
            debug!("Aborted: {}", e);
            None
        }
    };

    if let Some(thread_res) = timeout_res {
        if let Err(e) = thread_res {
            debug!("Timeout reached: {}", e);
            error!(
                "Timeout reached after {}s",
                opts.max_time.unwrap().to_string().bold()
            );
        } else {
            let run_res = thread_res.unwrap();
            if let Err(e) = run_res {
                error!("{}", e);
            }
            if !opts.quiet {
                println!(
                    "{} Done in {} with an average of {} req/s",
                    SUCCESS.to_string().green(),
                    HumanDuration(std::time::Instant::now().duration_since(start_time))
                        .to_string()
                        .bold(),
                    ((match mode {
                        Mode::Recursive =>
                            words.iter().fold(0, |acc, (_, v)| acc + v.words.len())
                                * tree.lock().depth(),
                        Mode::Classic => {
                            words.iter().fold(0, |acc, (_, v)| acc + v.words.len())
                        }
                        Mode::Spider => 1,
                    }) as f64
                        / std::time::Instant::now()
                            .duration_since(start_time)
                            .as_secs_f64())
                    .round()
                    .to_string()
                    .bold()
                );
            }

            let root = tree.lock().root.clone().unwrap().clone();

            if !opts.quiet {
                print_tree(&*root.lock())?;
            }

            // Remove save file after finishing resuming
            if has_saved && !opts.keep_save {
                tokio::fs::remove_file(opts.save_file.clone().unwrap()).await?;
            }
            if opts.output.is_some() {
                let res = utils::save_to_file(&opts, root, current_depth, tree.clone());

                match res {
                    Ok(_) => info!("Saved to {}", opts.output.unwrap().bold()),
                    Err(e) => {
                        error!("{}", e);
                    }
                }
            }
        }
    }

    // Wait for the abort signal if aborted has been set
    if aborted.load(Ordering::Relaxed) {
        while rx.try_recv().is_err() {
            // Sleep for a short time to avoid busy waiting and hogging the CPU
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
    }

    // Terminate the signal stream.
    ctrlc_handle.abort();

    // Wait for the signal handler to finish
    let signals_res = signals_task.await?;

    // If we didn't abort and the signal handler returns an error, this is unexpected
    // Because the signal handler should only error when aborted
    if aborted.load(Ordering::Relaxed) {
        if let Err(e) = signals_res {
            error!("{}", e);
        }
    }
    let tree = tree.lock().clone();
    Ok(tree)
}