Table of Contents 4 sections
What is CSRF?
Cross-Site Request Forgery (CSRF) is an attack that forces an authenticated user to perform unwanted actions on a web application. Unlike XSS, which exploits user trust in a site, CSRF exploits a site's trust in the user's browser.
If a WordPress administrator visits a malicious page while logged in, a CSRF attack could silently create a new admin account, change site settings, install a plugin, or delete content — all without the admin's knowledge.
How CSRF Attacks Work
The Attack Flow
- The victim logs into their WordPress site (creating an authenticated session)
- The victim visits a malicious page (via email link, forum post, or compromised site)
- The malicious page contains a hidden form or JavaScript that sends a request to the WordPress site
- The browser automatically includes the victim's session cookies with the request
- WordPress processes the request as if the admin made it intentionally
Example: Creating an Admin Account via CSRF
<!-- Malicious page hosted on attacker's server -->
<form action="https://target-site.com/wp-admin/user-new.php" method="POST" id="csrf-form">
<input type="hidden" name="user_login" value="hacker" />
<input type="hidden" name="email" value="hacker@evil.com" />
<input type="hidden" name="pass1" value="SuperSecureP@ss" />
<input type="hidden" name="role" value="administrator" />
<input type="hidden" name="createuser" value="1" />
</form>
<script>document.getElementById('csrf-form').submit();</script>
If the WordPress site doesn't properly verify nonces, this request will succeed because the browser sends the admin's session cookie.
Common CSRF Targets in WordPress
- Plugin settings pages — Changing plugin configurations without nonce checks
- User management — Creating, deleting, or modifying user roles
- Content management — Publishing, deleting, or modifying posts and pages
- Theme/plugin installation — Installing backdoored themes or plugins
- AJAX handlers — Admin-ajax.php endpoints without nonce verification
Preventing CSRF in WordPress
For Site Owners
- Keep WordPress and plugins updated — CSRF fixes are common in security patches
- Use a security plugin — Many add additional CSRF protections and SameSite cookie configuration
- Log out when done — Don't leave admin sessions active while browsing other sites
- Use separate browsers — Consider using one browser for admin tasks and another for general browsing
For Developers
- Always use nonces — Generate with
wp_nonce_field()and verify withwp_verify_nonce()orcheck_admin_referer() - Verify nonces on all state-changing requests — Every form submission and AJAX call that modifies data must include nonce verification
- Use
check_ajax_referer()— For AJAX endpoints, this function verifies both the nonce and the referrer - Set SameSite cookie attribute — Configure cookies with
SameSite=StrictorSameSite=Laxto prevent cross-origin cookie sending
FAQ
Frequently Asked Questions
CSRF (Cross-Site Request Forgery) is an attack that forces an authenticated user to perform unwanted actions. If a WordPress administrator visits a malicious page while logged in, a CSRF attack could silently create admin accounts, change settings, install plugins, or delete content without the admin's knowledge.
XSS exploits user trust in a site by injecting malicious scripts into the site itself. CSRF exploits a site's trust in the user's browser — it tricks the browser into sending authenticated requests to the site on behalf of the attacker.
WordPress nonces are unique tokens generated with wp_nonce_field() and verified with wp_verify_nonce(). They ensure that form submissions and AJAX requests originate from the actual WordPress site rather than from malicious third-party pages.
Keep WordPress and plugins updated, use security plugins that add CSRF protections, log out of admin sessions when browsing other sites, and consider using separate browsers for admin tasks and general browsing.
Common CSRF targets include plugin settings pages, user management (creating/deleting users), content management (publishing/deleting posts), theme and plugin installation, and AJAX handlers (admin-ajax.php endpoints without nonce verification).
Tags
Related Posts
Zero-Day Plugin Exploits on WordPress: Attacks Before a Patch Exists
A zero-day plugin exploit attacks a flaw before the developer has a fix. Learn what makes them dangerous and how to reduce your exposure to the unknown.
Cryptojacking on WordPress: When Hackers Mine Cryptocurrency With Your Resources
Cryptojacking hijacks your server or your visitors' browsers to mine cryptocurrency. Learn how to spot the signs, clean the infection, and prevent it.
Card Skimming and Magecart on WordPress: How Attackers Steal Payment Data at Checkout
Card skimming injects invisible code into your checkout to steal customer payment details. Learn how Magecart-style attacks hit WooCommerce and how to stop them.