hacktricks/network-services-pentesting/pentesting-ftp/README.md

303 lines
14 KiB
Markdown
Raw Normal View History

# 21 - Pentesting FTP
2022-04-28 16:01:33 +00:00
{% hint style="success" %}
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
<details>
<summary>Support HackTricks</summary>
2022-04-28 16:01:33 +00:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}
2022-04-28 16:01:33 +00:00
**Try Hard Security Group**
<figure><img src="../../.gitbook/assets/telegram-cloud-document-1-5159108904864449420.jpg" alt=""><figcaption></figcaption></figure>
{% embed url="https://discord.gg/tryhardsecurity" %}
***
## 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 chiaro** che utilizza come **carattere di nuova linea `0x0d 0x0a`** quindi a volte è necessario **connettersi usando `telnet`** o **`nc -C`**.
2024-02-10 13:03:23 +00:00
**Porta predefinita:** 21
```
PORT STATE SERVICE
21/tcp open ftp
```
2024-02-10 13:03:23 +00:00
### Connessioni Attive e Passive
In **Active FTP**, il **client** FTP prima **inizia** la **connessione** di controllo dalla sua porta N alla porta di comando del server FTP porta 21. Il **client** poi **ascolta** sulla porta **N+1** e invia la porta N+1 al server FTP. Il **server** FTP poi **inizia** la **connessione** dati, dalla **sua porta M alla porta N+1** del client FTP.
2022-10-02 18:09:01 +00:00
Ma, se il client FTP ha un firewall configurato che controlla le connessioni dati in entrata dall'esterno, allora l'Active FTP potrebbe essere un problema. E, una soluzione praticabile per questo è il Passive FTP.
2022-10-02 18:09:01 +00:00
Nel **Passive FTP**, il client inizia la connessione di controllo dalla sua porta N alla porta 21 del server FTP. Dopo questo, il client emette un **comando passv**. Il server poi invia al client uno dei suoi numeri di porta M. E il **client** **inizia** la **connessione** dati dalla **sua porta P alla porta M** del server FTP.
2022-10-02 18:09:01 +00:00
Source: [https://www.thesecuritybuddy.com/vulnerabilities/what-is-ftp-bounce-attack/](https://www.thesecuritybuddy.com/vulnerabilities/what-is-ftp-bounce-attack/)
2022-10-02 18:09:01 +00:00
### Debugging della connessione
2022-10-02 18:09:01 +00:00
I comandi **FTP** **`debug`** e **`trace`** possono essere utilizzati per vedere **come sta avvenendo la comunicazione**.
2022-10-02 18:09:01 +00:00
2024-02-10 13:03:23 +00:00
## Enumerazione
2022-05-01 13:25:53 +00:00
### Banner Grabbing
```bash
2021-03-28 12:47:37 -04:00
nc -vn <IP> 21
2020-09-19 19:49:07 +00:00
openssl s_client -connect crossfit.htb:21 -starttls ftp #Get certificate if any
```
### Connettersi a FTP utilizzando starttls
```
2021-03-25 11:29:43 +00:00
lftp
lftp :~> set ftp:ssl-force true
lftp :~> set ssl:verify-certificate no
lftp :~> connect 10.10.10.208
2024-02-10 13:03:23 +00:00
lftp 10.10.10.208:~> login
2021-03-25 11:29:43 +00:00
Usage: login <user|URL> [<pass>]
lftp 10.10.10.208:~> login username Password
```
### Unauth enum
2021-03-25 11:29:43 +00:00
2024-02-10 13:03:23 +00:00
Con **nmap**
2022-10-02 18:09:01 +00:00
```bash
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):
2024-02-10 13:03:23 +00:00
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
2022-10-02 18:09:01 +00:00
FEAT
211-Features:
2024-02-10 13:03:23 +00:00
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
2022-10-02 18:09:01 +00:00
STAT
#Info about the FTP server (version, configs, status...)
```
2024-02-10 13:03:23 +00:00
### Accesso anonimo
_anonymous : anonymous_\
_anonymous :_\
_ftp : ftp_
```bash
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](../../generic-methodologies-and-resources/brute-force.md#ftp)
Qui puoi trovare un'ottima lista con le credenziali ftp predefinite: [https://github.com/danielmiessler/SecLists/blob/master/Passwords/Default-Credentials/ftp-betterdefaultpasslist.txt](https://github.com/danielmiessler/SecLists/blob/master/Passwords/Default-Credentials/ftp-betterdefaultpasslist.txt)
### Automated
2021-07-06 18:13:26 +00:00
Il login anonimo e i controlli FTP di bounce vengono eseguiti per impostazione predefinita da nmap con l'opzione **-sC** o:
2021-07-06 18:13:26 +00:00
```bash
nmap --script ftp-* -p 21 <ip>
```
## Browser connection
Puoi connetterti a un server FTP utilizzando un browser (come Firefox) usando un URL come:
```bash
ftp://anonymous:anonymous@10.10.10.98
```
Nota che se un **web application** sta inviando dati controllati da un utente **direttamente a un server FTP** puoi inviare una doppia codifica URL `%0d%0a` (in doppia codifica URL questo è `%250d%250a`) byte e far sì che il **server FTP esegua azioni arbitrarie**. Una di queste possibili azioni arbitrarie è scaricare contenuti da un server controllato dall'utente, eseguire la scansione delle porte o cercare di comunicare con altri servizi basati su testo semplice (come http).
## Scarica tutti i file da FTP
```bash
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 nome utente/password contiene caratteri speciali, il [seguente comando](https://stackoverflow.com/a/113900/13647948) può essere utilizzato:
```bash
wget -r --user="USERNAME" --password="PASSWORD" ftp://server.com/
```
2024-02-10 13:03:23 +00:00
## Alcuni comandi FTP
2022-10-02 18:09:01 +00:00
* **`USER username`**
* **`PASS password`**
* **`HELP`** Il server indica quali comandi sono supportati
* \*\*`PORT 127,0,0,1,0,80`\*\*Questo indicherà al server FTP di stabilire una connessione con l'IP 127.0.0.1 sulla porta 80 (_devi mettere il 5° carattere come "0" e il 6° come la porta in decimale o 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**.
2024-02-10 13:03:23 +00:00
* **`LIST`** Questo invierà l'elenco dei file nella cartella corrente
* **`LIST -R`** Elenca ricorsivamente (se consentito dal server)
* **`APPE /path/something.txt`** Questo indicherà al FTP di memorizzare i dati ricevuti da una **connessione passiva** o da una connessione **PORT/EPRT** in un file. Se il nome del file esiste, aggiungerà i dati.
* **`STOR /path/something.txt`** Come `APPE` ma sovrascriverà i file
* **`STOU /path/something.txt`** Come `APPE`, ma se esiste non farà nulla.
* **`RETR /path/to/file`** Deve essere stabilita una connessione passiva o una connessione port. Poi, il server FTP invierà il file indicato attraverso quella connessione
* **`REST 6`** Questo indicherà al server che la prossima volta che invia qualcosa usando `RETR` dovrebbe iniziare al 6° byte.
* **`TYPE i`** Imposta il trasferimento su binario
2024-02-10 13:03:23 +00:00
* **`PASV`** Questo aprirà una connessione passiva e indicherà all'utente dove può connettersi
* **`PUT /tmp/file.txt`** Carica il file indicato sul FTP
![](<../../.gitbook/assets/image (386).png>)
2024-02-10 13:03:23 +00:00
## Attacco FTPBounce
Alcuni server FTP consentono il comando PORT. Questo comando può essere utilizzato per indicare al server che si desidera connettersi a un altro server FTP su una certa porta. Poi, puoi usare questo per scansionare quali porte di un host sono aperte attraverso un server FTP.
2024-02-10 13:03:23 +00:00
[**Scopri qui come abusare di un server FTP per scansionare le porte.**](ftp-bounce-attack.md)
Potresti anche abusare di questo comportamento per far interagire un server FTP con altri protocolli. Potresti **caricare un file contenente una richiesta HTTP** e far sì che il server FTP vulnerabile **la invii a un server HTTP arbitrario** (_magari per aggiungere un nuovo utente admin?_) o persino caricare una richiesta FTP e far sì che il server FTP vulnerabile scarichi un file da un altro server FTP.\
2024-02-10 13:03:23 +00:00
La teoria è semplice:
1. **Carica la richiesta (all'interno di un file di testo) sul server vulnerabile.** Ricorda che se vuoi parlare con un altro server HTTP o FTP devi cambiare le righe con `0x0d 0x0a`
2. **Usa `REST X` per evitare di inviare i caratteri che non vuoi inviare** (magari per caricare la richiesta all'interno del file dove dovevi mettere un'intestazione di immagine all'inizio)
3. **Usa `PORT` per connetterti al server e al servizio arbitrari**
2024-02-10 13:03:23 +00:00
4. **Usa `RETR` per inviare la richiesta salvata al server.**
È altamente probabile che questo **generi un errore come** _**Socket non scrivibile**_ **perché la connessione non dura abbastanza per inviare i dati con `RETR`**. Suggerimenti per cercare di evitare ciò sono:
* Se stai inviando una richiesta HTTP, **metti la stessa richiesta una dopo l'altra** fino a **\~0.5MB** almeno. Così:
{% file src="../../.gitbook/assets/posts.txt" %}
posts.txt
{% endfile %}
* Prova a **riempire la richiesta con dati "spazzatura" relativi al protocollo** (parlando con FTP magari solo comandi spazzatura o ripetendo l'istruzione `RETR` per ottenere il file)
* Basta **riempire la richiesta con molti caratteri nulli o altri** (divisi su righe o meno)
Comunque, qui hai un [vecchio esempio su come abusare di questo per far scaricare un file da un server FTP diverso.](ftp-bounce-download-2oftp-file.md)
## Vulnerabilità del server Filezilla
**FileZilla** di solito **lega** a **locale** un **servizio amministrativo** per il **FileZilla-Server** (porta 14147). Se riesci a 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.
2024-02-10 13:03:23 +00:00
## File di configurazione
```
ftpusers
ftp.conf
proftpd.conf
2022-10-02 21:20:06 +00:00
vsftpd.conf
```
### Post-Exploitation
La configurazione predefinita di vsFTPd può essere trovata in `/etc/vsftpd.conf`. Qui, potresti trovare alcune impostazioni pericolose:
2022-10-02 21:20:06 +00:00
* `anonymous_enable=YES`
* `anon_upload_enable=YES`
* `anon_mkdir_write_enable=YES`
* `anon_root=/home/username/ftp` - Directory per anonimi.
* `chown_uploads=YES` - Cambia la proprietà dei file caricati in modo anonimo
* `chown_username=username` - Utente a cui viene data la proprietà dei file caricati in modo anonimo
* `local_enable=YES` - Abilita gli utenti locali a effettuare il login
* `no_anon_password=YES` - Non chiedere la password agli anonimi
* `write_enable=YES` - Consenti comandi: STOR, DELE, RNFR, RNTO, MKD, RMD, APPE e SITE
2022-10-02 21:20:06 +00:00
### Shodan
* `ftp`
* `port:21`
***
**Try Hard Security Group**
<figure><img src="../../.gitbook/assets/telegram-cloud-document-1-5159108904864449420.jpg" alt=""><figcaption></figcaption></figure>
{% embed url="https://discord.gg/tryhardsecurity" %}
***
2023-09-03 01:48:41 +02:00
## HackTricks Automatic Commands
```
2021-08-12 09:00:24 -04:00
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
2021-08-15 13:23:50 -04:00
Entry_1:
2024-02-10 13:03:23 +00:00
Name: Notes
Description: Notes for FTP
Note: |
Anonymous Login
-bi <<< so that your put is done via binary
2021-08-15 13:23:50 -04:00
2024-02-10 13:03:23 +00:00
wget --mirror 'ftp://ftp_user:UTDRSCH53c"$6hys@10.10.10.59'
^^to download all dirs and files
2021-08-15 13:23:50 -04:00
2024-02-10 13:03:23 +00:00
wget --no-passive-ftp --mirror 'ftp://anonymous:anonymous@10.10.10.98'
if PASV transfer is disabled
2021-08-15 13:23:50 -04:00
2024-02-10 13:03:23 +00:00
https://book.hacktricks.xyz/pentesting/pentesting-ftp
2021-08-15 13:23:50 -04:00
Entry_2:
2024-02-10 13:03:23 +00:00
Name: Banner Grab
Description: Grab FTP Banner via telnet
Command: telnet -n {IP} 21
2021-08-15 13:23:50 -04:00
Entry_3:
2024-02-10 13:03:23 +00:00
Name: Cert Grab
Description: Grab FTP Certificate if existing
Command: openssl s_client -connect {IP}:21 -starttls ftp
2021-08-15 13:23:50 -04:00
Entry_4:
2024-02-10 13:03:23 +00:00
Name: nmap ftp
Description: Anon login and bounce FTP checks are performed
Command: nmap --script ftp-* -p 21 {IP}
2021-08-15 13:23:50 -04:00
Entry_5:
2024-02-10 13:03:23 +00:00
Name: Browser Connection
Description: Connect with Browser
Note: ftp://anonymous:anonymous@{IP}
2021-09-13 11:27:07 -04:00
Entry_6:
2024-02-10 13:03:23 +00:00
Name: Hydra Brute Force
Description: Need Username
Command: hydra -t 1 -l {Username} -P {Big_Passwordlist} -vV {IP} ftp
Entry_7:
2024-02-10 13:03:23 +00:00
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'
2021-08-12 09:00:24 -04:00
```
{% hint style="success" %}
Impara e pratica il hacking AWS:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Impara e pratica il hacking GCP: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
<details>
<summary>Supporta HackTricks</summary>
2022-04-28 16:01:33 +00:00
* Controlla i [**piani di abbonamento**](https://github.com/sponsors/carlospolop)!
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Condividi trucchi di hacking inviando PR ai** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repos su github.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}