vulpfest 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,886 @@
1
+ # scanner.py - Cyber Web Scanner v2
2
+ # Prinsip: setiap temuan berasal dari respons HTTP live dan disertai bukti.
3
+ import re
4
+ import ssl
5
+ import sys
6
+ import json
7
+ import time
8
+ import socket
9
+ from datetime import datetime, timezone
10
+ from urllib.parse import urlparse
11
+ from concurrent.futures import ThreadPoolExecutor
12
+
13
+ import requests
14
+ import urllib3
15
+ from bs4 import BeautifulSoup
16
+
17
+ from ssrf_guard import BlockedTarget, ResponseTooLarge, check_url, connect_public, guard_session
18
+ import deep_web
19
+ import dns_email
20
+ import repo_scan
21
+ import auth_scan
22
+
23
+ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
24
+
25
+ TIMEOUT = 8
26
+ UA = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
27
+ '(KHTML, like Gecko) Chrome/130.0 Safari/537.36 CyberWebScanner/2.0')
28
+
29
+ HEADER_INFO = {
30
+ 'Content-Security-Policy': 'Penting untuk mencegah serangan XSS.',
31
+ 'Strict-Transport-Security': 'Memaksa koneksi HTTPS.',
32
+ 'X-Content-Type-Options': 'Mencegah MIME-sniffing.',
33
+ 'X-Frame-Options': 'Mencegah clickjacking.',
34
+ 'Referrer-Policy': 'Membatasi kebocoran URL ke situs lain.',
35
+ }
36
+ LINK_RELS = {'stylesheet', 'icon', 'shortcut', 'preload', 'modulepreload', 'manifest', 'apple-touch-icon'}
37
+ MODULE_KEYS = ('security_headers', 'technology_info', 'robots_txt_info', 'exposed_git_info',
38
+ 'mixed_content_info', 'cookie_info', 'https_redirect_info', 'ssl_info',
39
+ 'sensitive_files', 'csp_quality', 'cors_info', 'email_security', 'cache_info', 'caa_info', 'tls_old_info', 'subdomain_info')
40
+
41
+
42
+ def _new_session():
43
+ s = requests.Session()
44
+ s.headers.update({
45
+ 'User-Agent': UA,
46
+ 'Accept': 'text/html,application/xhtml+xml,*/*;q=0.8',
47
+ 'Accept-Language': 'id,en;q=0.8',
48
+ })
49
+ return guard_session(s)
50
+
51
+
52
+ def _err(e):
53
+ return f'{e.__class__.__name__}: {str(e)[:200]}'
54
+
55
+
56
+ # --- Ambil halaman utama SATU kali, dipakai semua modul ---
57
+ FALLBACK_UAS = [UA, 'CyberWebScanner/2.0']
58
+
59
+
60
+ def fetch_main(session, url):
61
+ """Ambil halaman utama SATU kali. Beberapa WAF menggantung koneksi dari IP data center
62
+ untuk User-Agent tertentu, jadi bila timeout dicoba lagi dengan User-Agent cadangan."""
63
+ last = None
64
+ for ua in FALLBACK_UAS:
65
+ session.headers['User-Agent'] = ua
66
+ t0 = time.perf_counter()
67
+ ssl_error = None
68
+ try:
69
+ try:
70
+ r = session.get(url, timeout=(5, 12), verify=True)
71
+ except requests.exceptions.SSLError as e:
72
+ ssl_error = _err(e)
73
+ r = session.get(url, timeout=(5, 12), verify=False)
74
+ except requests.exceptions.Timeout as e:
75
+ last = e
76
+ continue
77
+ r.elapsed_ms = int((time.perf_counter() - t0) * 1000)
78
+ r.ua_used = ua
79
+ return r, ssl_error
80
+ raise last
81
+
82
+ def detect_block(resp):
83
+ h = resp.headers
84
+ if h.get('cf-mitigated', '').lower() == 'challenge':
85
+ return 'Diblokir tantangan bot Cloudflare'
86
+ vm = h.get('x-vercel-mitigated', '').lower()
87
+ if vm:
88
+ return f'Diblokir Vercel Security Checkpoint ({vm})'
89
+ if resp.status_code == 429:
90
+ return 'Dibatasi server (HTTP 429: terlalu banyak permintaan). Coba lagi beberapa menit lagi.'
91
+ if resp.status_code in (401, 403, 407, 503):
92
+ body = resp.text[:6000].lower()
93
+ markers = ('just a moment', 'cf-chl', 'attention required', 'captcha', 'access denied', 'request blocked',
94
+ 'security checkpoint', 'checking your browser', 'ddos protection', 'request unsuccessful', 'bot protection')
95
+ if any(m in body for m in markers):
96
+ return f'Diblokir WAF/proteksi bot (HTTP {resp.status_code})'
97
+ return f'Server menolak pemindai (HTTP {resp.status_code}); header halaman penolakan tidak mewakili situs.'
98
+ return None
99
+
100
+ # --- MODUL 1: HEADER KEAMANAN ---
101
+ def check_security_headers(resp):
102
+ h = resp.headers # CaseInsensitiveDict: tidak peka huruf besar/kecil
103
+ is_https = resp.url.startswith('https://')
104
+ found, missing, notes = {}, {}, []
105
+ csp = h.get('Content-Security-Policy', '')
106
+ for name, desc in HEADER_INFO.items():
107
+ val = h.get(name)
108
+ if val:
109
+ found[name] = val
110
+ elif name == 'X-Frame-Options' and 'frame-ancestors' in csp.lower():
111
+ found[name] = '(digantikan oleh CSP frame-ancestors)'
112
+ elif name == 'Strict-Transport-Security' and not is_https:
113
+ missing[name] = desc + ' (situs tidak dilayani lewat HTTPS)'
114
+ else:
115
+ missing[name] = desc
116
+ if 'Content-Security-Policy' in missing and h.get('Content-Security-Policy-Report-Only'):
117
+ notes.append('CSP hanya mode Report-Only (memantau, tidak memblokir).')
118
+ age = None
119
+ if found.get('Strict-Transport-Security'):
120
+ m = re.search(r'max-age\s*=\s*"?(\d+)', found['Strict-Transport-Security'], re.I)
121
+ age = int(m.group(1)) if m else 0
122
+ return {'ditemukan': found, 'tidak_ditemukan': missing, 'catatan': notes, 'hsts_max_age': age}
123
+
124
+
125
+ # --- MODUL 2: INFORMASI TEKNOLOGI ---
126
+ def find_tech_info(resp, soup):
127
+ h = resp.headers
128
+ info = {}
129
+ for key, label in (('Server', 'Server'), ('X-Powered-By', 'Powered-By'),
130
+ ('X-AspNet-Version', 'ASP.NET'), ('X-Generator', 'Generator')):
131
+ if h.get(key):
132
+ info[label] = h[key]
133
+ if 'cf-ray' in h:
134
+ info['CDN/WAF'] = 'Cloudflare'
135
+ gen = soup.find('meta', attrs={'name': re.compile(r'^generator$', re.I)})
136
+ if gen and gen.get('content'):
137
+ info['Generator (CMS)'] = gen['content']
138
+ return info
139
+
140
+
141
+ # --- MODUL 3: ROBOTS.TXT (di akar domain, isi divalidasi) ---
142
+ def check_robots_txt(session, base, verify):
143
+ url = base + '/robots.txt'
144
+ try:
145
+ r = session.get(url, timeout=TIMEOUT, verify=verify)
146
+ except requests.exceptions.RequestException as e:
147
+ return {'ditemukan': False, 'url': url, 'status_code': None, 'error': _err(e)}
148
+ ctype = r.headers.get('Content-Type', '').lower()
149
+ text = r.text if r.status_code == 200 else ''
150
+ valid = (r.status_code == 200 and 'html' not in ctype and not text.lstrip().startswith('<')
151
+ and re.search(r'^\s*(user-agent|disallow|allow|sitemap)\s*:', text, re.I | re.M))
152
+ if valid:
153
+ lines = [l.strip() for l in text.splitlines()]
154
+ return {
155
+ 'ditemukan': True, 'url': r.url, 'status_code': r.status_code,
156
+ 'paths': [l for l in lines if re.match(r'(dis)?allow\s*:', l, re.I)],
157
+ 'sitemaps': [l.split(':', 1)[1].strip() for l in lines if l.lower().startswith('sitemap:')],
158
+ 'isi': text[:3000],
159
+ }
160
+ note = None
161
+ if r.status_code == 200:
162
+ note = 'Server membalas 200 tetapi isinya bukan robots.txt (halaman catch-all).'
163
+ return {'ditemukan': False, 'url': r.url, 'status_code': r.status_code, 'catatan': note}
164
+
165
+
166
+ # --- MODUL 4: DIREKTORI .GIT (isi file wajib terverifikasi) ---
167
+ def check_exposed_git(session, base, verify):
168
+ detail = {}
169
+ checks = (('/.git/HEAD', r'^(ref:\s*refs/|[0-9a-f]{40}\s*$)'), ('/.git/config', r'^\s*\[core\]'))
170
+ for path, pattern in checks:
171
+ url = base + path
172
+ try:
173
+ r = session.get(url, timeout=TIMEOUT, verify=verify, allow_redirects=False)
174
+ except requests.exceptions.RequestException as e:
175
+ detail[path] = {'error': _err(e)}
176
+ continue
177
+ body = r.text[:500].strip() if r.status_code == 200 else ''
178
+ ok = bool(body) and bool(re.search(pattern, body, re.I | re.M)) and not body.startswith('<')
179
+ detail[path] = {'status_code': r.status_code, 'terverifikasi': ok}
180
+ if ok:
181
+ return {'ditemukan': True, 'status_code': 200, 'level_risiko': 'Kritis',
182
+ 'url': url, 'bukti': body[:200], 'detail': detail}
183
+ return {'ditemukan': False, 'status_code': detail.get('/.git/HEAD', {}).get('status_code'), 'detail': detail}
184
+
185
+
186
+ # --- MODUL 5: MIXED CONTENT (HTML halaman awal) ---
187
+ def check_mixed_content(resp, soup):
188
+ scope = 'Hanya HTML halaman awal (konten dari JavaScript tidak dicek)'
189
+ if not resp.url.startswith('https://'):
190
+ return {'ditemukan': False, 'status': 'Bukan situs HTTPS', 'cakupan': scope}
191
+ found = []
192
+ for tag in soup.find_all(['img', 'script', 'iframe', 'audio', 'video', 'source', 'embed', 'object', 'link', 'form']):
193
+ if tag.name == 'link':
194
+ rels = {x.lower() for x in (tag.get('rel') or [])}
195
+ if not rels & LINK_RELS:
196
+ continue
197
+ u = tag.get('href')
198
+ elif tag.name == 'form':
199
+ u = tag.get('action')
200
+ elif tag.name == 'object':
201
+ u = tag.get('data')
202
+ else:
203
+ u = tag.get('src')
204
+ if u and u.strip().lower().startswith('http://'):
205
+ found.append(f'<{tag.name}> {u.strip()}')
206
+ found = list(dict.fromkeys(found))
207
+ res = {'ditemukan': bool(found), 'cakupan': scope}
208
+ if found:
209
+ res.update(urls=found[:50], level_risiko='Menengah')
210
+ return res
211
+
212
+
213
+ # --- MODUL 6: COOKIE ---
214
+ def check_cookies(resp):
215
+ try:
216
+ raw = resp.raw.headers.getlist('Set-Cookie')
217
+ except Exception:
218
+ raw = []
219
+ bad = []
220
+ for c in raw:
221
+ low = c.lower()
222
+ miss = [flag for flag, key in (('Secure', 'secure'), ('HttpOnly', 'httponly'), ('SameSite', 'samesite'))
223
+ if not re.search(r';\s*' + key, low)]
224
+ if miss:
225
+ bad.append({'cookie': c.split('=', 1)[0].strip(), 'tidak_ada': miss})
226
+ return {'jumlah': len(raw), 'bermasalah': bad}
227
+
228
+
229
+ # --- MODUL 7: SERTIFIKAT SSL ---
230
+ def check_ssl(host, port=443):
231
+ try:
232
+ try:
233
+ import certifi
234
+ ctx = ssl.create_default_context(cafile=certifi.where())
235
+ except ImportError:
236
+ ctx = ssl.create_default_context()
237
+ with connect_public((host, port), timeout=TIMEOUT) as sock:
238
+ with ctx.wrap_socket(sock, server_hostname=host) as s:
239
+ cert = s.getpeercert()
240
+ tls = s.version()
241
+ exp = datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z').replace(tzinfo=timezone.utc)
242
+ issuer = dict(x[0] for x in cert.get('issuer', [])).get('organizationName', '-')
243
+ return {'valid': True, 'issuer': issuer, 'berlaku_sampai': exp.strftime('%Y-%m-%d'),
244
+ 'sisa_hari': (exp - datetime.now(timezone.utc)).days, 'tls_version': tls}
245
+ except ssl.SSLCertVerificationError as e:
246
+ return {'valid': False, 'error': getattr(e, 'verify_message', None) or str(e)[:200]}
247
+ except (OSError, ValueError) as e:
248
+ return {'valid': None, 'error': 'Tidak dapat memeriksa: ' + _err(e)}
249
+
250
+
251
+ # --- MODUL 8: REDIRECT HTTP KE HTTPS ---
252
+ def check_https_redirect(session, host):
253
+ try:
254
+ r = session.get(f'http://{host}/', timeout=TIMEOUT, verify=False)
255
+ return {'http_ke_https': r.url.startswith('https://'), 'url_akhir': r.url}
256
+ except requests.exceptions.RequestException as e:
257
+ return {'http_ke_https': None, 'error': _err(e)}
258
+
259
+
260
+ # --- MODUL 9: FILE SENSITIF LAIN (isi divalidasi) ---
261
+ SENSITIVE_FILES = {
262
+ '/.env': r'^[A-Z0-9_]+\s*=',
263
+ '/.DS_Store': r'^\x00\x00\x00\x01Bud1',
264
+ '/config.json': r'[:{]',
265
+ '/.well-known/security.txt': r'(?i)contact\s*:',
266
+ }
267
+
268
+ def check_sensitive_files(session, base, verify):
269
+ hits, checked = [], {}
270
+ for path, pattern in SENSITIVE_FILES.items():
271
+ url = base + path
272
+ try:
273
+ r = session.get(url, timeout=TIMEOUT, verify=verify, allow_redirects=False)
274
+ except requests.exceptions.RequestException as e:
275
+ checked[path] = _err(e); continue
276
+ body = r.text[:800] if r.status_code == 200 else ''
277
+ ok = (r.status_code == 200 and not body.lstrip().lower().startswith('<!doctype')
278
+ and not body.lstrip().startswith('<') and bool(re.search(pattern, body, re.M)))
279
+ checked[path] = f'HTTP {r.status_code}' + (' (cocok)' if ok else '')
280
+ if ok:
281
+ info = 'security.txt (praktik baik, bukan risiko)' if path == '/.well-known/security.txt' else None
282
+ hits.append({'path': path, 'url': url, 'bukti': body[:120].strip(), 'catatan': info})
283
+ return {'terekspos': [h for h in hits if '/.well-known' not in h['path']],
284
+ 'security_txt': any('/.well-known' in h['path'] for h in hits),
285
+ 'diperiksa': checked}
286
+
287
+
288
+ # --- MODUL 10: KUALITAS CSP ---
289
+ def analyze_csp(csp):
290
+ if not csp:
291
+ return {'ada': False}
292
+ dirs = {}
293
+ for part in csp.split(';'):
294
+ bits = part.strip().split()
295
+ if bits:
296
+ dirs[bits[0].lower()] = [b.lower() for b in bits[1:]]
297
+ script = dirs.get('script-src', dirs.get('default-src', []))
298
+ style = dirs.get('style-src', dirs.get('default-src', []))
299
+ weak, notes = [], []
300
+ has_nonce_hash = any(x.startswith(("'nonce-", "'sha256-", "'sha384-", "'sha512-")) for x in script)
301
+ if "'unsafe-inline'" in script and not has_nonce_hash:
302
+ weak.append("'unsafe-inline' pada script-src (mengizinkan skrip inline)")
303
+ if "'unsafe-eval'" in script:
304
+ weak.append("'unsafe-eval' pada script-src (mengizinkan eval)")
305
+ if any(x in ('*', 'http:', 'https:', 'data:') for x in script):
306
+ weak.append('sumber skrip terlalu luas (*, http:, https:, atau data:)')
307
+ if "'unsafe-inline'" in style:
308
+ notes.append("'unsafe-inline' pada style-src berisiko rendah dan tidak mengurangi skor (sesuai Mozilla Observatory)")
309
+ kurang = []
310
+ if 'object-src' not in dirs and 'default-src' not in dirs:
311
+ kurang.append('object-src')
312
+ elif 'object-src' in dirs and "'none'" not in dirs['object-src']:
313
+ kurang.append("object-src 'none'")
314
+ if 'base-uri' not in dirs:
315
+ kurang.append('base-uri')
316
+ if 'frame-ancestors' not in dirs:
317
+ kurang.append('frame-ancestors')
318
+ return {'ada': True, 'kelemahan': weak, 'kurang_direktif': kurang, 'catatan': notes,
319
+ 'tanpa_pembatas_skrip': 'script-src' not in dirs and 'default-src' not in dirs}
320
+
321
+ # --- MODUL 11: SALAH KONFIGURASI CORS ---
322
+ def check_cors(session, url, verify):
323
+ probe = 'https://evil.example.com'
324
+ try:
325
+ r = session.get(url, timeout=TIMEOUT, verify=verify, headers={'Origin': probe})
326
+ except requests.exceptions.RequestException as e:
327
+ return {'rentan': None, 'error': _err(e)}
328
+ acao = r.headers.get('Access-Control-Allow-Origin', '')
329
+ acac = r.headers.get('Access-Control-Allow-Credentials', '').lower()
330
+ reflected = acao == probe
331
+ return {'rentan': bool(reflected and acac == 'true'),
332
+ 'allow_origin': acao or '(tidak ada)',
333
+ 'refleksi_origin': reflected, 'allow_credentials': acac == 'true'}
334
+
335
+
336
+ # --- MODUL 14: DNS CAA ---
337
+ def check_caa(host):
338
+ root = host.lower().strip('.')
339
+ # Ambil CAA di domain dan satu tingkat di atasnya (CAA bisa diwarisi)
340
+ labels = root.split('.')
341
+ names = [root]
342
+ if len(labels) > 2:
343
+ names.append('.'.join(labels[-2:]))
344
+ issuers = []
345
+ for name in names:
346
+ recs = _dns_rr(name, 'CAA')
347
+ if recs:
348
+ for r in recs:
349
+ issuers.append(r)
350
+ break
351
+ clean = [x for x in issuers if re.search(r'\b(issue|issuewild|iodef)\b', x, re.I)]
352
+ cas = []
353
+ for x in clean:
354
+ m = re.search(r'"([^"]+)"', x)
355
+ if m and m.group(1).strip():
356
+ cas.append(m.group(1).strip())
357
+ return {'ada': bool(clean), 'issuers': clean[:10], 'ca': cas[:10]}
358
+
359
+
360
+ # --- MODUL 15: TLS LAMA (1.0/1.1 masih diterima?) ---
361
+ def check_old_tls(host, port=443):
362
+ import ssl as _ssl
363
+ try:
364
+ ctx = _ssl.SSLContext(_ssl.PROTOCOL_TLS_CLIENT)
365
+ ctx.check_hostname = False
366
+ ctx.verify_mode = _ssl.CERT_NONE
367
+ try:
368
+ ctx.maximum_version = _ssl.TLSVersion.TLSv1_1
369
+ ctx.minimum_version = _ssl.TLSVersion.TLSv1
370
+ except (ValueError, AttributeError):
371
+ return {'tls_lama_diterima': None, 'catatan': 'Tidak dapat diuji di lingkungan ini'}
372
+ with connect_public((host, port), timeout=TIMEOUT) as sock:
373
+ with ctx.wrap_socket(sock, server_hostname=host) as ss:
374
+ return {'tls_lama_diterima': True, 'versi': ss.version()}
375
+ except (_ssl.SSLError, OSError):
376
+ return {'tls_lama_diterima': False}
377
+ except Exception as e:
378
+ return {'tls_lama_diterima': None, 'catatan': _err(e)}
379
+
380
+ # --- MODUL 16: SUBDOMAIN TAKEOVER (PASIF) ---
381
+ SUBDOMAIN_PREFIXES = ('www', 'api', 'app', 'blog', 'mail', 'dev', 'staging', 'cdn', 'shop', 'docs', 'test', 'portal')
382
+
383
+ # layanan pihak ketiga -> (penanda CNAME, fingerprint "layanan kosong")
384
+ TAKEOVER_FINGERPRINTS = (
385
+ ('github.io', "there isn't a github pages site here"),
386
+ ('github.io', 'for root urls (like http://example.com/) you must provide an index'),
387
+ ('herokudns.com', "no such app"),
388
+ ('herokuapp.com', "no such app"),
389
+ ('s3.amazonaws.com', 'nosuchbucket'),
390
+ ('amazonaws.com', 'nosuchbucket'),
391
+ ('ghost.io', 'the thing you were looking for is no longer here'),
392
+ ('cargocollective.com', "404 not found"),
393
+ ('wordpress.com', 'do you want to register'),
394
+ ('pantheonsite.io', 'the gods are wise'),
395
+ ('zendesk.com', 'help center closed'),
396
+ ('readthedocs.io', "unknown domain"),
397
+ ('netlify.app', 'not found - request id'),
398
+ ('fastly.net', 'fastly error: unknown domain'),
399
+ ('surge.sh', 'project not found'),
400
+ ('bitbucket.io', 'repository not found'),
401
+ ('helpscoutdocs.com', 'no settings were found for this company'),
402
+ ('wpengine.com', "the site you were looking for couldn't be found"),
403
+ )
404
+ THIRD_PARTY_HOSTS = tuple(sorted(set(h for h, _ in TAKEOVER_FINGERPRINTS)))
405
+
406
+ def check_subdomain_takeover(session, root, verify):
407
+ """Pasif: untuk subdomain umum yang CNAME-nya mengarah ke layanan pihak ketiga,
408
+ cek apakah layanan tujuannya kosong (pola 'dangling'). Hanya membaca DNS & HTTP GET.
409
+ Tidak pernah mengklaim atau mendaftarkan apa pun."""
410
+ base = '.'.join(root.split('.')[-2:]) if root.count('.') >= 1 else root
411
+ hasil, rentan = [], []
412
+ for pre in SUBDOMAIN_PREFIXES:
413
+ fqdn = f'{pre}.{base}'
414
+ cnames = _dns_rr(fqdn, 'CNAME')
415
+ if not cnames:
416
+ continue
417
+ target = cnames[0].strip().rstrip('.').lower()
418
+ host = next((h for h in THIRD_PARTY_HOSTS if target.endswith(h)), None)
419
+ if not host:
420
+ continue # CNAME internal / bukan layanan yang kita kenali -> abaikan
421
+ entry = {'subdomain': fqdn, 'cname': target, 'layanan': host, 'status': 'Perlu dicek manual'}
422
+ try:
423
+ r = session.get(f'https://{fqdn}', timeout=TIMEOUT, verify=False, allow_redirects=True)
424
+ body = (r.text or '')[:8000].lower()
425
+ fp = next((p for h, p in TAKEOVER_FINGERPRINTS if h == host and p in body), None)
426
+ if fp:
427
+ entry['status'] = 'Rentan'
428
+ entry['bukti'] = fp
429
+ rentan.append(entry)
430
+ elif r.status_code < 400:
431
+ entry['status'] = 'Aktif (bukan masalah)'
432
+ else:
433
+ entry['status'] = f'Perlu dicek manual (HTTP {r.status_code})'
434
+ except requests.exceptions.RequestException:
435
+ entry['status'] = 'Perlu dicek manual (tidak merespons)'
436
+ hasil.append(entry)
437
+ return {'diperiksa': hasil, 'rentan': rentan, 'base': base}
438
+
439
+ # --- MODUL 13: HEADER CACHE HALAMAN SENSITIF ---
440
+ SENSITIVE_PATHS = ('/dashboard', '/account', '/profile', '/admin', '/settings')
441
+
442
+ def check_cache_headers(session, base, verify, home_text='', home_len=0):
443
+ """Halaman yang mungkin berisi data pengguna tidak boleh di-cache publik.
444
+ Hanya membaca header respons; tidak mengirim data atau login.
445
+ Menolak soft-200: path yang isinya sama dengan beranda (situs catch-all/SPA)
446
+ tidak dinilai, karena itu bukan halaman tersendiri."""
447
+ hasil = []
448
+ home_head = (home_text or '')[:2000]
449
+ for path in SENSITIVE_PATHS:
450
+ url = base + path
451
+ try:
452
+ r = session.get(url, timeout=TIMEOUT, verify=verify, allow_redirects=False)
453
+ except requests.exceptions.RequestException:
454
+ continue
455
+ if r.status_code != 200:
456
+ continue # redirect ke login / 404 = aman, tidak dinilai
457
+ body = r.text or ''
458
+ # Soft-200: isinya sama/sangat mirip dengan beranda -> catch-all, lewati.
459
+ if home_len and abs(len(body) - home_len) < max(40, home_len * 0.02):
460
+ continue
461
+ if home_head and body[:2000] == home_head:
462
+ continue
463
+ ctype = r.headers.get('Content-Type', '').lower()
464
+ if 'html' not in ctype and ctype:
465
+ continue # bukan halaman HTML
466
+ cc = r.headers.get('Cache-Control', '').lower()
467
+ aman = any(k in cc for k in ('no-store', 'private', 'no-cache'))
468
+ hasil.append({'path': path, 'cache_control': r.headers.get('Cache-Control', '(tidak ada)'), 'aman': aman})
469
+ bermasalah = [h for h in hasil if not h['aman']]
470
+ return {'diperiksa': hasil, 'bermasalah': bermasalah}
471
+
472
+ # --- MODUL 12: SPF & DMARC (via DNS-over-HTTPS) ---
473
+ def _dns_txt(name):
474
+ try:
475
+ r = requests.get('https://cloudflare-dns.com/dns-query',
476
+ params={'name': name, 'type': 'TXT'},
477
+ headers={'accept': 'application/dns-json'}, timeout=TIMEOUT)
478
+ return [a.get('data', '').strip('"') for a in r.json().get('Answer', [])]
479
+ except Exception:
480
+ return None
481
+
482
+ def _dns_rr(name, rrtype):
483
+ try:
484
+ r = requests.get('https://cloudflare-dns.com/dns-query',
485
+ params={'name': name, 'type': rrtype},
486
+ headers={'accept': 'application/dns-json'}, timeout=TIMEOUT)
487
+ return [a.get('data', '').strip() for a in r.json().get('Answer', [])]
488
+ except Exception:
489
+ return None
490
+
491
+
492
+ def check_email_security(host):
493
+ h0 = host.lower().strip('.')
494
+ platforms = ('onrender.com', 'vercel.app', 'herokuapp.com', 'github.io', 'netlify.app', 'pages.dev',
495
+ 'web.app', 'firebaseapp.com', 'azurewebsites.net', 'fly.dev', 'railway.app', 'replit.app',
496
+ 'glitch.me', 'appspot.com', 'blogspot.com', 'wordpress.com')
497
+ plat = next((p for p in platforms if h0.endswith('.' + p)), None)
498
+ if plat:
499
+ return {'domain': h0, 'error': f'Dilewati karena ini subdomain platform {plat}; DNS email dikelola penyedia platform'}
500
+ parts = host.lower().strip('.').split('.')
501
+ sld = {'co', 'ac', 'go', 'or', 'sch', 'web', 'my', 'biz', 'mil', 'net', 'com', 'org', 'edu', 'gov', 'ponpes', 'desa'}
502
+ n = 3 if len(parts) >= 3 and len(parts[-1]) == 2 and parts[-2] in sld else 2
503
+ root = '.'.join(parts[-n:])
504
+ spf_rec = _dns_txt(root)
505
+ dmarc_rec = _dns_txt('_dmarc.' + root)
506
+ if spf_rec is None and dmarc_rec is None:
507
+ return {'domain': root, 'error': 'Lookup DNS gagal'}
508
+ spf = next((t for t in (spf_rec or []) if t.lower().startswith('v=spf1')), None)
509
+ dmarc = next((t for t in (dmarc_rec or []) if t.lower().startswith('v=dmarc1')), None)
510
+ pol = None
511
+ if dmarc:
512
+ m = re.search(r'\bp\s*=\s*(none|quarantine|reject)', dmarc, re.I)
513
+ pol = m.group(1).lower() if m else None
514
+ return {'domain': root, 'spf': bool(spf), 'dmarc': bool(dmarc), 'dmarc_policy': pol}
515
+
516
+ # --- PERHITUNGAN SKOR (setiap pengurangan tercatat) ---
517
+ SEVERITY = {'Kritis': 'critical', 'Tinggi': 'high', 'Menengah': 'medium', 'Rendah': 'low'}
518
+ CATEGORY_LABELS = {'config': 'Konfigurasi', 'code': 'Kode dan dependensi', 'identity': 'Identitas dan akses', 'ops': 'Operasional'}
519
+ # Bobot cakupan per mode (lihat blueprint): mode yang belum dijalankan dihitung sebagai belum dicakup.
520
+ COVERAGE_MODES = (
521
+ ('surface', 'Permukaan', 40),
522
+ ('deep_web', 'Web mendalam', 25),
523
+ ('repo', 'Repositori', 20),
524
+ ('auth', 'Terautentikasi', 15),
525
+ )
526
+
527
+
528
+ def coverage_info(modes=('surface',)):
529
+ done = [m for m in COVERAGE_MODES if m[0] in modes]
530
+ return {
531
+ 'modes': [m[0] for m in done],
532
+ 'label': ' + '.join(m[1] for m in done) or '-',
533
+ 'percent': sum(m[2] for m in done),
534
+ 'not_checked': [m[1] for m in COVERAGE_MODES if m[0] not in modes],
535
+ }
536
+
537
+
538
+ def grade_from(score, criticals=0):
539
+ """Grade dari skor, dibatasi oleh jumlah temuan Kritis (1 temuan: maksimal D, 2 atau lebih: F)."""
540
+ grade = 'A' if score >= 90 else 'B' if score >= 80 else 'C' if score >= 70 else 'D' if score >= 60 else 'F'
541
+ if criticals >= 2:
542
+ return 'F'
543
+ if criticals == 1 and grade in ('A', 'B', 'C'):
544
+ return 'D'
545
+ return grade
546
+
547
+
548
+ def calculate_security_score(results):
549
+ modes = results.get('coverage_modes', ('surface',))
550
+ coverage = coverage_info(modes)
551
+ if not results.get('valid', True):
552
+ return {'score': 0, 'grade': 'N/A', 'grade_capped': False, 'coverage': coverage_info(()), 'subscores': {}, 'findings': [{
553
+ 'id': 'SCAN-INVALID', 'level': 'Info', 'severity': 'info', 'points': 0, 'category': 'ops',
554
+ 'track': 'beginner', 'confidence': 'high',
555
+ 'finding': 'Pemindaian tidak valid: ' + results.get('invalid_reason', '-'),
556
+ 'recommendation': 'Skor tidak dihitung agar tidak menyesatkan. Coba lagi nanti atau pastikan situs tidak memblokir pemindai.',
557
+ }]}
558
+ score = 100
559
+ findings = []
560
+
561
+ def add(fid, points, level, text, rec, category='config', track='beginner', confidence='high'):
562
+ nonlocal score
563
+ score -= points
564
+ findings.append({
565
+ 'id': fid, 'finding': f'Risiko {level}: {text}', 'title': text, 'recommendation': rec,
566
+ 'poin': -points, 'points': -points, 'level': level, 'severity': SEVERITY[level],
567
+ 'category': category, 'track': track, 'confidence': confidence,
568
+ })
569
+
570
+ is_https = str(results.get('evidence', {}).get('final_url', '')).startswith('https://')
571
+
572
+ if results.get('exposed_git_info', {}).get('ditemukan'):
573
+ add('GIT-EXPOSED', 50, 'Kritis', 'Direktori .git terekspos (isi file terverifikasi).',
574
+ 'Hapus folder .git dari server atau blokir akses ke /.git/ di web server.', 'ops')
575
+
576
+ ssl_info = results.get('ssl_info', {})
577
+ if not is_https:
578
+ add('NO-HTTPS', 30, 'Tinggi', 'Situs tidak dilayani lewat HTTPS.',
579
+ "Pasang sertifikat TLS (mis. Let's Encrypt) dan alihkan semua trafik ke HTTPS.")
580
+ elif ssl_info.get('valid') is False:
581
+ add('SSL-INVALID', 30, 'Tinggi', f"Sertifikat SSL tidak valid ({ssl_info.get('error', '-')}).",
582
+ 'Perbaiki atau perbarui sertifikat TLS agar dipercaya browser.')
583
+ elif isinstance(ssl_info.get('sisa_hari'), int) and ssl_info['sisa_hari'] < 14:
584
+ add('SSL-EXPIRING', 5, 'Rendah', f"Sertifikat SSL kedaluwarsa dalam {ssl_info['sisa_hari']} hari.",
585
+ 'Perpanjang sertifikat dan aktifkan perpanjangan otomatis.', 'ops')
586
+
587
+ if results.get('tls_old_info', {}).get('tls_lama_diterima') is True:
588
+ add('TLS-OLD', 10, 'Tinggi', 'Server masih menerima TLS 1.0/1.1 yang sudah usang.',
589
+ 'Nonaktifkan TLS 1.0 dan 1.1; izinkan hanya TLS 1.2 ke atas.', track='senior')
590
+ if is_https and results.get('caa_info', {}) and not results['caa_info'].get('ada') and not results['caa_info'].get('error'):
591
+ add('DNS-NO-CAA', 3, 'Rendah', 'Domain tidak punya DNS CAA.',
592
+ 'Tambahkan record CAA untuk membatasi CA mana yang boleh menerbitkan sertifikat.', 'ops', 'senior')
593
+ if is_https and results.get('https_redirect_info', {}).get('http_ke_https') is False:
594
+ add('HTTP-NO-REDIRECT', 10, 'Menengah', 'Versi http:// tidak dialihkan ke https://.', 'Tambahkan redirect 301 dari HTTP ke HTTPS.')
595
+
596
+ sh = results.get('security_headers', {})
597
+ missing = sh.get('tidak_ditemukan', {})
598
+ if is_https and 'Strict-Transport-Security' in missing:
599
+ add('HDR-NO-HSTS', 15, 'Tinggi', 'Header Strict-Transport-Security (HSTS) tidak ada.',
600
+ "Tambahkan 'Strict-Transport-Security: max-age=31536000; includeSubDomains'.")
601
+ age = sh.get('hsts_max_age')
602
+ if isinstance(age, int) and age < 15552000:
603
+ add('HDR-HSTS-SHORT', 5, 'Rendah', f'HSTS max-age terlalu pendek ({age} detik).', 'Naikkan max-age minimal 15552000 (6 bulan).', track='senior')
604
+ if 'Content-Security-Policy' in missing:
605
+ add('HDR-NO-CSP', 10, 'Menengah', 'Header Content-Security-Policy tidak ada.', "Tambahkan 'Content-Security-Policy' sesuai sumber aset situs.")
606
+ if 'X-Frame-Options' in missing:
607
+ add('HDR-NO-XFO', 10, 'Menengah', 'Header X-Frame-Options tidak ada (dan tidak ada CSP frame-ancestors).',
608
+ "Tambahkan 'X-Frame-Options: DENY' atau CSP 'frame-ancestors'.")
609
+ if 'X-Content-Type-Options' in missing:
610
+ add('HDR-NO-XCTO', 10, 'Menengah', 'Header X-Content-Type-Options tidak ada.', "Tambahkan 'X-Content-Type-Options: nosniff'.")
611
+ if 'Referrer-Policy' in missing:
612
+ add('HDR-NO-REFERRER', 5, 'Rendah', 'Header Referrer-Policy tidak ada.', "Tambahkan 'Referrer-Policy: strict-origin-when-cross-origin'.")
613
+
614
+ tech = results.get('technology_info', {})
615
+ leak = tech.get('Powered-By') or tech.get('ASP.NET')
616
+ if leak:
617
+ add('HDR-TECH-LEAK', 5, 'Rendah', f'Header membocorkan teknologi backend ({leak}).', "Hapus header 'X-Powered-By' / 'X-AspNet-Version'.")
618
+ server = str(tech.get('Server', ''))
619
+ if re.search(r'\d+\.\d+', server):
620
+ add('HDR-SERVER-VERSION', 5, 'Rendah', f'Header Server membocorkan versi ({server}).',
621
+ 'Sembunyikan versi server (mis. server_tokens off di Nginx).', confidence='medium')
622
+
623
+ mc = results.get('mixed_content_info', {})
624
+ if mc.get('ditemukan'):
625
+ add('MIXED-CONTENT', 10, 'Menengah', f"Ditemukan {len(mc.get('urls', []))} aset Mixed Content.", 'Muat semua aset lewat HTTPS.')
626
+
627
+ for h in results.get('cache_info', {}).get('bermasalah', []):
628
+ add('CACHE-SENSITIVE', 15, 'Tinggi', f"Halaman {h['path']} dapat di-cache (Cache-Control: {h['cache_control']}).",
629
+ "Set 'Cache-Control: no-store' pada halaman berisi data pengguna agar tidak tersimpan di cache bersama/CDN.",
630
+ 'identity', 'senior', 'medium')
631
+ bad = results.get('cookie_info', {}).get('bermasalah', [])
632
+ if bad:
633
+ names = ', '.join(c['cookie'] for c in bad[:5])
634
+ add('COOKIE-FLAGS', 5, 'Rendah', f'Cookie tanpa flag keamanan lengkap ({names}).', 'Set flag Secure, HttpOnly, dan SameSite.', 'identity')
635
+
636
+ for sd in results.get('subdomain_info', {}).get('rentan', []):
637
+ add('SUBDOMAIN-TAKEOVER', 40, 'Kritis', f"Subdomain {sd['subdomain']} rentan diambil alih (dangling CNAME ke {sd['layanan']}).",
638
+ f"Hapus record DNS untuk {sd['subdomain']} atau klaim kembali layanan di {sd['layanan']}.", 'ops', 'senior')
639
+ sf = results.get('sensitive_files', {})
640
+ for h in sf.get('terekspos', []):
641
+ add('FILE-EXPOSED', 40, 'Kritis', f"File sensitif terekspos ({h['path']}).",
642
+ f"Blokir akses publik ke {h['path']} di web server.", 'ops')
643
+
644
+ csp_q = results.get('csp_quality', {})
645
+ if csp_q.get('tanpa_pembatas_skrip'):
646
+ add('CSP-NO-SCRIPT-RESTRICTION', 10, 'Menengah',
647
+ 'CSP tidak membatasi skrip (tidak ada script-src atau default-src), sehingga hampir tidak melindungi dari XSS.',
648
+ "Tambahkan script-src (idealnya dengan nonce atau hash) atau default-src 'self' pada Content-Security-Policy.",
649
+ track='senior')
650
+ for w in csp_q.get('kelemahan', []):
651
+ add('CSP-WEAK', 5, 'Rendah', f'CSP lemah: {w}.', 'Perketat CSP; hindari unsafe-inline/unsafe-eval dan wildcard.', track='senior')
652
+ for d in csp_q.get('kurang_direktif', []):
653
+ add('CSP-MISSING-DIRECTIVE', 3, 'Rendah', f'CSP belum menetapkan {d}.', f"Tambahkan direktif {d} pada Content-Security-Policy.", track='senior')
654
+
655
+ cors = results.get('cors_info', {})
656
+ if cors.get('rentan'):
657
+ add('CORS-REFLECT', 20, 'Tinggi', 'CORS salah konfigurasi: origin sembarang dipantulkan dengan kredensial.',
658
+ 'Jangan pantulkan Origin; pakai allowlist dan hindari Allow-Credentials dengan Origin dinamis.', track='senior')
659
+
660
+ em = results.get('email_security', {})
661
+ if em and not em.get('error'):
662
+ if not em.get('spf'):
663
+ add('EMAIL-NO-SPF', 5, 'Rendah', f"Domain {em['domain']} tidak punya SPF.", 'Tambahkan record SPF (v=spf1 ...).', 'ops', 'senior')
664
+ if not em.get('dmarc'):
665
+ add('EMAIL-NO-DMARC', 5, 'Rendah', f"Domain {em['domain']} tidak punya DMARC.", 'Tambahkan record DMARC (v=DMARC1; p=...).', 'ops', 'senior')
666
+ elif em.get('dmarc_policy') == 'none':
667
+ add('EMAIL-DMARC-NONE', 3, 'Rendah', f"DMARC domain {em['domain']} hanya p=none (memantau saja).", 'Naikkan ke p=quarantine atau p=reject.', 'ops', 'senior')
668
+
669
+ dw = results.get('deep_web') or {}
670
+ if dw and not dw.get('error'):
671
+ for x in dw.get('secrets', []):
672
+ if x.get('severity') == 'critical':
673
+ add('JS-SECRET', 40, 'Kritis', f"Kunci rahasia terbaca di JavaScript publik: {x['type']} ({x['masked']}, {x['file']}).",
674
+ 'Cabut (revoke) kunci itu sekarang, buat kunci baru, lalu pindahkan pemakaiannya ke sisi server. Menghapus dari kode saja tidak cukup.',
675
+ 'code', 'beginner', x.get('confidence', 'high'))
676
+ else:
677
+ add('JS-PUBLIC-KEY', 3, 'Rendah', f"Kunci publik ditemukan di JavaScript: {x['type']} ({x['masked']}).",
678
+ 'Kunci ini memang terlihat di browser; pastikan dibatasi (HTTP referrer, API yang diizinkan, kuota) di konsol penyedianya.',
679
+ 'code', 'beginner', 'medium')
680
+ for x in dw.get('backups', []):
681
+ add('BACKUP-EXPOSED', 40, 'Kritis', f"File backup terbuka untuk publik ({x['path']}, {x['kind']}).",
682
+ f"Hapus {x['path']} dari server web; simpan backup di luar folder publik dan enkripsi.", 'ops')
683
+ for x in dw.get('config_files', []):
684
+ add('CONFIG-EXPOSED', 40, 'Kritis', f"File konfigurasi rahasia terbuka ({x['path']}: {x['kind']}).",
685
+ f"Blokir akses ke {x['path']}, lalu ganti semua kata sandi dan token yang ada di dalamnya.", 'ops')
686
+ for x in dw.get('ops', []):
687
+ add('OPS-ENDPOINT', 15, 'Tinggi', f"Endpoint operasional terbuka untuk publik ({x['path']}: {x['kind']}).",
688
+ f"Matikan atau lindungi {x['path']} dengan autentikasi atau pembatasan IP.", 'ops', 'senior')
689
+ if dw.get('debug'):
690
+ add('DEBUG-EXPOSED', 15, 'Tinggi', f"Halaman error menampilkan informasi debug ({dw['debug'][0]['kind']}).",
691
+ 'Matikan mode debug di produksi dan tampilkan halaman error umum tanpa jejak kode.', 'ops', 'beginner', 'medium')
692
+ if dw.get('sourcemaps'):
693
+ add('SOURCEMAP-EXPOSED', 10, 'Menengah', f"Source map terbuka ({len(dw['sourcemaps'])} file, mis. {dw['sourcemaps'][0]}).",
694
+ 'Jangan unggah file .map ke produksi atau batasi aksesnya (di Next.js: jangan menyalakan productionBrowserSourceMaps).', 'code')
695
+ for x in dw.get('libraries', []):
696
+ add('JS-LIB-VULN', 10, 'Menengah', f"Library JavaScript usang: {x['name']} {x['version']}.", x['issue'] + '.', 'code', 'beginner', 'medium')
697
+ if dw.get('api_docs'):
698
+ add('API-DOCS-EXPOSED', 5, 'Rendah', f"Dokumentasi API terbuka di produksi ({', '.join(x['path'] for x in dw['api_docs'][:3])}).",
699
+ 'Batasi dokumentasi API untuk tim internal, atau pastikan tidak memuat endpoint yang tidak untuk publik.', 'ops')
700
+ if dw.get('graphql'):
701
+ add('GRAPHQL-INTROSPECTION', 5, 'Rendah', f"Introspeksi GraphQL aktif ({dw['graphql'][0]['path']}).",
702
+ 'Matikan introspeksi di produksi dan batasi kedalaman serta jumlah query.', 'ops', 'senior')
703
+ if dw.get('sri_missing'):
704
+ add('SRI-MISSING', 3, 'Rendah', f"Skrip pihak ketiga tanpa Subresource Integrity ({', '.join(dw['sri_missing'][:3])}).",
705
+ "Tambahkan atribut integrity dan crossorigin pada skrip dari CDN agar perubahan isi skrip terdeteksi browser.", 'config', 'senior', 'medium')
706
+
707
+ de = results.get('dns_email') or {}
708
+ if de and not de.get('error'):
709
+ known = {sd.get('subdomain') for sd in results.get('subdomain_info', {}).get('rentan', [])}
710
+ for sd in de.get('takeover', []):
711
+ if sd['subdomain'] in known:
712
+ continue
713
+ add('SUBDOMAIN-TAKEOVER-CT', 40, 'Kritis', f"Subdomain {sd['subdomain']} kemungkinan dapat diambil alih ({sd['layanan']}: {sd['bukti']}).",
714
+ f"Hapus record DNS {sd['subdomain']} (CNAME ke {sd['cname']}) atau klaim kembali layanannya.", 'ops', 'senior', sd.get('confidence', 'medium'))
715
+ m = de.get('email') or {}
716
+ if m and not m.get('error'):
717
+ if m.get('spf_records', 0) > 1:
718
+ add('SPF-MULTIPLE', 5, 'Rendah', f"Domain {m['domain']} punya {m['spf_records']} record SPF, sehingga SPF menjadi tidak valid.",
719
+ 'Gabungkan menjadi satu record v=spf1.', 'ops', 'senior')
720
+ if m.get('spf'):
721
+ if m.get('spf_all') == '+':
722
+ add('SPF-PASS-ALL', 10, 'Menengah', 'SPF memakai +all, sehingga server mana pun boleh mengirim email atas nama domain ini.',
723
+ 'Ganti +all dengan ~all atau -all.', 'ops', 'senior')
724
+ elif m.get('spf_all') is None:
725
+ add('SPF-NO-ALL', 3, 'Rendah', 'SPF tidak diakhiri mekanisme all.', 'Akhiri SPF dengan ~all atau -all.', 'ops', 'senior')
726
+ if (m.get('spf_lookups') or 0) > 10:
727
+ add('SPF-TOO-MANY-LOOKUPS', 5, 'Rendah', f"SPF membutuhkan lebih dari 10 DNS lookup ({m['spf_lookups']}+), sehingga sebagian penerima menganggapnya tidak valid.",
728
+ 'Kurangi include atau ratakan (flatten) SPF.', 'ops', 'senior')
729
+ if not m.get('has_mx') and m.get('spf_all') != '-':
730
+ add('SPF-NONSENDING-NOT-STRICT', 3, 'Rendah', f"Domain {m['domain']} tidak menerima email (tanpa MX) tetapi SPF-nya tidak menolak semua pengirim.",
731
+ "Untuk domain yang tidak mengirim email, pakai SPF 'v=spf1 -all' dan DMARC p=reject.", 'ops', 'senior')
732
+ if m.get('dmarc') and not m.get('dmarc_rua'):
733
+ add('DMARC-NO-RUA', 2, 'Rendah', 'DMARC tidak punya alamat laporan (rua), sehingga penyalahgunaan domain tidak terpantau.',
734
+ 'Tambahkan rua=mailto:alamat@domain pada record DMARC.', 'ops', 'senior')
735
+ if m.get('dkim_checked') and not m.get('dkim_selectors'):
736
+ add('DKIM-NOT-FOUND', 2, 'Rendah', 'DKIM tidak ditemukan pada selector umum.',
737
+ 'Aktifkan DKIM di layanan pengirim email. Abaikan bila layanan Anda memakai selector khusus yang sudah aktif.', 'ops', 'senior', 'low')
738
+ if m.get('has_mx') and not m.get('mta_sts'):
739
+ add('MTA-STS-MISSING', 3, 'Rendah', 'Domain menerima email tetapi belum memakai MTA-STS.',
740
+ 'Pasang record _mta-sts dan kebijakan di https://mta-sts.<domain>/.well-known/mta-sts.txt agar email masuk wajib terenkripsi.', 'ops', 'senior')
741
+
742
+ rp = results.get('repo') or {}
743
+ if rp and not rp.get('error'):
744
+ for item in repo_scan.findings(rp):
745
+ add(*item)
746
+
747
+ ap = results.get('auth') or {}
748
+ if ap and not ap.get('error'):
749
+ for item in auth_scan.findings(ap):
750
+ add(*item)
751
+
752
+ score = max(0, score)
753
+ criticals = sum(1 for f in findings if f['severity'] == 'critical')
754
+ grade = grade_from(score, criticals)
755
+ capped = grade != grade_from(score, 0)
756
+ subscores = {}
757
+ for cat in CATEGORY_LABELS:
758
+ if cat == 'code' and not ({'repo', 'deep_web'} & set(modes)):
759
+ subscores[cat] = None # belum diperiksa
760
+ continue
761
+ if cat == 'identity' and 'auth' not in modes:
762
+ subscores[cat] = None
763
+ continue
764
+ subscores[cat] = max(0, 100 + sum(f['points'] for f in findings if f['category'] == cat))
765
+ if not findings:
766
+ findings.append({'id': 'NO-ISSUES', 'level': 'Info', 'severity': 'info', 'points': 0, 'category': 'config',
767
+ 'track': 'beginner', 'confidence': 'high',
768
+ 'finding': 'Tidak ditemukan masalah pada pemeriksaan dasar.',
769
+ 'recommendation': 'Pertahankan konfigurasi dan pindai ulang secara berkala.'})
770
+ return {'score': score, 'grade': grade, 'grade_capped': capped, 'findings': findings,
771
+ 'coverage': coverage, 'subscores': subscores, 'category_labels': CATEGORY_LABELS}
772
+
773
+ # --- FUNGSI UTAMA ---
774
+ def run_full_scan(target_url, modes=('surface', 'deep_web')):
775
+ raw = (target_url or '').strip()
776
+ auto_scheme = not re.match(r'^https?://', raw, re.I)
777
+ url = 'https://' + raw if auto_scheme else raw
778
+ session = _new_session()
779
+ results = {'target': url, 'valid': True,
780
+ 'scanned_at': datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')}
781
+ try:
782
+ check_url(url)
783
+ except BlockedTarget as e:
784
+ results.update(valid=False, invalid_reason=str(e))
785
+ for k in MODULE_KEYS:
786
+ results[k] = {'error': str(e)}
787
+ results['score_info'] = calculate_security_score(results)
788
+ return results
789
+ except requests.exceptions.RequestException:
790
+ pass # DNS gagal dll.: biarkan alur biasa yang menanganinya
791
+ try:
792
+ try:
793
+ resp, ssl_err = fetch_main(session, url)
794
+ except requests.exceptions.RequestException as e0:
795
+ if not auto_scheme or isinstance(e0, (requests.exceptions.Timeout, BlockedTarget, ResponseTooLarge)):
796
+ raise
797
+ url = 'http://' + raw
798
+ results['target'] = url
799
+ resp, ssl_err = fetch_main(session, url)
800
+ except requests.exceptions.RequestException as e:
801
+ if isinstance(e, (BlockedTarget, ResponseTooLarge)):
802
+ msg = str(e)
803
+ elif isinstance(e, requests.exceptions.Timeout):
804
+ msg = 'Situs tidak merespons dalam batas waktu. Kemungkinan proteksi bot situs memblokir server pemindai (IP data center), walaupun situs bisa dibuka di browser.'
805
+ else:
806
+ msg = f'Tidak dapat terhubung ke situs ({e.__class__.__name__}).'
807
+ results.update(valid=False, invalid_reason=msg)
808
+ for k in MODULE_KEYS:
809
+ results[k] = {'error': msg}
810
+ results['score_info'] = calculate_security_score(results)
811
+ return results
812
+
813
+ p = urlparse(resp.url)
814
+ base = f'{p.scheme}://{p.netloc}'
815
+ verify = ssl_err is None
816
+ results['evidence'] = {
817
+ 'final_url': resp.url,
818
+ 'status_code': resp.status_code,
819
+ 'response_time_ms': resp.elapsed_ms,
820
+ 'user_agent': getattr(resp, 'ua_used', UA),
821
+ 'redirects': [f'{h.status_code} {h.url}' for h in resp.history],
822
+ 'headers': dict(resp.headers),
823
+ }
824
+ block = detect_block(resp)
825
+ if block or resp.status_code >= 500:
826
+ results.update(valid=False, invalid_reason=block or f'Server membalas HTTP {resp.status_code}.')
827
+
828
+ soup = BeautifulSoup(resp.text, 'html.parser')
829
+ results['security_headers'] = check_security_headers(resp)
830
+ results['technology_info'] = find_tech_info(resp, soup)
831
+ results['mixed_content_info'] = check_mixed_content(resp, soup)
832
+ results['cookie_info'] = check_cookies(resp)
833
+ results['csp_quality'] = analyze_csp(resp.headers.get('Content-Security-Policy', ''))
834
+ alt = resp.headers.get('alt-svc', '')
835
+ results['protocol_info'] = {'http3': 'h3' in alt.lower(), 'alt_svc': alt[:200]}
836
+
837
+ jobs = {
838
+ 'robots_txt_info': lambda: check_robots_txt(session, base, verify),
839
+ 'exposed_git_info': lambda: check_exposed_git(session, base, verify),
840
+ 'https_redirect_info': lambda: check_https_redirect(session, p.hostname),
841
+ 'sensitive_files': lambda: check_sensitive_files(session, base, verify),
842
+ 'cors_info': lambda: check_cors(session, resp.url, verify),
843
+ 'email_security': lambda: check_email_security(p.hostname),
844
+ 'cache_info': lambda: check_cache_headers(session, base, verify, resp.text, len(resp.text or '')),
845
+ 'caa_info': lambda: check_caa(p.hostname),
846
+ 'subdomain_info': lambda: check_subdomain_takeover(session, p.hostname, verify),
847
+ }
848
+ if p.scheme != 'https':
849
+ results['ssl_info'] = {'valid': None, 'error': 'Situs tidak memakai HTTPS'}
850
+ elif ssl_err:
851
+ results['ssl_info'] = {'valid': False, 'error': ssl_err}
852
+ else:
853
+ jobs['ssl_info'] = lambda: check_ssl(p.hostname, p.port or 443)
854
+ jobs['tls_old_info'] = lambda: check_old_tls(p.hostname, p.port or 443)
855
+
856
+ with ThreadPoolExecutor(max_workers=4) as ex:
857
+ futures = {k: ex.submit(fn) for k, fn in jobs.items()}
858
+ for k, f in futures.items():
859
+ try:
860
+ results[k] = f.result()
861
+ except Exception as e:
862
+ results[k] = {'error': _err(e)}
863
+
864
+ if 'deep_web' in modes and results.get('valid', True):
865
+ em = results.get('email_security') or {}
866
+ with ThreadPoolExecutor(max_workers=2) as ex:
867
+ f_web = ex.submit(deep_web.run, session, base, resp, soup, verify)
868
+ f_dns = ex.submit(dns_email.run, session, em['domain'], TAKEOVER_FINGERPRINTS) if em.get('domain') and not em.get('error') else None
869
+ try:
870
+ results['deep_web'] = f_web.result()
871
+ except Exception as e:
872
+ results['deep_web'] = {'error': _err(e)}
873
+ if f_dns is not None:
874
+ try:
875
+ results['dns_email'] = f_dns.result()
876
+ except Exception as e:
877
+ results['dns_email'] = {'error': _err(e)}
878
+ results['coverage_modes'] = ['surface'] + (['deep_web'] if results.get('deep_web') and not results['deep_web'].get('error') else [])
879
+
880
+ results['score_info'] = calculate_security_score(results)
881
+ return results
882
+
883
+
884
+ if __name__ == '__main__':
885
+ target = sys.argv[1] if len(sys.argv) > 1 else 'example.com'
886
+ print(json.dumps(run_full_scan(target), indent=2))