Skip to main content

A device token driven bruteforce protection for the Django REST framework

Project description

Desription

drf_bf_protect X is the rudimentary implementation of a system for the Django REST framework to slow down a bruteforce attack by blocking logins. To not cut of users from the application, it uses a device cookie that is set on successfull login, to allow users to login from trusted devices even if their login is blocked.

Special Features

  • A login is blocked
    • ... regardless of whether it really exists.
    • ... if the attemps comes from different devices.
  • The login from a trusted device of the user is still available.

If a user logins successfully a device cookie is created and saved through the browser. This identifies the device a trusted one on logins in the future. So if a brute force attack agains that username starts, it will be locked after several attemps. The user is not longer able to logi. Except from a trusted device. There is a different counter for attemps with a valid device cookie. If the login with a valid device cookie fails several times, it is set invalid. So a valid device cookie for a user does not allow a brute force attack.

Installation and configuration

Install

Install the module using pip or add it to your requirements.

pyton -m pip install drf-bf-protect

Add to settings.py

Add the app to your INSTALLED_APPS:

INSTALLED_APPS  = [
# ...
'drf_bf_protect',
# ...
]

The following entry configures the module:

BF_PROTECT_SETTINGS  = {
    "cookie_name": "did",
    "failures_before_lock": 5,
    "lock_time_minutes": 30,
    "reset_failure_count_seconds": 300,
    "backend": "drf_bf_protect.backend.DatabaseBackend"
}

cookie_name: The name of the cookie in the broweser. (default: "did") failures_before_lock: How many attemps to a username, before the login is blocked (default: 5) lock_time_minutes: How many minutes is the username blocked. After this time a login attemp from an untrusted device is possible again. (default: 30) reset_failure_count_seconds: The time window in seconds in which the failed logins must appear to lock the login (default: 300) backend: Allows to specify another backend to handle the procedure. In the project exists only the DatabaseBackend at the moment.

So the default configuration from the example means: If a user successfully logs in, a cookie with the name 'did' is set. If for a username fails 5 logins in 300 seconds this username is locked for 30 minutes.

Hi! I'm your first Markdown file in StackEdit. If you want to learn about StackEdit, you can read me. If you want to play with Markdown, you can edit me. Once you have finished with me, you can create new files by opening the file explorer on the left corner of the navigation bar.

Decorate login view

To make sure that the login is protected, we decorate the login view you are using. It is possible that you need to create your own login view to make that possible.

from  drf_bf_protect.decorators  import  bf_protect

@bf_protect(fieldname='username', case_sensitiv=True)
def your_login_view(request, *args, **kwargs):
   . ..

fieldname: The name of the field of the incoming data. (default: 'username') case_sensitiv: If True, the username is interpreted case_sensitiv ('SpamHam' != 'spamham'). If set to False it is not ('SpamHam' = 'spamham'). This is important! Be sure your configuration here matches the configuration of you app. If there names are not case sensitiv but here they are, a brute force attack is possible to use different uppercase letters.

Administration

There are entries in Django's admin area. To unlock a user, delete its entry from the Locks there.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_bf_protect-0.0.1.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_bf_protect-0.0.1-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file drf_bf_protect-0.0.1.tar.gz.

File metadata

  • Download URL: drf_bf_protect-0.0.1.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for drf_bf_protect-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1370bb4611fdd3f1546828a541bffc1fb1a920be6461c560e7bb8bea92f7da4b
MD5 789a5d3071bc3590744530c7c6222267
BLAKE2b-256 d6b18cf2890e3513d189910a05dad6c2f903ef07d2fdb8e6e08b6bb1071243d9

See more details on using hashes here.

File details

Details for the file drf_bf_protect-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: drf_bf_protect-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for drf_bf_protect-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 32c01ea9722033c1caf4d38d3d1aa0ba4fae2c646ffb63fd86e91919590e4a91
MD5 a4bf6463932e3acd0ac3740d026f748c
BLAKE2b-256 4104a015e85e98ade2d616d7008dac4fc5d73ca869a84146ce23613b4de37864

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page