Sobes.tech
Junior — Middle

What methods can be used to prevent or reduce the impact of DoS attacks?

sobes.tech AI

Answer from AI

To prevent or mitigate the impact of DoS attacks, the following methods can be used:

  • Traffic filtering: blocking suspicious IP addresses or geographic regions.
  • Rate limiting: restricting the number of requests from a single client within a certain time frame.
  • Using CDN and load balancers: distributing the load among servers, which reduces the effect of the attack.
  • Authentication and CAPTCHAs: verifying that requests come from real users.
  • Monitoring and alerts: timely detection of anomalies in traffic.
  • Resource reservation: allocating additional resources to handle peak loads.

An example of simple rate limiting in Python using Flask and the Flask-Limiter library:

from flask import Flask
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

app = Flask(__name__)
limiter = Limiter(app, key_func=get_remote_address)

@app.route("/")
@limiter.limit("5 per minute")  # no more than 5 requests per minute from one IP

def index():
    return "Hello, world!"

if __name__ == "__main__":
    app.run()

This helps reduce the load from malicious actors attempting to overload the service.