Compare commits

2 Commits

Author SHA1 Message Date
Fretux 2751277a97 rollback index.html 2025-10-29 15:12:25 +01:00
Fretux 97580a2652 rollback backend.js 2025-10-29 15:10:48 +01:00
2 changed files with 144 additions and 17 deletions
+110 -10
View File
@@ -1,20 +1,120 @@
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 LOG_FILE = path.join(__dirname, 'login.log');
const users = JSON.parse(fs.readFileSync('TEST_DATA/users.json', 'utf8')); const users = JSON.parse(fs.readFileSync('TEST_DATA/users.json', 'utf8'));
app.post('/login', (req, res) => { const LINEAR_DELAY_MS = 1000;
const { username, password } = req.body; const PROGRESSIVE_DELAYS = [10000, 60000, 300000];
const user = users.find(u => u.username === username && u.password === password); const LOCKOUT_THRESHOLD = 5;
const LOCKOUT_DURATION = 15 * 60 * 1000;
const loginAttempts = {};
if (user) { function logAttempt(username, status, ip, extra = '') {
res.status(200).json({ ok: true, message: 'Login erfolgreich' }); const timestamp = new Date().toISOString();
} else { const logLine = `[${timestamp}] [${ip}] [${username}] [${status}] ${extra}\n`;
res.status(401).json({ ok: false, error: 'Falscher Benutzername oder Passwort' }); fs.appendFileSync(LOG_FILE, logLine);
console.log(logLine.trim());
}
app.post('/login', async (req, res) => {
const {username, password, captcha} = req.body;
const ip = req.ip || req.connection.remoteAddress;
const user = users.find(u => u.username === username && u.password === password);
const now = Date.now();
if (!loginAttempts[username]) {
loginAttempts[username] = {
count: 0,
lockedUntil: null,
lastFail: null,
requiresCaptcha: false,
cooldownUntil: null
};
} }
const state = loginAttempts[username];
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.cooldownUntil && now < state.cooldownUntil) {
const remaining = Math.ceil((state.cooldownUntil - now) / 1000);
logAttempt(username, 'COOLDOWN', ip, `Wartezeit noch ${remaining}s`);
return res.status(429).json({
ok: false,
error: `Bitte ${remaining}s warten, bevor du es erneut versuchst.`,
requiresCaptcha: state.requiresCaptcha
});
}
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
});
}
}
if (user) {
delete loginAttempts[username];
logAttempt(username, 'SUCCESS', ip);
return res.json({ok: true, message: 'Login erfolgreich'});
}
state.count++;
state.lastFail = now;
logAttempt(username, 'FAIL', ip, `Fehlversuche: ${state.count}`);
if (state.count >= 3) {
state.requiresCaptcha = true;
}
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.`
});
}
const delay = PROGRESSIVE_DELAYS[state.count - 1] || LINEAR_DELAY_MS;
state.cooldownUntil = now + delay;
return res.status(401).json({
ok: false,
error: `Falscher Benutzername oder Passwort. Wartezeit: ${delay / 1000}s`,
requiresCaptcha: state.requiresCaptcha
});
}); });
app.get('/login', (req, res) => { app.get('/login', (req, res) => {
@@ -22,5 +122,5 @@ app.get('/login', (req, res) => {
}); });
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`);
}); });
+33 -6
View File
@@ -85,6 +85,11 @@
color: #166534; color: #166534;
border: 1px solid #86efac; border: 1px solid #86efac;
} }
#captcha-container {
margin: 16px 0;
display: none;
}
</style> </style>
</head> </head>
<body> <body>
@@ -92,37 +97,59 @@
<h1>Login</h1> <h1>Login</h1>
<form id="login"> <form id="login">
<label for="username">Benutzername</label> <label for="username">Benutzername</label>
<input id="username" name="username" required /> <input id="username" name="username" required/>
<label for="password">Passwort</label> <label for="password">Passwort</label>
<input id="password" name="password" type="password" required /> <input id="password" name="password" type="password" required/>
<div id="captcha-container">
<div class="h-captcha" data-sitekey="10000000-ffff-ffff-ffff-000000000001"></div>
</div>
<button type="submit">Einloggen</button> <button type="submit">Einloggen</button>
</form> </form>
<div class="msg" id="msg"></div> <div class="msg" id="msg"></div>
</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.style.display = 'none'; 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 body = { username, password }; 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(body) body: JSON.stringify(body)
}); });
const data = await res.json();
let data = {};
try {
data = await res.json(); // versuche JSON zu parsen
} catch (e) {
console.warn("Antwort war kein JSON:", e);
data = { ok: false, error: res.statusText };
}
msg.style.display = 'block'; msg.style.display = 'block';
if (res.ok && data.ok) { if (res.ok && data.ok) {
msg.textContent = '✅ Erfolgreich eingeloggt!'; msg.textContent = '✅ Erfolgreich eingeloggt!';
msg.className = 'msg success'; msg.className = 'msg success';
hcaptcha.reset();
} else { } else {
msg.textContent = '❌ ' + (data.error || 'Login fehlgeschlagen'); msg.textContent = '❌ ' + (data.error || 'Login fehlgeschlagen');
msg.className = 'msg error'; msg.className = 'msg error';
if (data.requiresCaptcha) {
captchaContainer.style.display = 'block';
hcaptcha.reset();
}
} }
} catch (err) { } catch (err) {
msg.textContent = '⚠️ Netzwerkfehler: ' + err.message; msg.textContent = '⚠️ Netzwerkfehler: ' + err.message;
msg.className = 'msg error'; msg.className = 'msg error';