use crate::{
modules::{HttpModule, Module, ModuleName, ModuleVersion},
report::{Finding, ModuleResult, Severity},
Error,
};
use async_trait::async_trait;
use regex::Regex;
use reqwest::Client;
pub struct DirectoryListingDisclosure {
dir_listing_regex: Regex,
}
impl DirectoryListingDisclosure {
pub fn new() -> Self {
DirectoryListingDisclosure {
dir_listing_regex: Regex::new(r"<title>Index of .*</title>")
.expect("compiling http/directory_listing regexp"),
}
}
async fn is_directory_listing(&self, body: String) -> Result<bool, Error> {
let dir_listing_regex = self.dir_listing_regex.clone();
let res = tokio::task::spawn_blocking(move || dir_listing_regex.is_match(&body)).await?;
Ok(res)
}
}
impl Module for DirectoryListingDisclosure {
fn name(&self) -> ModuleName {
ModuleName::HttpDirectoryListingDisclosure
}
fn description(&self) -> String {
String::from("Check for enabled directory listing, which often leak information")
}
fn version(&self) -> ModuleVersion {
ModuleVersion(1, 0, 0)
}
fn is_aggressive(&self) -> bool {
false
}
fn severity(&self) -> Severity {
Severity::Medium
}
}
#[async_trait]
impl HttpModule for DirectoryListingDisclosure {
async fn scan(&self, http_client: &Client, endpoint: &str) -> Result<Option<Finding>, Error> {
let url = format!("{}/", &endpoint);
let res = http_client.get(&url).send().await?;
if !res.status().is_success() {
return Ok(None);
}
let body = res.text().await?;
if self.is_directory_listing(body).await? {
return Ok(Some(Finding {
module: self.name(),
module_version: self.version(),
severity: self.severity(),
result: ModuleResult::Url(url),
}));
}
Ok(None)
}
}
#[cfg(test)]
mod tests {
use super::DirectoryListingDisclosure;
#[tokio::test]
async fn is_directory_listing() {
let module = DirectoryListingDisclosure::new();
let body = String::from("Content <title>Index of kerkour.com</title> test");
let body2 = String::from(">ccece> Contrnt <tle>Index of kerkour.com</title> test");
let body3 = String::from("");
let body4 = String::from("lol lol lol ol ol< LO> OL <title>Index</title> test");
assert_eq!(true, module.is_directory_listing(body).await.unwrap());
assert_eq!(false, module.is_directory_listing(body2).await.unwrap());
assert_eq!(false, module.is_directory_listing(body3).await.unwrap());
assert_eq!(false, module.is_directory_listing(body4).await.unwrap());
}
}