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
//! robots.txt parsing for sensitive path discovery.
use gossan_core::Target;
use secfinding::{Evidence, Finding, Severity};
pub async fn probe(client: &reqwest::Client, target: &Target) -> anyhow::Result<Vec<Finding>> {
let Target::Web(asset) = target else {
return Ok(vec![]);
};
let url = format!("{}/robots.txt", asset.url.as_str().trim_end_matches('/'));
let mut findings = Vec::new();
if let Ok(resp) = client.get(&url).send().await {
if resp.status().as_u16() == 200 {
let body = gossan_core::net::bounded_text(resp, 4 * 1024 * 1024)
.await
.unwrap_or_default();
// Parse robots.txt into groups; extract Sitemap directives and
// disallow/allow lists that apply to the wildcard User-agent (*).
fn parse_robots(body: &str) -> (Vec<String>, Vec<String>) {
struct Group {
agents: Vec<String>,
allow: Vec<String>,
disallow: Vec<String>,
}
let mut groups: Vec<Group> = Vec::new();
let mut current = Group {
agents: Vec::new(),
allow: Vec::new(),
disallow: Vec::new(),
};
let mut sitemaps: Vec<String> = Vec::new();
for raw in body.lines() {
let line = raw.split('#').next().unwrap_or("").trim();
if line.is_empty() {
continue;
}
if let Some((k, v)) = line.split_once(':') {
let key = k.trim().to_lowercase();
let val = v.trim().to_string();
match key.as_str() {
"user-agent" => {
// start a new group if the current group already has directives
if !current.agents.is_empty()
&& (!current.allow.is_empty() || !current.disallow.is_empty())
{
groups.push(current);
current = Group {
agents: Vec::new(),
allow: Vec::new(),
disallow: Vec::new(),
};
}
current.agents.push(val);
}
"allow" => current.allow.push(val),
"disallow" => current.disallow.push(val),
"sitemap" => sitemaps.push(val),
_ => {}
}
}
}
// push last group
if !current.agents.is_empty()
|| !current.allow.is_empty()
|| !current.disallow.is_empty()
{
groups.push(current);
}
// Collect disallow rules applicable to the wildcard agent '*'
let mut wildcard_disallow: Vec<String> = Vec::new();
for g in groups.iter() {
if g.agents.iter().any(|a| a == "*") {
for d in &g.disallow {
if !d.is_empty() && d != "/" {
wildcard_disallow.push(d.clone());
}
}
}
}
(sitemaps, wildcard_disallow)
}
let (sitemaps, disallowed) = parse_robots(&body);
if !disallowed.is_empty() || !sitemaps.is_empty() {
// Build human-friendly detail
let mut detail_parts: Vec<String> = Vec::new();
if !disallowed.is_empty() {
detail_parts.push(format!("{} disallow rules", disallowed.len()));
}
if !sitemaps.is_empty() {
detail_parts.push(format!("{} sitemap(s)", sitemaps.len()));
}
let detail = if detail_parts.is_empty() {
"robots.txt parsed but no useful directives found".to_string()
} else {
format!(
"robots.txt: {} — show sample rules below.",
detail_parts.join(", ")
)
};
crate::try_push_finding(
crate::file_finding(
target,
Severity::Info,
"robots.txt parsed — directives found",
detail,
)
.evidence(Evidence::HttpResponse {
status: 200,
headers: vec![],
body_excerpt: Some(body.chars().take(500).collect::<String>().into()),
})
.tag("robots")
.tag("recon"),
&mut findings,
);
// Add a separate finding enumerating disallowed paths when present
if !disallowed.is_empty() {
crate::try_push_finding(
crate::file_finding(
target,
Severity::Low,
format!("robots.txt disallow rules ({})", disallowed.len()),
format!(
"Disallow rules (wildcard group '*'): {}",
disallowed.join(", ")
),
)
.tag("robots")
.tag("recon"),
&mut findings,
);
}
// Add sitemap findings if robots listed sitemaps
for sm in sitemaps.iter().take(5) {
crate::try_push_finding(
crate::file_finding(
target,
Severity::Info,
"robots.txt references a sitemap",
format!("robots.txt includes Sitemap: {}", sm),
)
.tag("robots")
.tag("sitemap"),
&mut findings,
);
}
}
}
}
Ok(findings)
}
#[cfg(test)]
mod tests {
#[test]
fn disallow_lines_filter_keeps_meaningful_paths() {
let body = "User-agent: *\nDisallow: /admin\nDisallow: /\nDisallow: /api/private\n";
let disallowed: Vec<String> = body
.lines()
.filter(|l| l.trim_start().to_lowercase().starts_with("disallow:"))
.filter_map(|l| l.split_once(':').map(|(_, v)| v.trim().to_string()))
.filter(|p| !p.is_empty() && p != "/")
.collect();
assert_eq!(disallowed, vec!["/admin", "/api/private"]);
}
#[test]
fn disallow_lines_are_case_insensitive() {
let body = "DISALLOW: /secret\nDisAllow: /debug\n";
let disallowed: Vec<String> = body
.lines()
.filter(|l| l.trim_start().to_lowercase().starts_with("disallow:"))
.filter_map(|l| l.split_once(':').map(|(_, v)| v.trim().to_string()))
.collect();
assert_eq!(disallowed, vec!["/secret", "/debug"]);
}
}