To those who pine for the days of velcro shoes...
If you want to preserve the spindown option for the HDD you will need to send the vsftpd log file to a location that IS using cramfs (ram disk). I chose the directory /var/log and named the file vsftpd.log
In order to get vsftpd to spit out the log file so that fail2ban can read it, vsftpd.conf (I recommend putting it in /opt/etc for simplicity) needs to be edited to contain these lines:
xferlog_enable=YES
vsftpd_log_file=/var/log/vsftpd.log
log_ftp_protocol=YES
xferlog_std_format=NO
syslog_enable=NO
dual_log_enable=NO
In fail2ban edit the jail.conf file to point to /tmp/var/log/vsftpd.log as the log file to parse in the vsftpd section (yes, I know the "tmp" is not in the vsftpd.conf section... it works as written so deal with it).
To prevent the logfile from getting too large you can either purge the file by hand (boring) or use a script to do it for you (sexy). I called mine logcheck (placed it in the /opt/sbin directory and made it executable using the "chmod 755 logcheck" command).
Here's the code
Code:
#!/bin/sh
filesize=`wc -c < /tmp/var/log/vsftpd.log`
# echo "vsftpd.log filesize is:" $filesize
if [ $filesize -gt 204800 ]
then
# echo "log file too big"
rm /tmp/var/log/vsftpd.log
touch /tmp/var/log/vsftpd.log
fail2ban-client reload
# else
# echo "log file size ok"
fi
I then added this script to run periodically using CRON. Yes, you'll need to install CRON just like any other ipkg (you know... type "ipkg install cron"). CRON is heavily documented on the ol' interweb and will not be discussed here!
Here's what I added to my crontab. It tells CRON to run logcheck every hour.
Code:
SHELL=/bin/sh
PATH=/opt/sbin:/opt/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
HOME=/
# ---------- ---------- Default is Empty ---------- ---------- #
0 0-23/1 * * * logcheck
You will need to edit your rc.local to launch the items (in order) once testing is successful (for reboots and power outages etc so you won't have to do this by hand every time).
Here's how I do it (these are the last three calls before the ending "fi" in rc.local):
Code:
# Start fail2ban for FTP DDOS thwarting
touch /tmp/var/log/vsftpd.log
fail2ban-client start
sleep 5
# start cron
cron
# Restart the ftpserver
killall vsftpd
vsftpd /opt/etc/vsftpd.conf
There are other parts of fail2ban to configure in the jail.conf but they are well documented on their home site and are personal to your own topography (same goes for some items in vsftpd.conf); but, if you must see mine...
vsftpd.conf
Code:
# Example config file /etc/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
nopriv_user=root
#
# Uncomment this to allow local users to log in.
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
anon_world_readable_only=NO
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/vsftpd.log
vsftpd_log_file=/var/log/vsftpd.log
log_ftp_protocol=YES
#
# If you want, you can have your log file in standard ftpd xferlog format
#xferlog_std_format=YES
xferlog_std_format=NO
#
# You may change the default value for timing out an idle session.
idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that turning on ascii_download_enable enables malicious remote parties
# to consume your I/O resources, by issuing the command "SIZE /big/file" in
# ASCII mode.
# These ASCII options are split into upload and download because you may wish
# to enable ASCII uploads (to prevent uploaded scripts etc. from breaking),
# without the DoS risk of SIZE and ASCII downloads. ASCII mangling should be
# on the client anyway..
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
# ftpd_banner=Welcome to the Repository Digital Archive FTP service.
ftpd_banner=Welcome
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd.banned_emails
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd.chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#dual_log_enable=YES
#ftp_username=guest
syslog_enable=NO
listen=YES
#tunable_dirlist_enable=YES
secure_chroot_dir=/
chroot_local_user=YES
#session_support=YES
# xxx
enable_iconv=YES
local_charset=UTF-8
remote_charset=CP950
# xxx
# guest_enable=YES
# guest_username=guest
max_clients=5
#local_max_rate=48000
force_dot_files=NO
pasv_enable=NO
# xxx
jail.conf (the default and vsftpd sections only as I have the others turned off)
Code:
# Fail2Ban configuration file
#
# Author: Cyril Jaquier
#
# $Revision: 552 $
#
# The DEFAULT allows a global definition of the options. They can be override
# in each jail afterwards.
[DEFAULT]
# "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not
# ban a host which matches an address in this list. Several addresses can be
# defined using space separator.
ignoreip = 127.0.0.1
# "bantime" is the number of seconds that a host is banned.
bantime = 900
# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime = 30
# "maxretry" is the number of failures before a host get banned.
maxretry = 10
# "backend" specifies the backend used to get files modification. Available
# options are "gamin", "polling" and "auto". This option can be overridden in
# each jail too (use "gamin" for a jail and "polling" for another).
#
# gamin: requires Gamin (a file alteration monitor) to be installed. If Gamin
# is not installed, Fail2ban will use polling.
# polling: uses a polling algorithm which does not require external libraries.
# auto: will choose Gamin if available and polling otherwise.
backend = auto
# This jail corresponds to the standard configuration in Fail2ban 0.6.
[vsftpd-iptables]
enabled = true
filter = vsftpd
action = iptables[name=VSFTPD, port=ftp, protocol=tcp]
# mail-whois[name=VSFTPD, dest=yourmail@mail.com]
logpath = /tmp/var/log/vsftpd.log
maxretry = 10
bantime = 900
...and now my fingers are bleeding!