# 21 - Pentesting FTP

<details>

<summary><strong>从零开始学习 AWS 黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS 红队专家）</strong></a><strong>！</strong></summary>

* 您在**网络安全公司**工作吗？ 想要看到您的**公司在 HackTricks 中做广告**吗？ 或者想要访问**PEASS 的最新版本或下载 HackTricks 的 PDF**吗？ 请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 发现我们的独家[NFTs](https://opensea.io/collection/the-peass-family)收藏品[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
* 获取[**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
* **加入** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord 群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或在 **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)\*\* 上关注我\*\*。
* **通过向** [**hacktricks 仓库**](https://github.com/carlospolop/hacktricks) **和** [**hacktricks-cloud 仓库**](https://github.com/carlospolop/hacktricks-cloud) **提交 PR 来分享您的黑客技巧**。

</details>

**Try Hard Security Group**

<figure><img src="/files/Kw46fTSYxPIOMyKySfL5" alt=""><figcaption></figcaption></figure>

{% embed url="<https://discord.gg/tryhardsecurity>" %}

***

## 基本信息

**文件传输协议（FTP）** 用作服务器和客户端之间在计算机网络上进行文件传输的标准协议。\
它是一种**明文**协议，使用**换行字符 `0x0d 0x0a`**，因此有时您需要使用 `telnet` 或 `nc -C` 进行连接。

**默认端口：** 21

```
PORT   STATE SERVICE
21/tcp open  ftp
```

### 连接活动和被动

在**主动FTP**中，FTP **客户端**首先从其端口N向FTP服务器的命令端口 - 端口21发起控制**连接**。然后，**客户端**监听端口**N+1**并将端口N+1发送给FTP服务器。FTP **服务器**然后从**其端口M向FTP客户端的端口N+1**发起数据**连接**。

但是，如果FTP客户端设置了控制外部数据连接的防火墙，则主动FTP可能会有问题。而对此的一个可行解决方案是被动FTP。

在**被动FTP**中，客户端从其端口N向FTP服务器的端口21发起控制连接。之后，客户端发出一个**passv命令**。服务器然后向客户端发送其端口号M。然后**客户端**从**其端口P到FTP服务器的端口M**发起数据**连接**。

来源：<https://www.thesecuritybuddy.com/vulnerabilities/what-is-ftp-bounce-attack/>

### 连接调试

**FTP**命令\*\*`debug`**和**`trace`**可用于查看**通信的发生方式\*\*。

## 枚举

### 横幅抓取

```bash
nc -vn <IP> 21
openssl s_client -connect crossfit.htb:21 -starttls ftp #Get certificate if any
```

### 使用starttls连接到FTP

```
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
```

### 未授权枚举

使用 **nmap**

```bash
sudo nmap -sV -p21 -sC -A 10.10.10.10
```

你可以使用命令 `HELP` 和 `FEAT` 来获取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...)
```

### 匿名登录

*anonymous : anonymous*\
\&#xNAN;*anonymous :*\
\&#xNAN;*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
```

### [暴力破解](/generic-methodologies-and-resources/brute-force.md#ftp)

在这里，您可以找到一个包含默认ftp凭据的好列表：<https://github.com/danielmiessler/SecLists/blob/master/Passwords/Default-Credentials/ftp-betterdefaultpasslist.txt>

### 自动化

默认情况下，nmap使用\*\*-sC\*\*选项执行匿名登录和反弹FTP检查：

```bash
nmap --script ftp-* -p 21 <ip>
```

## 浏览器连接

您可以使用浏览器（如Firefox）使用以下URL连接到FTP服务器：

```bash
ftp://anonymous:anonymous@10.10.10.98
```

注意，如果一个**web应用程序**直接将用户控制的数据发送到**FTP服务器**，你可以发送双重URL编码`%0d%0a`（在双重URL编码中为`%250d%250a`）字节，使**FTP服务器执行任意操作**。其中一种可能的任意操作是从用户控制的服务器下载内容，执行端口扫描或尝试与其他基于明文的服务（如http）通信。

## 从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
```

如果您的用户名/密码包含特殊字符，可以使用[以下命令](https://stackoverflow.com/a/113900/13647948)：

```bash
wget -r --user="USERNAME" --password="PASSWORD" ftp://server.com/
```

## 一些FTP命令

* **`USER username`**
* **`PASS password`**
* **`HELP`** 服务器指示支持哪些命令
* **`PORT 127,0,0,1,0,80`** 这将指示FTP服务器与IP 127.0.0.1的80端口建立连接（需要将第5个字符设为"0"，第6个字符设为十进制端口，或使用第5和第6个字符表示十六进制端口）。
* **`EPRT |2|127.0.0.1|80|`** 这将指示FTP服务器与IP 127.0.0.1的80端口建立TCP连接（由"2"指示）。此命令**支持IPv6**。
* **`LIST`** 这将发送当前文件夹中的文件列表
* **`LIST -R`** 递归列出（如果服务器允许）
* **`APPE /path/something.txt`** 这将指示FTP将从**被动**连接或**PORT/EPRT**连接接收的数据存储到文件中。如果文件名存在，将追加数据。
* **`STOR /path/something.txt`** 类似于`APPE`，但会覆盖文件
* **`STOU /path/something.txt`** 类似于`APPE`，但如果存在则不执行任何操作
* **`RETR /path/to/file`** 必须建立一个被动或端口连接。然后，FTP服务器将通过该连接发送指定的文件
* **`REST 6`** 这将指示服务器在下次使用`RETR`发送内容时从第6个字节开始。
* **`TYPE i`** 设置传输为二进制
* **`PASV`** 这将打开一个被动连接，并指示用户可以连接到哪里
* **`PUT /tmp/file.txt`** 将指定文件上传到FTP

![](/files/1EUosJzw3CWT1x5Lupn9)

## FTPBounce攻击

一些FTP服务器允许使用命令PORT。此命令可用于指示服务器您希望连接到其他FTP服务器的某个端口。然后，您可以使用此方法通过FTP服务器扫描主机的哪些端口是开放的。

[**在这里了解如何滥用FTP服务器来扫描端口。**](/network-services-pentesting/pentesting-ftp/ftp-bounce-attack.md)

您还可以滥用此行为使FTP服务器与其他协议交互。您可以**上传包含HTTP请求的文件**，并使易受攻击的FTP服务器**将其发送到任意HTTP服务器**（*也许添加新的管理员用户？*），或者上传FTP请求并使易受攻击的FTP服务器下载不同FTP服务器的文件。\
理论很简单：

1. \*\*将请求（放在文本文件中）上传到易受攻击的服务器。\*\*请记住，如果要与另一个HTTP或FTP服务器通信，需要使用`0x0d 0x0a`更改行。
2. **使用`REST X`避免发送不想发送的字符**（也许为了将请求上传到文件中，您需要在开头放一些图像标头）
3. **使用`PORT`连接到任意服务器和服务**
4. **使用`RETR`将保存的请求发送到服务器。**

这很可能会引发类似\_**Socket not writable**\_的错误，因为连接不足以使用`RETR`发送数据。尝试避免这种情况的建议包括：

* 如果要发送HTTP请求，**重复相同请求**直到至少\*\*\~0.5MB\*\*。就像这样：

{% file src="/files/2Nv2EKcXNY3phrIbEScH" %}
posts.txt
{% endfile %}

* 尝试使用与协议相关的“垃圾”数据填充请求（与FTP通信可能只是垃圾命令或重复`RETR`指令以获取文件）
* 只是使用大量空字符或其他字符填充请求（分行或不分行）

无论如何，这里有一个[旧示例，说明如何滥用此功能使FTP服务器从不同FTP服务器下载文件。](/network-services-pentesting/pentesting-ftp/ftp-bounce-download-2oftp-file.md)

## Filezilla服务器漏洞

**FileZilla**通常将**FileZilla-Server**（端口14147）绑定到**本地**的**管理服务**。如果您可以从**您的计算机**创建一个**隧道**以访问此端口，您可以使用**空密码**连接到**它**，并为FTP服务**创建**一个**新用户**。

## 配置文件

```
ftpusers
ftp.conf
proftpd.conf
vsftpd.conf
```

### 后渗透

vsFTPd的默认配置可以在`/etc/vsftpd.conf`中找到。在这里，您可能会发现一些危险的设置：

* `anonymous_enable=YES`
* `anon_upload_enable=YES`
* `anon_mkdir_write_enable=YES`
* `anon_root=/home/username/ftp` - 匿名用户的目录。
* `chown_uploads=YES` - 更改匿名上传文件的所有权
* `chown_username=username` - 被授予匿名上传文件所有权的用户
* `local_enable=YES` - 允许本地用户登录
* `no_anon_password=YES` - 不要求匿名用户输入密码
* `write_enable=YES` - 允许命令：STOR、DELE、RNFR、RNTO、MKD、RMD、APPE和SITE

### Shodan

* `ftp`
* `port:21`

***

**Try Hard Security Group**

<figure><img src="/files/Kw46fTSYxPIOMyKySfL5" alt=""><figcaption></figcaption></figure>

{% embed url="<https://discord.gg/tryhardsecurity>" %}

***

## HackTricks自动命令

```
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.xyz/pentesting/pentesting-ftp

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'
```

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

* 您在**网络安全公司**工作吗？想要看到您的**公司在HackTricks中宣传**吗？或者想要获取**PEASS的最新版本或下载HackTricks的PDF**吗？请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 探索[**PEASS Family**](https://opensea.io/collection/the-peass-family)，我们独家的[NFT收藏品](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS & HackTricks周边**](https://peass.creator-spring.com)
* **加入** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter**上关注我 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* 通过向[**hacktricks仓库**](https://github.com/carlospolop/hacktricks) **和** [**hacktricks-cloud仓库**](https://github.com/carlospolop/hacktricks-cloud) 提交PR来分享您的黑客技巧。

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/network-services-pentesting/pentesting-ftp.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
