Skip to content

Commit

Permalink
Merge pull request #466 from epi052/add-redirect-messages-to-normal-r…
Browse files Browse the repository at this point in the history
…eports

Add redirect messages to normal reports
  • Loading branch information
epi052 authored Jan 17, 2022
2 parents d9088be + d66ba9c commit 194eec1
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 61 deletions.
6 changes: 5 additions & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ Long form explanations of most of the items below can be found in the [CONTRIBUT

## Documentation
- [ ] New code is documented using [doc comments](https://doc.rust-lang.org/stable/rust-by-example/meta/doc.html)
- [ ] Documentation about your PR is included in the README, as needed
- [ ] Documentation about your PR is included in the `docs`, as needed. The docs live in a [separate repository](https://epi052.github.io/feroxbuster-docs/docs/). Update the appropriate pages at the links below.
- [ ] update [example config file section](https://epi052.github.io/feroxbuster-docs/docs/configuration/ferox-config-toml/)
- [ ] update [help output section](https://epi052.github.io/feroxbuster-docs/docs/configuration/command-line/)
- [ ] add an [example](https://epi052.github.io/feroxbuster-docs/docs/examples/)
- [ ] update [comparison table](https://epi052.github.io/feroxbuster-docs/docs/compare/)

## Additional Tests
- [ ] New code is unit tested
Expand Down
19 changes: 5 additions & 14 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,11 @@ fn main() {

let mut app = initialize();

let path = generate_to(shells::Bash, &mut app, "feroxbuster", outdir).unwrap();
println!("cargo:warning=completion file is generated: {path:?}");

let path = generate_to(shells::Zsh, &mut app, "feroxbuster", outdir).unwrap();
println!("cargo:warning=completion file is generated: {path:?}");

let path = generate_to(shells::Zsh, &mut app, "feroxbuster", outdir).unwrap();
println!("cargo:warning=completion file is generated: {path:?}");

let path = generate_to(shells::PowerShell, &mut app, "feroxbuster", outdir).unwrap();
println!("cargo:warning=completion file is generated: {path:?}");

let path = generate_to(shells::Elvish, &mut app, "feroxbuster", outdir).unwrap();
println!("cargo:warning=completion file is generated: {path:?}");
generate_to(shells::Bash, &mut app, "feroxbuster", outdir).unwrap();
generate_to(shells::Zsh, &mut app, "feroxbuster", outdir).unwrap();
generate_to(shells::Zsh, &mut app, "feroxbuster", outdir).unwrap();
generate_to(shells::PowerShell, &mut app, "feroxbuster", outdir).unwrap();
generate_to(shells::Elvish, &mut app, "feroxbuster", outdir).unwrap();

// 0xdf pointed out an oddity when tab-completing options that expect file paths, the fix we
// landed on was to add -o plusdirs to the bash completion script. The following code aims to
Expand Down
52 changes: 34 additions & 18 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
};

use anyhow::{Context, Result};
use console::style;
use reqwest::{
header::{HeaderMap, HeaderName, HeaderValue},
Method, Response, StatusCode, Url,
Expand Down Expand Up @@ -351,6 +352,30 @@ impl FeroxSerialize for FeroxResponse {
let method = self.method().as_str();
let wild_status = status_colorizer("WLD");

let mut url_with_redirect = match (
self.status().is_redirection(),
self.headers().get("Location").is_some(),
) {
(true, true) => {
// redirect with Location header, show where it goes if possible
let loc = self
.headers()
.get("Location")
.unwrap() // known Some() already
.to_str()
.unwrap_or("Unknown");

// prettify the redirect target
let loc = style(loc).yellow();

format!("{} => {loc}", self.url())
}
_ => {
// no redirect, just use the normal url
self.url().to_string()
}
};

if self.wildcard && matches!(self.output_level, OutputLevel::Default | OutputLevel::Quiet) {
// --silent was not used and response is a wildcard, special messages abound when
// this is the case...
Expand All @@ -369,25 +394,16 @@ impl FeroxSerialize for FeroxResponse {
);

if self.status().is_redirection() {
// when it's a redirect, show where it goes, if possible
if let Some(next_loc) = self.headers().get("Location") {
let next_loc_str = next_loc.to_str().unwrap_or("Unknown");

let redirect_msg = format!(
"{} {:>9} {:>9} {:>9} {} redirects to => {}\n",
wild_status,
"-",
"-",
"-",
self.url(),
next_loc_str
);

message.push_str(&redirect_msg);
}
// initial wildcard messages are wordy enough, put the redirect by itself
url_with_redirect = format!(
"{} {:>9} {:>9} {:>9} {}\n",
wild_status, "-", "-", "-", url_with_redirect
);

// base message + redirection message (either empty string or redir msg)
message.push_str(&url_with_redirect);
}

// base message + redirection message (if appropriate)
message
} else {
// not a wildcard, just create a normal entry
Expand All @@ -397,7 +413,7 @@ impl FeroxSerialize for FeroxResponse {
&lines,
&words,
&chars,
self.url().as_str(),
&url_with_redirect,
self.output_level,
)
}
Expand Down
11 changes: 9 additions & 2 deletions src/scanner/ferox_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl FeroxScanner {
log::trace!("enter: scan_url");
log::info!("Starting scan against: {}", self.target_url);

let scan_timer = Instant::now();
let mut scan_timer = Instant::now();
let mut dirlist_flag = false;

if self.handles.config.extract_links {
Expand Down Expand Up @@ -133,7 +133,10 @@ impl FeroxScanner {
))?;

progress_bar.reset_eta();
progress_bar.finish_with_message(&format!("=> {}", style("Directory listing").green()));
progress_bar.finish_with_message(&format!(
"=> {}",
style("Directory listing").blue().bright()
));

ferox_scan.finish()?;

Expand All @@ -145,6 +148,10 @@ impl FeroxScanner {
// waits until an outstanding permit is dropped, at which point, the freed permit is assigned
// to the caller.
let _permit = self.scan_limiter.acquire().await;
if self.handles.config.scan_limit > 0 {
scan_timer = Instant::now();
progress_bar.reset();
}

// Arc clones to be passed around to the various scans
let looping_words = self.wordlist.clone();
Expand Down
1 change: 0 additions & 1 deletion src/scanner/requester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub(super) struct Requester {
/// need a usize to determine the number of consecutive non-error calls that a requester has
/// seen; this will satisfy the non-mut self constraint (due to us being behind an Arc, and
/// the need for a counter)
#[allow(clippy::mutex_atomic)]
tuning_lock: Mutex<usize>,
}

Expand Down
43 changes: 20 additions & 23 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,31 +168,28 @@ pub async fn make_request(
} else if e.is_redirect() {
if let Some(last_redirect) = e.url() {
// get where we were headed (last_redirect) and where we came from (url)
let fancy_message = format!("{} !=> {}", url, last_redirect);

let report = if let Some(msg_status) = e.status() {
send_command!(tx_stats, AddStatus(msg_status));
create_report_string(
msg_status.as_str(),
method,
"-1",
"-1",
"-1",
&fancy_message,
output_level,
)
} else {
create_report_string(
"UNK",
method,
"-1",
"-1",
"-1",
&fancy_message,
output_level,
)
let fancy_message = format!(
"{} !=> {} ({})",
url,
last_redirect,
style("too many redirects").red(),
);

let msg_status = match e.status() {
Some(status) => status.to_string(),
None => "ERR".to_string(),
};

let report = create_report_string(
&msg_status,
method,
"-1",
"-1",
"-1",
&fancy_message,
output_level,
);

send_command!(tx_stats, AddError(Redirection));

ferox_print(&report, &PROGRESS_PRINTER)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_heuristics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,12 @@ fn heuristics_wildcard_test_with_redirect_as_response_code(
assert!(contents.contains("WLD"));
assert!(contents.contains("301"));
assert!(contents.contains("/some-redirect"));
assert!(contents.contains("redirects to => "));
assert!(contents.contains(" => "));
assert!(contents.contains(&srv.url("/")));
assert!(contents.contains("(url length: 32)"));

cmd.assert().success().stdout(
predicate::str::contains("redirects to => ")
predicate::str::contains(" => ")
.and(predicate::str::contains("/some-redirect"))
.and(predicate::str::contains("301"))
.and(predicate::str::contains(srv.url("/")))
Expand Down

0 comments on commit 194eec1

Please sign in to comment.