import html def escape_html(value): if value is None: return "—" return html.escape( str(value), quote=True, ) def display_value(value): if value is None or str(value).strip() == "": return "—" return escape_html(value) def format_security( encryption=None, cipher=None, akm=None, ): parts = [] for value in ( encryption, cipher, akm, ): if value is None: continue value = str(value).strip() if not value or value == "—": continue if value not in parts: parts.append(value) return " / ".join( escape_html(value) for value in parts ) or "—" def aggregate_credentials(credentials): """ Aggregate credentials by exact password value. A password is verified if at least one matching database record has verified=1. Sources are combined without duplicates. """ aggregated = {} for credential in credentials or []: password = credential["password"] if password is None or str(password) == "": continue password = str(password) if password not in aggregated: aggregated[password] = { "password": password, "verified": False, "sources": [], } item = aggregated[password] if credential["verified"]: item["verified"] = True source = credential["source"] if source is not None: source = str(source).strip() if source and source not in item["sources"]: item["sources"].append(source) return list(aggregated.values()) def format_credentials(credentials): aggregated = aggregate_credentials(credentials) if not aggregated: return "—" items = [] for credential in aggregated: password = escape_html( credential["password"] ) verified_class = ( "verified" if credential["verified"] else "unverified" ) verified_label = ( "Проверен" if credential["verified"] else "Не проверен" ) sources = ", ".join( escape_html(source) for source in credential["sources"] ) or "—" items.append( "
" f"{password}" f"
" f"{verified_label}" "
" f"
Source: {sources}
" "
" ) return "".join(items) def format_capture_materials( handshakes, ): """ Count logical EAPOL and PMKID records. This does not count packets or unique hashes. """ counts = { "EAPOL": 0, "PMKID": 0, } for handshake in handshakes or []: capture_type = str( handshake["type"] or "" ).strip().upper() if "PMKID" in capture_type: counts["PMKID"] += 1 elif "EAPOL" in capture_type: counts["EAPOL"] += 1 parts = [] for capture_type in ("EAPOL", "PMKID"): count = counts[capture_type] if count: parts.append( f"{capture_type} × {count}" ) return ", ".join(parts) if parts else "—" def build_standard_popup( *, essid=None, bssid=None, vendor=None, encryption=None, cipher=None, akm=None, channel=None, rssi=None, passwords=None, handshakes=None, observed_at=None, latitude=None, longitude=None, access_point_url=None, session_url=None, ): rows = [ f"{display_value(essid)}", f"BSSID: {display_value(bssid)}", f"Vendor: {display_value(vendor)}", ( "Security: " + format_security( encryption, cipher, akm, ) ), f"Channel: {display_value(channel)}", ] if rssi is not None: rows.append( f"RSSI: {escape_html(rssi)} dBm" ) rows.extend([ ( "Password:
" + format_credentials(passwords) ), ( "Материалы захвата: " + escape_html( format_capture_materials(handshakes) ) ), f"Дата наблюдения: {display_value(observed_at)}", ]) if latitude is not None and longitude is not None: coordinates = ( f"{float(latitude):.6f}, " f"{float(longitude):.6f}" ) rows.append( "Координаты: " f"{escape_html(coordinates)}" "
" "" ) if session_url: rows.append( f'' "Сессия наблюдения" ) if access_point_url: rows.append( f'' "Подробнее" ) return "
".join(rows)