Skip to content

Commit

Permalink
Merge pull request #127 from epi052/125-add-url-from-whence-we-came
Browse files Browse the repository at this point in the history
reduced log output by a lot; added redirection location on error
  • Loading branch information
epi052 authored Nov 18, 2020
2 parents 4f31ed1 + a93fe91 commit 85cba02
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 70 deletions.
46 changes: 24 additions & 22 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,33 @@ pub fn initialize(
.default_headers(header_map)
.redirect(policy);

let client = if proxy.is_some() && !proxy.unwrap().is_empty() {
match Proxy::all(proxy.unwrap()) {
Ok(proxy_obj) => client.proxy(proxy_obj),
Err(e) => {
eprintln!(
"{} {} Could not add proxy ({:?}) to Client configuration",
status_colorizer("ERROR"),
module_colorizer("Client::initialize"),
proxy
);
eprintln!(
"{} {} {}",
status_colorizer("ERROR"),
module_colorizer("Client::initialize"),
e
);
let client = match proxy {
// a proxy is specified, need to add it to the client
Some(some_proxy) => {
if !some_proxy.is_empty() {
// it's not an empty string
match Proxy::all(some_proxy) {
Ok(proxy_obj) => client.proxy(proxy_obj),
Err(e) => {
eprintln!(
"{} {} {}",
status_colorizer("ERROR"),
module_colorizer("Client::initialize"),
e
);

#[cfg(test)]
panic!();
#[cfg(not(test))]
exit(1);
#[cfg(test)]
panic!();
#[cfg(not(test))]
exit(1);
}
}
} else {
client // Some("") was used?
}
}
} else {
client
// no proxy specified
None => client,
};

match client.build() {
Expand Down
4 changes: 2 additions & 2 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl FeroxFilter for WildcardFilter {
/// Examine size, dynamic, and content_len to determine whether or not the response received
/// is a wildcard response and therefore should be filtered out
fn should_filter_response(&self, response: &FeroxResponse) -> bool {
log::trace!("enter: should_filter_response({:?} {:?})", self, response);
log::trace!("enter: should_filter_response({:?} {})", self, response);

// quick return if dont_filter is set
if CONFIGURATION.dont_filter {
Expand Down Expand Up @@ -114,7 +114,7 @@ pub struct StatusCodeFilter {
impl FeroxFilter for StatusCodeFilter {
/// Check `filter_code` against what was passed in via -C|--filter-status
fn should_filter_response(&self, response: &FeroxResponse) -> bool {
log::trace!("enter: should_filter_response({:?} {:?})", self, response);
log::trace!("enter: should_filter_response({:?} {})", self, response);

if response.status().as_u16() == self.filter_code {
log::debug!(
Expand Down
10 changes: 7 additions & 3 deletions src/heuristics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ pub async fn wildcard_test(

if !CONFIGURATION.quiet {
let msg = format!(
"{} {:>10} Wildcard response is dynamic; {} ({} + url length) responses; toggle this behavior by using {}\n",
"{} {:>8}l {:>8}w {:>8}c Wildcard response is dynamic; {} ({} + url length) responses; toggle this behavior by using {}\n",
status_colorizer("WLD"),
ferox_response.line_count(),
ferox_response.word_count(),
wildcard.dynamic,
style("auto-filtering").yellow(),
style(wc_length - url_len).cyan(),
Expand All @@ -110,8 +112,10 @@ pub async fn wildcard_test(

if !CONFIGURATION.quiet {
let msg = format!(
"{} {:>10} Wildcard response is static; {} {} responses; toggle this behavior by using {}\n",
"{} {:>8}l {:>8}w {:>8}c Wildcard response is static; {} {} responses; toggle this behavior by using {}\n",
status_colorizer("WLD"),
ferox_response.line_count(),
ferox_response.word_count(),
wc_length,
style("auto-filtering").yellow(),
style(wc_length).cyan(),
Expand Down Expand Up @@ -235,7 +239,7 @@ async fn make_wildcard_request(
}
}
}
log::trace!("exit: make_wildcard_request -> {:?}", ferox_response);
log::trace!("exit: make_wildcard_request -> {}", ferox_response);
return Some(ferox_response);
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ pub struct FeroxResponse {
headers: HeaderMap,
}

/// Implement Display for FeroxResponse
impl fmt::Display for FeroxResponse {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"FeroxResponse {{ url: {}, status: {}, content-length: {} }}",
self.url(),
self.status(),
self.content_length()
)
}
}

/// `FeroxResponse` implementation
impl FeroxResponse {
/// Get the `StatusCode` of this `FeroxResponse`
Expand Down
7 changes: 4 additions & 3 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub fn initialize(verbosity: u8) {
0 => (),
1 => env::set_var("RUST_LOG", "warn"),
2 => env::set_var("RUST_LOG", "info"),
3 => env::set_var("RUST_LOG", "debug,hyper=info,reqwest=info"),
_ => env::set_var("RUST_LOG", "trace,hyper=info,reqwest=info"),
3 => env::set_var("RUST_LOG", "feroxbuster=debug,info"),
_ => env::set_var("RUST_LOG", "feroxbuster=trace,info"),
}
}
}
Expand Down Expand Up @@ -55,9 +55,10 @@ pub fn initialize(verbosity: u8) {
};

let msg = format!(
"{} {:10.03} {}\n",
"{} {:10.03} {} {}\n",
style(level_name).bg(level_color).black(),
style(t).dim(),
record.target(),
style(record.args()).dim(),
);

Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn initialize() -> App<'static, 'static> {
.long("verbosity")
.takes_value(false)
.multiple(true)
.help("Increase verbosity level (use -vv or more for greater effect)"),
.help("Increase verbosity level (use -vv or more for greater effect. [CAUTION] 4 -v's is probably too much)"),
)
.arg(
Arg::with_name("proxy")
Expand Down
26 changes: 8 additions & 18 deletions src/reporter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::{CONFIGURATION, PROGRESS_PRINTER};
use crate::utils::{ferox_print, make_request, status_colorizer};
use crate::utils::{create_report_string, ferox_print, make_request};
use crate::{FeroxChannel, FeroxResponse};
use console::strip_ansi_codes;
use std::io::Write;
Expand Down Expand Up @@ -95,23 +95,13 @@ async fn spawn_terminal_reporter(
log::trace!("received {} on reporting channel", resp.url());

if CONFIGURATION.status_codes.contains(&resp.status().as_u16()) {
let report = if CONFIGURATION.quiet {
// -q used, just need the url
format!("{}\n", resp.url())
} else {
// normal printing with status and size
let status = status_colorizer(&resp.status().as_str());
format!(
// example output
// 200 3280 https://localhost.com/FAQ
"{} {:>8}l {:>8}w {:>8}c {}\n",
status,
resp.line_count(),
resp.word_count(),
resp.content_length(),
resp.url()
)
};
let report = create_report_string(
resp.status().as_str(),
&resp.line_count().to_string(),
&resp.word_count().to_string(),
&resp.content_length().to_string(),
&resp.url().to_string(),
);

// print to stdout
ferox_print(&report, &PROGRESS_PRINTER);
Expand Down
23 changes: 6 additions & 17 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ fn create_urls(target_url: &str, word: &str, extensions: &[String]) -> Vec<Url>
/// handles 2xx and 3xx responses by either checking if the url ends with a / (2xx)
/// or if the Location header is present and matches the base url + / (3xx)
fn response_is_directory(response: &FeroxResponse) -> bool {
log::trace!("enter: is_directory({:?})", response);
log::trace!("enter: is_directory({})", response);

if response.status().is_redirection() {
// status code is 3xx
Expand All @@ -311,10 +311,7 @@ fn response_is_directory(response: &FeroxResponse) -> bool {
}
}
None => {
log::debug!(
"expected Location header, but none was found: {:?}",
response
);
log::debug!("expected Location header, but none was found: {}", response);
log::trace!("exit: is_directory -> false");
return false;
}
Expand Down Expand Up @@ -370,7 +367,7 @@ async fn try_recursion(
transmitter: UnboundedSender<String>,
) {
log::trace!(
"enter: try_recursion({:?}, {}, {:?})",
"enter: try_recursion({}, {}, {:?})",
response,
base_depth,
transmitter
Expand Down Expand Up @@ -528,23 +525,15 @@ async fn make_requests(

if new_ferox_response.is_file() {
// very likely a file, simply request and report
log::debug!(
"Singular extraction: {} ({})",
new_ferox_response.url(),
new_ferox_response.status().as_str(),
);
log::debug!("Singular extraction: {}", new_ferox_response);

send_report(report_chan.clone(), new_ferox_response);

continue;
}

if !CONFIGURATION.no_recursion {
log::debug!(
"Recursive extraction: {} ({})",
new_ferox_response.url(),
new_ferox_response.status().as_str()
);
log::debug!("Recursive extraction: {}", new_ferox_response);

if new_ferox_response.status().is_success()
&& !new_ferox_response.url().as_str().ends_with('/')
Expand All @@ -570,7 +559,7 @@ async fn make_requests(

/// Simple helper to send a `FeroxResponse` over the tx side of an `mpsc::unbounded_channel`
fn send_report(report_sender: UnboundedSender<FeroxResponse>, response: FeroxResponse) {
log::trace!("enter: send_report({:?}, {:?}", report_sender, response);
log::trace!("enter: send_report({:?}, {}", report_sender, response);

match report_sender.send(response) {
Ok(_) => {}
Expand Down
46 changes: 42 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::{FeroxError, FeroxResult};
use crate::{
config::{CONFIGURATION, PROGRESS_PRINTER},
FeroxError, FeroxResult,
};
use console::{strip_ansi_codes, style, user_attended};
use indicatif::ProgressBar;
use reqwest::Url;
use reqwest::{Client, Response};
use reqwest::{Client, Response, Url};
#[cfg(not(target_os = "windows"))]
use rlimit::{getrlimit, setrlimit, Resource, Rlim};
use std::convert::TryInto;
Expand Down Expand Up @@ -244,7 +246,6 @@ pub async fn make_request(client: &Client, url: &Url) -> FeroxResult<Response> {

match client.get(url.to_owned()).send().await {
Ok(resp) => {
log::debug!("requested Url: {}", resp.url());
log::trace!("exit: make_request -> {:?}", resp);
Ok(resp)
}
Expand All @@ -253,6 +254,19 @@ pub async fn make_request(client: &Client, url: &Url) -> FeroxResult<Response> {
if e.to_string().contains("operation timed out") {
// only warn for timeouts, while actual errors are still left as errors
log::warn!("Error while making request: {}", e);
} 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() {
create_report_string(msg_status.as_str(), "-1", "-1", "-1", &fancy_message)
} else {
create_report_string("UNK", "-1", "-1", "-1", &fancy_message)
};

ferox_print(&report, &PROGRESS_PRINTER)
};
} else {
log::error!("Error while making request: {}", e);
}
Expand All @@ -261,6 +275,30 @@ pub async fn make_request(client: &Client, url: &Url) -> FeroxResult<Response> {
}
}

/// Helper to create the standard line for output to file/terminal
///
/// example output:
/// 200 127l 283w 4134c http://localhost/faq
pub fn create_report_string(
status: &str,
line_count: &str,
word_count: &str,
content_length: &str,
url: &str,
) -> String {
if CONFIGURATION.quiet {
// -q used, just need the url
format!("{}\n", url)
} else {
// normal printing with status and sizes
let color_status = status_colorizer(status);
format!(
"{} {:>8}l {:>8}w {:>8}c {}\n",
color_status, line_count, word_count, content_length, url
)
}
}

/// Attempts to set the soft limit for the RLIMIT_NOFILE resource
///
/// RLIMIT_NOFILE is the maximum number of file descriptors that can be opened by this process
Expand Down

0 comments on commit 85cba02

Please sign in to comment.