Package: brute-guard
BruteGuardA Lightweight tool for preventing Brute Force Attacks.FeaturesIntroductionsqlite3ExamplesBlock IPsBlock usernamesAttributesDefault configsControl objectIntroductionThe goal of this tool is to block IPs or usernames that try to access some resource and fail consecutively in a time range.Currently this tool support the follow databases:sqlite3SQLite3Block brute force attacks using sqlite3 under the hood.ExamplesBlock IPsBelow is an example that blocks a specific IP that fails 4 times (failures) consecutively in an interval of 1 second (failure_time). Each blocked IP will be blocked by 1 second (blocked_expires_at):importtimefrombrute_guard.sqlite3importBruteGuardbg=BruteGuard(blocked_expires_at="+1 second",failure_time="-1 second",failures=4,database_url="/tmp/bg.sqlite")bg.control.create_tables()# Create table if existsattacker_ip="10.10.10.10"# Registering fail accessbg.ip.access("fake-user1",attacker_ip,success=False)bg.ip.access("fake-user2",attacker_ip,success=False)bg.ip.access("some-user",attacker_ip,success=False)bg.ip.access("some-user",attacker_ip,success=False)ifbg.ip.is_blocked(attacker_ip):print(f'"{attacker_ip}" is blocked.')print("Sleeping 1 second")time.sleep(1)ifbg.ip.is_blocked(attacker_ip)isFalse:print(f'"{attacker_ip}" is allow.')Block usernamesBelow is an example that blocks a specific username that fails 3 times (failures) consecutively in an interval of 2 seconds (failure_time). Each blocked IP will be blocked by 2 seconds (blocked_expires_at):importtimefrombrute_guard.sqlite3importBruteGuardbg=BruteGuard(blocked_expires_at="+2 second",failure_time="-2 second",failures=3,database_url="/tmp/bg.sqlite")bg.control.create_tables()# Create table if existsusername="some.username"# Registering fail accessbg.user.access(username,"3.10.24.4",success=False)bg.user.access(username,"3.10.12.4",success=False)bg.user.access(username,"4.15.10.4",success=False)ifbg.user.is_blocked(username):print(f'"{username}" is blocked.')print("Sleeping 2 second")time.sleep(2)ifbg.user.is_blocked(username)isFalse:print(f'"{username}" is allow.')Attributes@dataclassclassBruteGuard:access_expires_at:str="+1 day"blocked_expires_at:str="+1 hour"failure_time:str="-10 second"failures:int=8purge_time:Optional[timedelta]=timedelta(minutes=60)database_url:str="/tmp/db.sqlite"access_expires_at: Time value for to expire access data using SQLite3Modifierspattern. You must use a plus signal because the record will expire in the future.Examples: "+1 day" | "+2 day" | etc.blocked_expires_at: Time value for to expire access data using SQLite3Modifierspattern. You must use a plus signal because the record will expire in the future.Examples: "+1 hour" | "+4 hour" | etc.failures: The amount of accepted consecutive failures.failure_time: Time interval for verifying the consecutive failures.Examples: "-5 second" | "-10 second" | etc.purge_time: Define time to purge expired data.If you use this configuration in the nextaccesswill be executed a delete (with vacuum) of expired data.To disable this expiration, setpurge_timetoNone.database_url: Connection string for use inconnectfunction. There is no any treatment in this value before passing to connect function, so, you can use any value accepted byconnectfunction.We recommend that you use a database in a file.We do not recommend that you use a memory database because if you start a multiprocessing application each process will have a different database.Note:failuresandfailure_timedefines the following: If an IP (or username) fails consecutivelyfailurestimes in the lastfailure_time(interval time) this IP will be blocked.Default configurationssynchronous = offauto_vacuum = 1cache_size = 2000Control objectThis object is used to control some operations in the database.frombrute_guard.sqlite3importBruteGuardbg=BruteGuard()bg.control.create_tables()bg.control.drop_tables()bg.control.purge_all()bg.control.purge_expired()create_tables: Createaccesstable andblockedtable if not exist.drop_tables: Dropaccesstable andblockedtable if exist.purge_all: Purge all data fromaccesstable andblockedtable.purge_expired: Purge all expired data fromaccesstable andblockedtable.