mirror of
https://github.com/mailcow/mailcow-dockerized.git
synced 2026-11-05 08:19:22 +00:00
[Web] Add TFA Ratelimit
This commit is contained in:
4 files changed
+42
No files matched your search
@@ -1857,6 +1857,40 @@ function get_tfa($username = null, $id = null) {
|
||||
}
|
||||
}
|
||||
function verify_tfa_login($username, $_data) {
|
||||
global $redis;
|
||||
global $TFA_MAX_FAILS;
|
||||
global $TFA_FAIL_WINDOW;
|
||||
|
||||
if (empty($username)) {
|
||||
return false;
|
||||
}
|
||||
// Count failed second factors per account, so codes cannot be guessed from many addresses
|
||||
$fails_key = 'TFA_FAILS_' . $username;
|
||||
if (intval($redis->get($fails_key)) >= $TFA_MAX_FAILS) {
|
||||
$_SESSION['return'][] = array(
|
||||
'type' => 'danger',
|
||||
'log' => array(__FUNCTION__, $username, '*'),
|
||||
'msg' => 'tfa_login_locked'
|
||||
);
|
||||
$verified = false;
|
||||
}
|
||||
else {
|
||||
$verified = verify_tfa_token($username, $_data);
|
||||
}
|
||||
if ($verified !== true) {
|
||||
// The window starts with the first failure and is not extended by further attempts
|
||||
if ($redis->incr($fails_key) == 1) {
|
||||
$redis->expire($fails_key, $TFA_FAIL_WINDOW);
|
||||
}
|
||||
// Same message format as failed passwords, so the default fail2ban rule counts it
|
||||
error_log("mailcow UI: Invalid password for " . $username . " (second factor) by " . $_SERVER['REMOTE_ADDR']);
|
||||
$redis->publish("F2B_CHANNEL", "mailcow UI: Invalid password for " . $username . " (second factor) by " . $_SERVER['REMOTE_ADDR']);
|
||||
return false;
|
||||
}
|
||||
$redis->del($fails_key);
|
||||
return true;
|
||||
}
|
||||
function verify_tfa_token($username, $_data) {
|
||||
global $pdo;
|
||||
global $yubi;
|
||||
global $u2f;
|
||||
|
||||
Reference in new issue
Block a user