-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
393 lines (334 loc) · 15.5 KB
/
app.js
File metadata and controls
393 lines (334 loc) · 15.5 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* Flask Session Tool — UI Application Logic
* jwt.io-style: live bidirectional sync between encoded ↔ decoded
*/
document.addEventListener('DOMContentLoaded', () => {
// ─── Tab switching ────────────────────────────────────────────────
document.querySelectorAll('.tab').forEach(tab => {
tab.addEventListener('click', () => {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
tab.classList.add('active');
document.getElementById('tab-' + tab.dataset.tab).classList.add('active');
});
});
// ─── Editor Elements ──────────────────────────────────────────────
const encodedInput = document.getElementById('encoded-input');
const decodedInput = document.getElementById('decoded-input');
const secretKey = document.getElementById('secret-key');
const compressOpt = document.getElementById('compress-option');
const saltOpt = document.getElementById('salt-option');
const verifyBanner = document.getElementById('verify-banner');
const verifyIcon = document.getElementById('verify-icon');
const verifyText = document.getElementById('verify-text');
const cookieInfo = document.getElementById('cookie-info');
// Prevent feedback loops
let syncing = false;
// ─── Encoded → Decoded (user pastes/types a cookie) ───────────────
async function onEncodedChange() {
if (syncing) return;
const cookie = encodedInput.value.trim();
if (!cookie) {
syncing = true;
decodedInput.value = '';
syncing = false;
cookieInfo.textContent = '—';
setBanner('neutral', '🔑', 'Enter a secret key to verify or forge cookies');
return;
}
try {
const result = await FlaskSession.decode(cookie);
syncing = true;
decodedInput.value = JSON.stringify(result.payload, null, 2);
syncing = false;
// Update info
const infoLines = [];
infoLines.push(result.compressed ? '📦 Compressed (zlib)' : '📄 Uncompressed');
infoLines.push('🕐 ' + result.timestampDate.toLocaleString());
infoLines.push('📏 ' + result.payloadRaw.length + ' bytes');
cookieInfo.innerHTML = infoLines.map(l => `<div>${l}</div>`).join('');
// Verify if secret is set
await verifySignature(cookie);
} catch (err) {
syncing = true;
decodedInput.value = '';
syncing = false;
cookieInfo.textContent = '—';
setBanner('invalid', '❌', 'Invalid cookie: ' + err.message);
}
}
// ─── Decoded → Encoded (user edits the JSON payload) ──────────────
async function onDecodedChange() {
if (syncing) return;
const json = decodedInput.value.trim();
const secret = secretKey.value.trim();
if (!json) {
syncing = true;
encodedInput.value = '';
syncing = false;
cookieInfo.textContent = '—';
setBanner('neutral', '🔑', 'Enter a secret key to verify or forge cookies');
return;
}
// Validate JSON
try {
JSON.parse(json);
} catch {
setBanner('invalid', '⚠️', 'Invalid JSON — fix the payload to re-encode');
return;
}
if (!secret) {
setBanner('neutral', '🔑', 'Enter a secret key to sign the cookie');
return;
}
try {
const cookie = await FlaskSession.encode(json, secret, {
compress: compressOpt.checked,
salt: saltOpt.value || 'cookie-session',
});
syncing = true;
encodedInput.value = cookie;
syncing = false;
// Update info
const result = await FlaskSession.decode(cookie);
const infoLines = [];
infoLines.push(result.compressed ? '📦 Compressed (zlib)' : '📄 Uncompressed');
infoLines.push('🕐 ' + result.timestampDate.toLocaleString());
infoLines.push('📏 ' + result.payloadRaw.length + ' bytes');
cookieInfo.innerHTML = infoLines.map(l => `<div>${l}</div>`).join('');
setBanner('valid', '✅', 'Signature valid — cookie is signed with your secret key');
} catch (err) {
setBanner('invalid', '❌', 'Encoding error: ' + err.message);
}
}
// ─── Secret key change → re-verify or re-encode ───────────────────
async function onSecretChange() {
const cookie = encodedInput.value.trim();
const json = decodedInput.value.trim();
const secret = secretKey.value.trim();
if (!secret) {
setBanner('neutral', '🔑', 'Enter a secret key to verify or forge cookies');
return;
}
// If there's a cookie, verify it
if (cookie) {
await verifySignature(cookie);
}
// If there's valid JSON, re-encode
if (json) {
try {
JSON.parse(json);
await onDecodedChange();
} catch { /* invalid JSON, skip */ }
}
}
// ─── Verify a cookie's signature ──────────────────────────────────
async function verifySignature(cookie) {
const secret = secretKey.value.trim();
if (!secret) {
setBanner('neutral', '🔑', 'Enter a secret key to verify the signature');
return;
}
try {
const result = await FlaskSession.verify(cookie, secret, saltOpt.value || 'cookie-session');
if (result.valid) {
setBanner('valid', '✅', 'Signature verified — secret key is correct');
} else {
setBanner('invalid', '❌', 'Invalid signature — wrong secret key');
}
} catch (err) {
setBanner('invalid', '⚠️', 'Verification error: ' + err.message);
}
}
// ─── Banner helper ────────────────────────────────────────────────
function setBanner(state, icon, text) {
verifyBanner.className = 'verify-banner ' + state;
verifyIcon.textContent = icon;
verifyText.textContent = text;
}
// ─── Wire up live events ──────────────────────────────────────────
encodedInput.addEventListener('input', debounce(onEncodedChange, 250));
decodedInput.addEventListener('input', debounce(onDecodedChange, 400));
secretKey.addEventListener('input', debounce(onSecretChange, 300));
compressOpt.addEventListener('change', () => onDecodedChange());
saltOpt.addEventListener('input', debounce(onSecretChange, 400));
// ─── Buttons ──────────────────────────────────────────────────────
// Paste
document.getElementById('paste-btn').addEventListener('click', async () => {
try {
const text = await navigator.clipboard.readText();
encodedInput.value = text;
onEncodedChange();
} catch {
encodedInput.focus();
}
});
// Copy encoded cookie
document.getElementById('copy-encoded').addEventListener('click', () => {
const v = encodedInput.value.trim();
if (v) {
navigator.clipboard.writeText(v);
showToast('Cookie copied!');
}
});
// Copy decoded JSON (minified)
document.getElementById('copy-decoded').addEventListener('click', () => {
const v = decodedInput.value.trim();
if (v) {
try {
const minified = JSON.stringify(JSON.parse(v));
navigator.clipboard.writeText(minified);
showToast('JSON copied (minified)!');
} catch {
navigator.clipboard.writeText(v);
showToast('Text copied!');
}
}
});
// Format JSON
document.getElementById('format-json').addEventListener('click', () => {
try {
const parsed = JSON.parse(decodedInput.value);
decodedInput.value = JSON.stringify(parsed, null, 2);
} catch {
showToast('Invalid JSON — cannot format');
}
});
// ─── BRUTE-FORCE TAB ──────────────────────────────────────────────
const bruteBtn = document.getElementById('brute-btn');
const bruteStopBtn = document.getElementById('brute-stop-btn');
const bruteOutput = document.getElementById('brute-output');
const bruteProgressContainer = document.getElementById('brute-progress');
const bruteProgressFill = document.getElementById('brute-progress-fill');
const bruteProgressText = document.getElementById('brute-progress-text');
let bruteAbort = null;
document.querySelectorAll('input[name="wordlist-src"]').forEach(radio => {
radio.addEventListener('change', () => {
document.getElementById('brute-wordlist').classList.toggle('hidden', radio.value !== 'custom');
document.getElementById('brute-file').classList.toggle('hidden', radio.value !== 'file');
});
});
bruteBtn.addEventListener('click', async () => {
const cookie = document.getElementById('brute-cookie').value.trim();
if (!cookie) {
bruteOutput.innerHTML = '<p class="error">❌ Please enter a session cookie</p>';
return;
}
const src = document.querySelector('input[name="wordlist-src"]:checked').value;
let wordlist = [];
if (src === 'builtin') {
wordlist = [...FlaskSession.COMMON_SECRETS];
} else if (src === 'custom') {
wordlist = document.getElementById('brute-wordlist').value.split('\n').filter(l => l.trim());
} else if (src === 'file') {
const file = document.getElementById('brute-file').files[0];
if (!file) {
bruteOutput.innerHTML = '<p class="error">❌ Please select a wordlist file</p>';
return;
}
const text = await file.text();
wordlist = text.split('\n').filter(l => l.trim());
}
if (wordlist.length === 0) {
bruteOutput.innerHTML = '<p class="error">❌ Wordlist is empty</p>';
return;
}
try {
const decoded = await FlaskSession.decode(cookie);
bruteOutput.innerHTML = `
<p>🔍 Decoded payload:</p>
<pre><code>${syntaxHighlight(JSON.stringify(decoded.payload, null, 2))}</code></pre>
<p>⏳ Trying ${wordlist.length.toLocaleString()} secrets...</p>
`;
} catch (err) {
bruteOutput.innerHTML = `<p class="error">❌ Invalid cookie: ${escapeHtml(err.message)}</p>`;
return;
}
bruteBtn.classList.add('hidden');
bruteStopBtn.classList.remove('hidden');
bruteProgressContainer.classList.remove('hidden');
bruteAbort = new AbortController();
const startTime = Date.now();
const found = await FlaskSession.bruteForce(
cookie, wordlist,
(checked, total) => {
const pct = Math.round((checked / total) * 100);
bruteProgressFill.style.width = pct + '%';
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
const rate = Math.round(checked / (elapsed || 1));
bruteProgressText.textContent = `${checked.toLocaleString()} / ${total.toLocaleString()} (${rate}/s) — ${elapsed}s`;
},
(secret, attempts) => {
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
bruteOutput.innerHTML = `
<div class="found-result">
<p class="success">🔓 Secret key found!</p>
<div class="found-key">
<span class="label">Secret:</span>
<code class="big-key">${escapeHtml(secret)}</code>
<button class="btn-small copy-key-btn" title="Copy key">📄</button>
</div>
<p class="meta">Found after ${attempts.toLocaleString()} attempts in ${elapsed}s</p>
</div>
`;
document.querySelector('.copy-key-btn')?.addEventListener('click', () => {
navigator.clipboard.writeText(secret);
showToast('Secret key copied!');
});
},
bruteAbort.signal,
);
if (!found) {
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
if (bruteAbort.signal.aborted) {
bruteOutput.innerHTML += `<p class="warning">⏹ Stopped after ${elapsed}s</p>`;
} else {
bruteOutput.innerHTML += `<p class="warning">❌ No key found in ${wordlist.length.toLocaleString()} attempts (${elapsed}s)</p>`;
}
}
bruteBtn.classList.remove('hidden');
bruteStopBtn.classList.add('hidden');
});
bruteStopBtn.addEventListener('click', () => {
if (bruteAbort) bruteAbort.abort();
});
// ─── Helpers ──────────────────────────────────────────────────────
function escapeHtml(str) {
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
function syntaxHighlight(json) {
json = escapeHtml(json);
return json.replace(
/("(\\u[\da-fA-F]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
(match) => {
let cls = 'json-number';
if (/^"/.test(match)) {
cls = /:$/.test(match) ? 'json-key' : 'json-string';
} else if (/true|false/.test(match)) {
cls = 'json-boolean';
} else if (/null/.test(match)) {
cls = 'json-null';
}
return `<span class="${cls}">${match}</span>`;
}
);
}
function debounce(fn, delay) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}
function showToast(msg) {
const toast = document.createElement('div');
toast.className = 'toast';
toast.textContent = msg;
document.body.appendChild(toast);
requestAnimationFrame(() => toast.classList.add('show'));
setTimeout(() => {
toast.classList.remove('show');
setTimeout(() => toast.remove(), 300);
}, 2000);
}
});