import html import json from reports.database import query_all, query_one from reports.template import ( ReportContext, card, page_begin, page_end, ) from reports.utils import ( format_datetime as format_report_datetime, ) from reports.map_popups import build_standard_popup def esc(value): if value is None: return "—" return html.escape( str(value), quote=True, ) def format_datetime(value): return format_report_datetime( value, "%d.%m.%y-%H.%M.%S" ) def format_popup_datetime(value): return format_report_datetime( value, "%d-%m-%Y-%H-%M-%S" ) def load_report_interval(conn): row = query_one( conn, """ SELECT MIN(start_time) AS first_time, MAX( COALESCE( end_time, start_time ) ) AS last_time FROM capture_sessions """, ) if not row: return ( None, None, ) return ( row["first_time"], row["last_time"], ) def load_access_points(conn): rows = query_all( conn, """ WITH ranked_observations AS ( SELECT obs.access_point_id, obs.session_id, obs.latitude, obs.longitude, obs.observed_at, obs.rssi, obs.channel, ROW_NUMBER() OVER ( PARTITION BY obs.access_point_id ORDER BY obs.observed_at DESC, obs.id DESC ) AS rn FROM access_point_observations AS obs WHERE obs.latitude IS NOT NULL AND obs.longitude IS NOT NULL AND obs.latitude != 0 AND obs.longitude != 0 ) SELECT ap.id, ap.bssid, ap.essid, ap.vendor, ap.encryption, ap.cipher, ap.akm, ap.has_handshake, ap.has_pmkid, ap.is_cracked, obs.latitude, obs.longitude, obs.session_id, obs.observed_at, obs.rssi, obs.channel FROM access_points AS ap JOIN ranked_observations AS obs ON obs.access_point_id = ap.id AND obs.rn = 1 ORDER BY obs.observed_at DESC, ap.bssid """, ) if not rows: return [] credentials = query_all( conn, """ WITH latest_observations AS ( SELECT obs.access_point_id, ROW_NUMBER() OVER ( PARTITION BY obs.access_point_id ORDER BY obs.observed_at DESC, obs.id DESC ) AS rn FROM access_point_observations AS obs WHERE obs.latitude IS NOT NULL AND obs.longitude IS NOT NULL AND obs.latitude != 0 AND obs.longitude != 0 ) SELECT c.access_point_id, c.password, c.source, c.verified FROM credentials AS c JOIN latest_observations AS obs ON obs.access_point_id = c.access_point_id AND obs.rn = 1 ORDER BY c.created_at DESC, c.id DESC """, ) credentials_by_ap = {} for credential in credentials: access_point_id = credential["access_point_id"] credentials_by_ap.setdefault( access_point_id, [], ).append({ "password": credential["password"], "source": credential["source"], "verified": credential["verified"], }) handshakes = query_all( conn, """ WITH ranked_observations AS ( SELECT obs.access_point_id, obs.session_id, ROW_NUMBER() OVER ( PARTITION BY obs.access_point_id ORDER BY obs.observed_at DESC, obs.id DESC ) AS rn FROM access_point_observations AS obs WHERE obs.latitude IS NOT NULL AND obs.longitude IS NOT NULL AND obs.latitude != 0 AND obs.longitude != 0 ) SELECT hs.access_point_id, hs.session_id, hs.type, hs.captured_at FROM handshakes AS hs JOIN ranked_observations AS obs ON obs.access_point_id = hs.access_point_id AND obs.session_id = hs.session_id AND obs.rn = 1 ORDER BY hs.captured_at DESC, hs.id DESC """, ) handshakes_by_ap_session = {} for handshake in handshakes: key = ( handshake["access_point_id"], handshake["session_id"], ) handshakes_by_ap_session.setdefault( key, [], ).append({ "type": handshake["type"], "captured_at": handshake["captured_at"], }) observations = [] for row in rows: observation = dict(row) access_point_id = observation["id"] session_id = observation["session_id"] observation["passwords"] = credentials_by_ap.get( access_point_id, [], ) observation["handshakes"] = ( handshakes_by_ap_session.get( (access_point_id, session_id), [], ) ) observations.append(observation) return observations def build_marker_js(observations): ap_features = [] handshake_features = [] pmkid_features = [] password_features = [] open_features = [] for row in observations: latitude = row["latitude"] longitude = row["longitude"] if ( latitude is None or longitude is None ): continue popup = build_standard_popup( essid=row["essid"], bssid=row["bssid"], vendor=row["vendor"], encryption=row["encryption"], cipher=row["cipher"], akm=row["akm"], channel=row["channel"], rssi=row["rssi"], passwords=row["passwords"], handshakes=row["handshakes"], observed_at=format_popup_datetime( row["observed_at"] ), latitude=latitude, longitude=longitude, access_point_url=( f"/reports/access-points/{row['bssid']}" ), ) feature = { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ float(longitude), float(latitude), ], }, "properties": { "popup": popup, }, } ap_features.append(feature) if row["has_handshake"]: handshake_features.append( feature ) if row["has_pmkid"]: pmkid_features.append( feature ) if row["is_cracked"] or row["passwords"]: password_features.append( feature ) if ( str(row["encryption"] or "").strip().upper() == "OPEN" ): open_features.append( feature ) return ( "const apFeatures = " + json.dumps( ap_features, ensure_ascii=False, ) + ";\n" "const handshakeFeatures = " + json.dumps( handshake_features, ensure_ascii=False, ) + ";\n" "const pmkidFeatures = " + json.dumps( pmkid_features, ensure_ascii=False, ) + ";\n" "const passwordFeatures = " + json.dumps( password_features, ensure_ascii=False, ) + ";\n" + "const openFeatures = " + json.dumps( open_features, ensure_ascii=False, ) + ";\n" ) def render_access_points_map( observations, context, ): marker_js = build_marker_js( observations ) maplibre_css_url = context.asset_url( "maplibre/maplibre-gl.css" ) maplibre_js_url = context.asset_url( "maplibre/maplibre-gl.js" ) map_html = """
""" map_html = map_html.replace( "__MAPLIBRE_CSS_URL__", esc(maplibre_css_url), ) map_html = map_html.replace( "__MAPLIBRE_JS_URL__", esc(maplibre_js_url), ) map_html = map_html.replace( "__MARKER_JS__", marker_js, ) return map_html def render_access_points_page( conn, context=None, ): if context is None: context = ReportContext( mode="server" ) first_time, last_time = ( load_report_interval(conn) ) observations = load_access_points( conn ) html = page_begin( "📡 Access Points", active="access_points", context=context, section_title="Reports", ) html += f"""