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
use crate::config::{CONFIGURATION, PROGRESS_PRINTER};
use crate::utils::{
    ferox_print, format_url, get_url_path_length, make_request, module_colorizer, status_colorizer,
};
use console::style;
use indicatif::ProgressBar;
use reqwest::Response;
use std::process;
use uuid::Uuid;

/// length of a standard UUID, used when determining wildcard responses
const UUID_LENGTH: u64 = 32;

/// Data holder for two pieces of data needed when auto-filtering out wildcard responses
///
/// `dynamic` is the size of the response that will later be combined with the length
/// of the path of the url requested and used to determine interesting pages from custom
/// 404s where the requested url is reflected back in the response
///
/// `size` is size of the response that should be included with filters passed via runtime
/// configuration and any static wildcard lengths.
#[derive(Default, Debug)]
pub struct WildcardFilter {
    pub dynamic: u64,
    pub size: u64,
}

/// Simple helper to return a uuid, formatted as lowercase without hyphens
///
/// `length` determines the number of uuids to string together. Each uuid
/// is 32 characters long. So, a length of 1 return a 32 character string,
/// a length of 2 returns a 64 character string, and so on...
fn unique_string(length: usize) -> String {
    log::trace!("enter: unique_string({})", length);
    let mut ids = vec![];

    for _ in 0..length {
        ids.push(Uuid::new_v4().to_simple().to_string());
    }

    let unique_id = ids.join("");

    log::trace!("exit: unique_string -> {}", unique_id);
    unique_id
}

/// Tests the given url to see if it issues a wildcard response
///
/// In the event that url returns a wildcard response, a
/// [WildcardFilter](struct.WildcardFilter.html) is created and returned to the caller.
pub async fn wildcard_test(target_url: &str, bar: ProgressBar) -> Option<WildcardFilter> {
    log::trace!("enter: wildcard_test({:?})", target_url);

    if CONFIGURATION.dontfilter {
        // early return, dontfilter scans don't need tested
        log::trace!("exit: wildcard_test -> None");
        return None;
    }

    if let Some(resp_one) = make_wildcard_request(&target_url, 1).await {
        bar.inc(1);

        // found a wildcard response
        let mut wildcard = WildcardFilter::default();

        let wc_length = resp_one.content_length().unwrap_or(0);

        if wc_length == 0 {
            log::trace!("exit: wildcard_test -> Some({:?})", wildcard);
            return Some(wildcard);
        }

        // content length of wildcard is non-zero, perform additional tests:
        //   make a second request, with a known-sized (64) longer request
        if let Some(resp_two) = make_wildcard_request(&target_url, 3).await {
            bar.inc(1);

            let wc2_length = resp_two.content_length().unwrap_or(0);

            if wc2_length == wc_length + (UUID_LENGTH * 2) {
                // second length is what we'd expect to see if the requested url is
                // reflected in the response along with some static content; aka custom 404
                let url_len = get_url_path_length(&resp_one.url());

                if !CONFIGURATION.quiet {
                    ferox_print(
                    &format!(
                            "{} {:>10} Wildcard response is dynamic; {} ({} + url length) responses; toggle this behavior by using {}",
                            status_colorizer("WLD"),
                            wc_length - url_len,
                            style("auto-filtering").yellow(),
                            style(wc_length - url_len).cyan(),
                            style("--dontfilter").yellow()
                        ), &PROGRESS_PRINTER
                    );
                }

                wildcard.dynamic = wc_length - url_len;
            } else if wc_length == wc2_length {
                if !CONFIGURATION.quiet {
                    ferox_print(&format!(
                        "{} {:>10} Wildcard response is static; {} {} responses; toggle this behavior by using {}",
                        status_colorizer("WLD"),
                        wc_length,
                        style("auto-filtering").yellow(),
                        style(wc_length).cyan(),
                        style("--dontfilter").yellow()
                    ), &PROGRESS_PRINTER);
                }
                wildcard.size = wc_length;
            }
        } else {
            bar.inc(2);
        }

        log::trace!("exit: wildcard_test -> Some({:?})", wildcard);
        return Some(wildcard);
    }

    log::trace!("exit: wildcard_test -> None");
    None
}

/// Generates a uuid and appends it to the given target url. The reasoning is that the randomly
/// generated unique string should not exist on and be served by the target web server.
///
/// Once the unique url is created, the request is sent to the server. If the server responds
/// back with a valid status code, the response is considered to be a wildcard response. If that
/// wildcard response has a 3xx status code, that redirection location is displayed to the user.
async fn make_wildcard_request(target_url: &str, length: usize) -> Option<Response> {
    log::trace!("enter: make_wildcard_request({}, {})", target_url, length);

    let unique_str = unique_string(length);

    let nonexistent = match format_url(
        target_url,
        &unique_str,
        CONFIGURATION.addslash,
        &CONFIGURATION.queries,
        None,
    ) {
        Ok(url) => url,
        Err(e) => {
            log::error!("{}", e);
            log::trace!("exit: make_wildcard_request -> None");
            return None;
        }
    };

    let wildcard = status_colorizer("WLD");

    match make_request(&CONFIGURATION.client, &nonexistent.to_owned()).await {
        Ok(response) => {
            if CONFIGURATION
                .statuscodes
                .contains(&response.status().as_u16())
            {
                // found a wildcard response
                let url_len = get_url_path_length(&response.url());
                let content_len = response.content_length().unwrap_or(0);

                if !CONFIGURATION.quiet {
                    ferox_print(
                        &format!(
                            "{} {:>10} Got {} for {} (url length: {})",
                            wildcard,
                            content_len,
                            status_colorizer(&response.status().as_str()),
                            response.url(),
                            url_len
                        ),
                        &PROGRESS_PRINTER,
                    );
                }
                if response.status().is_redirection() {
                    // show where it goes, if possible
                    if let Some(next_loc) = response.headers().get("Location") {
                        if let Ok(next_loc_str) = next_loc.to_str() {
                            if !CONFIGURATION.quiet {
                                ferox_print(
                                    &format!(
                                        "{} {:>10} {} redirects to => {}",
                                        wildcard,
                                        content_len,
                                        response.url(),
                                        next_loc_str
                                    ),
                                    &PROGRESS_PRINTER,
                                );
                            }
                        } else if !CONFIGURATION.quiet {
                            ferox_print(
                                &format!(
                                    "{} {:>10} {} redirects to => {:?}",
                                    wildcard,
                                    content_len,
                                    response.url(),
                                    next_loc
                                ),
                                &PROGRESS_PRINTER,
                            );
                        }
                    }
                }
                log::trace!("exit: make_wildcard_request -> {:?}", response);
                return Some(response);
            }
        }
        Err(e) => {
            log::warn!("{}", e);
            log::trace!("exit: make_wildcard_request -> None");
            return None;
        }
    }
    log::trace!("exit: make_wildcard_request -> None");
    None
}

/// Simply tries to connect to all given sites before starting to scan
///
/// In the event that no sites can be reached, the program will exit.
///
/// Any urls that are found to be alive are returned to the caller.
pub async fn connectivity_test(target_urls: &[String]) -> Vec<String> {
    log::trace!("enter: connectivity_test({:?})", target_urls);

    let mut good_urls = vec![];

    for target_url in target_urls {
        let request = match format_url(
            target_url,
            "",
            CONFIGURATION.addslash,
            &CONFIGURATION.queries,
            None,
        ) {
            Ok(url) => url,
            Err(e) => {
                log::error!("{}", e);
                continue;
            }
        };

        match make_request(&CONFIGURATION.client, &request).await {
            Ok(_) => {
                good_urls.push(target_url.to_owned());
            }
            Err(e) => {
                if !CONFIGURATION.quiet {
                    ferox_print(
                        &format!("Could not connect to {}, skipping...", target_url),
                        &PROGRESS_PRINTER,
                    );
                }
                log::error!("{}", e);
            }
        }
    }

    if good_urls.is_empty() {
        log::error!("Could not connect to any target provided, exiting.");
        log::trace!("exit: connectivity_test");
        eprintln!(
            "{} {} Could not connect to any target provided",
            status_colorizer("ERROR"),
            module_colorizer("heuristics::connectivity_test"),
        );

        process::exit(1);
    }

    log::trace!("exit: connectivity_test -> {:?}", good_urls);

    good_urls
}

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

    #[test]
    /// request a unique string of 32bytes * a value returns correct result
    fn unique_string_returns_correct_length() {
        for i in 0..10 {
            assert_eq!(unique_string(i).len(), i * 32);
        }
    }
}