AntiBrutePlugin.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. if (!defined('GNUSOCIAL')) { exit(1); }
  3. class AntiBrutePlugin extends Plugin {
  4. protected $failed_attempts = 0;
  5. protected $unauthed_user = null;
  6. protected $client_ip = null;
  7. const FAILED_LOGIN_IP_SECTION = 'failed_login_ip';
  8. public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
  9. {
  10. if (common_is_email($nickname)) {
  11. $this->unauthed_user = User::getKV('email', common_canonical_email($nickname));
  12. } else {
  13. $this->unauthed_user = User::getKV('nickname', Nickname::normalize($nickname));
  14. }
  15. if (!$this->unauthed_user instanceof User) {
  16. // Unknown username continue processing StartCheckPassword (maybe uninitialized LDAP user etc?)
  17. return true;
  18. }
  19. // This probably needs some work. For example with IPv6 you can easily generate new IPs...
  20. $client_ip = common_client_ip();
  21. $this->client_ip = $client_ip[0] ?: $client_ip[1]; // [0] is proxy, [1] should be the real IP
  22. $this->failed_attempts = (int)$this->unauthed_user->getPref(self::FAILED_LOGIN_IP_SECTION, $this->client_ip);
  23. switch (true) {
  24. case $this->failed_attempts >= 5:
  25. common_log(LOG_WARNING, sprintf('Multiple failed login attempts for user %s from IP %s - brute force attack?',
  26. $this->unauthed_user->getNickname(), $this->client_ip));
  27. // 5 seconds is a good max waiting time anyway...
  28. sleep($this->failed_attempts % 5 + 1);
  29. break;
  30. case $this->failed_attempts > 0:
  31. common_debug(sprintf('Previously failed login on user %s from IP %s - sleeping %u seconds.',
  32. $this->unauthed_user->getNickname(), $this->client_ip, $this->failed_attempts));
  33. sleep($this->failed_attempts);
  34. break;
  35. default:
  36. // No sleeping if it's our first failed attempt.
  37. }
  38. return true;
  39. }
  40. public function onEndCheckPassword($nickname, $password, $authenticatedUser)
  41. {
  42. if ($authenticatedUser instanceof User) {
  43. // We'll trust this IP for this user and remove failed logins for the database..
  44. $authenticatedUser->delPref(self::FAILED_LOGIN_IP_SECTION, $this->client_ip);
  45. return true;
  46. }
  47. // See if we have an unauthed user from before. If not, it might be because the User did
  48. // not exist yet (such as autoregistering with LDAP, OpenID etc.).
  49. if ($this->unauthed_user instanceof User) {
  50. // And if the login failed, we'll increment the attempt count.
  51. common_debug(sprintf('Failed login tests for user %s from IP %s',
  52. $this->unauthed_user->getNickname(), $this->client_ip));
  53. $this->unauthed_user->setPref(self::FAILED_LOGIN_IP_SECTION, $this->client_ip, ++$this->failed_attempts);
  54. }
  55. return true;
  56. }
  57. public function onPluginVersion(&$versions)
  58. {
  59. $versions[] = array('name' => 'AntiBrute',
  60. 'version' => GNUSOCIAL_VERSION,
  61. 'author' => 'Mikael Nordfeldth',
  62. 'homepage' => 'http://gnu.io/',
  63. 'description' =>
  64. // TRANS: Plugin description.
  65. _m('Anti bruteforce method(s).'));
  66. return true;
  67. }
  68. }