Blog | G5 Cyber Security

Web Security: Beyond Common Attacks

TL;DR

SQL injection and Cross-Site Scripting (XSS) are well-known web security threats, but modern attacks go further. This guide covers techniques like CSRF, session fixation, insecure direct object references, and more, with practical steps to protect your applications.

1. Understanding the Threat Landscape

While SQL injection and XSS remain prevalent, attackers are increasingly using sophisticated methods. Here’s a breakdown of common attacks beyond those basics:

2. Protecting Against CSRF

  1. Use Anti-CSRF Tokens: Generate a unique, unpredictable token for each user session and include it in forms. Verify the token on server-side submission.
  2. # Example (Python/Flask)
    from flask import Flask, render_template, request, session
    import secrets
    
    app = Flask(__name__)
    app.secret_key = secrets.token_hex(16)
    
    @app.route('/form')
    def form():
        session['csrf_token'] = secrets.token_hex(16)
        return render_template('form.html', csrf_token=session['csrf_token'])
    
    @app.route('/submit', methods=['POST'])
    def submit():
        if request.form['csrf_token'] != session['csrf_token']:
            return 'CSRF token invalid!'
        # Process form data...
        return 'Form submitted successfully!'
  3. SameSite Cookies: Set the SameSite attribute on cookies to Strict or Lax to prevent cross-site requests.
  4. Set-Cookie: sessionid=value; SameSite=Strict

3. Preventing Session Fixation

  1. Regenerate Session IDs on Login: After successful authentication, create a new session ID and invalidate the old one.
  2. # Example (PHP)
    session_start();
    if (isset($_POST['login'])) {
        // Authentication logic...
        session_regenerate_id(true);
    }
    
  3. Use Secure Session Cookies: Ensure session cookies are marked as Secure and HttpOnly.

4. Mitigating IDOR Vulnerabilities

  1. Implement Proper Authorization Checks: Always verify that the user has permission to access the requested resource before granting access. Never trust client-side data for authorization decisions.
  2. Use Indirect Object References: Instead of using direct object IDs, use opaque identifiers or mapping tables.

5. Addressing SSRF Risks

  1. Input Validation and Sanitization: Thoroughly validate and sanitize any user-supplied input used in server-side requests.
  2. Whitelist Allowed Domains/IPs: Restrict the domains or IP addresses that the server is allowed to connect to.
  3. Disable Unnecessary Network Access: Limit the server’s ability to make external network connections if it’s not required for its functionality.

6. Strengthening Authentication and Authorization

7. Injection Attack Prevention

Exit mobile version