Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement]Lock account for timeout duration after failed login attempts #51948

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

rohitrs1983
Copy link
Contributor

@rohitrs1983 rohitrs1983 commented Oct 15, 2024

Lock account for timeout duration after failed login attempts

Why I'm doing:

What I'm doing:

Fixes #51917

$ mysql -h127.0.0.1 -P9030 -u jack -p
Enter password:
ERROR 1045 (28000): Access denied for user 'jack' (using password: YES)
$ mysql -h127.0.0.1 -P9030 -u jack -p
Enter password:
ERROR 1045 (28000): Access denied for user 'jack' (using password: YES)
$ mysql -h127.0.0.1 -P9030 -u jack -p
Enter password:
ERROR 5204 (HY000): Access denied for user 'jack'@'127.0.0.1'. Account is blocked for 30 seconds due to 3 consecutive failed logins.
$ mysql -h127.0.0.1 -P9030 -u jack -p
Enter password:
ERROR 5204 (HY000): Access denied for user 'jack'@'127.0.0.1'. Account is blocked for 4 seconds due to 3 consecutive failed logins.
$ mysql -h127.0.0.1 -P9030 -u jack -p
Enter password:
mysql>

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

@rohitrs1983 rohitrs1983 requested review from a team as code owners October 15, 2024 19:59
Copy link

sonarcloud bot commented Oct 15, 2024

@kevincai
Copy link
Contributor

@mergify rebase

Copy link
Contributor

mergify bot commented Nov 25, 2024

rebase

✅ Branch has been successfully rebased

Copy link
Contributor

@kevincai kevincai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rohitrs1983 what if the user account get locked and then never attempted for a long time. The failed record is not cleaned. May need to add periodic task to clean expired locks.

@rohitrs1983
Copy link
Contributor Author

@rohitrs1983 what if the user account get locked and then never attempted for a long time. The failed record is not cleaned. May need to add periodic task to clean expired locks.

@kevincai have fix the issue. i have added the cleanup mechanism as part of password validation process with a default cleanup timeout is 24h.

@rohitrs1983 rohitrs1983 requested a review from kevincai December 7, 2024 07:30
Copy link

sonarcloud bot commented Dec 7, 2024

Copy link

github-actions bot commented Dec 7, 2024

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

github-actions bot commented Dec 7, 2024

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

return checkPasswordForNative(remoteUser, remoteHost, remotePasswd, randomString);
UserIdentity authenticatedUser = null;
String userAndHost = remoteUser.concat("@").concat(remoteHost);
for (Map.Entry<String, Long> attempt : FAIL_TIME.entrySet()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not actually related to the checkPassword to the given remoteUser/remoteHost. Can extract it to a separate method to explicit what it is doing.

Also can reduce the check interval of the cleanup, no need to check for every checkPassowrd, but probably every 30 seconds or every minute is good enough.

Comment on lines +260 to +266
if (!isUserLocked(attempt.getKey())) {
clearFailedAttemptRecords(attempt.getKey());
} else {
if (getRemainingLockedTime(attempt.getKey()) <= 0) {
clearFailedAttemptRecords(attempt.getKey());
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!isUserLocked(attempt.getKey())) {
clearFailedAttemptRecords(attempt.getKey());
} else {
if (getRemainingLockedTime(attempt.getKey()) <= 0) {
clearFailedAttemptRecords(attempt.getKey());
}
}
if (!isUserLocked(attempt.getKey()) || getRemainingLockedTime(attempt.getKey()) <= 0) {
clearFailedAttemptRecords(attempt.getKey());
}

clearFailedAttemptRecords(userAndHost);
}
}
resetFailedAttemptRecords();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, shall move this to the periodic cleanup routine.

String userAndHost = authInfo.fullUserName.concat("@").concat(authInfo.remoteIp);
if (globalStateMgr.getAuthenticationMgr().isUserLocked(userAndHost)) {
throw new AccessDeniedException("Access denied for " + userAndHost + ". Locked for " +
globalStateMgr.getAuthenticationMgr().getRemainingLockedTime(userAndHost) + " seconds.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure it is good or not to show end user the seconds remaining for next attempt.

return null;
}
authenticatedUser = checkPasswordForNative(remoteUser, remoteHost, remotePasswd, randomString);
if (Config.max_failed_login_attempts >= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to prompt to user, the account will be locked for next xx fail attempts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Database password anti-brute force cracking
2 participants