Second-Order SQL Injection
SQLi where the payload is stored safely on one request and only executed later, when the application reads the stored value back into a query without re-sanitizing it.
How it works
The insertion path is often correctly parameterized, so the malicious value lands in the database verbatim and inert. The vulnerability fires on a later, different code path that pulls that stored value and concatenates it into a new query — a registration/profile field reused in an admin report, an imported record re-queried during processing, a migration script that interpolates stored identifiers (advanced techniques). Because the taint is laundered through storage, the source and sink are decoupled, so scanners and code review that follow a single request miss it — the classic reason second-order bugs survive. Detection requires testing stored inputs again after the secondary action that consumes them.
Techniques / payloads
- Store, then trigger. Plant a payload in a field whose downstream use is a SQL context, then perform the action that re-queries it (view profile, export, run report, run migration).
- Blind by nature. The response to the triggering action rarely reflects data, so extraction is usually boolean/time (Blind SQL Injection) or OOB.
- OOB egress for second-order. NetSPI’s case chains a stored payload in a date field into MSSQL
xp_dirtree, firing a DNS lookup (UNC-path injection) that carries schema/usernames out-of-band when an Excel report is later exported (stored procedures + DNS egress). - Migration/upgrade triggers. flowintel interpolates stored connector identifiers into SQL during an upgrade: an attacker with prior write access plants data that executes when the migration runs (flowintel migration SQLi).
Notable cases
- OpenEMR < 8.5.0 — second-order SQLi in patient demographics: user-supplied data stored then unsafely reused in a later query (advisory).
- All-in-One WP Migration CVE-2026-19949 (>3M sites) — second-order SQLi in the archive-restore path; attacker uses trackback functionality to extract a secret key, then imports a malicious
.wpressarchive, chaining to unauthenticated RCE (SecurityWeek). - flowintel < 4.0.0 — migration-script second-order SQLi triggered automatically during upgrade.
- Cross-system variant: data laundered through another system instead of your DB. Virtualizor CVE-2026-135174 trusted the email an OAuth provider returned and put it into SQL in
enduser/auth_callback.php; registering an IdP account with SQL in the email gave unauth admin login (advisory). Trilium CVE-2026-135686 interpolated entity names and column keys from a sync partner inupdateNormalEntity(), letting a malicious peer copy sessions andetapi_tokensinto synced rows (advisory). Same lesson: taint doesn’t clear because the value came from a “trusted” upstream. - CTF demonstrations — the retrieve-the-flag writeup shows the store/retrieve mechanics cleanly in a controlled lab (exploiting second-order SQLi).
Defenses
Parameterize at every sink, not just at the point of storage — the stored value is still untrusted when it is read back. Treat data flowing out of the database with the same suspicion as request input (taint does not clear on a round-trip through storage). Review migration and batch-processing code specifically, since those sinks run with elevated privilege and are easy to overlook. Allow-list stored identifiers reused as columns/tables.
Related
Sources
- Softaculous Virtualizor SQL injection (critical) · original
- Trilium SQL injection in updateNormalEntity() (critical) · original
- Second-Order SQLi with Stored Procedures and DNS-Based Egress · original
- OpenEMR 8.5.0 Second-Order SQL Injection · original
- flowintel migration script SQL injection · original
- All-in-One WP Migration second-order SQLi to RCE · original
- Exploiting Second-Order SQL Injection to Retrieve the Flag · original
- Advanced SQL Injection Techniques in Modern Web Apps · original