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
use std::{error::Error, time::Duration, process::exit};
use colored::Colorize;
use differ::{Differ, Tag};
use governor::{Quota, RateLimiter};
use indicatif::ProgressBar;
use itertools::iproduct;
use regex::Regex;
use reqwest::{redirect, Proxy};
use tokio::{fs::File, io::AsyncWriteExt, sync::mpsc};
use crate::utils;
// the Job struct which will be used to define our settings for the detection jobs
#[derive(Clone, Debug)]
pub struct JobSettings {
match_status: String,
drop_after_fail: String,
}
// the Job struct will be used as jobs for the detection phase
#[derive(Clone, Debug)]
pub struct Job {
settings: Option<JobSettings>,
url: Option<String>,
payload: Option<String>,
}
// the JobResult struct which will be used as jobs
// to save the data to a file
#[derive(Clone, Debug)]
pub struct JobResult {
pub data: String,
}
// this asynchronous function will send the url as jobs to all the workers
// each worker will perform tests to detect path normalization misconfigurations.
pub async fn send_url(
mut tx: spmc::Sender<Job>,
urls: Vec<String>,
payloads: Vec<String>,
rate: u32,
match_status: String,
drop_after_fail: String,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
//set rate limit
let lim = RateLimiter::direct(Quota::per_second(std::num::NonZeroU32::new(rate).unwrap()));
// the job settings
let job_settings = JobSettings {
match_status: match_status.to_string(),
drop_after_fail: drop_after_fail,
};
// send the jobs
for (url, payload) in iproduct!(urls, payloads) {
let msg = Job {
settings: Some(job_settings.clone()),
url: Some(url.clone()),
payload: Some(payload.clone()),
};
if let Err(_) = tx.send(msg) {
continue;
}
lim.until_ready().await;
}
Ok(())
}
// this function will test for path normalization vulnerabilities
pub async fn run_tester(
pb: ProgressBar,
rx: spmc::Receiver<Job>,
tx: mpsc::Sender<JobResult>,
timeout: usize,
http_proxy: String,
) -> JobResult {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
reqwest::header::USER_AGENT,
reqwest::header::HeaderValue::from_static(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:95.0) Gecko/20100101 Firefox/95.0",
),
);
let client;
if !http_proxy.is_empty() {
//no certs
client = reqwest::Client::builder()
.default_headers(headers)
.redirect(redirect::Policy::none())
.timeout(Duration::from_secs(timeout.try_into().unwrap()))
.danger_accept_invalid_hostnames(true)
.danger_accept_invalid_certs(true)
.build()
.unwrap();
}else{
let proxy = match Proxy::all(http_proxy) {
Ok(proxy) => proxy,
Err(e) => {
pb.println(format!("Could not setup proxy, err: {:?}", e));
exit(1);
}
};
//no certs
client = reqwest::Client::builder()
.default_headers(headers)
.redirect(redirect::Policy::none())
.timeout(Duration::from_secs(timeout.try_into().unwrap()))
.danger_accept_invalid_hostnames(true)
.danger_accept_invalid_certs(true)
.proxy(proxy)
.build()
.unwrap();
}
while let Ok(job) = rx.recv() {
let job_url = job.url.unwrap();
let job_payload = job.payload.unwrap();
let job_settings = job.settings.unwrap();
let job_url_new = job_url.clone();
let job_payload_new = job_payload.clone();
pb.inc(1);
let url = match reqwest::Url::parse(&job_url_new) {
Ok(url) => url,
Err(_) => {
continue;
}
};
let mut job_url_with_path: String = String::from("");
let mut job_url_without_path: String = String::from("");
let schema = url.scheme().to_string();
let path = url.path().to_string();
let host = match url.host_str() {
Some(host) => host,
None => continue,
};
job_url_with_path.push_str(&schema);
job_url_with_path.push_str("://");
job_url_with_path.push_str(&host);
job_url_with_path.push_str(&path);
job_url_without_path.push_str(&schema);
job_url_without_path.push_str("://");
job_url_without_path.push_str(&host);
job_url_without_path.push_str("/");
let path_cnt = path.split("/").count() + 5;
let mut payload = String::from(job_payload);
let new_url = String::from(&job_url);
let mut track_status_codes = 0;
for _ in 0..path_cnt {
let job_url_without_path = job_url_without_path.clone();
let mut new_url = new_url.clone();
if !new_url.as_str().ends_with("/") {
new_url.push_str("/");
}
new_url.push_str(&payload);
pb.set_message(format!(
"{} {}",
"scanning ::".bold().white(),
new_url.bold().blue(),
));
let new_url2 = new_url.clone();
let get = client.get(new_url);
let req = match get.build() {
Ok(req) => req,
Err(_) => {
continue;
}
};
let resp = match client.execute(req).await {
Ok(resp) => resp,
Err(_) => {
continue;
}
};
let pub_get = client.get(job_url_without_path);
let pub_req = match pub_get.build() {
Ok(pub_req) => pub_req,
Err(_) => {
continue;
}
};
let pub_resp = match client.execute(pub_req).await {
Ok(pub_resp) => pub_resp,
Err(_) => {
continue;
}
};
let content_length = match resp.content_length() {
Some(content_length) => content_length.to_string(),
None => { "" }.to_owned(),
};
let backonemore_url = new_url2.clone();
if job_settings.match_status.contains(resp.status().as_str()) {
// strip the suffix hax and traverse back one more level
// to reach the internal doc root.
let backonemore = match backonemore_url.strip_suffix(job_payload_new.as_str()) {
Some(backonemore) => backonemore,
None => "",
};
let get = client.get(backonemore);
let request = match get.build() {
Ok(request) => request,
Err(_) => {
continue;
}
};
let response_title = match client.execute(request).await {
Ok(response_title) => response_title,
Err(_) => {
continue;
}
};
let result_url = backonemore.clone();
let get = client.get(backonemore);
let request = match get.build() {
Ok(request) => request,
Err(_) => {
continue;
}
};
let response = match client.execute(request).await {
Ok(response) => response,
Err(_) => {
continue;
}
};
let internal_get = client.get(backonemore);
let internal_req = match internal_get.build() {
Ok(internal_req) => internal_req,
Err(_) => {
continue;
}
};
let internal_resp = match client.execute(internal_req).await {
Ok(internal_resp) => internal_resp,
Err(_) => {
continue;
}
};
let internal_cl = match internal_resp.text().await {
Ok(internal_cl) => internal_cl,
Err(_) => continue,
};
let public_cl = match pub_resp.text().await {
Ok(public_cl) => public_cl,
Err(_) => continue,
};
// we hit the internal doc root.
let (ok, distance_between_responses) =
utils::get_response_change(&internal_cl, &public_cl);
if response.status().as_str() != "400"
&& ok
&& result_url.contains(&job_payload_new)
{
// track the status codes
if job_settings.drop_after_fail == response.status().as_str() {
track_status_codes += 1;
if track_status_codes >= 5 {
return JobResult {
data: "".to_string(),
};
}
}
pb.println(format!(
"{} {}",
"found internal doc root :: ".bold().green(),
result_url.bold().blue(),
));
let mut title = String::from("");
let content = match response_title.text().await {
Ok(content) => content,
Err(_) => "".to_string(),
};
let re = Regex::new(r"<title>(.*?)</title>").unwrap();
for cap in re.captures_iter(&content) {
title.push_str(&cap[1]);
}
// fetch the server from the headers
let server = match response.headers().get("Server") {
Some(server) => match server.to_str() {
Ok(server) => server,
Err(_) => "Unknown",
},
None => "Unknown",
};
let internal_resp_text_lines = internal_cl.lines().collect::<Vec<_>>();
let public_resp_text_lines = public_cl.lines().collect::<Vec<_>>();
let character_differences =
Differ::new(&public_resp_text_lines, &internal_resp_text_lines);
pb.println(format!(
"\n{}{}{} {}",
"(".bold().white(),
"*".bold().blue(),
")".bold().white(),
"found some response changes:".bold().green(),
));
for span in character_differences.spans() {
match span.tag {
Tag::Equal => (), // ignore
Tag::Insert => (), // ignore
Tag::Delete => (), // ignore
Tag::Replace => {
let mut i = 0;
for line in &internal_resp_text_lines[span.b_start..span.b_end] {
i = i + 1;
if i < 100 {
if line.to_string() == "" {
pb.println(format!("\n{}", line.bold().white(),));
} else {
pb.println(format!("{}", line.bold().white(),));
}
} else {
pb.println(format!("\n{}", "...".bold().white(),));
break;
}
}
}
}
}
pb.println(format!("\n"));
if response.status().is_client_error() {
pb.println(format!(
"{}{}{} {}{}{}\n{}{}{} {}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t",
"[".bold().white(),
"OK".bold().green(),
"]".bold().white(),
"[".bold().white(),
result_url.bold().cyan(),
"]".bold().white(),
"[".bold().white(),
"*".bold().green(),
"]".bold().white(),
"Response:".bold().white(),
"payload:".bold().white(),
"[".bold().white(),
job_payload_new.bold().blue(),
"]".bold().white(),
"status:".bold().white(),
"[".bold().white(),
response.status().as_str().bold().blue(),
"]".bold().white(),
"content_length:".bold().white(),
"[".bold().white(),
content_length.yellow(),
"]".bold().white(),
"server:".bold().white(),
"[".bold().white(),
server.bold().purple(),
"]".bold().white(),
"title:".bold().white(),
"[".bold().white(),
title.bold().purple(),
"]".bold().white(),
"deviation:".bold().white(),
"[".bold().white(),
distance_between_responses.to_string().bold().purple(),
"]".bold().white(),
));
}
if response.status().is_success() {
pb.println(format!(
"{}{}{} {}{}{}\n{}{}{} {}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t",
"[".bold().white(),
"OK".bold().green(),
"]".bold().white(),
"[".bold().white(),
result_url.bold().cyan(),
"]".bold().white(),
"[".bold().white(),
"*".bold().green(),
"]".bold().white(),
"Response:".bold().white(),
"payload:".bold().white(),
"[".bold().white(),
job_payload_new.bold().blue(),
"]".bold().white(),
"status:".bold().white(),
"[".bold().white(),
response.status().as_str().bold().green(),
"]".bold().white(),
"content_length:".bold().white(),
"[".bold().white(),
content_length.yellow(),
"]".bold().white(),
"server:".bold().white(),
"[".bold().white(),
server.bold().purple(),
"]".bold().white(),
"title:".bold().white(),
"[".bold().white(),
title.bold().purple(),
"]".bold().white(),
"deviation:".bold().white(),
"[".bold().white(),
distance_between_responses.to_string().bold().purple(),
"]".bold().white(),
));
}
if response.status().is_redirection() {
pb.println(format!(
"{}{}{} {}{}{}\n{}{}{} {}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t",
"[".bold().white(),
"OK".bold().green(),
"]".bold().white(),
"[".bold().white(),
result_url.bold().cyan(),
"]".bold().white(),
"[".bold().white(),
"*".bold().green(),
"]".bold().white(),
"Response:".bold().white(),
"payload:".bold().white(),
"[".bold().white(),
job_payload_new.bold().blue(),
"]".bold().white(),
"status:".bold().white(),
"[".bold().white(),
response.status().as_str().bold().blue(),
"]".bold().white(),
"content_length:".bold().white(),
"[".bold().white(),
content_length.yellow(),
"]".bold().white(),
"server:".bold().white(),
"[".bold().white(),
server.bold().purple(),
"]".bold().white(),
"title:".bold().white(),
"[".bold().white(),
title.bold().purple(),
"]".bold().white(),
"deviation:".bold().white(),
"[".bold().white(),
distance_between_responses.to_string().bold().purple(),
"]".bold().white(),
));
}
if response.status().is_server_error() {
pb.println(format!(
"{}{}{} {}{}{}\n{}{}{} {}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t",
"[".bold().white(),
"OK".bold().green(),
"]".bold().white(),
"[".bold().white(),
result_url.bold().cyan(),
"]".bold().white(),
"[".bold().white(),
"*".bold().green(),
"]".bold().white(),
"Response:".bold().white(),
"payload:".bold().white(),
"[".bold().white(),
job_payload_new.bold().blue(),
"]".bold().white(),
"status:".bold().white(),
"[".bold().white(),
response.status().as_str().bold().red(),
"]".bold().white(),
"content_length:".bold().white(),
"[".bold().white(),
content_length.yellow(),
"]".bold().white(),
"server:".bold().white(),
"[".bold().white(),
server.bold().purple(),
"]".bold().white(),
"title:".bold().white(),
"[".bold().white(),
title.bold().purple(),
"]".bold().white(),
"deviation:".bold().white(),
"[".bold().white(),
distance_between_responses.to_string().bold().purple(),
"]".bold().white(),
));
}
if response.status().is_informational() {
pb.println(format!(
"{}{}{} {}{}{}\n{}{}{} {}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t {} {}{}{}\n\t",
"[".bold().white(),
"OK".bold().green(),
"]".bold().white(),
"[".bold().white(),
result_url.bold().cyan(),
"]".bold().white(),
"[".bold().white(),
"*".bold().green(),
"]".bold().white(),
"Response:".bold().white(),
"payload:".bold().white(),
"[".bold().white(),
job_payload_new.bold().blue(),
"]".bold().white(),
"status:".bold().white(),
"[".bold().white(),
response.status().as_str().bold().purple(),
"]".bold().white(),
"content_length:".bold().white(),
"[".bold().white(),
content_length.yellow(),
"]".bold().white(),
"server:".bold().white(),
"[".bold().white(),
server.bold().purple(),
"]".bold().white(),
"title:".bold().white(),
"[".bold().white(),
title.bold().purple(),
"]".bold().white(),
"deviation:".bold().white(),
"[".bold().white(),
distance_between_responses.to_string().bold().purple(),
"]".bold().white(),
));
}
// send the result message through the channel to the workers.
let result_msg = JobResult {
data: result_url.to_owned(),
};
let result_job = result_msg.clone();
if let Err(_) = tx.send(result_msg).await {
continue;
}
pb.inc_length(1);
return result_job;
}
}
payload.push_str(&job_payload_new);
}
}
return JobResult {
data: "".to_string(),
};
}
pub async fn save_traversals(_: ProgressBar, mut outfile: File, traversal: String) {
let mut outbuf = traversal.as_bytes().to_owned();
outbuf.extend_from_slice(b"\n");
if let Err(_) = outfile.write(&outbuf).await {
// pb.println(format!("failed to write output '{:?}': {:?}", outbuf, e));
return;
}
}