Add protection against brute force attacks via account locking, captcha and logging.
This commit is contained in:
+2
-1
@@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
"name": "bruteforce",
|
"name": "bruteforce",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
},
|
},
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"axios": "^1.12.2",
|
||||||
"bcryptjs": "^3.0.2",
|
"bcryptjs": "^3.0.2",
|
||||||
"body-parser": "^2.2.0",
|
"body-parser": "^2.2.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
|
|||||||
+120
-24
@@ -1,46 +1,142 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
|
const axios = require('axios');
|
||||||
|
const HCAPTCHA_SECRET = '0x0000000000000000000000000000000000000000';
|
||||||
|
|
||||||
app.use(express.json());
|
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
|
// Konfiguration
|
||||||
const loginAttempts = {}; // { username: { count: Number, lastFail: timestamp } }
|
const LINEAR_DELAY_MS = 1000; // 1s
|
||||||
const BASE_DELAY = 2000; // 2 Sekunden
|
const PROGRESSIVE_DELAYS = [10000, 60000, 300000]; // 10s, 1min, 5min
|
||||||
const MAX_DELAY = 32000; // 32 Sekunden
|
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) => {
|
app.post('/login', async (req, res) => {
|
||||||
const { username, password } = req.body;
|
const { username, password, captcha } = req.body;
|
||||||
const user = users.find(u => u.username === username && u.password === password);
|
const ip = req.ip || req.connection.remoteAddress;
|
||||||
|
|
||||||
if (user) {
|
const user = users.find(u => u.username === username && u.password === password);
|
||||||
// Bei Erfolg: Fehlversuche zurücksetzen
|
const now = Date.now();
|
||||||
if (loginAttempts[username]) {
|
|
||||||
delete loginAttempts[username];
|
// Init
|
||||||
}
|
|
||||||
return res.json({ ok: true });
|
|
||||||
} else {
|
|
||||||
// Fehlversuch zählen und Verzögerung berechnen
|
|
||||||
if (!loginAttempts[username]) {
|
if (!loginAttempts[username]) {
|
||||||
loginAttempts[username] = { count: 1, lastFail: Date.now() };
|
loginAttempts[username] = {
|
||||||
} else {
|
count: 0,
|
||||||
loginAttempts[username].count += 1;
|
lockedUntil: null,
|
||||||
loginAttempts[username].lastFail = Date.now();
|
lastFail: null,
|
||||||
|
requiresCaptcha: false
|
||||||
|
};
|
||||||
}
|
}
|
||||||
// Exponentielle Verzögerung, maximal MAX_DELAY
|
const state = loginAttempts[username];
|
||||||
const delay = Math.min(BASE_DELAY * Math.pow(2, loginAttempts[username].count - 1), MAX_DELAY);
|
|
||||||
|
// 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));
|
await new Promise(r => setTimeout(r, delay));
|
||||||
return res.status(401).json({ ok: false, error: `Falscher Benutzername oder Passwort. Wartezeit: ${delay / 1000}s` });
|
|
||||||
}
|
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) => {
|
app.get('/login', (req, res) => {
|
||||||
res.sendFile(__dirname + '/index.html');
|
res.sendFile(__dirname + '/index.html');
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`läuft http://localhost:${port}/login`);
|
console.log(`Sicherer Login-Server läuft auf http://localhost:${port}/login`);
|
||||||
});
|
});
|
||||||
+118
-32
@@ -1,62 +1,148 @@
|
|||||||
|
<!doctype html>
|
||||||
<!doctype html>
|
|
||||||
<html lang="de">
|
<html lang="de">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<title>Login Demo</title>
|
<title>Login</title>
|
||||||
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||||
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
|
||||||
<style>
|
<style>
|
||||||
body{font-family:system-ui,Segoe UI,Roboto,Arial;margin:32px}
|
body {
|
||||||
form{max-width:360px}
|
font-family: 'Inter', sans-serif;
|
||||||
input{display:block;margin:8px 0;padding:8px;width:100%}
|
background: #f4f6f8;
|
||||||
button{padding:8px 12px}
|
display: flex;
|
||||||
.msg{margin-top:12px}
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
min-height: 100vh;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.login-container {
|
||||||
|
background: white;
|
||||||
|
padding: 32px;
|
||||||
|
border-radius: 12px;
|
||||||
|
box-shadow: 0 8px 24px rgba(0,0,0,0.1);
|
||||||
|
max-width: 400px;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
font-size: 24px;
|
||||||
|
margin-bottom: 24px;
|
||||||
|
text-align: center;
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
|
||||||
|
label {
|
||||||
|
font-weight: 600;
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 4px;
|
||||||
|
color: #444;
|
||||||
|
}
|
||||||
|
|
||||||
|
input {
|
||||||
|
width: 100%;
|
||||||
|
padding: 10px;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
width: 100%;
|
||||||
|
padding: 12px;
|
||||||
|
background-color: #4F46E5;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 16px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover {
|
||||||
|
background-color: #4338ca;
|
||||||
|
}
|
||||||
|
|
||||||
|
.msg {
|
||||||
|
margin-top: 16px;
|
||||||
|
padding: 12px;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-size: 14px;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.msg.error {
|
||||||
|
background-color: #fee2e2;
|
||||||
|
color: #b91c1c;
|
||||||
|
border: 1px solid #fca5a5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.msg.success {
|
||||||
|
background-color: #dcfce7;
|
||||||
|
color: #166534;
|
||||||
|
border: 1px solid #86efac;
|
||||||
|
}
|
||||||
|
|
||||||
|
#captcha-container {
|
||||||
|
margin: 16px 0;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Login (Demo)</h1>
|
<div class="login-container">
|
||||||
|
<h1>Login</h1>
|
||||||
<form id="login">
|
<form id="login">
|
||||||
<label>Benutzername
|
<label for="username">Benutzername</label>
|
||||||
<input id="username" name="username" autocomplete="username" required />
|
<input id="username" name="username" required />
|
||||||
</label>
|
<label for="password">Passwort</label>
|
||||||
<label>Passwort
|
<input id="password" name="password" type="password" required />
|
||||||
<input id="password" name="password" type="password" autocomplete="current-password" required />
|
<div id="captcha-container">
|
||||||
</label>
|
<div class="h-captcha" data-sitekey="10000000-ffff-ffff-ffff-000000000001"></div>
|
||||||
<button type="submit">Login</button>
|
</div>
|
||||||
|
<button type="submit">Einloggen</button>
|
||||||
</form>
|
</form>
|
||||||
<div class="msg" id="msg"></div>
|
<div class="msg" id="msg"></div>
|
||||||
|
</div>
|
||||||
|
<script src="https://hcaptcha.com/1/api.js" async defer></script>
|
||||||
<script>
|
<script>
|
||||||
const form = document.getElementById('login');
|
const form = document.getElementById('login');
|
||||||
const msg = document.getElementById('msg');
|
const msg = document.getElementById('msg');
|
||||||
|
const captchaContainer = document.getElementById('captcha-container');
|
||||||
form.addEventListener('submit', async (e) => {
|
form.addEventListener('submit', async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
msg.textContent = 'Sende...';
|
msg.style.display = 'none';
|
||||||
const username = document.getElementById('username').value;
|
const username = document.getElementById('username').value;
|
||||||
const password = document.getElementById('password').value;
|
const password = document.getElementById('password').value;
|
||||||
|
const captcha = hcaptcha.getResponse();
|
||||||
|
const body = { username, password, captcha };
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/login', {
|
const res = await fetch('/login', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ username, password })
|
body: JSON.stringify(body)
|
||||||
});
|
});
|
||||||
|
const data = await res.json();
|
||||||
let data;
|
msg.style.display = 'block';
|
||||||
const ct = res.headers.get('content-type') || '';
|
|
||||||
if (ct.includes('application/json')) {
|
|
||||||
data = await res.json();
|
|
||||||
} else {
|
|
||||||
const text = await res.text();
|
|
||||||
data = { ok: res.ok, error: text || res.statusText };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res.ok && data.ok) {
|
if (res.ok && data.ok) {
|
||||||
msg.textContent = 'Erfolgreich authentifiziert!';
|
msg.textContent = '✅ Erfolgreich eingeloggt!';
|
||||||
|
msg.className = 'msg success';
|
||||||
|
hcaptcha.reset();
|
||||||
} else {
|
} else {
|
||||||
msg.textContent = 'Fehler: ' + (data.error || res.statusText);
|
msg.textContent = '❌ ' + (data.error || 'Login fehlgeschlagen');
|
||||||
|
msg.className = 'msg error';
|
||||||
|
if (data.requiresCaptcha) {
|
||||||
|
captchaContainer.style.display = 'block';
|
||||||
|
hcaptcha.reset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
msg.textContent = 'Netzwerkfehler';
|
msg.textContent = '⚠️ Netzwerkfehler: ' + err.message;
|
||||||
|
msg.className = 'msg error';
|
||||||
|
msg.style.display = 'block';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user