← All posts
Web Security · 2 min

Seven ways to protect WordPress from SQL injection

SQL injection is one of the most damaging attacks a WordPress site faces. The measures that actually work, in order of how much they matter.

Illustration of SQL injection defences for a WordPress site

SQL injection is among the most common attacks against WordPress sites, and among the most damaging. A successful one compromises the database itself — exposing everything from customer records to credentials.

The defences are not complicated. Work through them in order and the attack surface shrinks reliably.

1. Use prepared statements

The single most effective measure, and the only genuinely structural one. Never interpolate user input into a query string. If you are writing a plugin or theme, route everything through $wpdb->prepare().

// Dangerous — input goes straight into the query
$wpdb->get_results( "SELECT * FROM orders WHERE user_id = $user_id" );

// Safe — passed as a placeholder
$wpdb->get_results(
    $wpdb->prepare( "SELECT * FROM orders WHERE user_id = %d", $user_id )
);

The important discipline is not writing your own escaping. Use the API.

2. Keep WordPress and plugins updated

Most real compromises come not from novel vulnerabilities but from known ones left unpatched. Plugins in particular are a primary way in. Turn on automatic updates, and delete unused plugins and themes rather than deactivating them — deactivated code is still on disk.

3. Put a WAF in front

A web application firewall filters malicious requests before they reach the site. It is not a substitute for fixing the underlying issue, but it is a useful layer that buys you time between disclosure and patching.

4. Strengthen authentication

Complex, unique passwords and two-factor authentication. Brute-forcing the admin login is frequently the precursor to everything that follows — harden the entrance and much of the later attack chain never gets started.

5. Restrict database privileges

Do not give the WordPress database user more rights than it needs. Most sites never require DROP or GRANT. Scope the privileges down and an attacker who does get in finds far less they can do with it.

6. Audit and monitor

Regular security audits, and monitoring for unusual behaviour. Injection attempts leave traces in the logs even when they fail. Noticing early means fixing before exploitation.

7. Train users and administrators

Phishing and similar attacks target people, not software. Preventing that first foothold is what breaks the chain that leads to SQL injection and everything after it.

The point

Item 1 is the actual fix; 2 through 7 are the layers around it. Prepared statements everywhere, patches applied promptly, privileges scoped down — those three alone stop the large majority of real-world attempts.

Originally published on Medium · Medium

Ishan Nim
Head of Penetration Testing & Cybersecurity, Cyber Crew

Let’s connect — especially if you’re into red teaming, payload engineering, or the fascinating depths of the dark web.

[email protected]linkedin.com/in/ishannim