#!/usr/bin/env python3 """Normalize FOFA CSV for CVE-2026-8065 — url column + target lists.""" from __future__ import annotations import csv import io import os import re import sys DIR = os.path.dirname(os.path.abspath(__file__)) HTTPS_PORTS = {443, 8443, 9443} # cert="Hitachi" is noisy — flag rows more likely RTU500 web / OT RTU_HOST_RE = re.compile( r"(rtu500|rtu-500|rtu500|hitachienergy|hitachi-energy|" r"solutions\.hitachienergy|substation|scada|cmu|rtutil|" r"remote.?terminal|grid.?automation)", re.I, ) RTU_TITLE_RE = re.compile( r"(rtu500|rtu\s*500|configuration management|remote terminal|hitachi energy|cmu)", re.I, ) NOISE_HOST_RE = re.compile( r"(edgekey|akamai|designcheck|lumada-auth|shop-|\.shop\.|finemax\.net)", re.I, ) def make_url(host, port_s): host = (host or "").strip() try: port = int(str(port_s or "443").strip() or 443) except ValueError: port = 443 if not host: return None if host.count(":") == 1: h, p = host.rsplit(":", 1) if p.isdigit(): host, port = h, int(p) host = host.strip("/") if port in HTTPS_PORTS: return "https://%s" % host if port == 443 else "https://%s:%d" % (host, port) if port == 80: return "http://%s" % host return "http://%s:%d" % (host, port) def rtu_priority(host, title): if NOISE_HOST_RE.search(host or ""): return "low" blob = "%s %s" % (host or "", title or "") if RTU_HOST_RE.search(blob) or RTU_TITLE_RE.search(blob): return "high" if "hitachi" in (host or "").lower(): return "medium" return "low" def format_csv(src_path): lines = open(src_path, encoding="utf-8-sig", errors="replace").read().splitlines() header_idx = None query = 'cert="Hitachi" && port="443"' for i, line in enumerate(lines): if line.startswith("# Query:"): query = line.split(":", 1)[-1].strip() if line.strip().startswith("host,"): header_idx = i break if header_idx is None: raise SystemExit("no CSV header in %s" % src_path) reader = csv.DictReader(lines[header_idx:]) seen = set() rows = [] for row in reader: host = (row.get("host") or "").strip() ip = (row.get("ip") or "").strip() port = str(row.get("port") or "").strip() title = (row.get("title") or "").strip() url = make_url(host, port) if not url or url in seen: continue seen.add(url) pri = rtu_priority(host, title) rows.append({ "host": host, "ip": ip, "port": port, "title": title, "url": url, "rtu_priority": pri, }) base = os.path.splitext(os.path.basename(src_path))[0] csv_out = src_path txt_all = os.path.join(DIR, "fofa_targets_1000.txt") txt_pri = os.path.join(DIR, "fofa_targets_rtu_priority.txt") buf = io.StringIO() buf.write("# FOFA — %d unique URLs (deduped)\n" % len(rows)) buf.write("# Query: %s\n" % query) buf.write("# CVE-2026-8065 PoC: python poc.py --list fofa_targets_rtu_priority.txt --mode check -j 20\n") buf.write("# Note: cert=Hitachi is broad; use rtu_priority=high first, then medium.\n") buf.write("host,ip,port,title,url,rtu_priority\n") w = csv.writer(buf, lineterminator="\n") for r in rows: w.writerow([r["host"], r["ip"], r["port"], r["title"], r["url"], r["rtu_priority"]]) with open(csv_out, "w", encoding="utf-8", newline="") as f: f.write(buf.getvalue()) high = [r for r in rows if r["rtu_priority"] == "high"] med = [r for r in rows if r["rtu_priority"] == "medium"] priority = high + med with open(txt_all, "w", encoding="utf-8") as f: f.write("# %d URLs | %s\n" % (len(rows), query[:100])) f.write("# authorized OT testing only\n") for r in rows: f.write(r["url"] + "\n") with open(txt_pri, "w", encoding="utf-8") as f: f.write("# %d priority URLs (high=%d medium=%d)\n" % (len(priority), len(high), len(med))) f.write("# python poc.py --list fofa_targets_rtu_priority.txt --mode check -j 20\n") for r in priority: f.write("%s\t%s\n" % (r["url"], r["rtu_priority"])) print("csv -> %s (%d rows)" % (csv_out, len(rows))) print("all txt -> %s" % txt_all) print("priority -> %s (high=%d medium=%d low=%d)" % ( txt_pri, len(high), len(med), len(rows) - len(priority), )) def main(): path = sys.argv[1] if len(sys.argv) > 1 else os.path.join(DIR, "fofa_csv_1000.csv") if not os.path.isabs(path): path = os.path.join(DIR, path) format_csv(path) if __name__ == "__main__": main()