From d0b5b770a6643fcd1e42d08c3a0a1998ca7a2968 Mon Sep 17 00:00:00 2001 From: WildSoft Date: Thu, 15 Oct 2026 11:39:31 +0300 Subject: [PATCH] Update public source --- reports/wifitrip.py | 332 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 260 insertions(+), 72 deletions(-) diff --git a/reports/wifitrip.py b/reports/wifitrip.py index 938a6ae..24a6ecc 100644 --- a/reports/wifitrip.py +++ b/reports/wifitrip.py @@ -1603,6 +1603,169 @@ 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." + ) + + handshake_ids = {} + + fields = ( + "bssid", + "type", + "hash22000", + "message_pair", + "captured_at", + "latitude", + "longitude", + "speed", + ) + + for row, handshake in zip( + rows, + session_handshakes, + ): + if any( + row[field] != handshake[field] + for field in fields + ): + raise WifitripError( + "Existing session handshake mapping " + "does not match the .wifitrip package." + ) + + handshake_ids[ + handshake["handshake_key"] + ] = row["id"] + + 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() @@ -1687,12 +1850,97 @@ def import_wifitrip(conn, package_path): if existing_id is not None: result["duplicates"] += 1 - logger.info( - "Skipping duplicate .wifitrip session: " - "hash=%s local_id=%s", - session_hash, - existing_id, - ) + try: + conn.execute("BEGIN") + + calculated_hash = _session_hash( + conn, + existing_id, + ) + + if calculated_hash != session_hash: + raise WifitripError( + "Existing session hash verification failed: " + f"expected {session_hash}, " + 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", + session_hash, + existing_id, + ) + + except Exception as exc: + conn.rollback() + + result["failed"] += 1 + result["failed_sessions"].append( + { + "session_hash": session_hash, + "error": str(exc), + } + ) + + logger.exception( + "Failed to update duplicate .wifitrip session: " + "hash=%s", + session_hash, + ) continue @@ -1955,72 +2203,12 @@ def import_wifitrip(conn, package_path): f"calculated {calculated_hash}" ) - 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_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 not None: - continue - - 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"], - credential["verified"], - ), - ) - - conn.execute( - """ - UPDATE access_points - SET - is_cracked=1 - WHERE id=? - """, - (access_point_id,), - ) + _import_session_credentials( + conn, + session_credentials, + ap_ids, + handshake_ids, + ) conn.commit()