272 lines
7.0 KiB
Python
272 lines
7.0 KiB
Python
"""
|
|
GeoJSON data for the Auto Wi-Fi Verification capture map.
|
|
"""
|
|
|
|
import math
|
|
import sqlite3
|
|
from collections import defaultdict
|
|
|
|
from reports.database import close as close_database
|
|
from reports.database import connect as connect_database
|
|
from reports.map_popups import build_standard_popup
|
|
|
|
|
|
TECHNICAL_STATUSES = {
|
|
"CONNECTION_TIMEOUT",
|
|
"NETWORK_ERROR",
|
|
"UNKNOWN_ERROR",
|
|
}
|
|
|
|
|
|
def _valid_coordinates(latitude, longitude):
|
|
try:
|
|
latitude = float(latitude)
|
|
longitude = float(longitude)
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
if not math.isfinite(latitude) or not math.isfinite(longitude):
|
|
return None
|
|
|
|
if not -90 <= latitude <= 90:
|
|
return None
|
|
|
|
if not -180 <= longitude <= 180:
|
|
return None
|
|
|
|
return latitude, longitude
|
|
|
|
|
|
def _classify_access_point(credentials):
|
|
"""
|
|
Classify an AP using the latest attempt for each exact password.
|
|
|
|
Priority:
|
|
green - any verified credential or successful attempt;
|
|
red - every unique password has latest status INVALID_PASSWORD;
|
|
orange - at least one latest status is a technical error;
|
|
gray - all other cases.
|
|
"""
|
|
passwords = {}
|
|
|
|
for item in credentials:
|
|
password = item["password"]
|
|
state = passwords.setdefault(
|
|
password,
|
|
{
|
|
"verified": False,
|
|
"latest": None,
|
|
"latest_order": None,
|
|
},
|
|
)
|
|
|
|
if item["verified"]:
|
|
state["verified"] = True
|
|
|
|
if item["attempt_status"] == "SUCCESS":
|
|
state["verified"] = True
|
|
|
|
if item["attempt_status"]:
|
|
order = (
|
|
item["attempt_started_at"] or "",
|
|
item["attempt_id"] or 0,
|
|
)
|
|
if state["latest_order"] is None or order > state["latest_order"]:
|
|
state["latest_order"] = order
|
|
state["latest"] = item["attempt_status"]
|
|
|
|
if any(item["verified"] for item in passwords.values()):
|
|
return "green"
|
|
|
|
latest_statuses = [
|
|
item["latest"]
|
|
for item in passwords.values()
|
|
]
|
|
|
|
if latest_statuses and all(
|
|
status == "INVALID_PASSWORD"
|
|
for status in latest_statuses
|
|
):
|
|
return "red"
|
|
|
|
if any(
|
|
status in TECHNICAL_STATUSES
|
|
for status in latest_statuses
|
|
):
|
|
return "orange"
|
|
|
|
return "gray"
|
|
|
|
|
|
def build_auto_verification_geojson():
|
|
connection = connect_database()
|
|
|
|
try:
|
|
connection.row_factory = sqlite3.Row
|
|
|
|
rows = connection.execute(
|
|
"""
|
|
SELECT
|
|
ap.id AS access_point_id,
|
|
ap.bssid,
|
|
ap.essid,
|
|
ap.vendor,
|
|
ap.encryption,
|
|
ap.cipher,
|
|
ap.akm,
|
|
ap.channel,
|
|
ap.last_rssi,
|
|
ap.last_seen,
|
|
ap.last_latitude,
|
|
ap.last_longitude,
|
|
|
|
c.id AS credential_id,
|
|
c.password,
|
|
c.source,
|
|
c.verified,
|
|
|
|
a.id AS attempt_id,
|
|
a.status AS attempt_status,
|
|
a.started_at AS attempt_started_at
|
|
|
|
FROM access_points AS ap
|
|
|
|
INNER JOIN credentials AS c
|
|
ON c.access_point_id = ap.id
|
|
|
|
LEFT JOIN wifi_verification_attempts AS a
|
|
ON a.credential_id = c.id
|
|
|
|
WHERE
|
|
c.password IS NOT NULL
|
|
AND TRIM(c.password) <> ''
|
|
AND ap.last_latitude IS NOT NULL
|
|
AND ap.last_longitude IS NOT NULL
|
|
|
|
ORDER BY
|
|
ap.id,
|
|
c.id,
|
|
a.started_at DESC,
|
|
a.id DESC
|
|
"""
|
|
).fetchall()
|
|
|
|
finally:
|
|
close_database(connection)
|
|
|
|
access_points = {}
|
|
|
|
for row in rows:
|
|
ap_id = row["access_point_id"]
|
|
|
|
if ap_id not in access_points:
|
|
coordinates = _valid_coordinates(
|
|
row["last_latitude"],
|
|
row["last_longitude"],
|
|
)
|
|
if coordinates is None:
|
|
continue
|
|
|
|
access_points[ap_id] = {
|
|
"row": row,
|
|
"credentials": {},
|
|
}
|
|
|
|
if ap_id not in access_points:
|
|
continue
|
|
|
|
credential_id = row["credential_id"]
|
|
credentials = access_points[ap_id]["credentials"]
|
|
|
|
if credential_id not in credentials:
|
|
credentials[credential_id] = {
|
|
"id": credential_id,
|
|
"password": row["password"],
|
|
"source": row["source"],
|
|
"verified": bool(row["verified"]),
|
|
"attempt_id": None,
|
|
"attempt_status": None,
|
|
"attempt_started_at": None,
|
|
}
|
|
|
|
credential = credentials[credential_id]
|
|
|
|
if row["attempt_id"] is not None:
|
|
order = (
|
|
row["attempt_started_at"] or "",
|
|
row["attempt_id"],
|
|
)
|
|
current_order = (
|
|
credential["attempt_started_at"] or "",
|
|
credential["attempt_id"] or 0,
|
|
)
|
|
|
|
if credential["attempt_id"] is None or order > current_order:
|
|
credential["attempt_id"] = row["attempt_id"]
|
|
credential["attempt_status"] = row["attempt_status"]
|
|
credential["attempt_started_at"] = row["attempt_started_at"]
|
|
|
|
features = []
|
|
counts = {
|
|
"green": 0,
|
|
"red": 0,
|
|
"orange": 0,
|
|
"gray": 0,
|
|
}
|
|
|
|
for item in access_points.values():
|
|
row = item["row"]
|
|
credentials = list(item["credentials"].values())
|
|
|
|
if not credentials:
|
|
continue
|
|
|
|
coordinates = _valid_coordinates(
|
|
row["last_latitude"],
|
|
row["last_longitude"],
|
|
)
|
|
if coordinates is None:
|
|
continue
|
|
|
|
latitude, longitude = coordinates
|
|
status = _classify_access_point(credentials)
|
|
|
|
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["last_rssi"],
|
|
passwords=credentials,
|
|
handshakes=None,
|
|
observed_at=row["last_seen"],
|
|
latitude=latitude,
|
|
longitude=longitude,
|
|
)
|
|
|
|
features.append({
|
|
"type": "Feature",
|
|
"geometry": {
|
|
"type": "Point",
|
|
"coordinates": [longitude, latitude],
|
|
},
|
|
"properties": {
|
|
"status": status,
|
|
"popup": popup,
|
|
"bssid": row["bssid"] or "",
|
|
"essid": row["essid"] or "",
|
|
},
|
|
})
|
|
|
|
counts[status] += 1
|
|
|
|
return {
|
|
"type": "FeatureCollection",
|
|
"features": features,
|
|
"counts": counts,
|
|
"total": len(features),
|
|
}
|