# 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 ([[raw/gauravsingh-cybersecurity.github.io/advanced-sql-injection-techniques-in-modern-web-apps-3348|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 ([[raw/netspi.com/second-order-sql-injection-with-stored-procedures-and-dns-based-egress-4213|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 ([[raw/github.com/flowintel-migration-script-sql-injection-13899|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 ([[raw/github.com/openemr-8-5-0-second-order-sql-injection-14659|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 `.wpress` archive, chaining to unauthenticated RCE ([[raw/securityweek.com/over-3-million-wordpress-sites-affected-by-migration-plugin-vulnerabil-11883|SecurityWeek]]).
- **flowintel < 4.0.0** — migration-script second-order SQLi triggered automatically during upgrade.
- **CTF demonstrations** — the retrieve-the-flag writeup shows the store/retrieve mechanics cleanly in a controlled lab ([[raw/medium.com/exploiting-second-order-sql-injection-to-retrieve-the-flag-4217|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
- [[SQL Injection]]
- [[Blind SQL Injection]]
- [[Out-of-Band Detection]]
- [[SQL Injection to RCE]]
## Sources
- [[raw/netspi.com/second-order-sql-injection-with-stored-procedures-and-dns-based-egress-4213|Second-Order SQLi with Stored Procedures and DNS-Based Egress]]
- [[raw/github.com/openemr-8-5-0-second-order-sql-injection-14659|OpenEMR 8.5.0 Second-Order SQL Injection]]
- [[raw/github.com/flowintel-migration-script-sql-injection-13899|flowintel migration script SQL injection]]
- [[raw/securityweek.com/over-3-million-wordpress-sites-affected-by-migration-plugin-vulnerabil-11883|All-in-One WP Migration second-order SQLi to RCE]]
- [[raw/medium.com/exploiting-second-order-sql-injection-to-retrieve-the-flag-4217|Exploiting Second-Order SQL Injection to Retrieve the Flag]]