You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
5.0 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Tracks Authentication Attempts and Prevents Brute-force Attacks module
  5. # Copyright (C) 2015-Today GRAP (http://www.grap.coop)
  6. # @author Sylvain LE GAL (https://twitter.com/legalsylvain)
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. import logging
  23. from openerp import fields, http, registry, SUPERUSER_ID
  24. from openerp.http import request
  25. from openerp.addons.web.controllers.main import Home, ensure_db
  26. _logger = logging.getLogger(__name__)
  27. class LoginController(Home):
  28. @http.route()
  29. def web_login(self, redirect=None, **kw):
  30. if request.httprequest.method == 'POST':
  31. ensure_db()
  32. remote = request.httprequest.remote_addr
  33. # Get registry and cursor
  34. config_obj = registry(request.session.db)['ir.config_parameter']
  35. attempt_obj = registry(
  36. request.session.db)['res.authentication.attempt']
  37. banned_remote_obj = registry(
  38. request.session.db)['res.banned.remote']
  39. cursor = attempt_obj.pool.cursor()
  40. # Get Settings
  41. max_attempts_qty = int(config_obj.search_read(
  42. cursor, SUPERUSER_ID,
  43. [('key', '=', 'auth_brute_force.max_attempt_qty')],
  44. ['value'])[0]['value'])
  45. environ_log = config_obj.search_read(
  46. cursor, SUPERUSER_ID,
  47. [('key', '=', 'auth_brute_force.environ_log')],
  48. ['value'])
  49. # Test if remote user is banned
  50. banned = banned_remote_obj.search(cursor, SUPERUSER_ID, [
  51. ('remote', '=', remote)])
  52. if banned:
  53. _logger.warning(
  54. "Authentication tried from remote '%s'. The request has"
  55. " been ignored because the remote has been banned after"
  56. " %d attempts without success. Login tried : '%s'." % (
  57. remote, max_attempts_qty, request.params['login']))
  58. request.params['password'] = ''
  59. else:
  60. # Try to authenticate
  61. result = request.session.authenticate(
  62. request.session.db, request.params['login'],
  63. request.params['password'])
  64. # Log attempt
  65. cursor.commit()
  66. environ = ''
  67. if environ_log:
  68. filter_value = environ_log[0]['value']
  69. filter_keys = [k.strip() for k in filter_value.split(',')]
  70. for key, value in request.httprequest.environ.items():
  71. if key in filter_keys or filter_value == '*':
  72. environ += '%s=%s\n' % (key, value)
  73. attempt_obj.create(cursor, SUPERUSER_ID, {
  74. 'attempt_date': fields.Datetime.now(),
  75. 'login': request.params['login'],
  76. 'remote': remote,
  77. 'environ': environ,
  78. 'result': banned and 'banned' or (
  79. result and 'successfull' or 'failed'),
  80. })
  81. cursor.commit()
  82. if not banned and not result:
  83. # Get last bad attempts quantity
  84. attempts_qty = len(attempt_obj.search_last_failed(
  85. cursor, SUPERUSER_ID, remote))
  86. if max_attempts_qty <= attempts_qty:
  87. # We ban the remote
  88. _logger.warning(
  89. "Authentication failed from remote '%s'. "
  90. "The remote has been banned. Login tried : '%s'." % (
  91. remote, request.params['login']))
  92. banned_remote_obj.create(cursor, SUPERUSER_ID, {
  93. 'remote': remote,
  94. 'ban_date': fields.Datetime.now(),
  95. })
  96. cursor.commit()
  97. else:
  98. _logger.warning(
  99. "Authentication failed from remote '%s'."
  100. " Login tried : '%s'. Attempt %d / %d." % (
  101. remote, request.params['login'], attempts_qty,
  102. max_attempts_qty))
  103. cursor.close()
  104. return super(LoginController, self).web_login(redirect=redirect, **kw)