以 fail2ban 偵測 ftp 連線攻擊 – Pure-FTPd 篇

在 CentOS 6.x 下安裝
# yum install pure-ftpd –enablerepo=rpmforge

啟動 Pure-FTPd FTP Server
# /etc/init.d/pure-ftpd start
Starting pure-ftpd:                                        [  OK  ]

修改 /etc/rsyslog.conf 設定檔,讓 Pure-FTPd FTP Server 設定檔能獨立成一個檔案
# vim /etc/rsyslog.conf
ftp.*                                                /var/log/pureftpd.log

重新啟動 Syslog Server
# /etc/init.d/rsyslog restart

檢查 log 檔是否有產生
# ls -l /var/log/pureftpd.log
-rw——-. 1 root root 0 Jan  1 14:54 /var/log/pureftpd.log
[@more@]修改 fail2ban 設定檔
# vim /etc/fail2ban/jail.conf
加入下面的設定
[pure-ftpd]
enabled  = true
filter   = pure-ftpd
action   = iptables[name=pure-ftpd, port=ftp, protocol=tcp]
logpath  = /var/log/pureftpd.log
maxretry = 3
bantime  = 86400

重新啟動 fail2ban
# service fail2ban restart

# fail2ban-client status
Status
|- Number of jail:      2
`- Jail list:           pure-ftpd, ssh-iptables

嘗試錯誤連線幾次

# fail2ban-client status pure-ftpd
Status for the jail: pure-ftpd
|- filter
|  |- File list:        /var/log/pureftpd.log
|  |- Currently failed: 0
|  `- Total failed:     3
`- action
   |- Currently banned: 1
   |  `- IP list:       192.168.1.1
   `- Total banned:     1

# iptables -t filter -L fail2ban-pure-ftpd -n
Chain fail2ban-pure-ftpd (1 references)
target     prot opt source               destination
DROP       all  —  192.168.1.1          0.0.0.0/0
RETURN     all  —  0.0.0.0/0            0.0.0.0/0

以 fail2ban 偵測 ftp 連線攻擊 – ProFTPD 篇

修改 /etc/proftpd.conf 讓 ProFTPD FTP Server 產生單獨的 log 檔
# vim /etc/proftpd.conf
# 產生 log 檔
SystemLog                       /var/log/proftpd/proftpd.log
TransferLog                     /var/log/proftpd/xferlog

重新啟動 ProFTPD Server
# service proftpd restart
Shutting down proftpd:                                     [  OK  ]
Starting proftpd:                                          [  OK  ]

在 /var/log/proftpd 目錄之下可以看到 Log 檔
# ls -l /var/log/proftpd/proftpd.log
-rw-r—–. 1 root root 129 Jan  1 14:24 /var/log/proftpd/proftpd.log[@more@]
修改 /etc/fail2ban/jail.conf 設定檔
# vim /etc/fail2ban/jail.conf
[proftpd-iptables]

enabled  = true
filter   = proftpd
action   = iptables[name=ProFTPD, port=ftp, protocol=tcp]
#           sendmail-whois[name=ProFTPD, dest=you@example.com]
logpath  = /var/log/proftpd/proftpd.log
maxretry = 3
bantime  = 86400

重新啟動 fail2ban
# service fail2ban restart
Stopping fail2ban:                                         [  OK  ]
Starting fail2ban:                                         [  OK  ]

# fail2ban-client status
Status
|- Number of jail:      2
`- Jail list:           proftpd-iptables, ssh-iptables

嘗試連線錯誤幾次之後
# fail2ban-client status proftpd-iptables
Status for the jail: proftpd-iptables
|- filter
|  |- File list:        /var/log/proftpd/proftpd.log
|  |- Currently failed: 0
|  `- Total failed:     3
`- action
   |- Currently banned: 1
   |  `- IP list:       192.168.1.1
   `- Total banned:     1

在防火牆中的規則
# iptables -t filter -L fail2ban-ProFTPD -n
Chain fail2ban-ProFTPD (1 references)
target     prot opt source               destination
DROP       all  —  192.168.1.1          0.0.0.0/0
RETURN     all  —  0.0.0.0/0            0.0.0.0/0

在 CentOS 6.x 上安裝 ProFTPD FTP Server

安裝 ProFTPD FTP Server
# yum install proftpd –enablerepo=epel

啟動 ProFTPD FTP Server
# service proftpd start
Starting proftpd:                                          [FAILED]

檢查錯誤的地方
# proftpd –configtest
Checking syntax of configuration file
. proftpd[2211]: warning: unable to determine IP address of ‘.’
. proftpd[2211]: error: no valid servers configured
. proftpd[2211]: Fatal: error processing configuration file ‘/etc/proftpd.conf’

看起來似乎是 ProFTPD FTP Server 沒有 IP Address[@more@]解決方式:
加入紅色一行
# vim /etc/proftpd.conf
ServerName                      “ProFTPD server”
ServerIdent                     on “FTP Server ready.”
ServerAdmin                     root@localhost
ServerType                      standalone
#ServerType                     inetd
DefaultServer                   on
DefaultAddress                  192.168.1.9
AccessGrantMsg                  “User %u logged in.”
#DisplayConnect                 /etc/ftpissue
#DisplayLogin                   /etc/ftpmotd
#DisplayGoAway                  /etc/ftpgoaway
DeferWelcome                    off

重新啟動 ProFTPD FTP Server
# /etc/init.d/proftpd start
Starting proftpd:                                          [  OK  ]

以 fail2ban 偵測 ftp 連線攻擊 – vsFTPd 篇

修改 vsFTPd FTP Server 設定
# vim /etc/vsftpd/vsftpd.conf
# The target log file can be vsftpd_log_file or xferlog_file.
# This depends on setting xferlog_std_format parameter
xferlog_enable=YES
#
# The name of log file when xferlog_enable=YES and xferlog_std_format=YES
# WARNING – changing this filename affects /etc/logrotate.d/vsftpd.log
xferlog_file=/var/log/xferlog
#
# Switches between logging into vsftpd_log_file and xferlog_file files.
# NO writes to vsftpd_log_file, YES to xferlog_file
#xferlog_std_format=YES

重新啟動 vsFTPd FTP Server
# service vsftpd restart
[@more@]修改 fail2ban 設定
# vim /etc/fail2ban/jail.conf
[vsftpd-iptables]

enabled  = true
filter   = vsftpd
action   = iptables[name=VSFTPD, port=ftp, protocol=tcp]
#           sendmail-whois[name=VSFTPD, dest=you@example.com]
logpath  = /var/log/vsftpd.log
maxretry = 3
bantime  = 86400

重新啟動 fail2ban
# service fail2ban restart

目前已經有二個阻擋的規則 ssh-iptables, vsftpd-iptables
# fail2ban-client status
Status
|- Number of jail:      2
`- Jail list:           ssh-iptables, vsftpd-iptables

以 Client IP 192.168.1.1 連線 Server IP 192.168.1.9 做錯誤連線

可以在 vsftpd-iptables 列出有偵測到的連線攻擊
# fail2ban-client status vsftpd-iptables
Status for the jail: vsftpd-iptables
|- filter
|  |- File list:        /var/log/vsftpd.log
|  |- Currently failed: 0
|  `- Total failed:     5
`- action
   |- Currently banned: 1
   |  `- IP list:       192.168.1.1
   `- Total banned:     1

在防火牆規則上的規則
# iptables -t filter -L fail2ban-VSFTPD -n
Chain fail2ban-VSFTPD (1 references)
target     prot opt source               destination
DROP       all  —  192.168.1.1          0.0.0.0/0
RETURN     all  —  0.0.0.0/0            0.0.0.0/0

解決 CactiEZ 上的 FTP Server 無法連線登入的問題

因為 CactiEZ 為了客製化系統及縮小光碟的使用量,所以有些套件預設是不安裝的,所以安裝完成也沒有 FTP Server。
安裝 vsFTPD FTP Server
# yum install vsftpd

啟動 FTP Server
# service vsftpd start

進行連線,卻發現無法連線
[@more@]判別應該是防火牆擋掉了,所以先把防火牆停掉
# service iptables stop

重新連線,還是無法連線

本來以為是 SELinux 的問題,可是檢查之後,發現 SELinux 預設是關閉的
# grep ^SELINUX= /etc/selinux/config
SELINUX=disabled

後來在網路上找了一下,找到了這一篇,可能是問題的所在 vsftp的問題!!內詳
只要將匿名者可以登入的功能取消,就正常了!
# sed -i ‘s/anonymous_enable=YES/anonymous_enable=NO/’ /etc/vsftpd/vsftpd.conf

啟動 FTP Server
# service vsftpd start

讓 vsftpd FTP Server 同時支援 IPv4 及 IPv6

CentOS Linux 上安裝的 vsftpd FTP Server 預設只支援 IPv4,並不支援 IPv6,
# lftp 2001:288:a229:1::109
lftp 2001:288:a229:1::109:~> ls
‘ls’ 位置 0 [重新連接前延遲(秒): 51]

可以發現無法以 IPv6 的方式連線

啟用 IPv6 的方式
# vim /etc/vsftpd/vsftpd.conf

listen=YES
改成
#listen=YES


#listen_ipv6=YES
改成
listen_ipv6=YES

重新啟動 vsftpd FTP Server
# service vsftpd restart
正在關閉 vsftpd:                                          [  確定  ]
正在啟動 vsftpd 中的 vsftpd:                       [  確定  ][@more@]以 IPv6 的方式連線
# lftp 2001:288:a229:1::109
lftp 2001:288:a229:1::109:~> ls
drwxrwxr-x   37 504      504          4096 Dec 01 07:34 CentOS
drwxr-xr-x    3 0        0            4096 Oct 17 15:57 Linux
drwxr-xr-x    2 0        0            4096 Nov 23 03:00 ezgo
drwxr-xr-x    2 0        0            4096 Dec 05 12:19 ezgo_New
drwxr-xr-x    8 0        0            4096 Nov 25 09:34 raspbian
lftp 2001:288:a229:1::109:/>

但可以以 IPv6 連線,却會發現無法以 IPv4 的方式連線
# lftp 140.111.74.109
lftp 140.111.74.109:~> ls
‘ls’ 位置 0 [重新連接前延遲(秒): 28]

但是不能同時開啟 IPv4 和 IPv6,否則啟動時會出現錯誤訊息
# service vsftpd restart
正在關閉 vsftpd:                                          [  確定  ]
正在啟動 vsftpd 中的 vsftpd:500 OOPS: run two copies of vsftpd for IPv4 and IPv6
                                                           [失敗]

如果要同時能以 IPv4 和 IPv6 的方式來連線,則要在 /etc/vsftpd 目錄下增加 vsftpd_ipv6.conf 設定檔
# cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd_ipv6.conf

修改 /etc/vsftpd/vsftpd.conf
改成下面的設定
# vim /etc/vsftpd/vsftpd.conf
改成下面的設定
listen=YES
#listen_ipv6=YES

修改 /etc/vsftpd/vsftpd_ipv6.conf
# vim /etc/vsftpd/vsftpd_ipv6.conf
改成下面的設定
#listen=YES
listen_ipv6=YES

重新啟動 vsftpd FTP Server
# service vsftpd restart
正在關閉 vsftpd:                                          [  確定  ]
正在啟動 vsftpd 中的 vsftpd:                       [  確定  ]
正在啟動 vsftpd_ipv6 中的 vsftpd:               [  確定  ]

測試發現以 IPv4 和 IPv6 均可以正常連線
# lftp 140.111.74.109
lftp 140.111.74.109:~> ls
drwxrwxr-x   37 504      504          4096 Dec 01 07:34 CentOS
drwxr-xr-x    3 0        0            4096 Oct 17 15:57 Linux
drwxr-xr-x    2 0        0            4096 Nov 23 03:00 ezgo
drwxr-xr-x    2 0        0            4096 Dec 05 12:19 ezgo_New
drwxr-xr-x    8 0        0            4096 Nov 25 09:34 raspbian
lftp 140.111.74.109:/> bye

# lftp 2001:288:a229:1::109
lftp 2001:288:a229:1::109:~> ls
drwxrwxr-x   37 504      504          4096 Dec 01 07:34 CentOS
drwxr-xr-x    3 0        0            4096 Oct 17 15:57 Linux
drwxr-xr-x    2 0        0            4096 Nov 23 03:00 ezgo
drwxr-xr-x    2 0        0            4096 Dec 05 12:19 ezgo_New
drwxr-xr-x    8 0        0            4096 Nov 25 09:34 raspbian
lftp 2001:288:a229:1::109:/> bye

連線 FTP Server 出現 500 OOPS: cannot change directory:/root

新架設完成的 vsfTPd FTP Server 連線時,出現 500 OOPS: cannot change directory:/root 的錯誤訊息,看起來應該是 SELinux 的問題,解決的方式有二個。
一個是關閉 SELinux
# vim /etc/selinux/config

SELINUX=enforcing
改成
SELINUX=disabled

重新開機即可,不過並不建議這種方式。
[@more@]第二種方式,開啟 SELinux 的設定中有關於 ftp 的部分權限
# sestatus -b | grep ftp
allow_ftpd_anon_write                       off
allow_ftpd_full_access                      off
allow_ftpd_use_cifs                         off
allow_ftpd_use_nfs                          off
ftp_home_dir                                off
ftpd_connect_db                             off
ftpd_use_fusefs                             off
ftpd_use_passive_mode                       off
httpd_enable_ftp_server                     off
tftp_anon_write                             off
tftp_use_cifs                               off
tftp_use_nfs                                off

# setsebool -P ftp_home_dir  on

重新啟動 FTP Server 即可,如果還是不行,請再開啟下面的選項

# setsebool -P allow_ftpd_full_access  on

讓 FTP Server 的 log 檔每天輪替

因為 FTP Server 的 log 檔似乎不是每天都會做輪替
# ls -l /var/log/vsftpd.log*
-rw——-. 1 root root 760342 2013-10-21 12:24 /var/log/vsftpd.log
-rw——-. 1 root root 131065 2013-10-15 02:59 /var/log/vsftpd.log-20131015
-rw——-. 1 root root 890346 2013-10-20 03:24 /var/log/vsftpd.log-20131020

如果要做記錄檔的分析,會比較麻煩一些,不容易處理單一天數的記錄,所以想把 log 檔改成每天輪替。[@more@]修改 FTP Server log 檔輪替的設定檔
# vim /etc/logrotate.d/vsftpd
/var/log/vsftpd.log {
    # ftpd doesn’t handle SIGHUP properly
    daily
    rotate 2
    nocompress
    missingok
}

/var/log/xferlog {
    # ftpd doesn’t handle SIGHUP properly
    nocompress
    missingok
}

daily  是每天做輪替,rotate 2 是保留二個備份檔

重新啟動 log 服務
# service rsyslog restart
正在關閉系統記錄器:                                       [  確定  ]
正在啟動系統記錄器:                                       [  確定  ]