2025년 7월 10일 목요일

Segfault ethical hacking week 13

Understanding CSRF (Cross-Site Request Forgery): A Deep Dive 

CSRF (Cross-Site Request Forgery) is a widespread and critical web vulnerability. It works by exploiting the trust a web application has in an authenticated user's browser, tricking it into performing unauthorized actions on the user's behalf.

In a CSRF attack, an attacker crafts a malicious request and tricks an authenticated user into sending it to a target application. Because the browser automatically includes the user's valid session credentials (like cookies) with the request, the server processes it as a legitimate action initiated by the user.

A typical CSRF attack flow looks like this:

  1. The user logs into a web application, establishing an active session.

  2. The attacker creates a malicious link or a hidden HTML form that targets a specific action on the site (e.g., changing a password, transferring funds).

  3. The attacker persuades the user to click the link or load the page containing the form, often through phishing emails or malicious websites.

  4. The user's browser automatically attaches authentication cookies to the forged request.

  5. The server receives the request and processes it, believing it came directly from the legitimate user.

Since the victim is often completely unaware that the action occurred, CSRF can lead to severe consequences, such as account compromise, unauthorized data manipulation, or financial theft.

Effective prevention strategies include:

  • Implementing unique anti-CSRF tokens for each state-changing request.

  • Setting the SameSite attribute on session cookies to restrict cross-site sending.

  • Validating the Referer or Origin headers to confirm the request's source.

  • Requiring re-authentication or CAPTCHA for highly sensitive actions.


CSRF vs. XSS: Key Differences and Mitigations Explained 

When studying web security, XSS (Cross-Site Scripting) and CSRF (Cross-Site Request Forgery) are two of the most common attacks you'll encounter. While both are dangerous, they operate in fundamentally different ways.

What Is XSS (Cross-Site Scripting)?

  • Definition: XSS is a vulnerability where an attacker injects malicious scripts into a website, which are then executed in a victim's browser. This allows the attacker to steal cookies, hijack user sessions, or deface websites.

  • Key Characteristics:

    • Occurs when user-supplied input is not properly sanitized before being displayed on a page.

    • The malicious script runs within the victim's browser (client-side).

What Is CSRF (Cross-Site Request Forgery)?

  • Definition: CSRF is an attack that tricks a server into performing an action that an authenticated user did not intend to execute. The attacker forges a request that the victim's browser sends using their existing session credentials.

  • Key Characteristics:

    • Forces a victim's browser to send an unauthorized request.

    • Relies on an existing authenticated session (managed via cookies).

    • The server is tricked into treating the forged request as legitimate.

Request Method Examples

CSRF attacks can abuse any HTTP method, but GET and POST are common.

  • GET Method Example: An attacker could embed a malicious image tag where the src attribute points to a state-changing action.

    HTML
    <img src="http://vulnerable.com/change-password?new_password=1234" width="0" height="0">
    
  • POST Method Example: An attacker can create a hidden form on their own website that targets an action on the vulnerable site.

    HTML
    <form action="http://vulnerable.com/transfer-funds" method="POST">
      <input type="hidden" name="to_account" value="attacker_account">
      <input type="hidden" name="amount" value="1000">
    </form>
    <script> document.forms[0].submit(); </script>
    

Core Security Concepts & Mitigations

Same Origin Policy (SOP)

  • Definition: SOP is a fundamental security mechanism enforced by browsers. It prevents scripts from a page at one origin (e.g., attacker.com) from accessing data or properties of a page at another origin (e.g., victim.com).

  • Same origin means the protocol, domain, and port must all match.

  • Relevance to CSRF: While SOP prevents an attacker's script from reading the response from a cross-origin request, it does not stop the browser from sending the request in the first place. This is why CSRF attacks work.

CSRF Mitigation Strategies

Effective defenses are layered and combine multiple techniques.

  1. Anti-CSRF Tokens

    • The server generates a unique, unpredictable token and embeds it in a hidden field in forms.

    • When the user submits the form, the token is sent back.

    • The server validates that the received token matches the one it issued for that session. Since an attacker cannot predict this token due to SOP, the forged request will fail validation.

  2. Referer Validation

    • The server checks the Referer header to ensure the request originated from its own domain.

    • Limitation: This can be bypassed, as some browsers or configurations may not send the header, or an attacker can use techniques to suppress it (<meta name="referrer" content="no-referrer">).

  3. SameSite Cookies

    • This is a modern and highly effective defense. By setting the SameSite attribute on a cookie to Strict or Lax, the browser is instructed not to send the cookie along with cross-site requests.

    • SameSite=Strict: The cookie is only sent for same-site requests.

    • SameSite=Lax: The cookie is sent with top-level GET requests but withheld on cross-domain POST requests.


Attack Flow and Summary

XSS Attack Flow

[Attacker injects a malicious script into a website] ➡️ [Victim loads the compromised page] ➡️ [The script executes in the victim's browser] ➡️ [Script steals session cookies or manipulates the page]

CSRF Attack Flow

[Victim logs into a trusted site] ➡️ [Attacker crafts a malicious request for an action on that site] ➡️ [Victim is tricked into loading the attacker's page or clicking a link] ➡️ [Victim's browser automatically sends the forged request with session cookies] ➡️ [Server processes the request as a valid user action]

Summary of Differences

AspectXSS (Cross-Site Scripting)CSRF (Cross-Site Request Forgery)
Execution PointIn the client's browserOn the server side (triggered by the client)
Primary GoalSteal user data (e.g., cookies) or control the pagePerform unauthorized state-changing actions
Core DefenseInput/output sanitization and encodingAnti-CSRF tokens, SameSite cookies
Trust ExploitedThe trust a user has in a websiteThe trust a website has in a user's browser

댓글 없음:

댓글 쓰기

Segfault ethical hacking week 16

Who Are You, and What Can You Do? (Authentication & Authorization Vulnerabilities) It's hard to imagine a web service without a logi...