Softpanorama
May the source be with you, but remember the KISS principle ;-)

Contents Bulletin Latest Last year Top visited Scriptorama

FTP server security

News

Recommended Links General Steps pure-ftpd Securing vsftpd Securing wu-ftpd Humor Etc

For Solaris

  1. WU-FTPD supplied with Solaris 9 and 10 is OK
  2. TCP wrappers are obligatory
  3. Separate disk partition for downloads is obligatory; should be mounted nosuid

General Steps

Securing vsftp

Red Hat Linux provides vsftpd FTP servers — a standalone, security oriented implementation of the FTP service.

securing Wu-Ftpd server

Old News

Securing FTP (1/2)

Jim has written a great article on how you can upgrade your FTP site's security through simple configuration changes.Securing FTP

This document contains information about securing the Internet File Transfer Protocol (FTP) server. While there are a variety of daemons around, most of the material covered here pertains only to wu-ftpd.

In the past there have been bugs found in older versions of various ftpd daemons. Crackers have exploited these bugs and will continue to do so. See http://www.netect.com/advisory_0209.html for the latest attack exploiting a problem in the realpath() function. So, the first order of business is to upgrade that server to the latest version! My favorite site for finding wu-ftpd is at ftp://ftp.vr.net/pub/wu-ftpd/wu-ftpd/, which also includes "vr" patches; they're in my mind akin to the Linux kernel's "ac" patches.

Now on to some really fun steps (as if compiling ftp isn't already fun):

  1. Check your /etc/ftpusers (a misnomer in my opinion).

    This file includes the login names of those users who are NOT allowed to login to your site. This file may look something like:

    ~[root(lindstrom)1]: cat /etc/ftpusers
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    news
    uucp
    operator
    games
    nobody

    At the very least, this file should contain root, system accounts, and nobody. Other possible users will include anonymous and ftp. Adding those accounts will effectively disable anonymous ftp. Removing the ftp user from /etc/passwd will also disable anonymous ftp.

Lets take a peek at /etc/ftpaccess as well

This file configures ftpd, but only if ftpd is called with -a. I recommend using this handy feature. Discussing all the possibilities of this file is out of the scope of this document. I urge you to man ftpaccess to get a better understanding of it.

Setting up the FTP area file structures

Finally we setup restrictions at the file level.

The root directory and every directory under it should not be owned by the same user/group of the ftp account. We certainly don't want anonymous users writing to just anywhere.

d--x--x--x 2 ftpadmin ftpadmin 1024 Feb 12 18:34 bin/
d--x--x--x 2 ftpadmin ftpadmin 1024 Jul 11 06:36 etc/
drwxr-xr-x 2 ftpadmin ftpadmin 1024 Feb 5 14:48 lost+found/
drwxr-xr-x 7 ftpadmin ftpadmin 1024 Mar 31 17:04 pub/
drwxrws-wt 2 ftpadmin ftpadmin 1024 Jul 18 01:00 incoming
or if you want to use the 'secret' directory approach for uploads: drwxr-x--x 3 ftpadmin ftpadmin 1024 Jan 2 1999 incoming/
drwxrws-wt 3 ftpadmin ftpadmin 1024 Jan 2 1999 incoming/secret_upload_dir

Other points of interest

For the paranoid, wu-ftpd has something just for you. Define paranoid in config.h and "questionable functions" will be disabled. Check out FIXES-2.4-HOBBIT from wu-ftpd source for more info on this.

Support for S/Key and OPIE are included in wu-ftpd, and I've seen patches for SSL and SRP floating around the internet. SSL for wu-ftpd is at ftp://ftp2.replay.com/pub/crypto/crypto/SSLapps/. However, it's a bit dated as far as keeping up with recent versions of wu-ftpd. There i s also edssl (a non SSL to SSL translator) in the /pub/crypto/crypto/SSLapps/SSLlynx/ directory on that site. SRP patches can be found at MrWizard's homepage along with some other nifty things.

Appendix F: Installing in a chroot Environment

This appendix provides instructions for installing and running Sun ONE Identity Server in a chroot environment. This appendix contains the following sections:

Chroot comes from the change root function in Unix®. When Identity Server is installed in a chroot environment, a named directory becomes the root directory. The new chroot directory then becomes the starting point in the path for all Identity Server searches. The chroot prevents malicious programs from accessing the real root file system, providing an added measure of security for the Web Server and Directory Server processes that power Identity Server.

Before beginning the chroot creation process, the following points should be noted:

Identity Server processes that serve requests cannot see or access files outside the chroot directory so the appropriate Identity Server files must be copied into the chroot directory structure. The first step is to create the user root environment for Identity Server. This new root must contain everything that Identity Server expects from the natural root (/).


Note

$CHROOT is the variable name for the chroot directory.


  1. Create the following system directories on the computer system that will host Identity Server.
  2. /dev

    /etc

    /sbin

    /usr

    /var

    /proc

    /opt

    /etc/lib

    /usr/platform

    /usr/bin

    /usr/sbin

    /usr/lib

    /usr/openwin/lib

    /var/opt

    /var/tmp

    /usr/share

    /usr/share/lib

  3. Create system soft links using the command ln -s.
  4. The following soft links should be created:

  5. Create devices under $CHROOT using the command mknod.

  6. Tip

    For example, to create the device /dev/tcp under the $CHROOT directory, enter:

    mknod dev/tcp c 11 42


    The following devices should be created under the $CHROOT directory:

  7. Copy files from directory /etc to corresponding locations under the $CHROOT directory. (For example, copy /etc/hosts to $CHROOT/etc/hosts.)
  8. The following files should be copied:

  9. Copy binaries to the corresponding locations under the $CHROOT directory. (For example, copy /usr/bin/sh to $CHROOT/usr/bin/sh.)
  10. The following files should be copied:

  11. Copy libraries to corresponding locations under the $CHROOT directory. (For example, copy /usr/lib/libc.so* to $CHROOT/usr/lib/.)

  12. Note

    If the computer system in which Identity Server is installed is a 64-bit machine, the libraries from /usr/lib/64 and /usr/lib/lwp/64 need to be copied to $CHROOT/usr/lib/64 and $CHROOT/usr/lib/lwp/64, respectively.


    The following files should be copied:

  13. Copy the zoneinfo files to corresponding locations under the $CHROOT directory. (For example, copy /usr/share/lib/zoneinfo/* to $CHROOT/usr/share/lib/.)
  14. Copy the locale files to corresponding locations under the $CHROOT directory. (For example, copy /usr/lib/locale/* to $CHROOT/usr/lib/locale.)

Installing Identity Server Under chroot

  1. Install Identity Server 6.1 in a directory other than $CHROOT.
  2. If an installation directory is not specified, Identity Server is installed, by default, in /opt/IdentityServer_base .

  3. Stop the instance of Sun ONE Web Server.
  4. ./WebServer_base/https-instanceName/https-stop

  5. Stop the instance of Sun ONE Directory Server.
  6. ./DirectoryServer_base/slapd-instanceName/stop-slapd

  7. Copy the entire Identity Server directly from IdentityServer_base to the $CHROOT directory using the same structure.
  8. Copy files from the following locations to the corresponding directories under $CHROOT:
  9. Create a loopback mount for Java.
  10. The following command makes Identity Server ready to run under chroot environment:

    mount -F lofs /usr/j2se $CHROOT/usr/j2se

  11. Create a loopback mount for /usr/share/lib and /usr/lib/mps.
  12. Examples:

    mount -F lofs /usr/share/lib $CHROOT/usr/share/lib

    mount -F lofs /usr/lib/mps $CHROOT/usr/lib/mps

Starting Identity Server In chroot

To start Identity Server in the chroot environment, both Directory Server and Web Server must be started.

  1. Start Directory Server using the chroot command:
  2. chroot $ROOT $IS_ROOT/DSServers/slapd-hostName/start-slapd

  3. Start Web Server using the chroot command:
  4. chroot $ROOT $IS_ROOT/SUNWam/servers/https-instanceName/https-start

Identity Server Log Files In chroot

Identity Server normally creates log files (Error Log, Debug, etc.) in the following directory:

/var/opt/SUNWam/debug and /var/opt/SUNWam/logs

Under chroot these log files will be created at the following locations:

$CHROOT/var/opt/SUNWam/log

$CHROOT/var/opt/SUNWam/debug

Setup an FTP user account minus shells

It's important to give to your strictly FTP users no real shell account on the Linux system. In this manner, if for any reasons someone could successfully get out of the FTP chrooted environment, it would not have the possibility of executing any user tasks since it doesn't have a bash shell. First, create new users for this purpose;

These users will be the users allowed to connect to your FTP server.

This has to be separate from a regular user account with unlimited access because of how the chroot environment works. Chroot makes it appear from the user's perspective as if the level of the file system you've placed them in is the top level of the file system.

Use the following command to create users in the /etc/passwd file. This step must be done for each additional new user you allow to access your FTP server.

[root@deep ] /# mkdir /home/ftp
[root@deep ] /# useradd -d /home/ftp/ftpadmin/ -s /dev/null ftpadmin > /dev/null 2>&1
[root@deep ] /# passwd ftpadmin      

        Changing password for user ftpadmin
        New UNIX password:
        Retype new UNIX password:
        passwd: all authentication tokens updated successfully
      

Once the home/ftp/ directory has been created you don't have to use this command again for additional FTP users.

  1. Edit the /etc/shells file, vi /etc/shells and add a non-existent shell name like null, for example. This fake shell will limit access on the system for FTP users.
    [root@deep ] /# vi /etc/shells
       
    /bin/bash
    /bin/sh
    /dev/null  
            

    /dev/null, This is our added no-existent shell. With Red Hat Linux, a special device name /dev/null exists for purposes such as these.

  2. Now, edit your /etc/passwd file and add manually the /./ line to divide the /home/ftp directory with the /ftpadmin directory where the user ftpadmin should be automatically chdir'd to. This step must be done for each FTP user you add to your passwd file.
    ftpadmin:x:502:502::/home/ftp/ftpadmin/:/dev/null       

    To read:

    ftpadmin:x:502:502::/home/ftp/./ftpadmin/:/dev/null
                                 ^^

    The account is ftpadmin, but you'll notice the path to the home directory is a bit odd. The first part /home/ftp/ indicates the filesystem that should be considered their new root directory. The dot . divides that from the directory they should be automatically chdir'd. change directory'd into, /ftpadmin/.

Once again, the /dev/null part disables their login as a regular user. With this modification, the user ftpadmin now has a fake shell instead of a real shell resulting in properly limited access on the system.

Setup a chroot user environment

What you're essentially doing is creating a skeleton root file system with enough components necessary, binaries, password files, etc. to allow Unix to do a chroot when the user logs in. Note that if you use the --enable-ls option during compilation as seen above, the /home/ftp/bin, and /home/ftp/lib directories are not required since this new option allows Wu-ftpd to use its own ls function. We still continue to demonstrate the old method for people that prefer to copy /bin/ls to the chroot'd FTP directory, /home/ftp/bin and create the appropriated library related to ls.

The following are the necessary steps to run Wu-ftpd software in a chroot jail:

First create all the necessary chrooted environment directories as shown below:

        [root@deep ] /# mkdir /home/ftp/dev
        [root@deep ] /# mkdir /home/ftp/etc
        [root@deep ] /# mkdir /home/ftp/bin    
        [root@deep ] /# mkdir /home/ftp/lib    
      
The last two are required only if you are not using the --enable-ls option.

Change the new directories permission to 0511 for security reasons: The chmod command will make our chrooted dev, etc, bin, and lib directories readable and executable by the super-user root and executable by the user-group and all users.

    [root@deep ] /# chmod 0511 /home/ftp/dev/
    [root@deep ] /# chmod 0511 /home/ftp/etc/
    [root@deep ] /# chmod 0511 /home/ftp/bin    
    [root@deep ] /# chmod 0511 /home/ftp/lib    
  1. Copy the /bin/ls binary to /home/ftp/bin directory and change the permission of the ls program to 0111. You don't want users to be able to modify the binaries:
        [root@deep ] /# cp /bin/ls /home/ftp/bin            
        [root@deep ] /# chmod 0111 /bin/ls /home/ftp/bin/ls 
    

    This step is necessary only if you're not using the --enable-ls option during the configure time of Wu-ftpd. See the Compile and Optimize section in this chapter for more information.

  2. Find the shared library dependencies of the ls Linux binary program:
    1.             [root@deep ] /# ldd /bin/ls          
      
                  libc.so.6 => /lib/libc.so.6 (0x00125000)
                  /lib/ld-linux.so.2 =7gt; /lib/ld-linux.so.2 (0x00110000)
                
    2. Copy the shared libraries identified above to your new lib directory under /home/ftp directory:
                      [root@deep ] /# cp /lib/libc.so.6 /home/ftp/lib/ 
                      [root@deep ] /# cp /lib/ld-linux.so.2 /home/ftp/lib/ 
                    
    3. These library are needed to make ls work. Also, steps 3 and 4 above are required only if you want to use the ls Linux binary program instead of the --enable-ls option that uses the new internal ls capability of Wu-ftpd.
  3. Create your /home/ftp/dev/null file:
                  [root@deep ] /# mknod /home/ftp/dev/null c 1 3
                  [root@deep ] /# chmod 666 /home/ftp/dev/null           
  4. Copy the group and passwd files in /home/ftp/etc directory. This should not be the same as your real ones. For this reason, we'll remove all non FTP users except for the super-user root in both of these files, passwd and group.
    1.                 [root@deep ] /# cp /etc/passwd /home/ftp/etc/
                      [root@deep ] /# cp /etc/group /home/ftp/etc/
                    
    2. Edit the passwd file, vi /home/ftp/etc/passwd and delete all entries except for the super-user root and your allowed FTP users. It is very important that the passwd file in the chroot environment has entries like:
                      root:x:0:0:root:/:/dev/null
                      ftpadmin:x:502:502::/ftpadmin/:/dev/null             
    3. : We can notice two things here: first, the home directory for all users inside this modified passwd file are now changed to reflect the new chrooted FTP directory i.e. /home/ftp/./ftpadmin/ begins /ftpadmin/, and also, the name of the user's login shell for the root account has been changed to /dev/null.

    4. Edit the group file, vi /home/ftp/etc/group and delete all entries except for the super-user root and all your allowed FTP users. The group file should correspond to your normal group file:
                      root:x:0:root
                      ftpadmin:x:502:             
  5. Now we must set passwd, and group files in the chroot jail directory immutable for better security.
    1.                 [root@deep ] /# cd /home/ftp/etc/
                      [root@deep ] /# chattr +i passwd             
    2. Set the immutable bit on group file:
                      [root@deep ] /# cd /home/ftp/etc/
                      [root@deep ] /# chattr +i group             

Configure the /etc/ftphosts file

The /etc/ftphosts file is used to define whether users are allowed to log in from certain hosts or whether there are denied access.

  1. Create the ftphosts file, touch /etc/ftphosts and add for example in this file the following lines:
              # Example host access file
              #
              # Everything after a '#' is treated as comment,
              # empty lines are ignored
              allow ftpadmin 208.164.186.1 208.164.186.2 208.164.186.4
              deny ftpadmin 208.164.186.5
            

    In the example below, we allow the user ftpadmin to connect via FTP from the explicitly listed addresses 208.164.186.1 208.164.186.2 208.164.186.4, and deny the specified ftpadmin user to connect from the site 208.164.186.5.

  2. Now, change its default permission to be 600:
              [root@deep ] /# chmod 600 /etc/ftphosts        
Configure the /etc/ftpusers file

The /etc/ftpusers/ file specifies those users that are NOT allowed to connect to your FTP server.

  1. Create the ftpusers file, touch /etc/ftpusers and add in this file the following users for security reasons:
                root
                bin
                daemon
                adm
                lp
                sync
                shutdown
                halt
                mail
                news
                uucp
                operator
                games
                nobody
              
  2. Now, change its default permission to be 600:
                [root@deep ] /# chmod 600 /etc/ftpusers          
Configure the /etc/pam.d/ftp file

Configure your /etc/pam.d/ftp file to use pam authentication by creating the /etc/pam.d/ftp file and add the following lines:

          #%PAM-1.0
          auth       required     /lib/security/pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed
          auth       required     /lib/security/pam_pwdb.so shadow nullok
          auth       required     /lib/security/pam_shells.so
          account    required     /lib/security/pam_pwdb.so
          session    required     /lib/security/pam_pwdb.so


Copyright © 1996-2012 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine. This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

Disclaimer:

Last modified: February 27, 2011