Release WiFiTrip format v2.0.0

This commit is contained in:
2026-11-22 09:55:25 +03:00
parent 427f5759c9
commit cb2e659a57
9 changed files with 1117 additions and 3374 deletions
+155 -544
View File
@@ -24,7 +24,7 @@ logger = logging.getLogger(__name__)
WIFITRIP_FORMAT = "wifitrip"
WIFITRIP_VERSION = 1
WIFITRIP_VERSION = 2
SESSION_HASH_VERSION = 1
REQUIRED_FILES = (
@@ -34,7 +34,6 @@ REQUIRED_FILES = (
"access_points.json",
"observations.json",
"handshakes.json",
"credentials.json",
)
SHA256_RE = re.compile(r"^[0-9a-f]{64}$")
@@ -167,6 +166,12 @@ def _read_package(path):
"Archive contains duplicate file names."
)
if "credentials.json" in names:
raise WifitripError(
"Archive contains unsupported file: "
"credentials.json"
)
missing = [
name
for name in REQUIRED_FILES
@@ -212,7 +217,6 @@ def _validate_package(data):
access_points = data["access_points.json"]
observations = data["observations.json"]
handshakes = data["handshakes.json"]
credentials = data["credentials.json"]
_require_dict(
manifest,
@@ -238,10 +242,6 @@ def _validate_package(data):
handshakes,
"handshakes.json",
)
_require_list(
credentials,
"credentials.json",
)
if manifest.get("format") != WIFITRIP_FORMAT:
raise WifitripError(
@@ -659,97 +659,6 @@ def _validate_package(data):
f"handshakes.json[{index}].speed",
)
for index, credential in enumerate(
credentials
):
_require_dict(
credential,
f"credentials.json[{index}]",
)
session_hash = credential.get(
"session_hash"
)
bssid = credential.get(
"bssid"
)
handshake_key = credential.get(
"handshake_key"
)
_validate_hash(
session_hash,
f"credentials.json[{index}].session_hash",
)
_require_bssid(
bssid,
f"credentials.json[{index}].bssid",
)
if session_hash not in session_versions:
raise WifitripError(
"Credential references unknown session: "
f"{session_hash}"
)
if bssid not in ap_map:
raise WifitripError(
"Credential references unknown BSSID: "
f"{bssid}"
)
if handshake_key is not None:
_require_string(
handshake_key,
f"credentials.json[{index}].handshake_key",
)
if handshake_key not in handshake_keys.get(
session_hash,
set(),
):
raise WifitripError(
"Credential references unknown handshake_key: "
f"{session_hash}: {handshake_key}"
)
_require_string(
credential.get("password"),
f"credentials.json[{index}].password",
)
if not credential.get("password"):
raise WifitripError(
"Credential password must not be empty."
)
_require_string(
credential.get("source"),
f"credentials.json[{index}].source",
allow_none=True,
)
_require_string(
credential.get("created_at"),
f"credentials.json[{index}].created_at",
allow_none=True,
)
verified = credential.get(
"verified"
)
_require_integer(
verified,
f"credentials.json[{index}].verified",
allow_none=False,
)
if verified not in (0, 1):
raise WifitripError(
"Credential verified must be 0 or 1."
)
def _session_hash(conn, session_id):
@@ -891,7 +800,110 @@ def _zip_write_json(archive, filename, value):
)
def export_wifitrip(conn, output_path=None):
def select_wifitrip_session_ids(
conn,
mode="all",
limit=1,
date_from=None,
date_to=None,
):
if mode == "latest":
if str(limit).lower() == "all":
rows = conn.execute(
"""
SELECT id
FROM capture_sessions
ORDER BY start_time DESC, id DESC
"""
).fetchall()
else:
try:
limit = int(limit)
except (TypeError, ValueError):
raise WifitripError(
"Invalid session limit."
)
available_count = conn.execute(
"SELECT COUNT(*) FROM capture_sessions"
).fetchone()[0]
if limit < 1 or limit > available_count:
raise WifitripError(
"Session limit must be between 1 and "
f"{available_count}, or all."
)
rows = conn.execute(
"""
SELECT id
FROM capture_sessions
ORDER BY start_time DESC, id DESC
LIMIT ?
""",
(limit,),
).fetchall()
elif mode == "range":
try:
start_date = datetime.strptime(
date_from,
"%Y-%m-%d",
).date()
end_date = datetime.strptime(
date_to,
"%Y-%m-%d",
).date()
except (TypeError, ValueError):
raise WifitripError(
"A valid start and end date are required."
)
if start_date > end_date:
raise WifitripError(
"Start date must not be later than end date."
)
rows = conn.execute(
"""
SELECT id
FROM capture_sessions
WHERE substr(start_time, 1, 10)
BETWEEN ? AND ?
ORDER BY start_time DESC, id DESC
""",
(
start_date.isoformat(),
end_date.isoformat(),
),
).fetchall()
elif mode == "all":
rows = conn.execute(
"""
SELECT id
FROM capture_sessions
ORDER BY start_time DESC, id DESC
"""
).fetchall()
else:
raise WifitripError(
"Invalid session selection mode."
)
return [
row["id"]
for row in rows
]
def export_wifitrip(
conn,
output_path=None,
session_ids=None,
):
started = time.monotonic()
if output_path is None:
@@ -909,18 +921,47 @@ def export_wifitrip(conn, output_path=None):
exist_ok=True,
)
sessions = conn.execute(
"""
SELECT
id,
start_time,
end_time,
session_hash,
session_hash_version
FROM capture_sessions
ORDER BY id
"""
).fetchall()
if session_ids is None:
sessions = conn.execute(
"""
SELECT
id,
start_time,
end_time,
session_hash,
session_hash_version
FROM capture_sessions
ORDER BY id
"""
).fetchall()
else:
selected_ids = [
int(session_id)
for session_id in session_ids
]
if selected_ids:
placeholders = ",".join(
"?" for _ in selected_ids
)
sessions = conn.execute(
f"""
SELECT
id,
start_time,
end_time,
session_hash,
session_hash_version
FROM capture_sessions
WHERE id IN ({placeholders})
ORDER BY start_time DESC, id DESC
""",
selected_ids,
).fetchall()
else:
sessions = []
session_by_id = {}
exported_sessions = []
@@ -1118,7 +1159,6 @@ def export_wifitrip(conn, output_path=None):
session_ids,
).fetchall()
handshake_key_by_id = {}
handshake_records = {}
handshakes = []
@@ -1137,13 +1177,6 @@ def export_wifitrip(conn, output_path=None):
f"h{len(records) + 1:06d}"
)
handshake_key_by_id[
row["id"]
] = (
session_hash,
handshake_key,
)
records.append(
handshake_key
)
@@ -1163,111 +1196,6 @@ def export_wifitrip(conn, output_path=None):
}
)
session_ap_ids = {}
for row in observation_rows:
session_ap_ids.setdefault(
row["session_id"],
set(),
).add(
row["bssid"]
)
for row in handshake_rows:
session_ap_ids.setdefault(
row["session_id"],
set(),
).add(
row["bssid"]
)
credentials_rows = conn.execute(
"""
SELECT
credentials.id,
credentials.access_point_id,
credentials.handshake_id,
access_points.bssid,
credentials.password,
credentials.source,
credentials.created_at,
credentials.verified,
handshakes.session_id AS handshake_session_id
FROM credentials
JOIN access_points
ON access_points.id =
credentials.access_point_id
LEFT JOIN handshakes
ON handshakes.id =
credentials.handshake_id
ORDER BY credentials.id
"""
).fetchall()
credentials = []
for row in credentials_rows:
if row["handshake_session_id"] is not None:
session_id = row[
"handshake_session_id"
]
if session_id not in session_by_id:
continue
session_hash = session_by_id[
session_id
]["session_hash"]
handshake_ref = (
handshake_key_by_id.get(
row["handshake_id"]
)
)
handshake_key = (
handshake_ref[1]
if handshake_ref
else None
)
credentials.append(
{
"session_hash": session_hash,
"bssid": row["bssid"],
"handshake_key": handshake_key,
"password": row["password"],
"source": row["source"],
"created_at": row["created_at"],
"verified": row["verified"],
}
)
continue
for session_id, bssids in (
session_ap_ids.items()
):
if row["bssid"] not in bssids:
continue
if session_id not in session_by_id:
continue
credentials.append(
{
"session_hash": session_by_id[
session_id
]["session_hash"],
"bssid": row["bssid"],
"handshake_key": None,
"password": row["password"],
"source": row["source"],
"created_at": row["created_at"],
"verified": row["verified"],
}
)
bssids = set()
for observation in observations:
@@ -1280,11 +1208,6 @@ def export_wifitrip(conn, output_path=None):
handshake["bssid"]
)
for credential in credentials:
bssids.add(
credential["bssid"]
)
access_points = [
{
"bssid": bssid
@@ -1368,12 +1291,6 @@ def export_wifitrip(conn, output_path=None):
handshakes,
)
_zip_write_json(
archive,
"credentials.json",
credentials,
)
os.replace(
temporary_path,
output_path,
@@ -1408,9 +1325,6 @@ def export_wifitrip(conn, output_path=None):
"handshakes": len(
handshakes
),
"credentials": len(
credentials
),
"file": str(output_path),
"size": size,
"duration": duration,
@@ -1419,11 +1333,10 @@ def export_wifitrip(conn, output_path=None):
logger.info(
"Completed .wifitrip export: "
"sessions=%d observations=%d handshakes=%d "
"credentials=%d file=%s size=%d duration=%.3fs",
"file=%s size=%d duration=%.3fs",
result["sessions"],
result["observations"],
result["handshakes"],
result["credentials"],
output_path,
size,
duration,
@@ -1604,221 +1517,6 @@ def _session_id_by_hash(
return row["id"]
def _map_existing_handshake_ids(
conn,
session_id,
session_handshakes,
):
rows = conn.execute(
"""
SELECT
handshakes.id,
access_points.bssid,
handshakes.type,
handshakes.hash22000,
handshakes.message_pair,
handshakes.captured_at,
handshakes.latitude,
handshakes.longitude,
handshakes.speed
FROM handshakes
JOIN access_points
ON access_points.id =
handshakes.access_point_id
WHERE handshakes.session_id=?
ORDER BY handshakes.id
""",
(session_id,),
).fetchall()
if len(rows) != len(session_handshakes):
raise WifitripError(
"Existing session handshake count does not "
"match the .wifitrip package."
)
expected_keys = [
f"h{index:06d}"
for index in range(
1,
len(session_handshakes) + 1,
)
]
package_keys = {
handshake["handshake_key"]
for handshake in session_handshakes
}
if package_keys != set(expected_keys):
raise WifitripError(
"Existing session handshake keys do not "
"match the exported sequence."
)
fields = (
"bssid",
"type",
"hash22000",
"message_pair",
"captured_at",
"latitude",
"longitude",
"speed",
)
local_ids_by_signature = {}
for row in rows:
signature = tuple(
row[field]
for field in fields
)
local_ids_by_signature.setdefault(
signature,
[],
).append(row["id"])
signature_positions = {}
handshake_ids = {}
ordered_handshakes = sorted(
session_handshakes,
key=lambda handshake: (
handshake["handshake_key"]
),
)
for handshake in ordered_handshakes:
signature = tuple(
handshake[field]
for field in fields
)
local_ids = local_ids_by_signature.get(
signature,
)
position = signature_positions.get(
signature,
0,
)
if local_ids is None or position >= len(local_ids):
raise WifitripError(
"Existing session handshake mapping "
"does not match the .wifitrip package."
)
handshake_ids[
handshake["handshake_key"]
] = local_ids[position]
signature_positions[signature] = position + 1
return handshake_ids
def _import_session_credentials(
conn,
session_credentials,
ap_ids,
handshake_ids,
):
for credential in session_credentials:
access_point_id = ap_ids[
credential["bssid"]
]
handshake_id = None
if credential["handshake_key"] is not None:
handshake_id = handshake_ids[
credential["handshake_key"]
]
existing_verified = conn.execute(
"""
SELECT COALESCE(MAX(verified), 0)
FROM credentials
WHERE access_point_id=?
AND password=?
""",
(
access_point_id,
credential["password"],
),
).fetchone()[0]
effective_verified = int(
bool(existing_verified)
or bool(credential["verified"])
)
existing_credential = conn.execute(
"""
SELECT id
FROM credentials
WHERE access_point_id=?
AND handshake_id IS ?
AND password=?
LIMIT 1
""",
(
access_point_id,
handshake_id,
credential["password"],
),
).fetchone()
if existing_credential is None:
conn.execute(
"""
INSERT INTO credentials (
access_point_id,
handshake_id,
password,
source,
created_at,
verified
)
VALUES (?, ?, ?, ?, ?, ?)
""",
(
access_point_id,
handshake_id,
credential["password"],
credential["source"],
credential["created_at"],
effective_verified,
),
)
if effective_verified:
conn.execute(
"""
UPDATE credentials
SET verified=1
WHERE access_point_id=?
AND password=?
""",
(
access_point_id,
credential["password"],
),
)
conn.execute(
"""
UPDATE access_points
SET is_cracked=1
WHERE id=?
""",
(access_point_id,),
)
def import_wifitrip(conn, package_path):
started = time.monotonic()
@@ -1836,12 +1534,10 @@ def import_wifitrip(conn, package_path):
gps_tracks = package["gps_tracks.json"]
observations = package["observations.json"]
handshakes = package["handshakes.json"]
credentials = package["credentials.json"]
gps_by_session = {}
observations_by_session = {}
handshakes_by_session = {}
credentials_by_session = {}
for point in gps_tracks:
gps_by_session.setdefault(
@@ -1861,12 +1557,6 @@ def import_wifitrip(conn, package_path):
[],
).append(handshake)
for credential in credentials:
credentials_by_session.setdefault(
credential["session_hash"],
[],
).append(credential)
result = {
"file": str(package_path),
"sessions": len(sessions),
@@ -1917,62 +1607,11 @@ def import_wifitrip(conn, package_path):
f"calculated {calculated_hash}"
)
session_handshakes = (
handshakes_by_session.get(
session_hash,
[],
)
)
session_credentials = (
credentials_by_session.get(
session_hash,
[],
)
)
bssids = {
handshake["bssid"]
for handshake in session_handshakes
}
bssids.update(
credential["bssid"]
for credential in session_credentials
)
created_access_points = set()
ap_ids = {}
for bssid in sorted(bssids):
ap_ids[bssid] = (
_get_or_create_import_ap(
conn,
bssid,
created_access_points,
)
)
handshake_ids = (
_map_existing_handshake_ids(
conn,
existing_id,
session_handshakes,
)
)
_import_session_credentials(
conn,
session_credentials,
ap_ids,
handshake_ids,
)
conn.commit()
logger.info(
"Updated duplicate .wifitrip session "
"credentials: hash=%s local_id=%s",
"Validated duplicate .wifitrip session: "
"hash=%s local_id=%s",
session_hash,
existing_id,
)
@@ -2040,13 +1679,6 @@ def import_wifitrip(conn, package_path):
)
)
session_credentials = (
credentials_by_session.get(
session_hash,
[],
)
)
bssids = set()
for observation in session_observations:
@@ -2059,11 +1691,6 @@ def import_wifitrip(conn, package_path):
handshake["bssid"]
)
for credential in session_credentials:
bssids.add(
credential["bssid"]
)
for bssid in sorted(bssids):
ap_ids[bssid] = (
_get_or_create_import_ap(
@@ -2181,8 +1808,6 @@ def import_wifitrip(conn, package_path):
latest_observation,
)
handshake_ids = {}
for handshake in session_handshakes:
access_point_id = ap_ids[
handshake["bssid"]
@@ -2218,10 +1843,6 @@ def import_wifitrip(conn, package_path):
),
)
handshake_ids[
handshake["handshake_key"]
] = cursor.lastrowid
if handshake["type"] == "PMKID":
conn.execute(
"""
@@ -2255,13 +1876,6 @@ def import_wifitrip(conn, package_path):
f"calculated {calculated_hash}"
)
_import_session_credentials(
conn,
session_credentials,
ap_ids,
handshake_ids,
)
conn.commit()
result["imported"] += 1
@@ -2342,9 +1956,6 @@ def validate_wifitrip(package_path):
"handshakes": len(
package["handshakes.json"]
),
"credentials": len(
package["credentials.json"]
),
"session_hash_versions": manifest[
"session_hash_versions"
],