diff --git a/package.json b/package.json index bd5e768..862c82d 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,4 @@ -{ +{ "name": "bruteforce", "version": "1.0.0", "description": "", @@ -12,6 +12,7 @@ }, "private": true, "dependencies": { + "axios": "^1.12.2", "bcryptjs": "^3.0.2", "body-parser": "^2.2.0", "cors": "^2.8.5", diff --git a/public/Backend.js b/public/Backend.js index 2d87279..7350628 100644 --- a/public/Backend.js +++ b/public/Backend.js @@ -1,46 +1,142 @@ const express = require('express'); const fs = require('fs'); +const path = require('path'); const app = express(); const port = 3000; +const axios = require('axios'); +const HCAPTCHA_SECRET = '0x0000000000000000000000000000000000000000'; app.use(express.json()); -const users = JSON.parse(fs.readFileSync('C:/Users/kiana/WebstormProjects/BruteForce/TEST_DATA/users.json', 'utf8')); +const LOG_FILE = path.join(__dirname, 'login.log'); +const users = JSON.parse(fs.readFileSync('C:/Users/frederik/WebstormProjects/BruteForce/TEST_DATA/users.json', 'utf8')); -// Map für Fehlversuche und Verzögerung pro Benutzer -const loginAttempts = {}; // { username: { count: Number, lastFail: timestamp } } -const BASE_DELAY = 2000; // 2 Sekunden -const MAX_DELAY = 32000; // 32 Sekunden +// Konfiguration +const LINEAR_DELAY_MS = 1000; // 1s +const PROGRESSIVE_DELAYS = [10000, 60000, 300000]; // 10s, 1min, 5min +const LOCKOUT_THRESHOLD = 5; +const LOCKOUT_DURATION = 15 * 60 * 1000; // 15 Minuten + +const loginAttempts = {}; // { username: { count, lockedUntil, lastFail, requiresCaptcha } } + +// Logging-Funktion +function logAttempt(username, status, ip, extra = '') { + const timestamp = new Date().toISOString(); + const logLine = `[${timestamp}] [${ip}] [${username}] [${status}] ${extra}\n`; + fs.appendFileSync(LOG_FILE, logLine); + console.log(logLine.trim()); +} app.post('/login', async (req, res) => { - const { username, password } = req.body; - const user = users.find(u => u.username === username && u.password === password); + const { username, password, captcha } = req.body; + const ip = req.ip || req.connection.remoteAddress; - if (user) { - // Bei Erfolg: Fehlversuche zurücksetzen - if (loginAttempts[username]) { - delete loginAttempts[username]; - } - return res.json({ ok: true }); - } else { - // Fehlversuch zählen und Verzögerung berechnen - if (!loginAttempts[username]) { - loginAttempts[username] = { count: 1, lastFail: Date.now() }; - } else { - loginAttempts[username].count += 1; - loginAttempts[username].lastFail = Date.now(); - } - // Exponentielle Verzögerung, maximal MAX_DELAY - const delay = Math.min(BASE_DELAY * Math.pow(2, loginAttempts[username].count - 1), MAX_DELAY); - await new Promise(r => setTimeout(r, delay)); - return res.status(401).json({ ok: false, error: `Falscher Benutzername oder Passwort. Wartezeit: ${delay / 1000}s` }); + const user = users.find(u => u.username === username && u.password === password); + const now = Date.now(); + + // Init + if (!loginAttempts[username]) { + loginAttempts[username] = { + count: 0, + lockedUntil: null, + lastFail: null, + requiresCaptcha: false + }; } + const state = loginAttempts[username]; + + // Lockout prüfen + if (state.lockedUntil && now < state.lockedUntil) { + const remaining = Math.ceil((state.lockedUntil - now) / 1000); + logAttempt(username, 'LOCKED', ip, `Noch gesperrt: ${remaining}s`); + return res.status(423).json({ + ok: false, + error: `Benutzer gesperrt. Bitte in ${remaining}s erneut versuchen.` + }); + } + + if (state.requiresCaptcha) { + if (!captcha || typeof captcha !== 'string') { + logAttempt(username, 'CAPTCHA_MISSING', ip); + return res.status(403).json({ + ok: false, + error: 'Captcha fehlt', + requiresCaptcha: true + }); + } + + try { + const verify = await axios.post( + 'https://hcaptcha.com/siteverify', + new URLSearchParams({ + secret: HCAPTCHA_SECRET, + response: captcha, + remoteip: req.ip + }) + ); + + if (!verify.data.success) { + logAttempt(username, 'CAPTCHA_FAIL', ip); + return res.status(403).json({ + ok: false, + error: 'Captcha ungültig', + requiresCaptcha: true + }); + } + } catch (e) { + console.error('Captcha-Überprüfung fehlgeschlagen:', e.message); + return res.status(500).json({ + ok: false, + error: 'Captcha-Fehler', + requiresCaptcha: true + }); + } + } + + // Erfolg + if (user) { + loginAttempts[username] = undefined; + logAttempt(username, 'SUCCESS', ip); + return res.json({ ok: true, message: 'Login erfolgreich' }); + } + + // Fehlversuch + state.count++; + state.lastFail = now; + logAttempt(username, 'FAIL', ip, `Fehlversuche: ${state.count}`); + + // Lockout nach Schwelle + if (state.count >= LOCKOUT_THRESHOLD) { + state.lockedUntil = now + LOCKOUT_DURATION; + state.count = 0; + logAttempt(username, 'LOCKOUT', ip); + return res.status(423).json({ + ok: false, + error: `Zu viele Fehlversuche. Benutzer für 15 Minuten gesperrt.` + }); + } + + // Captcha aktivieren nach 3 Fehlversuchen + if (state.count >= 3) { + state.requiresCaptcha = true; + } + + // Verzögerung anwenden + const delay = PROGRESSIVE_DELAYS[state.count - 1] || LINEAR_DELAY_MS; + await new Promise(r => setTimeout(r, delay)); + + return res.status(401).json({ + ok: false, + error: `Falscher Benutzername oder Passwort. Wartezeit: ${delay / 1000}s`, + requiresCaptcha: state.requiresCaptcha + }); }); +// Einfaches Frontend für Tests app.get('/login', (req, res) => { res.sendFile(__dirname + '/index.html'); }); app.listen(port, () => { - console.log(`läuft http://localhost:${port}/login`); -}); + console.log(`Sicherer Login-Server läuft auf http://localhost:${port}/login`); +}); \ No newline at end of file diff --git a/public/index.html b/public/index.html index a97148a..686e818 100644 --- a/public/index.html +++ b/public/index.html @@ -1,62 +1,148 @@ - - +
- -