~/service-ftp

FTP

CPTS Notes7 min read

TL;DR

FTP (File Transfer Protocol). Control channel on TCP 21, data channel on TCP 20. Cleartext by default. FTPS adds TLS. SFTP is a separate protocol (an SSH subsystem on port 22).

Two data modes decide which side opens the data channel:

  • Active: the server opens the data channel back to the client from port 20. A client-side firewall blocks it.
  • Passive: the server gives the client a port and the client opens the data channel. This works through a client firewall. Most clients default to passive.

First three checks: anonymous login, the software and version, and whether you can write.

Commands

Inside an ftp session:

Command Does
ls / dir List the current directory.
ls -R List the whole tree.
cd <dir> Change directory.
lcd <dir> Change the local directory.
get <file> Download a file.
mget * Download many files.
put <file> Upload a file.
mput <files> Upload many files.
binary Binary transfer mode. Use it for executables and archives.
passive Toggle passive mode.
prompt Toggle the per-file confirmation.
status Show the connection and transfer settings.
!<cmd> Run a local shell command.
bye Close the session.

Switch to binary before you download an executable, archive, or database:

ftp> binary # switch to binary mode
200 Switching to Binary mode.
ftp> get backup.zip # download the file intact

ASCII mode rewrites line endings and corrupts binary files. Most clients switch to binary on a Unix server. Windows (IIS) servers often start in ASCII. status shows the current type.

More session habits:

ftp> ls -la # list hidden files such as .ssh/ and .bash_history
ftp> get "Important Notes.txt" # quote filenames with spaces
ftp> passive # toggle passive mode when ls hangs after "Entering Passive Mode"

Some servers ignore -a. After toggling passive, run the listing again.

Enumeration

Service scan with the default FTP scripts ftp-anon and ftp-syst:

sudo nmap -p21 -sC -sV -oA scans/ftp $TARGET # version, anonymous login, and server status

ftp-anon tests anonymous login and prints the root listing. ftp-syst runs the STAT command, which returns the server version and config.

Grab the banner by hand:

nc -nv $TARGET 21 # read the banner

-n skips DNS resolution. -v prints the connection status.

Read the TLS certificate on an FTPS server:

openssl s_client -connect $TARGET:21 -starttls ftp # print the TLS certificate

-starttls ftp upgrades the plain connection before the handshake. The certificate often leaks the hostname and an admin email.

Log in anonymously (user anonymous or ftp, blank password or any email):

ftp $TARGET # default port 21
ftp $TARGET 2121 # non-standard port

Labs often move FTP off port 21. Check the full port scan for it.

List before you download. On a large tree, a recursive listing shows everything without pulling a single file:

ftp> ls -R # list the whole tree
ftp> get <file> # download only what matters

For server detail, status, debug, and trace inside the client show the version, the transfer mode, and the raw commands.

Mirror the whole tree when it is small or you want it offline:

wget -m --no-passive ftp://anonymous:anonymous@$TARGET # download the whole tree

-m mirrors (recursive download that keeps timestamps and structure). --no-passive forces active mode when the server’s passive mode fails. wget writes everything under a folder named after the target IP. This is slow on big trees, so list first.

The FTP NSE scripts beyond the two defaults:

ls /usr/share/nmap/scripts | grep ftp # list the FTP NSE scripts

Useful ones: ftp-brute, ftp-bounce, ftp-vsftpd-backdoor, ftp-proftpd-backdoor, ftp-vuln-cve2010-4221.

After a foothold, read the vsftpd config on the host. /etc/vsftpd.conf shows the anonymous and write settings. /etc/ftpusers lists the accounts blocked from FTP.

Common misconfigurations

  • Allows anonymous read.
  • Allows anonymous write.
  • Weak or default credentials.
  • Credentials reused on other services.
  • Cleartext login on an untrusted network.

Version exploits

Match the exact version from -sV before trusting any of these.

  • vsftpd 2.3.4 backdoor: a username ending in :) opens a root shell on port 6200.
  • ProFTPd 1.3.5 mod_copy (CVE-2015-3306): unauthenticated SITE CPFR/CPTO copies any file. Write to a web root for RCE.
  • ProFTPd 1.3.3c backdoor: the release shipped with a command backdoor.
sudo nmap -p21 --script ftp-vsftpd-backdoor $TARGET # check for the vsftpd 2.3.4 backdoor
use exploit/unix/ftp/vsftpd_234_backdoor # Metasploit module for the vsftpd backdoor

Attack playbook

  1. Search the files. Anonymous or weak creds let you read. Pull configs, source, and notes for credentials and host detail. The usual FTP win. Listing and download are in Enumeration.
  2. Reuse credentials. Creds found over FTP, and the FTP login itself, often work on SSH, SMB, or the web app. Try them on every service.
  3. Write for code execution. If you can upload and the FTP root serves another process, drop a payload: a webshell in a web root, a file an LFI includes, or a scheduled task. Test first: upload a file, then open http://$TARGET/test.txt. If it loads, the FTP root is the web root.
    ftp> put test.txt # upload a test file
  4. Version exploit. Only a few daemons are vulnerable. Match the -sV version against the Version exploits above.
  5. Brute or spray. Last resort. Slow, noisy, and fail2ban blocks it. Spray one password across a user list instead of hammering one account. See Spraying and Defaults.

Upload a payload to a writable root that serves the web directory:

ftp $TARGET # log in
ftp> put shell.aspx # upload the webshell

Brute-force credentials:

hydra -L users.txt -P passwords.txt ftp://$TARGET # brute-force logins from user and password lists

-L reads a username list. -P a password list. Lowercase -l/-p take a single value.

Scan a third host through the FTP server (FTP bounce):

sudo nmap -Pn -b anonymous:anonymous@$TARGET 127.0.0.1 # scan the server's localhost through FTP bounce

-b user:pass@server relays the scan through the FTP server, so Nmap scans 127.0.0.1 from the server’s view. This reaches ports a firewall blocks from your host.

TFTP

Trivial FTP. UDP 69, no authentication, no directory listing. You must know the filename. Found on internal networks and embedded devices.

sudo nmap -sU -p69 --script tftp-enum $TARGET # guess common TFTP filenames
tftp $TARGET # connect
tftp> get <known-file> # download a file by name
tftp> put <file> # upload a file

Practice boxes

Box Difficulty FTP’s role
Fawn Starting Point Anonymous login, read the flag. Pure FTP intro.
Access Easy Anonymous read leaks files and credentials.
Netmon Easy Anonymous read of a PRTG config file leaks credentials.
Devel Easy Anonymous write mapped to the IIS web root. Upload a webshell for RCE.
ServMon Easy Anonymous read leaks notes and a password file for a web-app chain.
Reel Hard Anonymous documents feed a CVE-2017-0199 RTF phishing attack, with SMTP user enum.

On easy boxes FTP is often the whole foothold. On harder ones it is a stepping stone: a file leak or an upload that enables the real attack elsewhere.