乐闻世界logo
搜索文章和话题

What is CSRF attack and how does it work?

2月21日 16:10

CSRF (Cross-Site Request Forgery) is a type of web attack where attackers trick users into performing unintended actions on authenticated websites.

How It Works

  1. User Login: User logs into the target website (e.g., banking site), server creates session and returns Cookie
  2. Cookie Storage: Browser stores the target website's Cookie locally
  3. Visit Malicious Site: User visits a malicious website controlled by the attacker
  4. Send Request: Malicious site contains requests to the target website (e.g., form submission, AJAX request)
  5. Auto-send Cookie: Browser automatically sends the target website's Cookie
  6. Execute Action: Server validates the Cookie and executes the requested action

Attack Conditions

  • User is logged into the target website
  • Target website uses Cookie for authentication
  • Browser automatically sends Cookies
  • Target website lacks other protection mechanisms

Common Attack Scenarios

  • Bank transfers
  • Password changes
  • Data deletion
  • Sending emails
  • Adding admin accounts

Defense Measures

  1. CSRF Token: Add random Token to forms, server validates it
  2. SameSite Cookie: Set Cookie's SameSite attribute
  3. Verify Referer/Origin: Check request source
  4. Double Submit Cookie: Include Token in both Cookie and request parameters
  5. Custom Header: Use custom Header for validation

Difference from XSS

  • CSRF: Uses user's identity, doesn't require script injection
  • XSS: Injects malicious scripts, can steal Cookies or execute arbitrary operations

CSRF attacks exploit the browser's automatic Cookie sending mechanism and are a significant threat to web application security.

标签:CSRF