Coder Social home page Coder Social logo

django-brute_security's Introduction

django-brute_security

Provides simple IP based brute force security for django user login

Basic Functinality

This module provides basic functions to check if an IP trys to bruteforce on or many accounts at your django page. It also provides posibilities to lock an IP.

Installation

The installation is pretty easy. Put the module into your settings installed_apps. Resync your database. Set the following variables in your projects settings.py (if you dont want to use the default values)

The following variable configures the available login attempts a user can make before his IP is blocked. If nothing is set in your settings.py the default of 5 attempts will be used.

    BRUTE_FORCE_THRESHOLD = 5

The following variable configures the time until a blocked IP is set free, and how long the last login can be in the past and still accumulates with new login errors. Default is 300 seconds (5 minutes)

    BRUTE_FORCE_RESET_THRESHOLD = 300 #in seconds

Set the BRUTE_FORCE_PURGE_MULTIPLIER variable to set a multiplier which is used in the purge method that cleans the database of old entries. If you call brute_force_purge with default values, entries that are older than the default BRUTE_FORCE_RESET_THRESHOLD times the default BRUTE_FORCE_PURGE_MULTIPLIER will be deleted. Default is 2

    BRUTE_FORCE_PURGE_MULTIPLIER = 2   

Usage

You should check for brute force attacks prior the validation of the login form.

Use brute_force_check(request.META['REMOTE_ADDR']) to check if the given IP has a entry in the security database.

It will return False if the IP is blocked at the current time.

It will return an Object if the IP has an entry but is not blocked currently

It will return None if there is no entry for it yet

Use brute_force_add(request.META['REMOTE_ADDR'], [brute_force_object]) to add or update a entry in the brute_force table. (Use the entry from brute_force_check as entry attribute to save one database hit).

Use brute_force_purge() to purge old entries and keep the database table clean.

Example:

from django.contrib import messages
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth import login
from django.contrib.auth.forms import AuthenticationForm
from brute_security.utils import brute_force_check, brute_force_add

def my_login_view(request):
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        bfc_entry = brute_force_check(request.META['REMOTE_ADDR'])
        if bfc_entry == False:
           messages.add_message(request, messages.ERROR, _('Too many login attempts, please try again later.')) 
        else:
            if form.is_valid():
                response = login(request, form.get_user())
                return response
            else:
                brute_force_add(request.META['REMOTE_ADDR'], bfc_entry)        
    else:
        form = AuthenticationForm()
    return render_to_response('my_login_template.html', {
        'form': form,
        }, context_instance=RequestContext(request))

django-brute_security's People

Contributors

se-schwarz avatar

Stargazers

Bjorn Meyer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.