-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhidden_file_scanner.py
More file actions
442 lines (366 loc) · 18.7 KB
/
Copy pathhidden_file_scanner.py
File metadata and controls
442 lines (366 loc) · 18.7 KB
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
#!/usr/bin/env python3
"""
Hidden file scanner for discovering sensitive files and directories.
Scans for .git, .env, backup files, and other hidden resources.
"""
import asyncio
import aiohttp
import os
import time
from typing import List, Dict, Set, Optional, Any, Tuple
from urllib.parse import urljoin, urlparse
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TaskProgressColumn
from rich.table import Table
from rich.panel import Panel
import re
console = Console()
class HiddenFileScanner:
"""Scanner for hidden files and sensitive resources."""
def __init__(self, base_url: str, wordlist_path: Optional[str] = None):
self.base_url = base_url
self.wordlist_path = wordlist_path or self._get_default_wordlist()
self.discovered_files = []
self.session = None
self.timeout = aiohttp.ClientTimeout(total=10)
def _get_default_wordlist(self) -> str:
"""Get path to default hidden files wordlist."""
import os
# Create default wordlist if it doesn't exist
default_wordlist = "wordlists/hidden_files.txt"
os.makedirs("wordlists", exist_ok=True)
if not os.path.exists(default_wordlist):
self._create_default_wordlist(default_wordlist)
return default_wordlist
def _create_default_wordlist(self, path: str):
"""Create a default hidden files wordlist."""
hidden_files = [
# Version control
".git", ".git/config", ".git/HEAD", ".git/index", ".git/logs/HEAD",
".svn", ".svn/entries", ".svn/wc.db", ".svn/format",
".hg", ".hg/hgrc", ".hg/requires", ".hg/store",
# Environment and configuration
".env", ".env.local", ".env.development", ".env.production", ".env.test",
".env.backup", ".env.old", ".env.bak", ".env.save",
"config.php", "config.ini", "config.json", "config.xml",
"settings.php", "settings.ini", "settings.json", "settings.xml",
"database.yml", "database.json", "database.xml",
"application.yml", "application.json", "application.xml",
# Backup and temporary files
"backup", "backup/", "backup.zip", "backup.tar", "backup.tar.gz",
"backup.sql", "backup.db", "backup.bak", "backup.old",
"bak", "bak/", "old", "old/", "tmp", "tmp/", "temp", "temp/",
"cache", "cache/", "logs", "logs/", "log", "log/",
# Web server files
".htaccess", ".htpasswd", ".htaccess.bak", ".htaccess.old",
"web.config", "web.config.bak", "web.config.old",
"robots.txt", "robots.txt.bak", "robots.txt.old",
"sitemap.xml", "sitemap.xml.bak", "sitemap.xml.old",
# Development and debugging
"debug", "debug.php", "debug.log", "debug.txt",
"test", "test.php", "test.html", "test.txt",
"dev", "dev.php", "dev.html", "dev.txt",
"info.php", "info.html", "info.txt",
"phpinfo.php", "phpinfo.html", "phpinfo.txt",
# IDE and editor files
".vscode", ".vscode/settings.json", ".vscode/launch.json",
".idea", ".idea/workspace.xml", ".idea/modules.xml",
".sublime-project", ".sublime-workspace",
".vimrc", ".viminfo", ".vim/",
# OS and system files
".DS_Store", "Thumbs.db", "desktop.ini",
".bash_history", ".bashrc", ".bash_profile",
".ssh", ".ssh/config", ".ssh/id_rsa", ".ssh/id_rsa.pub",
".ssh/known_hosts", ".ssh/authorized_keys",
# Database files
"database.sql", "database.sql.bak", "database.sql.old",
"dump.sql", "dump.sql.bak", "dump.sql.old",
"backup.sql", "backup.sql.bak", "backup.sql.old",
"data.sql", "data.sql.bak", "data.sql.old",
# Application specific
"wp-config.php", "wp-config.php.bak", "wp-config.php.old",
"config.php", "config.php.bak", "config.php.old",
"configuration.php", "configuration.php.bak", "configuration.php.old",
"settings.php", "settings.php.bak", "settings.php.old",
# CMS and framework files
"composer.json", "composer.lock", "package.json", "package-lock.json",
"yarn.lock", "Gemfile", "Gemfile.lock", "requirements.txt",
"Pipfile", "Pipfile.lock", "poetry.lock", "Cargo.toml", "Cargo.lock",
# Security and authentication
".htaccess", ".htpasswd", ".htaccess.bak", ".htaccess.old",
"auth.php", "auth.php.bak", "auth.php.old",
"login.php", "login.php.bak", "login.php.old",
"admin.php", "admin.php.bak", "admin.php.old",
# API and documentation
"api", "api/", "api/v1", "api/v2", "api/v3",
"swagger", "swagger.json", "swagger.yaml", "swagger.yml",
"openapi", "openapi.json", "openapi.yaml", "openapi.yml",
"docs", "docs/", "documentation", "documentation/",
# Common file extensions
".log", ".log.bak", ".log.old", ".log.1", ".log.2",
".sql", ".sql.bak", ".sql.old", ".sql.1", ".sql.2",
".bak", ".old", ".backup", ".save", ".tmp", ".temp",
".cache", ".session", ".cookie", ".config", ".conf",
# Hidden directories
".git/", ".svn/", ".hg/", ".ssh/", ".vim/", ".vscode/",
".idea/", ".sublime-project/", ".sublime-workspace/",
"backup/", "old/", "tmp/", "temp/", "cache/", "logs/",
"dev/", "test/", "debug/", "api/", "docs/", "admin/",
# Common sensitive paths
"admin", "admin/", "administrator", "administrator/",
"manage", "manage/", "management", "management/",
"system", "system/", "sys", "sys/",
"internal", "internal/", "private", "private/",
"secret", "secret/", "hidden", "hidden/",
# Backup patterns
"*.bak", "*.backup", "*.old", "*.save", "*.tmp", "*.temp",
"*.log", "*.sql", "*.db", "*.cache", "*.session",
"*~", "*#", ".#*", ".#*#", "*.swp", "*.swo",
# Configuration files
"config", "config/", "configuration", "configuration/",
"settings", "settings/", "setup", "setup/",
"init", "init/", "bootstrap", "bootstrap/",
"startup", "startup/", "launch", "launch/",
# Development files
"dev", "dev/", "development", "development/",
"test", "test/", "testing", "testing/",
"staging", "staging/", "beta", "beta/",
"debug", "debug/", "debugger", "debugger/",
# Database files
"db", "db/", "database", "database/",
"data", "data/", "storage", "storage/",
"dump", "dump/", "backup", "backup/",
"export", "export/", "import", "import/",
# Log files
"log", "log/", "logs", "logs/",
"error", "error/", "errors", "errors/",
"access", "access/", "access.log", "error.log",
"debug.log", "info.log", "warn.log", "fatal.log"
]
with open(path, 'w') as f:
for file_path in hidden_files:
f.write(f"{file_path}\n")
console.print(f"✅ Created default hidden files wordlist at {path}")
async def __aenter__(self):
"""Async context manager entry."""
connector = aiohttp.TCPConnector(limit=50, limit_per_host=10)
self.session = aiohttp.ClientSession(
timeout=self.timeout,
connector=connector,
headers={
'User-Agent': 'Mozilla/5.0 (compatible; HiddenFileScanner/1.0)',
'Accept': 'text/html,application/xhtml+xml,application/json,*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive',
}
)
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Async context manager exit."""
if self.session:
await self.session.close()
async def scan_hidden_files(self, methods: List[str] = None,
status_codes: List[int] = None,
max_concurrent: int = 50) -> List[Dict[str, Any]]:
"""
Scan for hidden files and sensitive resources.
Args:
methods: HTTP methods to test (default: GET, HEAD)
status_codes: Status codes to consider as "found" (default: 200, 301, 302, 401, 403)
max_concurrent: Maximum concurrent requests
Returns:
List of discovered hidden files with details
"""
if methods is None:
methods = ['GET', 'HEAD']
if status_codes is None:
status_codes = [200, 201, 301, 302, 401, 403, 405, 500]
# Load wordlist
with open(self.wordlist_path, 'r') as f:
hidden_files = [line.strip() for line in f if line.strip() and not line.startswith('#')]
console.print(f"🔍 Scanning for hidden files on {self.base_url}")
console.print(f"📋 Loaded {len(hidden_files)} hidden file patterns")
discovered = []
semaphore = asyncio.Semaphore(max_concurrent)
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TaskProgressColumn(),
console=console
) as progress:
task = progress.add_task("Scanning hidden files...", total=len(hidden_files))
# Create tasks for all hidden files
tasks = []
for hidden_file in hidden_files:
task = self._test_hidden_file(hidden_file, methods, status_codes, semaphore, progress)
tasks.append(task)
# Run all tasks concurrently
results = await asyncio.gather(*tasks, return_exceptions=True)
# Process results
for result in results:
if isinstance(result, dict) and result.get('found'):
discovered.append(result)
progress.update(task, completed=len(hidden_files))
# Sort by sensitivity level and status code
discovered.sort(key=lambda x: (self._get_sensitivity_level(x['path']), x['status_code']))
return discovered
def _get_sensitivity_level(self, path: str) -> int:
"""Get sensitivity level of a hidden file (lower = more sensitive)."""
sensitive_patterns = [
(r'\.env', 1), # Environment files
(r'\.git', 1), # Git repository
(r'\.ssh', 1), # SSH keys
(r'config\.php', 2), # Configuration files
(r'wp-config', 2), # WordPress config
(r'backup', 3), # Backup files
(r'\.log', 4), # Log files
(r'\.bak', 4), # Backup files
(r'\.old', 4), # Old files
]
for pattern, level in sensitive_patterns:
if re.search(pattern, path, re.IGNORECASE):
return level
return 5 # Default level
async def _test_hidden_file(self, hidden_file: str, methods: List[str],
status_codes: List[int], semaphore: asyncio.Semaphore,
progress) -> Dict[str, Any]:
"""Test a single hidden file."""
async with semaphore:
url = urljoin(self.base_url, hidden_file)
for method in methods:
try:
async with self.session.request(method, url, allow_redirects=False) as response:
if response.status in status_codes:
result = {
'path': hidden_file,
'url': url,
'method': method,
'status_code': response.status,
'status_text': response.reason,
'content_type': response.headers.get('content-type', ''),
'content_length': response.headers.get('content-length', ''),
'server': response.headers.get('server', ''),
'found': True,
'sensitivity_level': self._get_sensitivity_level(hidden_file),
'timestamp': time.time()
}
# Try to get response body for analysis
try:
if response.status == 200:
content = await response.text()
result['content_preview'] = content[:500] + "..." if len(content) > 500 else content
# Check for sensitive content
result['sensitive_content'] = self._check_sensitive_content(content)
except Exception:
pass
progress.advance(progress.tasks[0].id)
return result
except Exception as e:
continue
progress.advance(progress.tasks[0].id)
return {'path': hidden_file, 'found': False}
def _check_sensitive_content(self, content: str) -> List[str]:
"""Check for sensitive content in response."""
sensitive_patterns = [
(r'password\s*=', 'Password found'),
(r'secret\s*=', 'Secret found'),
(r'api_key\s*=', 'API key found'),
(r'token\s*=', 'Token found'),
(r'database\s*=', 'Database config found'),
(r'mysql', 'MySQL reference found'),
(r'postgresql', 'PostgreSQL reference found'),
(r'redis', 'Redis reference found'),
(r'aws', 'AWS reference found'),
(r'google', 'Google API reference found'),
(r'facebook', 'Facebook API reference found'),
(r'twitter', 'Twitter API reference found'),
(r'private_key', 'Private key found'),
(r'public_key', 'Public key found'),
(r'\.env', 'Environment file content'),
(r'config', 'Configuration content'),
(r'admin', 'Admin content'),
(r'root', 'Root access content'),
]
found_patterns = []
for pattern, description in sensitive_patterns:
if re.search(pattern, content, re.IGNORECASE):
found_patterns.append(description)
return found_patterns
def display_results(self, results: List[Dict[str, Any]]):
"""Display hidden file scanning results."""
if not results:
console.print("❌ No hidden files discovered")
return
# Group by sensitivity level
sensitivity_groups = {
1: "🔴 Critical",
2: "🟡 High",
3: "🟠 Medium",
4: "🟢 Low",
5: "⚪ Info"
}
# Summary table
table = Table(title="🔍 Hidden File Scanning Results")
table.add_column("Sensitivity", style="cyan")
table.add_column("Path", style="yellow")
table.add_column("Status", style="green")
table.add_column("Content-Type", style="blue")
table.add_column("Sensitive Content", style="red")
for result in results[:20]: # Show first 20
sensitivity = sensitivity_groups.get(result.get('sensitivity_level', 5), "⚪ Info")
status_color = "green" if result['status_code'] == 200 else "yellow"
sensitive_content = ", ".join(result.get('sensitive_content', []))[:30]
table.add_row(
sensitivity,
result['path'],
f"[{status_color}]{result['status_code']}[/{status_color}]",
result.get('content_type', '')[:30],
sensitive_content
)
if len(results) > 20:
table.add_row(f"... and {len(results) - 20} more", "", "", "", "")
console.print(table)
# Group by sensitivity level
grouped_results = {}
for result in results:
level = result.get('sensitivity_level', 5)
if level not in grouped_results:
grouped_results[level] = []
grouped_results[level].append(result)
# Show sensitivity summary
sensitivity_table = Table(title="📊 Sensitivity Level Summary")
sensitivity_table.add_column("Level", style="cyan")
sensitivity_table.add_column("Description", style="yellow")
sensitivity_table.add_column("Count", style="green")
sensitivity_table.add_column("Examples", style="blue")
for level in sorted(grouped_results.keys()):
count = len(grouped_results[level])
description = sensitivity_groups.get(level, "Unknown")
examples = ", ".join([r['path'] for r in grouped_results[level][:3]])
if len(grouped_results[level]) > 3:
examples += f" (+{len(grouped_results[level]) - 3} more)"
sensitivity_table.add_row(str(level), description, str(count), examples)
console.print(sensitivity_table)
async def scan_hidden_files_for_crawler(base_url: str, wordlist_path: Optional[str] = None) -> List[Dict[str, Any]]:
"""
Scan for hidden files for use in the crawler.
Args:
base_url: Base URL to scan
wordlist_path: Path to custom wordlist
Returns:
List of discovered hidden files
"""
async with HiddenFileScanner(base_url, wordlist_path) as scanner:
results = await scanner.scan_hidden_files()
scanner.display_results(results)
return results
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage: python hidden_file_scanner.py <base_url> [wordlist_path]")
sys.exit(1)
base_url = sys.argv[1]
wordlist_path = sys.argv[2] if len(sys.argv) > 2 else None
asyncio.run(scan_hidden_files_for_crawler(base_url, wordlist_path))