Brute Force Attack ⚠️ Deep Dive 🪪

Aditya Pandey
5 min readMar 10, 2024

--

Description

A brute force attack is a trial-and-error method used to discover passwords or encryption keys by systematically attempting all possible combinations. It involves repeatedly trying different password or key combinations until the correct one is found. Brute force attacks are often employed against login pages, cryptographic systems, or any system where user authentication is required.

How It Works
1. Username and Passwords:
— Attackers attempt various combinations of usernames and passwords.
— For each combination, they try to log in to the targeted system or application.

2. Encryption Keys:
— In the context of encryption, attackers systematically test different keys until they find the one that decrypts the data.

3. Automated Tools:
— Brute force attacks can be manual, but automated tools significantly increase the speed and efficiency of the attack.

Countering Brute Force Attacks:

1. Account Lockout Mechanism:

Description:
Implement an account lockout mechanism that temporarily locks a user’s account after a certain number of failed login attempts.

Countermeasure

from flask import Flask, request, jsonify
from flask_limiter import Limiter

app = Flask(__name__)
limiter = Limiter(app, key_func=lambda: request.remote_addr)

@app.route('/login', methods=['POST'])
@limiter.limit("5 per minute") # Allow 5 login attempts per minute
def login():
username = request.form.get('username')
password = request.form.get('password')

# Check username and password
if validate_credentials(username, password):
return jsonify({'message': 'Login successful'})
else:
return jsonify({'message': 'Invalid username or password'}), 401

2. CAPTCHA Challenges

Description:
Integrate CAPTCHA challenges to ensure that login attempts are made by humans, not automated scripts.

Countermeasure

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, ValidationError
from flask import Flask, render_template, request, jsonify
from flask_limiter import Limiter

app = Flask(__name__)
limiter = Limiter(app, key_func=lambda: request.remote_addr)

class LoginForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
submit = SubmitField('Login')

@app.route('/login', methods=['GET', 'POST'])
@limiter.limit("5 per minute") # Allow 5 login attempts per minute
def login():
form = LoginForm()
if form.validate_on_submit():
# Validate CAPTCHA
if validate_captcha():
# Continue with the login process
username = form.username.data
password = form.password.data
if validate_credentials(username, password):
return jsonify({'message': 'Login successful'})
else:
return jsonify({'message': 'Invalid username or password'}), 401
else:
return jsonify({'message': 'CAPTCHA validation failed'}), 401
return render_template('login.html', form=form)

3. Rate Limiting

Description
Implement rate limiting to restrict the number of login attempts within a specific time frame.

Countermeasure

from flask import Flask, request, jsonify
from flask_limiter import Limiter

app = Flask(__name__)
limiter = Limiter(app, key_func=lambda: request.remote_addr)

@app.route('/login', methods=['POST'])
@limiter.limit("5 per minute") # Allow 5 login attempts per minute
def login():
username = request.form.get('username')
password = request.form.get('password')

# Check username and password
if validate_credentials(username, password):
return jsonify({'message': 'Login successful'})
else:
return jsonify({'message': 'Invalid username or password'}), 401

4. Strong Password Policies

Description
Enforce strong password policies to make it more difficult for attackers to guess passwords.

Countermeasure

from passlib.hash import sha256_crypt

def hash_password(password):
# Use a strong cryptographic hash function
return sha256_crypt.using(rounds=1000).hash(password)

def validate_credentials(username, password):
# Retrieve stored hashed password for the given username from the database
stored_password_hash = get_stored_password(username)

# Verify the entered password against the stored hash
return sha256_crypt.verify(password, stored_password_hash)

5. Two-Factor Authentication (2FA)

Description
Implement two-factor authentication to add an extra layer of security, requiring users to provide a second verification method.

Countermeasure

from flask import Flask, request, jsonify
from flask_limiter import Limiter
from flask_otp import OTP

app = Flask(__name__)
limiter = Limiter(app, key_func=lambda: request.remote_addr)

@app.route('/login', methods=['POST'])
@limiter.limit("5 per minute") # Allow 5 login attempts per minute
def login():
username = request.form.get('username')
password = request.form.get('password')
otp_code = request.form.get('otp_code')

# Check username and password
if validate_credentials(username, password) and validate_otp(username, otp_code):
return jsonify({'message': 'Login successful'})
else:
return jsonify({'message': 'Invalid username, password, or OTP'}), 401

These countermeasures provide a layered approach to mitigate the risk of brute force attacks. Depending on your application’s requirements, you can implement one or more of these strategies to enhance security. Keep in mind that a combination of preventive measures often provides the most robust defense against brute force attacks.

6. Multi-Factor Authentication (MFA)

Description
Extend security with multi-factor authentication, combining multiple authentication factors to ensure a higher level of identity verification.

Countermeasure

from flask import Flask, request, jsonify
from flask_limiter import Limiter
from flask_otp import OTP

app = Flask(__name__)
limiter = Limiter(app, key_func=lambda: request.remote_addr)

@app.route('/login', methods=['POST'])
@limiter.limit("5 per minute") # Allow 5 login attempts per minute
def login():
username = request.form.get('username')
password = request.form.get('password')
otp_code = request.form.get('otp_code')
biometric_data = request.form.get('biometric_data')

# Check username and password
if validate_credentials(username, password) and validate_otp(username, otp_code) and validate_biometrics(username, biometric_data):
return jsonify({'message': 'Login successful'})
else:
return jsonify({'message': 'Invalid username, password, OTP, or biometric data'}), 401

7. Monitoring and Logging

Description
Implement comprehensive logging and monitoring to detect and respond to unusual patterns or suspicious activities.

Countermeasure

import logging

@app.route('/login', methods=['POST'])
@limiter.limit("5 per minute") # Allow 5 login attempts per minute
def login():
username = request.form.get('username')
password = request.form.get('password')

# Check username and password
if validate_credentials(username, password):
logging.info(f"Successful login attempt for user: {username}")
return jsonify({'message': 'Login successful'})
else:
logging.warning(f"Failed login attempt for user: {username}")
return jsonify({'message': 'Invalid username or password'}), 401

8. Geographical Restrictions

Description
Implement geographical restrictions to allow logins only from specific geographic locations.

Countermeasure

from flask import Flask, request, jsonify
from flask_limiter import Limiter

app = Flask(__name__)
limiter = Limiter(app, key_func=lambda: request.remote_addr)

ALLOWED_REGIONS = ['US', 'Canada']

@app.route('/login', methods=['POST'])
@limiter.limit("5 per minute") # Allow 5 login attempts per minute
def login():
username = request.form.get('username')
password = request.form.get('password')
user_region = get_user_geographical_region(username)

# Check if the user's region is allowed
if user_region in ALLOWED_REGIONS and validate_credentials(username, password):
return jsonify({'message': 'Login successful'})
else:
return jsonify({'message': 'Invalid username, password, or geographical location'}), 401

9. Honeypots

Description
Deploy honeypots or fake credentials that, when accessed, trigger alerts or lockouts to catch and deter attackers.

Countermeasure

@app.route('/login', methods=['POST'])
@limiter.limit("5 per minute") # Allow 5 login attempts per minute
def login():
username = request.form.get('username')
password = request.form.get('password')

# Check if the entered credentials match the honeypot
if is_honeypot_credentials(username, password):
# Trigger alert or lockout
return jsonify({'message': 'Invalid username or password'}), 401

# Continue with regular login checks
if validate_credentials(username, password):
return jsonify({'message': 'Login successful'})
else:
return jsonify({'message': 'Invalid username or password'}), 401

10. Progressive Delays

Description
Implement progressive delays between login attempts to slow down brute force attacks.

Countermeasure

from time import sleep

@app.route('/login', methods=['POST'])
@limiter.limit("5 per minute") # Allow 5 login attempts per minute
def login():
username = request.form.get('username')
password = request.form.get('password')

# Introduce a delay based on the number of failed attempts
delay_seconds = calculate_delay_seconds(get_failed_login_attempts(username))
sleep(delay_seconds)

# Continue with regular login checks
if validate_credentials(username, password):
return jsonify({'message': 'Login successful'})
else:
return jsonify({'message': 'Invalid username or password'}), 401

These additional countermeasures provide further strategies to enhance the security posture against brute force attacks. Combining multiple techniques creates a robust defense mechanism, making it more challenging for attackers to compromise user credentials or gain unauthorized access. Always adapt these strategies based on your specific application requirements and continuously monitor for emerging threats.

--

--

Aditya Pandey
Aditya Pandey

Written by Aditya Pandey

Cyber security researcher | GNU / Linux Modder | GameDev

No responses yet