-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader_server.py
More file actions
565 lines (533 loc) · 16.2 KB
/
header_server.py
File metadata and controls
565 lines (533 loc) · 16.2 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
"""
Header Analyzer - OSINT Tool
Local proxy server for the GUI. Fetches HTTP headers and returns analysis as JSON.
Dependencies:
pip install flask flask-cors requests
Python >= 3.11 recommended.
"""
import json
import socket
import ipaddress
import requests
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
HEADER_DB = {
"strict-transport-security": {
"category": "security",
"risk": "red",
"risk_label": "Critical",
"description": "Forces HTTPS. Missing = downgrade attacks possible.",
"good": True,
},
"content-security-policy": {
"category": "security",
"risk": "red",
"risk_label": "Critical",
"description": "Controls allowed content sources. Missing = XSS risk.",
"good": True,
},
"x-frame-options": {
"category": "security",
"risk": "yellow",
"risk_label": "Medium",
"description": "Prevents clickjacking. Deprecated by CSP frame-ancestors.",
"good": True,
},
"x-content-type-options": {
"category": "security",
"risk": "yellow",
"risk_label": "Medium",
"description": "Prevents MIME-type sniffing. Should be 'nosniff'.",
"good": True,
},
"referrer-policy": {
"category": "security",
"risk": "yellow",
"risk_label": "Medium",
"description": "Controls referrer info sent with requests.",
"good": True,
},
"permissions-policy": {
"category": "security",
"risk": "yellow",
"risk_label": "Medium",
"description": "Controls browser feature access (camera, mic, geolocation).",
"good": True,
},
"cross-origin-opener-policy": {
"category": "security",
"risk": "yellow",
"risk_label": "Medium",
"description": "Isolates browsing context. Mitigates Spectre attacks.",
"good": True,
},
"cross-origin-embedder-policy": {
"category": "security",
"risk": "yellow",
"risk_label": "Medium",
"description": "Controls cross-origin resource embedding.",
"good": True,
},
"cross-origin-resource-policy": {
"category": "security",
"risk": "yellow",
"risk_label": "Medium",
"description": "Controls cross-origin resource loading.",
"good": True,
},
"x-xss-protection": {
"category": "security",
"risk": "yellow",
"risk_label": "Medium",
"description": "Legacy XSS filter. Deprecated — use CSP instead.",
"good": True,
},
"server": {
"category": "fingerprint",
"risk": "red",
"risk_label": "Exposure",
"description": "Reveals web server software and version.",
"good": False,
},
"x-powered-by": {
"category": "fingerprint",
"risk": "red",
"risk_label": "Exposure",
"description": "Reveals backend language/framework (PHP, ASP.NET, etc.).",
"good": False,
},
"x-aspnet-version": {
"category": "fingerprint",
"risk": "red",
"risk_label": "Exposure",
"description": "Reveals exact ASP.NET version. Remove immediately.",
"good": False,
},
"x-aspnetmvc-version": {
"category": "fingerprint",
"risk": "red",
"risk_label": "Exposure",
"description": "Reveals ASP.NET MVC version.",
"good": False,
},
"x-generator": {
"category": "fingerprint",
"risk": "red",
"risk_label": "Exposure",
"description": "Reveals CMS or generator (WordPress, Drupal, etc.).",
"good": False,
},
"x-drupal-cache": {
"category": "fingerprint",
"risk": "yellow",
"risk_label": "Exposure",
"description": "Reveals Drupal CMS usage.",
"good": False,
},
"x-wordpress-cache": {
"category": "fingerprint",
"risk": "yellow",
"risk_label": "Exposure",
"description": "Reveals WordPress CMS usage.",
"good": False,
},
"via": {
"category": "fingerprint",
"risk": "yellow",
"risk_label": "Info",
"description": "Reveals proxy/CDN infrastructure path.",
"good": False,
},
"x-varnish": {
"category": "fingerprint",
"risk": "yellow",
"risk_label": "Info",
"description": "Reveals Varnish cache usage and request IDs.",
"good": False,
},
"x-cache": {
"category": "cache",
"risk": "green",
"risk_label": "Info",
"description": "Cache status from CDN or proxy.",
"good": False,
},
"x-cache-hits": {
"category": "cache",
"risk": "green",
"risk_label": "Info",
"description": "Number of cache hits.",
"good": False,
},
"cf-cache-status": {
"category": "cache",
"risk": "green",
"risk_label": "Info",
"description": "Cloudflare cache status.",
"good": False,
},
"cf-ray": {
"category": "cdn",
"risk": "green",
"risk_label": "Info",
"description": "Cloudflare request ID. Confirms Cloudflare CDN.",
"good": False,
},
"cf-connecting-ip": {
"category": "cdn",
"risk": "green",
"risk_label": "Info",
"description": "Original client IP as seen by Cloudflare.",
"good": False,
},
"content-type": {
"category": "general",
"risk": "green",
"risk_label": "Normal",
"description": "MIME type of the response body.",
"good": True,
},
"content-length": {
"category": "general",
"risk": "green",
"risk_label": "Normal",
"description": "Size of the response body in bytes.",
"good": True,
},
"content-encoding": {
"category": "general",
"risk": "green",
"risk_label": "Normal",
"description": "Compression method used (gzip, br, etc.).",
"good": True,
},
"transfer-encoding": {
"category": "general",
"risk": "green",
"risk_label": "Normal",
"description": "Transfer encoding (chunked, etc.).",
"good": True,
},
"cache-control": {
"category": "cache",
"risk": "green",
"risk_label": "Normal",
"description": "Caching directives for browsers and proxies.",
"good": True,
},
"expires": {
"category": "cache",
"risk": "green",
"risk_label": "Normal",
"description": "Legacy cache expiry date.",
"good": True,
},
"etag": {
"category": "cache",
"risk": "green",
"risk_label": "Normal",
"description": "Resource version identifier for conditional requests.",
"good": True,
},
"last-modified": {
"category": "cache",
"risk": "green",
"risk_label": "Normal",
"description": "Last modification timestamp of the resource.",
"good": True,
},
"location": {
"category": "general",
"risk": "green",
"risk_label": "Normal",
"description": "Redirect target URL.",
"good": True,
},
"access-control-allow-origin": {
"category": "cors",
"risk": "yellow",
"risk_label": "CORS",
"description": "CORS allowed origins. '*' = open to all domains.",
"good": True,
},
"access-control-allow-methods": {
"category": "cors",
"risk": "green",
"risk_label": "CORS",
"description": "Allowed HTTP methods for CORS requests.",
"good": True,
},
"access-control-allow-headers": {
"category": "cors",
"risk": "green",
"risk_label": "CORS",
"description": "Allowed headers in CORS requests.",
"good": True,
},
"access-control-allow-credentials": {
"category": "cors",
"risk": "yellow",
"risk_label": "CORS",
"description": "Allows cookies in cross-origin requests.",
"good": True,
},
"set-cookie": {
"category": "cookies",
"risk": "yellow",
"risk_label": "Cookie",
"description": "Sets a cookie. Check for HttpOnly, Secure, SameSite flags.",
"good": True,
},
"www-authenticate": {
"category": "auth",
"risk": "yellow",
"risk_label": "Auth",
"description": "Authentication challenge. Reveals auth mechanism.",
"good": True,
},
"authorization": {
"category": "auth",
"risk": "red",
"risk_label": "Critical",
"description": "Authorization token. Should never appear in responses.",
"good": False,
},
"date": {
"category": "general",
"risk": "green",
"risk_label": "Normal",
"description": "Date and time the response was sent.",
"good": True,
},
"age": {
"category": "cache",
"risk": "green",
"risk_label": "Normal",
"description": "Seconds the response has been cached.",
"good": True,
},
"vary": {
"category": "cache",
"risk": "green",
"risk_label": "Normal",
"description": "Response varies based on these request headers.",
"good": True,
},
"alt-svc": {
"category": "general",
"risk": "green",
"risk_label": "Normal",
"description": "Advertises alternative services (HTTP/3, QUIC).",
"good": True,
},
"link": {
"category": "general",
"risk": "green",
"risk_label": "Normal",
"description": "Related resources (preload, canonical, etc.).",
"good": True,
},
"x-request-id": {
"category": "fingerprint",
"risk": "yellow",
"risk_label": "Info",
"description": "Internal request identifier. May reveal infrastructure.",
"good": False,
},
"x-runtime": {
"category": "fingerprint",
"risk": "yellow",
"risk_label": "Exposure",
"description": "Server-side processing time. Reveals performance characteristics.",
"good": False,
},
"x-response-time": {
"category": "fingerprint",
"risk": "yellow",
"risk_label": "Info",
"description": "Response time in ms. Reveals backend performance.",
"good": False,
},
"nel": {
"category": "general",
"risk": "green",
"risk_label": "Normal",
"description": "Network Error Logging configuration.",
"good": True,
},
"report-to": {
"category": "general",
"risk": "green",
"risk_label": "Normal",
"description": "Reporting endpoint for browser policy violations.",
"good": True,
},
"expect-ct": {
"category": "security",
"risk": "yellow",
"risk_label": "Medium",
"description": "Certificate Transparency enforcement. Deprecated.",
"good": True,
},
"pragma": {
"category": "cache",
"risk": "green",
"risk_label": "Normal",
"description": "Legacy HTTP/1.0 cache control directive.",
"good": True,
},
}
SECURITY_REQUIRED = [
"strict-transport-security",
"content-security-policy",
"x-frame-options",
"x-content-type-options",
"referrer-policy",
"permissions-policy",
]
FINGERPRINT_DANGEROUS = [
"server",
"x-powered-by",
"x-aspnet-version",
"x-aspnetmvc-version",
"x-generator",
]
def analyze_cookie(value: str) -> dict:
flags = []
issues = []
v_lower = value.lower()
if "httponly" in v_lower:
flags.append("HttpOnly")
else:
issues.append("missing HttpOnly")
if "secure" in v_lower:
flags.append("Secure")
else:
issues.append("missing Secure")
if "samesite" in v_lower:
if "samesite=none" in v_lower:
flags.append("SameSite=None")
issues.append("SameSite=None is risky")
elif "samesite=lax" in v_lower:
flags.append("SameSite=Lax")
elif "samesite=strict" in v_lower:
flags.append("SameSite=Strict")
else:
issues.append("missing SameSite")
return {"flags": flags, "issues": issues}
def analyze_cors(value: str) -> dict:
issues = []
if value.strip() == "*":
issues.append("wildcard origin — any domain can make requests")
return {"issues": issues}
@app.route("/analyze", methods=["GET", "POST"])
def analyze():
if request.method == "POST":
data = request.get_json()
url = data.get("url", "").strip()
else:
url = request.args.get("url", "").strip()
if not url:
return jsonify({"error": "No URL provided"}), 400
if not url.startswith("http"):
url = "https://" + url
try:
resp = requests.get(
url,
timeout=10,
allow_redirects=True,
headers={"User-Agent": "Mozilla/5.0 (compatible; HeaderAnalyzer-OSINT/1.0)"},
verify=False,
)
except requests.exceptions.SSLError:
try:
resp = requests.get(url, timeout=10, allow_redirects=True, verify=False,
headers={"User-Agent": "Mozilla/5.0 (compatible; HeaderAnalyzer-OSINT/1.0)"})
except Exception as e:
return jsonify({"error": str(e)}), 500
except Exception as e:
return jsonify({"error": str(e)}), 500
headers_raw = dict(resp.headers)
headers_lower = {k.lower(): v for k, v in headers_raw.items()}
analyzed = []
for name, value in headers_raw.items():
name_lower = name.lower()
info = HEADER_DB.get(name_lower, {
"category": "other",
"risk": "green",
"risk_label": "Normal",
"description": "Non-standard or custom header.",
"good": True,
})
entry = {
"name": name,
"value": value,
"risk": info["risk"],
"risk_label": info["risk_label"],
"category": info["category"],
"description": info["description"],
"notes": [],
}
if name_lower == "set-cookie":
cookie_analysis = analyze_cookie(value)
entry["notes"] = cookie_analysis["issues"]
if cookie_analysis["issues"]:
entry["risk"] = "yellow"
if name_lower == "access-control-allow-origin":
cors_analysis = analyze_cors(value)
entry["notes"] = cors_analysis["issues"]
if cors_analysis["issues"]:
entry["risk"] = "red"
entry["risk_label"] = "Critical"
analyzed.append(entry)
missing_security = []
for h in SECURITY_REQUIRED:
if h not in headers_lower:
info = HEADER_DB.get(h, {})
missing_security.append({
"name": h,
"risk": info.get("risk", "red"),
"risk_label": info.get("risk_label", "Missing"),
"description": info.get("description", ""),
})
exposed_fingerprint = []
for h in FINGERPRINT_DANGEROUS:
if h in headers_lower:
exposed_fingerprint.append({
"name": h,
"value": headers_lower[h],
})
counts = {"red": 0, "yellow": 0, "green": 0}
for h in analyzed:
counts[h["risk"]] = counts.get(h["risk"], 0) + 1
for h in missing_security:
counts["red"] += 1
try:
from urllib.parse import urlparse
hostname = urlparse(url).hostname
ip = socket.gethostbyname(hostname)
except Exception:
ip = "-"
result = {
"url": url,
"final_url": resp.url,
"status_code": resp.status_code,
"ip": ip,
"headers": analyzed,
"missing_security": missing_security,
"exposed_fingerprint": exposed_fingerprint,
"counts": counts,
"total": len(analyzed),
}
return jsonify(result)
@app.route("/ping")
def ping():
return jsonify({"status": "ok"})
if __name__ == "__main__":
import urllib3
urllib3.disable_warnings()
print("\n[*] Header Analyzer server running on http://localhost:5000")
print("[*] Open header_analyzer.html in your browser\n")
app.run(host="127.0.0.1", port=5000, debug=False)