113 lines
3.1 KiB
Python
113 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import re
|
|
import secrets
|
|
import string
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
from pathlib import Path
|
|
|
|
|
|
PORT = 7777
|
|
TIMEOUT = 10
|
|
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 random_password() -> str:
|
|
alphabet = string.ascii_letters + string.digits
|
|
return "pw" + "".join(secrets.choice(alphabet) for _ in range(12))
|
|
|
|
|
|
def run_sdb(target: str, *args: str) -> subprocess.CompletedProcess[bytes]:
|
|
command = [str(SDB), args[0], target, *args[1:]]
|
|
try:
|
|
return subprocess.run(
|
|
command,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
timeout=TIMEOUT,
|
|
check=False,
|
|
)
|
|
except subprocess.TimeoutExpired as exc:
|
|
raise RuntimeError(f"sdb timeout during {args[0]}") from exc
|
|
|
|
|
|
def output(result: subprocess.CompletedProcess[bytes]) -> bytes:
|
|
return result.stdout + b"\n" + result.stderr
|
|
|
|
|
|
def run(target: str) -> int:
|
|
if not SDB.is_file():
|
|
raise RuntimeError(f"sdb client not found: {SDB}")
|
|
|
|
username = "reuse" + secrets.token_hex(5)
|
|
password = random_password()
|
|
|
|
registered = run_sdb(target, "register", username, password)
|
|
if registered.returncode != 0:
|
|
raise RuntimeError(f"register failed: {output(registered).decode(errors='replace').strip()}")
|
|
|
|
with tempfile.NamedTemporaryFile(prefix="sondib-probe-", suffix=".pcm") as pcm:
|
|
pcm.write(b"\0" * 1024)
|
|
pcm.flush()
|
|
uploaded = run_sdb(
|
|
target,
|
|
"upload",
|
|
username,
|
|
password,
|
|
"deleted-probe",
|
|
"p",
|
|
pcm.name,
|
|
)
|
|
|
|
match = SID_KEY.search(output(uploaded))
|
|
if match is None:
|
|
raise RuntimeError(
|
|
f"upload did not return SID/key: {output(uploaded).decode(errors='replace').strip()}"
|
|
)
|
|
sid, key = (part.decode() for part in match.groups())
|
|
|
|
before = run_sdb(target, "download", sid, key)
|
|
if not before.stdout:
|
|
raise RuntimeError(
|
|
f"fresh track was not downloadable: {output(before).decode(errors='replace').strip()}"
|
|
)
|
|
|
|
# The client may return a non-zero status for an accepted mutation.
|
|
run_sdb(target, "delete", username, password, sid, key)
|
|
time.sleep(0.2)
|
|
after = run_sdb(target, "download", sid, key)
|
|
|
|
if after.stdout:
|
|
sys.stdout.buffer.write(after.stdout)
|
|
sys.stdout.buffer.flush()
|
|
log(f"target={target} sid={sid} leaked_bytes={len(after.stdout)}")
|
|
return 0
|
|
|
|
if after.returncode != 0:
|
|
log(f"target={target} sid={sid} deleted_track_blocked")
|
|
return 1
|
|
raise RuntimeError("download returned no data without a protocol error")
|
|
|
|
|
|
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, RuntimeError) as exc:
|
|
log(f"target={sys.argv[1]} error={exc}")
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|