Menü schliessen
Created: January 27th 2025
Last updated: February 25th 2025
Categories: Cyber Security,  IT Development
Author: Ian Walser

CSRF Attacks: How They Work & How to Secure Your WordPress Website

Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Introduction

Cross-Site Request Forgery (CSRF) is one of the most overlooked web security vulnerabilities that can compromise user accounts, steal sensitive data, and even hijack WordPress websites. In this post, we will break down how CSRF attacks work and provide step-by-step guidance on securing your WordPress site from these threats.

What is a CSRF Attack?

CSRF is an attack that forces an authenticated user to execute unwanted actions on a web application without their consent. This happens when a malicious actor tricks the user into performing an action, such as changing their email address or making a transaction, without their knowledge.

How Does CSRF Work?

A CSRF attack typically follows these steps:

  1. A user logs into a website and remains authenticated.
  2. The attacker sends the user a malicious link or embeds a request in a webpage.
  3. Without realizing, the user clicks on the link or loads the page, sending a request to the authenticated site.
  4. The request executes with the user's credentials, leading to unauthorized actions.

Example of a CSRF Attack

Consider a WordPress administrator who is logged into their dashboard. An attacker might trick them into visiting a malicious website that contains the following hidden form:

<form action="https://example.com/wp-admin/options-general.php" method="POST">
    <input type="hidden" name="admin_email" value="attacker@example.com">
    <input type="submit" value="Submit">
</form>

When the admin visits the page, the form is automatically submitted, changing the admin email without their knowledge.

How to Secure Your WordPress Website from CSRF Attacks

1. Use Nonces in WordPress

WordPress provides nonces (numbers used once) to protect against CSRF. They are unique tokens that verify requests.

Adding a Nonce to a Form

wp_nonce_field('update_settings', 'security_token');

This function generates a hidden input field with a nonce value.

Verifying the Nonce

if (!isset($_POST['security_token']) || !wp_verify_nonce($_POST['security_token'], 'update_settings')) {
    die('Security check failed');
}

This ensures that only valid requests are processed.

2. Restrict HTTP Methods

CSRF attacks often exploit GET requests. Ensure that sensitive actions use POST:

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    die('Invalid request method');
}

3. Enable CORS Protection

Configuring CORS properly prevents unauthorized cross-origin requests:

Header always set Access-Control-Allow-Origin "https://yourdomain.com"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS"

4. Use Security Plugins

Several WordPress plugins can help protect against CSRF attacks:

  • Wordfence Security - Provides firewall and CSRF protection.
  • All-in-One WP Security - Includes CSRF defense mechanisms.
  • Important: As good as these plugins can be, using them won't guarantee a perfectly safe website.

Conclusion

CSRF attacks can be devastating if left unchecked, but by implementing nonces, restricting HTTP methods, and using security plugins, you can significantly reduce the risk. Keeping WordPress secure is an ongoing effort, so always stay updated on best practices.