README.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. Django Axes
  2. ===========
  3. .. image:: https://secure.travis-ci.org/django-pci/django-axes.png?branch=master
  4. :alt: Build Status
  5. :target: http://travis-ci.org/django-pci/django-axes
  6. ``django-axes`` is a very simple way for you to keep track of failed login
  7. attempts, both for the Django admin and for the rest of your site. The name is
  8. sort of a geeky pun, since ``axes`` can be read interpreted as:
  9. * "access", as in monitoring access attempts
  10. * "axes", as in tools you can use hack (generally on wood). In this case,
  11. however, the "hacking" part of it can be taken a bit further: ``django-axes``
  12. is intended to help you *stop* people from hacking (popular media
  13. definition) your website. Hilarious, right? That's what I thought too!
  14. Requirements
  15. ============
  16. ``django-axes`` requires a supported Django version. The application is
  17. intended to work around the Django admin and the regular
  18. ``django.contrib.auth`` login-powered pages.
  19. Look at https://www.djangoproject.com/download/ to check if your version
  20. is supported.
  21. Installation
  22. ============
  23. You can install the latest stable package running this command::
  24. $ pip install django-axes
  25. Development
  26. ===========
  27. You can contribute to this project forking it from github and sending pull requests.
  28. Running tests
  29. -------------
  30. Clone the repository and install the django version you want. Then run::
  31. $ ./runtests.py
  32. Configuration
  33. =============
  34. Just add `axes` to your ``INSTALLED_APPS``::
  35. INSTALLED_APPS = (
  36. 'django.contrib.admin',
  37. 'django.contrib.auth',
  38. 'django.contrib.contenttypes',
  39. 'django.contrib.sessions',
  40. 'django.contrib.sites',
  41. ...
  42. 'axes',
  43. ...
  44. )
  45. Remember to run ``python manage.py migrate`` to sync the database.
  46. Customizing Axes
  47. ----------------
  48. You have a couple options available to you to customize ``django-axes`` a bit.
  49. These should be defined in your ``settings.py`` file.
  50. * ``AXES_LOGIN_FAILURE_LIMIT``: The number of login attempts allowed before a
  51. record is created for the failed logins. Default: ``3``
  52. * ``AXES_LOCK_OUT_AT_FAILURE``: After the number of allowed login attempts
  53. are exceeded, should we lock out this IP (and optional user agent)?
  54. Default: ``True``
  55. * ``AXES_USE_USER_AGENT``: If ``True``, lock out / log based on an IP address
  56. AND a user agent. This means requests from different user agents but from
  57. the same IP are treated differently. Default: ``False``
  58. * ``AXES_COOLOFF_TIME``: If set, defines a period of inactivity after which
  59. old failed login attempts will be forgotten. Can be set to a python
  60. timedelta object or an integer. If an integer, will be interpreted as a
  61. number of hours. Default: ``None``
  62. * ``AXES_LOGGER``: If set, specifies a logging mechanism for axes to use.
  63. Default: ``'axes.watch_login'``
  64. * ``AXES_LOCKOUT_TEMPLATE``: If set, specifies a template to render when a
  65. user is locked out. Template receives cooloff_time and failure_limit as
  66. context variables. Default: ``None``
  67. * ``AXES_LOCKOUT_URL``: If set, specifies a URL to redirect to on lockout. If
  68. both AXES_LOCKOUT_TEMPLATE and AXES_LOCKOUT_URL are set, the template will
  69. be used. Default: ``None``
  70. * ``AXES_VERBOSE``: If ``True``, you'll see slightly more logging for Axes.
  71. Default: ``True``
  72. * ``AXES_USERNAME_FORM_FIELD``: the name of the form field that contains your
  73. users usernames. Default: ``username``
  74. * ``AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP``: If ``True`` prevents to login
  75. from IP under particular user if attempts limit exceed, otherwise lock out
  76. based on IP.
  77. Default: ``False``
  78. * ``AXES_NEVER_LOCKOUT_WHITELIST``: If ``True``, users can always login from
  79. whitelisted IP addresses.
  80. Default: ``False``
  81. * ``AXES_BEHIND_REVERSE_PROXY``: If ``True``, it will look for the IP address from the header defined at ``AXES_REVERSE_PROXY_HEADER``. Please make sure if you enable this setting to configure your proxy to set the correct value for the header, otherwise you could be attacked by setting this header directly in every request.
  82. Default: ``False``
  83. * ``AXES_REVERSE_PROXY_HEADER``: If ``AXES_BEHIND_REVERSE_PROXY`` is ``True``, it will look for the IP address from this header.
  84. Default: ``HTTP_X_FORWARDED_FOR``
  85. Usage
  86. =====
  87. Using ``django-axes`` is extremely simple. All you need to do is periodically
  88. check the Access Attempts section of the admin.
  89. By default, django-axes will lock out repeated attempts from the same IP
  90. address. You can allow this IP to attempt again by deleting the relevant
  91. ``AccessAttempt`` records in the admin.
  92. You can also use the ``axes_reset`` management command using Django's
  93. ``manage.py``.
  94. * ``manage.py axes_reset`` will reset all lockouts and access records.
  95. * ``manage.py axes_reset ip`` will clear lockout/records for ip
  96. In your code, you can use ``from axes.utils import reset``.
  97. * ``reset()`` will reset all lockouts and access records.
  98. * ``reset(ip=ip)`` will clear lockout/records for ip
  99. * ``reset(username=username)`` will clear lockout/records for a username
  100. Issues
  101. ======
  102. Not being locked out after failed attempts
  103. ------------------------------------------
  104. You may find that Axes is not capturing your failed login attempts. It may
  105. be that you need to manually add watch_login to your login url.
  106. For example, in your urls.py::
  107. ...
  108. from my.custom.app import login
  109. from axes.decorators import watch_login
  110. ...
  111. urlpatterns = patterns('',
  112. (r'^login/$', watch_login(login)),
  113. ...
  114. Locked out without reason
  115. -------------------------
  116. It may happen that you have suddenly become locked out without a single failed
  117. attempt. One possible reason is that you are using some custom login form and the
  118. username field is named something different than "username", e.g. "email". This
  119. leads to all users attempts being lumped together. To fix this add the following
  120. to your settings:
  121. AXES_USERNAME_FORM_FIELD = "email"
  122. Using a captcha
  123. ===============
  124. Using https://github.com/mbi/django-simple-captcha you do the following:
  125. 1. Change axes lockout url in ``settings.py``::
  126. AXES_LOCKOUT_URL = '/locked'
  127. 2. Add the url in ``urls.py``::
  128. url(r'^locked/$', locked_out, name='locked_out'),
  129. 3. Create a captcha form::
  130. class AxesCaptchaForm(forms.Form):
  131. captcha = CaptchaField()
  132. 4. Create a captcha view for the above url that resets on captcha success and redirects::
  133. def locked_out(request):
  134. if request.POST:
  135. form = AxesCaptchaForm(request.POST)
  136. if form.is_valid():
  137. ip = get_ip_address_from_request(request)
  138. reset(ip=ip)
  139. return HttpResponseRedirect(reverse_lazy('signin'))
  140. else:
  141. form = AxesCaptchaForm()
  142. return render_to_response('locked_out.html', dict(form=form), context_instance=RequestContext(request))
  143. 5. Add a captcha template::
  144. <form action="" method="post">
  145. {% csrf_token %}
  146. {{ form.captcha.errors }}
  147. {{ form.captcha }}
  148. <div class="form-actions">
  149. <input type="submit" value="Submit" />
  150. </div>
  151. </form>
  152. Done.