-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawlerSQL.py
More file actions
286 lines (247 loc) · 10.7 KB
/
crawlerSQL.py
File metadata and controls
286 lines (247 loc) · 10.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
import os
import re
import time
import json
import logging
import argparse
import threading
from urllib.parse import urljoin, urlparse, urldefrag
from concurrent.futures import ThreadPoolExecutor, as_completed
import requests
from bs4 import BeautifulSoup
import urllib.robotparser
# ----------------- CONFIG -----------------
ALLOWED_EXT = {'.sql', '.xls', '.xlsx', '.zip', '.bak', '.tar', '.gz'}
COMMON_PATHS = [
"backup.sql", "db.sql", "database.sql", "dump.sql", "backup.zip", "db.zip",
"backup/backup.sql", "uploads/backup.sql", "export.xlsx", "export.xls"
]
COMMON_PARAMS = ["file", "download", "db", "dump", "path", "name", "f"]
USER_AGENT = "MiCrawlerLegal/4.0 (+https://example.com - contact@tuempresa.com)"
RATE_DELAY_DEFAULT = 1.0
MAX_WORKERS = 8
MAX_DOWNLOAD_WORKERS = 6
MAX_DEPTH = None
STATE_FILE = "crawler_state.json"
DOWNLOAD_DIR = os.path.join(os.getcwd(), "descargas_permitidas")
MAX_FILE_SIZE = 200 * 1024 * 1024 # 200 MB
REQUEST_TIMEOUT = 20
CONTENTTYPE_EXT = {
"application/sql": ".sql",
"application/x-sql": ".sql",
"text/x-sql": ".sql",
"application/zip": ".zip",
"application/x-gzip": ".gz",
"application/x-tar": ".tar",
"application/vnd.ms-excel": ".xls",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
"application/octet-stream": None,
}
STATIC_EXT = {".jpg", ".jpeg", ".png", ".gif", ".svg", ".ico", ".css", ".js", ".woff", ".woff2", ".ttf"}
# ----------------- LOGGING -----------------
logger = logging.getLogger("crawler")
logger.setLevel(logging.INFO)
fmt = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
sh = logging.StreamHandler()
sh.setFormatter(fmt)
logger.addHandler(sh)
fh = logging.FileHandler("crawler.log", encoding='utf-8')
fh.setFormatter(fmt)
logger.addHandler(fh)
# ----------------- GLOBALS -----------------
session = requests.Session()
session.headers.update({"User-Agent": USER_AGENT})
session.max_redirects = 10
robots_cache = {}
robots_lock = threading.Lock()
domain_last_request = {}
domain_lock = threading.Lock()
state_lock = threading.Lock()
# ----------------- UTIL -----------------
def normalizar_url(url: str) -> str:
url, _ = urldefrag(url)
return url.rstrip("/") if url.endswith("/") else url
def get_domain(url: str) -> str:
return urlparse(url).netloc.lower()
def wait_for_domain(domain: str, delay: float = RATE_DELAY_DEFAULT):
with domain_lock:
last = domain_last_request.get(domain)
now = time.time()
if last and now - last < delay:
time.sleep(delay - (now - last))
domain_last_request[domain] = time.time()
def cache_robots_for_domain(scheme_netloc: str):
with robots_lock:
if scheme_netloc in robots_cache:
return robots_cache[scheme_netloc]
rp = urllib.robotparser.RobotFileParser()
try:
rp.set_url(f"{scheme_netloc}/robots.txt")
rp.read()
except Exception:
rp = None
robots_cache[scheme_netloc] = rp
return rp
def can_fetch(url: str) -> bool:
parsed = urlparse(url)
base = f"{parsed.scheme}://{parsed.netloc}"
rp = cache_robots_for_domain(base)
return True if rp is None else rp.can_fetch(USER_AGENT, url)
def safe_filename(name: str) -> str:
return re.sub(r'[^A-Za-z0-9._-]', '_', name)
def content_type_to_ext(content_type: str):
if not content_type:
return None
return CONTENTTYPE_EXT.get(content_type.split(";")[0].strip().lower())
# ----------------- PERSISTENCE -----------------
def load_state():
if os.path.exists(STATE_FILE):
try:
with open(STATE_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
logger.warning("No se pudo leer state file, iniciando nuevo estado.")
return {"visited": [], "to_visit": [], "files": []}
def save_state(state):
with state_lock:
with open(STATE_FILE, "w", encoding="utf-8") as f:
json.dump(state, f, indent=2, ensure_ascii=False)
# ----------------- SCRAPING -----------------
def obtener_links(url: str):
try:
wait_for_domain(get_domain(url))
resp = session.get(url, timeout=REQUEST_TIMEOUT)
resp.raise_for_status()
soup = BeautifulSoup(resp.text, "html.parser")
return [normalizar_url(urljoin(url, a['href'])) for a in soup.find_all("a", href=True)]
except Exception:
return []
# ----------------- DESCARGA -----------------
def descargar_file(url: str, max_size=MAX_FILE_SIZE) -> bool:
if not can_fetch(url):
return False
try:
wait_for_domain(get_domain(url))
# HEAD para verificar tamaño
head = session.head(url, timeout=REQUEST_TIMEOUT, allow_redirects=True)
if head.status_code != 200:
return False
content_length = head.headers.get("Content-Length")
if content_length and content_length.isdigit() and int(content_length) > max_size:
logger.warning(f"[skip grande] {url}")
return False
with session.get(url, stream=True, timeout=REQUEST_TIMEOUT) as r:
if r.status_code != 200:
return False
ctype = r.headers.get("Content-Type", "").lower()
ext = content_type_to_ext(ctype)
fname = os.path.basename(urlparse(url).path) or "archivo_descargado"
base, file_ext = os.path.splitext(fname)
if file_ext.lower() not in ALLOWED_EXT and ext:
fname = base + ext
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
safe_name = safe_filename(fname)
destino = os.path.join(DOWNLOAD_DIR, safe_name)
total = 0
with open(destino, "wb") as f:
for chunk in r.iter_content(8192):
if chunk:
total += len(chunk)
if total > max_size:
f.close()
os.remove(destino)
return False
f.write(chunk)
logger.info(f"[descargado] {destino} ({total} bytes)")
return True
except Exception:
return False
# ----------------- CRAWLER -----------------
def worker_crawl(url, depth, visited, to_visit, files_to_download, max_depth):
if url in visited:
return
if not can_fetch(url):
logger.debug(f"[robots.txt] Denegado {url}")
return
visited.add(url)
logger.info(f"[visitando] {url}")
links = obtener_links(url)
# --- Detección de "Index of" ---
try:
resp = session.get(url, timeout=REQUEST_TIMEOUT)
if "index of" in resp.text.lower():
logger.warning(f"[index of] {url}")
links += [normalizar_url(urljoin(url, a['href'])) for a in BeautifulSoup(resp.text, "html.parser").find_all("a", href=True)]
except Exception:
pass
for link in links:
if link in visited:
continue
if any(link.lower().endswith(ext) for ext in STATIC_EXT):
continue
if max_depth is None or depth < max_depth:
to_visit.add((link, depth + 1))
if any(link.lower().endswith(ext) for ext in ALLOWED_EXT):
files_to_download.add(link)
# --- Brute force de paths ---
base = f"{urlparse(url).scheme}://{urlparse(url).netloc}/"
for path in COMMON_PATHS:
bruteforce_url = urljoin(base, path)
if bruteforce_url not in visited:
to_visit.add((bruteforce_url, depth + 1))
# --- Brute force de parámetros ---
for p in COMMON_PARAMS:
test_url = f"{url}?{p}=backup.sql"
if test_url not in visited:
to_visit.add((test_url, depth + 1))
def crawl(start_url: str, max_workers=MAX_WORKERS, max_depth=MAX_DEPTH):
start_url = normalizar_url(start_url)
state = load_state()
visited = set(state.get("visited", []))
to_visit = set((tuple(x) if isinstance(x, list) else x) for x in state.get("to_visit", [])) or {(start_url, 0)}
files_to_download = set(state.get("files", []))
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = []
while to_visit:
url, depth = to_visit.pop()
futures.append(executor.submit(worker_crawl, url, depth, visited, to_visit, files_to_download, max_depth))
if len(visited) % 20 == 0:
save_state({"visited": list(visited), "to_visit": list(to_visit), "files": list(files_to_download)})
for f in as_completed(futures):
pass
logger.info("[fin crawling] Iniciando descargas...")
with ThreadPoolExecutor(max_workers=MAX_DOWNLOAD_WORKERS) as dw_executor:
dw_futures = [dw_executor.submit(descargar_file, f) for f in files_to_download]
for _ in as_completed(dw_futures):
pass
save_state({"visited": list(visited), "to_visit": list(to_visit), "files": list(files_to_download)})
# ----------------- CLI -----------------
def main():
global MAX_WORKERS, MAX_DOWNLOAD_WORKERS, MAX_DEPTH, RATE_DELAY_DEFAULT, MAX_FILE_SIZE
parser = argparse.ArgumentParser(description="Crawler mejorado (auditoría). Usa con permisos.")
parser.add_argument("--url", "-u", help="URL inicial")
parser.add_argument("--workers", "-w", type=int, default=MAX_WORKERS, help="Hilos para crawling")
parser.add_argument("--dw", type=int, default=MAX_DOWNLOAD_WORKERS, help="Hilos para descargas")
parser.add_argument("--depth", "-d", type=int, default=-1, help="Profundidad máxima ( -1 = ilimitado )")
parser.add_argument("--rate", type=float, default=RATE_DELAY_DEFAULT, help="Delay mínimo por dominio (segundos)")
parser.add_argument("--max-size-mb", type=int, default=MAX_FILE_SIZE // (1024 * 1024), help="Tamaño máximo archivo (MB)")
parser.add_argument("--fresh", action="store_true", help="Ignorar estado previo y empezar desde cero")
args = parser.parse_args()
if not args.url:
args.url = input("Introduce la URL inicial (solo sitios donde tengas permiso): ").strip()
if not args.url.startswith("http"):
logger.error("La URL debe incluir http/https.")
return
MAX_WORKERS = args.workers
MAX_DOWNLOAD_WORKERS = args.dw
MAX_DEPTH = None if args.depth < 0 else args.depth
RATE_DELAY_DEFAULT = max(0.1, args.rate)
MAX_FILE_SIZE = max(1, args.max_size_mb) * 1024 * 1024
if args.fresh and os.path.exists(STATE_FILE):
os.remove(STATE_FILE)
logger.info("[estado] Estado anterior eliminado, empezando limpio.")
logger.info(f"Iniciando crawl en {args.url}")
logger.info(f"Workers: {MAX_WORKERS}, Download workers: {MAX_DOWNLOAD_WORKERS}, rate: {RATE_DELAY_DEFAULT}s, max file: {MAX_FILE_SIZE} bytes")
crawl(args.url, max_workers=MAX_WORKERS, max_depth=MAX_DEPTH)
if __name__ == "__main__":
main()