Second-Order Vulnerabilities
A second-order vulnerability happens when the malicious input does nothing at the moment you send it: it gets stored (in the database, a file, a log) and fires later, in another endpoint, role or process that reads that data and uses it without sanitising it. The invariant it breaks is “whatever is already in my database can be trusted”. The bug isn’t where the data comes in, but where it gets reused.
flowchart LR
A[Attacker] -->|"1. Sign-up: username = admin'-- "| E["Entry point (sanitised or parameterised)"]
E -->|"2. Stored as is"| DB[(Database / file / log)]
DB -->|"3. Another flow reads it"| U["Point of use (query, template, shell, export)"]
U -->|"4. Concatenated without sanitising"| X[Injection: SQLi, XSS, SSTI, RCE]
Threat model
Section titled “Threat model”The same as the equivalent first-order injection (reading or modifying data, session theft, RCE), with two aggravating factors: the trigger usually runs in more privileged contexts (the admin panel, background jobs with more permissions, the support team opening a ticket), and scanners almost never catch it, because the response to the injecting request looks normal.
Anatomy
Section titled “Anatomy”There are three pieces, and all three can live in different parts of the code:
- Entry point: the form or API that receives the data. It’s often well protected (the
INSERTis parameterised, length is validated), which is exactly why the data gets in. - Storage: the database, a file, a queue, a log. It keeps the data literally, quotes, braces and line breaks included.
- Point of use: another function that reads that data and puts it into a hand-built query, a template, an OS command or an export. This is where the bug is.
Developers parameterise where data comes “from the user”, but not where they use data that comes “from the database”, because they consider it internal. To the attacker, it’s the same data.
Variants
Section titled “Variants”| Variant | Where it’s stored | Where it fires | Result |
|---|---|---|---|
| Second-order SQLi | Username, profile | Password change, reports, internal searches | Read or modify other users’ data |
| Stored XSS / blind XSS | Comments, tickets, logged User-Agent | Admin or support panel | Session theft from a high-privilege role |
| Second-order SSTI | Name, signature, subject | Email or notification templates | RCE on the mail server or worker |
| Deferred command injection | Uploaded file name | Scheduled task or worker that processes it | RCE with the worker’s permissions |
| Log injection | Headers, username at login | Log viewer, SIEM, parser | Forged log entries, XSS in the viewer |
| CSV / formula injection | Any exportable field | Export opened in a spreadsheet | Formulas executed on the machine of whoever opens it |
| Deferred path traversal | File or resource name | Backup, compression, deletion | Read or delete files outside their path |
| Deferred deserialisation | Serialised object in DB or cache | Process that retrieves it | RCE |
Red Team
Section titled “Red Team”Discovery
Section titled “Discovery”- Map the life cycle of each piece of data, not just the endpoints. For every field the application stores, note where it shows up again: profile, listings, emails, PDFs, exports, admin panel, logs.
- Use unique markers (canaries): easy-to-search values that include the characters that
break each context, for example
zq7'"<x>{{7*7}}${7*7}. Then search for that marker across every response (in Burp: Search over the HTTP history). - OAST for triggers you can’t see: if the point of use is in a process you can’t access (a worker, an admin’s browser), use payloads that make a request to a server you control (Burp Collaborator or interactsh). An interaction hours later confirms the trigger.
- Walk the flows with another role if the scope gives you several accounts: what you store as a normal user may fire when an administrator views it.
Exploitation by type (lab)
Section titled “Exploitation by type (lab)”Second-order SQLi. The sign-up form parameterises the INSERT, but the password change
builds the query with the username read from the session:
# Vulnerable: the name comes from the DB, "so it's safe"cur.execute("UPDATE users SET password='%s' WHERE username='%s'" % (new_hash, session_user))You sign up as admin'-- -. The INSERT stores it literally. When you change your password,
the query becomes this and changes admin’s password:
UPDATE users SET password='<hash>' WHERE username='admin'-- -'sqlmap has two options for this case: --second-url points to the URL where the result of the
injection shows up, and --second-req loads from a file the full request that triggers it.
sqlmap -r signup.req -p username --second-req profile.req --batchHere profile.req is the page that shows the value or the request that triggers it. In
practice you have to handle the session of the freshly registered user (--eval or a
tamper) and avoid duplicate usernames between tests; an UPDATE with no visible output can
only be exploited by timing or errors. A custom script is often faster.
Stored XSS and blind XSS. A field you never see rendered (the reason in a ticket, your User-Agent stored in an access log) may be displayed in an internal panel. Use a payload that loads a script from your lab server so you know when and where it ran. Payload details in XSS.
Second-order SSTI. The profile name is inserted into the welcome or “forgot your password”
email template. Put {{7*7}} in the name and check the email: if 49 arrives, there’s
SSTI in the notification system, which usually runs as a separate service.
Deferred command injection. You upload report;sleep 10;.pdf and a nightly job builds
the command as a string and hands it to a shell (os.system("convert " + name + " out.png"),
sh -c "convert $name" or an eval). Note: an unquoted variable in bash (convert $name)
doesn’t re-parse the ;; it only gives argument injection (a name starting with -).
There’s no immediate response: the test is an OAST payload
(;nslookup marker.your-oast.lab;) and waiting. See command injection.
CSV / formula injection. A field starting with =, +, - or @ is interpreted as a
formula when the export is opened in a spreadsheet. In a lab, =1+1 is enough to demonstrate
it; the real impact depends on the spreadsheet application and its warnings.
Log injection. A line break in the login username (ana%0a2026-11-05 OK admin) creates
fake log entries if it’s stored without being neutralised. See CRLF.
Researcher workflow
Section titled “Researcher workflow”- List every field that gets stored and every place where it shows up again.
- Put a different canary in each field, so you know where each trigger comes from.
- Walk every flow that reads that data: profile, admin, emails, exports, PDFs.
- For invisible triggers, swap the canary for an OAST payload and let time pass.
- Once you confirm a point of use, tune the payload to its context (SQL, HTML, template, shell).
- Burp Suite: Repeater for the request pair (store and trigger), Search on the HTTP history for canaries, Collaborator for OAST (in Pro).
- interactsh: open alternative to Collaborator for receiving DNS/HTTP interactions.
- sqlmap with
--second-urlor--second-reqfor the SQL case.
Impact and chaining
Section titled “Impact and chaining”Second-order bugs often give horizontal or vertical privilege escalation (changing someone else’s password, stealing the admin’s session) and, when the point of use is a worker or a template system, RCE on an internal server that wasn’t exposed. From there you continue as in from web vulnerability to shell.
Blue Team
Section titled “Blue Team”Detection
Section titled “Detection”- Your own canaries in QA test data, and searching for SQL or template errors in processes that don’t receive direct input (reports, workers, email sending).
- SQL or template syntax errors in logs whose origin is an internal job: it almost always means a stored value has broken something.
- Unexpected outbound connections from workers or mail servers (a sign of OAST or of exploitation).
Telemetry and sources
Section titled “Telemetry and sources”Database logs (failed queries), scheduled task and queue logs, notification service logs, egress and DNS from internal servers.
Hardening
Section titled “Hardening”- Parameterised queries at every point of use, not only at the entry point. Data read from the database is treated the same as data coming from the user.
- Context-aware output encoding at display time (HTML, attribute, JS, URL), even if the data comes from your own database.
- Templates with data passed as variables, never concatenated into the template.
- Never pass stored data to a shell: use APIs that take arguments as a list, and server-generated file names.
- CSV exports: prefix fields that start with
=,+,-,@, tab (0x09) or carriage return (0x0D) with a single quote, wrap every cell in double quotes and double any embedded double quotes, as OWASP recommends (,and;separators inside a field would otherwise start a new cell). - Neutralise line breaks before writing to logs.
Response
Section titled “Response”Find every record that contains the payload (not just the one that fired), clean or rewrite that data, fix the point of use and review what the affected process did with its permissions while it was compromised.
CVEs and real cases
Section titled “CVEs and real cases”- Log4Shell (CVE-2021-44228, 2021): the
${jndi:...}payload was sent in headers or fields that were written to logs and evaluated when logged; sometimes in the same request, and sometimes on a different internal system that processed that data later (deferred trigger). - Blind XSS in support panels: a common finding in bug bounty programmes; the payload sent through a contact form runs days later in the support team’s internal tool.
- Second-order SQLi in password change or profile features: a recurring pattern in custom applications, and one of PortSwigger’s classic examples.
Product-specific CVEs on NVD (https://nvd.nist.gov/vuln/search) and GitHub Advisories (https://github.com/advisories).
Test checklist
Section titled “Test checklist”- Map of stored fields and every place where they show up again.
- A unique canary per field with SQL, HTML, template and shell characters.
- Admin flows, emails, PDFs, exports and visible logs reviewed.
- OAST payloads in fields whose use you can’t see, with waiting time.
- Trigger tested with another account or role, if the scope allows it.
- Impact demonstrated harmlessly and in scope.