Spaces:
Sleeping
Sleeping
File size: 7,793 Bytes
3c56493 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | #!/usr/bin/env python3
"""
Author: Michal Szymanski <misiektoja-github@rm-rf.ninja>
v1.2
Automatic extractor for secret keys used for TOTP generation in Spotify Web Player JavaScript bundles
https://github.com/misiektoja/spotify_monitor#debugging-tools
Python pip3 requirements:
playwright
Install:
pip install playwright
playwright install
---------------
Change log:
v1.2 (12 Oct 25):
- Added CLI output modes (--secret, --secretbytes and --secretdict CLI flags, see -h for help)
- Added --all mode writing all secret formats to files (secrets.json, secretBytes.json, secretDict.json) (thx @tomballgithub)
v1.1 (12 Jul 25):
- Added JSON array output for plain secrets and secret bytes
v1.0 (09 Jul 25):
- Initial proof-of-concept, confirmed to extract v10, v11 and v12 secrets
"""
import asyncio
import re
from datetime import datetime
import json
from typing import List, Dict, Any
from playwright.async_api import async_playwright
import argparse
import sys
BUNDLE_RE = re.compile(r"""(?x)(?:vendor~web-player|encore~web-player|web-player)\.[0-9a-f]{4,}\.(?:js|mjs)""")
TIMEOUT = 45000 # 45s
VERBOSE = True
OUTPUT_FILES = {
'plain_json': 'secrets.json',
'bytes_json_array': 'secretBytes.json',
'bytes_json_dict': 'secretDict.json',
}
def _inline_int_array(nums):
return '[ ' + ', '.join(str(n) for n in nums) + ' ]'
def _write_secretbytes_compact(fp, items):
fp.write('[\n')
last = len(items) - 1
for i, itm in enumerate(items):
comma = ',' if i < last else ''
fp.write(f' {{ "version": {itm["version"]}, "secret": {_inline_int_array(itm["secret"])} }}{comma}\n')
fp.write(']\n')
def _write_secretdict_compact(fp, mapping):
keys = sorted(mapping.keys(), key=lambda k: int(k))
fp.write('{\n')
for i, v in enumerate(keys):
comma = ',' if i < len(keys) - 1 else ''
fp.write(f' "{v}": {_inline_int_array(mapping[v])}{comma}\n')
fp.write('}\n')
def log(m):
if VERBOSE:
print(f"[{datetime.now().strftime('%H:%M:%S')}] {m}")
def summarise(caps: List[Dict[str, Any]], mode=None):
real = {}
for cap in caps:
sec = cap.get('secret')
if not isinstance(sec, str):
continue
ver = cap.get('version') or (isinstance(cap.get('obj'), dict) and cap['obj'].get('version'))
if ver is None:
continue
real[str(ver)] = sec
if not real:
log('No real secrets with version.')
return
sorted_items = sorted(real.items(), key=lambda kv: int(kv[0]))
formatted_data = [{"version": int(v), "secret": s} for v, s in sorted_items]
secret_bytes = [{"version": int(v), "secret": [ord(c) for c in s]} for v, s in sorted_items]
secret_dict = {v: [ord(c) for c in s] for v, s in sorted_items}
if mode is None:
print("\n--- List of extracted secrets ---\n")
for v, s in sorted(real.items(), key=lambda kv: int(kv[0])):
print(f"v{v}: '{s}'")
print("\n--- Plain secrets (JSON array) ---\n")
print(json.dumps(formatted_data, indent=2))
print("\n--- Secret bytes (JSON array) ---\n")
print('[')
for idx, itm in enumerate(secret_bytes):
comma = ',' if idx < len(secret_bytes) - 1 else ''
print(' {')
print(f' "version": {itm["version"]},')
print(f' "secret": {_inline_int_array(itm["secret"])}')
print(f' }}{comma}')
print(']')
print("\n--- Secret bytes (JSON object/dict): version -> byte list mapping ---\n")
print('{')
last = len(real) - 1
for idx, (v, s) in enumerate(sorted(real.items(), key=lambda kv: int(kv[0]))):
arr = [ord(c) for c in s]
comma = ',' if idx < last else ''
print(f' "{v}": {_inline_int_array(arr)}{comma}')
print('}')
elif mode == 'secret':
print(json.dumps(formatted_data, indent=2))
elif mode == 'secretbytes':
print('[')
for idx, itm in enumerate(secret_bytes):
comma = ',' if idx < len(secret_bytes) - 1 else ''
print(f' {{ "version": {itm["version"]}, "secret": {_inline_int_array(itm["secret"])} }}{comma}')
print(']')
elif mode == 'secretdict':
print('{')
last = len(real) - 1
for idx, (v, s) in enumerate(sorted(real.items(), key=lambda kv: int(kv[0]))):
arr = [ord(c) for c in s]
comma = ',' if idx < last else ''
print(f' "{v}": {_inline_int_array(arr)}{comma}')
print('}')
elif mode == 'all':
try:
with open(OUTPUT_FILES['plain_json'], 'w') as f:
json.dump(formatted_data, f, indent=2)
f.write('\n')
with open(OUTPUT_FILES['bytes_json_array'], 'w') as f:
_write_secretbytes_compact(f, secret_bytes)
with open(OUTPUT_FILES['bytes_json_dict'], 'w') as f:
_write_secretdict_compact(f, secret_dict)
if VERBOSE:
print(f"[+] Wrote plain secrets to {OUTPUT_FILES['plain_json']}")
print(f"[+] Wrote secret bytes array to {OUTPUT_FILES['bytes_json_array']}")
print(f"[+] Wrote secret bytes dict to {OUTPUT_FILES['bytes_json_dict']}")
except Exception as e:
print(f"Error writing output files: {e}", file=sys.stderr)
async def grab_live():
hook = """(()=>{if(globalThis.__secretHookInstalled)return;globalThis.__secretHookInstalled=true;globalThis.__captures=[];
Object.defineProperty(Object.prototype,'secret',{configurable:true,set:function(v){try{__captures.push({secret:v,version:this.version,obj:this});}catch(e){}
Object.defineProperty(this,'secret',{value:v,writable:true,configurable:true,enumerable:true});}});})();"""
async with async_playwright() as p:
b = await p.chromium.launch(headless=True)
ctx = await b.new_context()
await ctx.add_init_script(hook)
pg = await ctx.new_page()
pg.on('response', lambda resp: BUNDLE_RE.fullmatch(resp.url.split('/')[-1]) and log(f"↓ {resp.url.split('/')[-1]} ({resp.status})"))
log('→ opening open.spotify.com ...')
await pg.goto('https://open.spotify.com', timeout=TIMEOUT)
await pg.wait_for_load_state('networkidle', timeout=TIMEOUT)
await pg.wait_for_timeout(3000)
caps = await pg.evaluate('__captures')
if caps:
for c in caps:
if isinstance(c.get('secret'), str) and c.get('version') is not None:
log(f"✔ secret({c.get('version')}) → {c.get('secret')}")
await b.close()
return caps or []
def main():
parser = argparse.ArgumentParser(description='Extract Spotify web-player TOTP secrets')
parser.add_argument('--secret', action='store_true', help='Output plain secrets JSON only')
parser.add_argument('--secretbytes', action='store_true', help='Output secret-bytes JSON only')
parser.add_argument('--secretdict', action='store_true', help='Output version->byte-list dict JSON only')
parser.add_argument('--all', action='store_true', help='Write plain, bytes array and bytes dict JSON files')
args = parser.parse_args()
mode = None
if args.secret:
mode = 'secret'
elif args.secretbytes:
mode = 'secretbytes'
elif args.secretdict:
mode = 'secretdict'
elif args.all:
mode = 'all'
global VERBOSE
if mode and mode != 'all':
VERBOSE = False
elif mode == 'all':
VERBOSE = True
try:
caps = asyncio.run(grab_live())
summarise(caps, mode)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main() |