import json, requests, time, hmac, hashlib, base64, struct, email.utils, sys B = 'https://www.cometapi.com' def server_ts(s): r = s.get(B + '/api/status', timeout=20) d = r.headers.get('Date') try: return email.utils.parsedate_to_datetime(d).timestamp() except Exception: return time.time() def totp(secret, ts, step=30): key = base64.b32decode(secret + '=' * ((8 - len(secret) % 8) % 8)) h = hmac.new(key, struct.pack('>Q', int(ts // step)), hashlib.sha1).digest() o = h[-1] % 16 return str((struct.unpack('>I', h[o:o + 4])[0] % 0x80000000) % 1000000).zfill(6) def run(result_file): d = json.load(open(result_file)) s = requests.Session() for k, v in d['cookies'].items(): try: s.cookies.set(k, v, domain='www.cometapi.com') except Exception: pass H = {'User-Agent': 'Mozilla/5.0 Chrome/141', 'Accept': 'application/json', 'Origin': B, 'Referer': B + '/console/personal', 'X-Organization-Id': str(d.get('org')), 'X-Workspace-Id': str(d.get('ws')), 'Content-Type': 'application/json'} st = s.get(B + '/api/user/2fa/status', headers=H, timeout=20).json() print('2fa status before', st) if st.get('data', {}).get('enabled'): print('already enabled'); return for attempt in range(5): r = s.post(B + '/api/user/2fa/setup', headers=H, json={}, timeout=25) if r.status_code != 200: print('setup', r.status_code, r.text[:120]); time.sleep(60); continue j = r.json() if 'data' not in j: print('setup body', j); time.sleep(60); continue sec = j['data']['secret'] codes = j['data']['backup_codes'] ts = server_ts(s) print('secret', sec, 'srv_ts', ts, 'local', time.time()) for off in (0, -30, 30): code = totp(sec, ts + off) r2 = s.post(B + '/api/user/2fa/enable', headers=H, json={"code": code}, timeout=25) print(' try', off, code, r2.status_code, r2.text[:140]) if r2.status_code == 429: time.sleep(70); break if '"success":true' in r2.text: json.dump({"secret": sec, "backup": codes}, open(result_file.replace('.json', '_2fa.json'), 'w')) print('ENABLED') me = s.get(B + '/api/user/self', headers=H, timeout=20).json()['data'] print('quota', me.get('quota'), 'fct', me.get('first_create_token'), 'fac', me.get('first_api_call')) return time.sleep(45) print('FAILED to enable') if __name__ == '__main__': run(sys.argv[1])