211 lines
6.6 KiB
Python
211 lines
6.6 KiB
Python
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
from typing import Any
|
|
|
|
|
|
class HandshakesRepository:
|
|
def __init__(self, conn: sqlite3.Connection) -> None:
|
|
self._conn = conn
|
|
|
|
def list_eligible(
|
|
self,
|
|
method_id: str,
|
|
method_version: int,
|
|
limit: int | None,
|
|
handshake_ids: list[int] | tuple[int, ...] | None = None,
|
|
) -> list[sqlite3.Row]:
|
|
if handshake_ids is not None and not handshake_ids:
|
|
return []
|
|
|
|
id_filter = ""
|
|
parameters: list[Any] = [
|
|
method_id,
|
|
method_version,
|
|
method_id,
|
|
method_version,
|
|
]
|
|
|
|
if handshake_ids is not None:
|
|
placeholders = ", ".join(
|
|
"?" for _ in handshake_ids
|
|
)
|
|
id_filter = f"AND h.id IN ({placeholders})"
|
|
parameters.extend(handshake_ids)
|
|
|
|
parameters.append(
|
|
limit if limit is not None else -1
|
|
)
|
|
|
|
return self._conn.execute(
|
|
f"""
|
|
SELECT
|
|
h.id AS handshake_id,
|
|
h.hash22000,
|
|
h.access_point_id,
|
|
h.captured_at
|
|
FROM handshakes AS h
|
|
WHERE h.hash22000 IS NOT NULL
|
|
AND TRIM(h.hash22000) <> ''
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM credentials AS c
|
|
WHERE c.handshake_id = h.id
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM crack_attempts AS a
|
|
WHERE a.handshake_id = h.id
|
|
AND a.method_id = ?
|
|
AND a.method_version = ?
|
|
AND a.status = 'COMPLETED'
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM crack_reservations AS r
|
|
WHERE r.handshake_id = h.id
|
|
AND r.method_id = ?
|
|
AND r.method_version = ?
|
|
AND r.status = 'ACTIVE'
|
|
)
|
|
{id_filter}
|
|
ORDER BY h.id
|
|
LIMIT ?
|
|
""",
|
|
parameters,
|
|
).fetchall()
|
|
|
|
def list_eligible_for_search_context(
|
|
self,
|
|
token_hash: str,
|
|
method_id: str,
|
|
method_version: int,
|
|
limit: int | None,
|
|
) -> list[sqlite3.Row]:
|
|
return self._conn.execute(
|
|
"""
|
|
SELECT
|
|
h.id AS handshake_id,
|
|
h.hash22000,
|
|
h.access_point_id,
|
|
h.captured_at
|
|
FROM crack_search_context_handshakes AS sch
|
|
INNER JOIN handshakes AS h
|
|
ON h.id = sch.handshake_id
|
|
WHERE sch.token_hash = ?
|
|
AND h.hash22000 IS NOT NULL
|
|
AND TRIM(h.hash22000) <> ''
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM credentials AS c
|
|
WHERE c.handshake_id = h.id
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM crack_attempts AS a
|
|
WHERE a.handshake_id = h.id
|
|
AND a.method_id = ?
|
|
AND a.method_version = ?
|
|
AND a.status = 'COMPLETED'
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM crack_reservations AS r
|
|
WHERE r.handshake_id = h.id
|
|
AND r.method_id = ?
|
|
AND r.method_version = ?
|
|
AND r.status = 'ACTIVE'
|
|
)
|
|
ORDER BY h.id
|
|
LIMIT ?
|
|
""",
|
|
(
|
|
token_hash,
|
|
method_id,
|
|
method_version,
|
|
method_id,
|
|
method_version,
|
|
limit if limit is not None else -1,
|
|
),
|
|
).fetchall()
|
|
|
|
def get_method_statistics(
|
|
self,
|
|
method_id: str,
|
|
method_version: int,
|
|
) -> sqlite3.Row:
|
|
return self._conn.execute(
|
|
"""
|
|
SELECT
|
|
COUNT(*) AS total_count,
|
|
SUM(
|
|
CASE
|
|
WHEN EXISTS (
|
|
SELECT 1
|
|
FROM credentials AS c
|
|
WHERE c.handshake_id = h.id
|
|
)
|
|
THEN 1
|
|
ELSE 0
|
|
END
|
|
) AS found_count,
|
|
SUM(
|
|
CASE
|
|
WHEN NOT EXISTS (
|
|
SELECT 1
|
|
FROM credentials AS c
|
|
WHERE c.handshake_id = h.id
|
|
)
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM crack_attempts AS a
|
|
WHERE a.handshake_id = h.id
|
|
AND a.method_id = ?
|
|
AND a.method_version = ?
|
|
AND a.status = 'COMPLETED'
|
|
)
|
|
THEN 1
|
|
ELSE 0
|
|
END
|
|
) AS covered_count,
|
|
SUM(
|
|
CASE
|
|
WHEN NOT EXISTS (
|
|
SELECT 1
|
|
FROM credentials AS c
|
|
WHERE c.handshake_id = h.id
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM crack_attempts AS a
|
|
WHERE a.handshake_id = h.id
|
|
AND a.method_id = ?
|
|
AND a.method_version = ?
|
|
AND a.status = 'COMPLETED'
|
|
)
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM crack_reservations AS r
|
|
WHERE r.handshake_id = h.id
|
|
AND r.method_id = ?
|
|
AND r.method_version = ?
|
|
AND r.status = 'ACTIVE'
|
|
)
|
|
THEN 1
|
|
ELSE 0
|
|
END
|
|
) AS reserved_count
|
|
FROM handshakes AS h
|
|
WHERE h.hash22000 IS NOT NULL
|
|
AND TRIM(h.hash22000) <> ''
|
|
""",
|
|
(
|
|
method_id,
|
|
method_version,
|
|
method_id,
|
|
method_version,
|
|
method_id,
|
|
method_version,
|
|
),
|
|
).fetchone()
|