AntiBrutePlugin.php 3.4 KB

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