#!/usr/bin/env python3 """Walidacja: dla proby URL-i z klasy SAFE_HTTPS porownuje odpowiedz http i https (dlugosc, typ, finalny URL), zeby wykryc falszywe 200 (strona bledu HTML).""" import json import concurrent.futures as cf import ssl import random import urllib.request import urllib.error UA = {"User-Agent": "curl/8.7.1"} d = json.load(open("/tmp/http-urlaudit.json")) safe = [i["expanded"] for i in d["SAFE_HTTPS"]] random.seed(11) sample = random.sample(safe, 40) def info(url): ctx = ssl.create_default_context() req = urllib.request.Request(url, headers=dict(UA, Range="bytes=0-1")) try: with urllib.request.urlopen(req, timeout=25, context=ctx) as r: return { "status": r.status, "final": r.geturl(), "ctype": r.headers.get("Content-Type", ""), "clen": r.headers.get("Content-Length", ""), "crange": r.headers.get("Content-Range", ""), } except urllib.error.HTTPError as e: return {"status": e.code, "final": e.geturl(), "ctype": e.headers.get("Content-Type", ""), "clen": e.headers.get("Content-Length", ""), "crange": ""} except Exception as e: return {"status": "ERR", "final": "", "ctype": type(e).__name__, "clen": "", "crange": ""} def check(u): h = info(u) s = info("https://" + u[len("http://"):]) return u, h, s print(f"probka: {len(sample)} URL-i\n") suspect = 0 with cf.ThreadPoolExecutor(max_workers=10) as pool: for u, h, s in pool.map(check, sample): flag = " " if h["status"] == 200 and s["status"] == 200: if h["clen"] != s["clen"] or h["ctype"] != s["ctype"]: flag = "!!" suspect += 1 print(f"{flag} http={h['status']:<4} https={s['status']:<4} " f"len {h['clen'][:10]} vs {s['clen'][:10]} | {h['ctype'][:28]} vs {s['ctype'][:28]} | {u[:70]}") print() print(f"podejrzane (rozny content http vs https): {suspect}/{len(sample)}")