在Fail2Ban中,如何更改SSH端口号?


24

在我的服务器中,ssh端口不是标准的22。我设置了另一个端口。如果我设置了fail2ban,它将能够检测到该端口吗?我如何告诉它检查该端口而不是端口22?

输出iptables -L -v -n

 Chain fail2ban-ssh (1 references)
 pkts bytes target     prot opt in     out     source               destination                                                                                         
    0     0 DROP       all  --  *      *       119.235.2.158        0.0.0.0/0                                                                                           
    0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0                                                                                           

 Chain fail2ban-ssh-ddos (0 references)
 pkts bytes target     prot opt in     out     source               destination

服务iptables状态的输出:

iptables: unrecognized service

总结fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf

Summary
=======

Addresses found:
[1]
[2]
[3]
    113.59.222.240 (Wed Mar 21 18:24:47 2012)
    113.59.222.240 (Wed Mar 21 18:24:52 2012)
    119.235.14.153 (Wed Mar 21 21:52:53 2012)
    113.59.222.21 (Thu Mar 22 07:50:44 2012)
    176.9.57.203 (Fri Mar 23 19:34:29 2012)
    176.9.57.203 (Fri Mar 23 19:34:42 2012)
    113.59.222.56 (Sat Mar 31 14:23:52 2012)
    113.59.222.56 (Sat Mar 31 14:24:05 2012)
    119.235.14.183 (Mon Apr 02 20:49:13 2012)
    119.235.14.168 (Sat Apr 21 09:58:56 2012)
    119.235.2.158 (Wed Apr 25 13:11:03 2012)
    119.235.2.158 (Wed Apr 25 13:11:40 2012)
    119.235.2.158 (Wed Apr 25 13:11:43 2012)
    119.235.2.158 (Wed Apr 25 13:11:47 2012)
    119.235.2.158 (Wed Apr 25 13:12:49 2012)
    119.235.2.158 (Wed Apr 25 13:12:52 2012)
    119.235.2.158 (Wed Apr 25 13:12:55 2012)
    119.235.2.158 (Wed Apr 25 13:12:58 2012)
    119.235.2.158 (Wed Apr 25 13:13:02 2012)
    119.235.2.158 (Wed Apr 25 13:13:04 2012)
    119.235.2.158 (Wed Apr 25 13:13:25 2012)
    119.235.2.158 (Wed Apr 25 13:19:18 2012)
    119.235.2.158 (Wed Apr 25 13:19:52 2012)
    119.235.2.158 (Wed Apr 25 13:19:55 2012)
    119.235.2.158 (Wed Apr 25 13:19:55 2012)
    119.235.2.158 (Wed Apr 25 13:19:58 2012)
    119.235.2.158 (Wed Apr 25 13:20:02 2012)
    119.235.2.158 (Wed Apr 25 13:20:05 2012)
    119.235.2.158 (Wed Apr 25 13:40:16 2012)
[4]
[5]
    119.235.2.158 (Wed Apr 25 13:11:38 2012)
    119.235.2.158 (Wed Apr 25 13:12:46 2012)
    119.235.2.158 (Wed Apr 25 13:19:49 2012)
[6]
    119.235.2.155 (Wed Mar 21 13:13:30 2012)
    113.59.222.240 (Wed Mar 21 18:24:43 2012)
    119.235.14.153 (Wed Mar 21 21:52:51 2012)
    176.9.57.203 (Fri Mar 23 19:34:26 2012)
    119.235.2.158 (Wed Apr 25 13:19:15 2012)
[7]
[8]
[9]
[10]

Date template hits:
1169837 hit(s): MONTH Day Hour:Minute:Second
0 hit(s): WEEKDAY MONTH Day Hour:Minute:Second Year
0 hit(s): WEEKDAY MONTH Day Hour:Minute:Second
0 hit(s): Year/Month/Day Hour:Minute:Second
0 hit(s): Day/Month/Year Hour:Minute:Second
0 hit(s): Day/Month/Year Hour:Minute:Second
0 hit(s): Day/MONTH/Year:Hour:Minute:Second
0 hit(s): Month/Day/Year:Hour:Minute:Second
0 hit(s): Year-Month-Day Hour:Minute:Second
0 hit(s): Day-MONTH-Year Hour:Minute:Second[.Millisecond]
0 hit(s): Day-Month-Year Hour:Minute:Second
0 hit(s): TAI64N
0 hit(s): Epoch
0 hit(s): ISO 8601
0 hit(s): Hour:Minute:Second
0 hit(s): <Month/Day/Year@Hour:Minute:Second>

Success, the total number of match is 37

However, look at the above section 'Running tests' which could contain important
information.

jail.conf

    # Fail2Ban configuration file.
#
# This file was composed for Debian systems from the original one
#  provided now under /usr/share/doc/fail2ban/examples/jail.conf
#  for additional examples.
#
# To avoid merges during upgrades DO NOT MODIFY THIS FILE
# and rather provide your changes in /etc/fail2ban/jail.local
#
# Author: Yaroslav O. Halchenko <debian@onerussian.com>
#
# $Revision: 281 $
#

# 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
ignoreip = 127.0.0.1
bantime  = 14400
maxretry = 3

# "backend" specifies the backend used to get files modification. Available
# options are "gamin", "polling" and "auto".
# yoh: For some reason Debian shipped python-gamin didn't work as expected
#      This issue left ToDo, so polling is default backend for now
backend = polling

#
# Destination email address used solely for the interpolations in
# jail.{conf,local} configuration files.
destemail = root@localhost

#
# ACTIONS
#

# Default banning action (e.g. iptables, iptables-new,
# iptables-multiport, shorewall, etc) It is used to define 
# action_* variables. Can be overriden globally or per 
# section within jail.local file
banaction = iptables-multiport

# email action. Since 0.8.1 upstream fail2ban uses sendmail
# MTA for the mailing. Change mta configuration parameter to mail
# if you want to revert to conventional 'mail'.
mta = sendmail

# Default protocol
protocol = tcp

#
# Action shortcuts. To be used to define action parameter

# The simplest action to take: ban only
action_ = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s]

# ban & send an e-mail with whois report to the destemail.
action_mw = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s]
              %(mta)s-whois[name=%(__name__)s, dest="%(destemail)s", protocol="%(protocol)s]

# ban & send an e-mail with whois report and relevant log lines
# to the destemail.
action_mwl = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s]
               %(mta)s-whois-lines[name=%(__name__)s, dest="%(destemail)s", logpath=%(logpath)s]

# Choose default action.  To change, just override value of 'action' with the
# interpolation to the chosen action shortcut (e.g.  action_mw, action_mwl, etc) in jail.local
# globally (section [DEFAULT]) or per specific section 
action = %(action_)s

#
# JAILS
#

# Next jails corresponds to the standard configuration in Fail2ban 0.6 which
# was shipped in Debian. Enable any defined here jail by including
#
# [SECTION_NAME] 
# enabled = true

#
# in /etc/fail2ban/jail.local.
#
# Optionally you may override any other parameter (e.g. banaction,
# action, port, logpath, etc) in that section within jail.local

[ssh]

enabled = true
port    = ssh
filter  = sshd
logpath  = /var/log/auth.log
maxretry = 4

# Generic filter for pam. Has to be used with action which bans all ports
# such as iptables-allports, shorewall
[pam-generic]

enabled = false
# pam-generic filter can be customized to monitor specific subset of 'tty's
filter  = pam-generic
# port actually must be irrelevant but lets leave it all for some possible uses
port = all
banaction = iptables-allports
port     = anyport
logpath  = /var/log/auth.log
maxretry = 6

[xinetd-fail]

enabled   = false
filter    = xinetd-fail
port      = all
banaction = iptables-multiport-log
logpath   = /var/log/daemon.log
maxretry  = 2


[ssh-ddos]

enabled = true
port    = ssh
filter  = sshd-ddos
logpath  = /var/log/auth.log
maxretry = 6

#
# HTTP servers
#

[apache]

enabled = false
port    = http,https
filter  = apache-auth
logpath = /var/log/apache*/*error.log
maxretry = 6

# default action is now multiport, so apache-multiport jail was left
# for compatibility with previous (<0.7.6-2) releases
[apache-multiport]

enabled   = false
port      = http,https
filter    = apache-auth
logpath   = /var/log/apache*/*error.log
maxretry  = 6

[apache-noscript]

enabled = false
port    = http,https
filter  = apache-noscript
logpath = /var/log/apache*/*error.log
maxretry = 6

[apache-overflows]

enabled = false
port    = http,https
filter  = apache-overflows
logpath = /var/log/apache*/*error.log
maxretry = 2

 [nginx-auth]
 enabled = true
 filter = nginx-auth
 action = iptables-multiport[name=NoAuthFailures, port="http,https"]
 logpath = /var/log/nginx*/*error*.log
 bantime = 600 # 10 minutes
 maxretry = 6

 [nginx-login]
 enabled = true
 filter = nginx-login
 action = iptables-multiport[name=NoLoginFailures, port="http,https"]
 logpath = /var/log/nginx*/*access*.log
 bantime = 600 # 10 minutes
 maxretry = 6

 [nginx-badbots]
 enabled  = true
 filter = apache-badbots
 action = iptables-multiport[name=BadBots, port="http,https"]
 logpath = /var/log/nginx*/*access*.log
 bantime = 86400 # 1 day
 maxretry = 1

 [nginx-noscript]
 enabled = true
 action = iptables-multiport[name=NoScript, port="http,https"]
 filter = nginx-noscript
 logpath = /var/log/nginx*/*access*.log
 maxretry = 6
 bantime  = 86400 # 1 day

 [nginx-proxy]
 enabled = true
 action = iptables-multiport[name=NoProxy, port="http,https"]
 filter = nginx-proxy
 logpath = /var/log/nginx*/*access*.log
 maxretry = 0
 bantime  = 86400 # 1 day


#
# FTP servers
#

[vsftpd]

enabled  = false
port     = ftp,ftp-data,ftps,ftps-data
filter   = vsftpd
logpath  = /var/log/vsftpd.log
# or overwrite it in jails.local to be
# logpath = /var/log/auth.log
# if you want to rely on PAM failed login attempts
# vsftpd's failregex should match both of those formats
maxretry = 6


[proftpd]

enabled  = false
port     = ftp,ftp-data,ftps,ftps-data
filter   = proftpd
logpath  = /var/log/proftpd/proftpd.log
maxretry = 6


[wuftpd]

enabled  = false
port     = ftp,ftp-data,ftps,ftps-data
filter   = wuftpd
logpath  = /var/log/auth.log
maxretry = 6


#
# Mail servers
#

[postfix]

enabled  = false
port     = smtp,ssmtp
filter   = postfix
logpath  = /var/log/mail.log


[couriersmtp]

enabled  = false
port     = smtp,ssmtp
filter   = couriersmtp
logpath  = /var/log/mail.log


#
# Mail servers authenticators: might be used for smtp,ftp,imap servers, so
# all relevant ports get banned
#

[courierauth]

enabled  = false
port     = smtp,ssmtp,imap2,imap3,imaps,pop3,pop3s
filter   = courierlogin
logpath  = /var/log/mail.log


[sasl]

enabled  = false
port     = smtp,ssmtp,imap2,imap3,imaps,pop3,pop3s
filter   = sasl
# You might consider monitoring /var/log/warn.log instead
# if you are running postfix. See http://bugs.debian.org/507990
logpath  = /var/log/mail.log


# DNS Servers


# These jails block attacks against named (bind9). By default, logging is off
# with bind9 installation. You will need something like this:
#
# logging {
#     channel security_file {
#         file "/var/log/named/security.log" versions 3 size 30m;
#         severity dynamic;
#         print-time yes;
#     };
#     category security {
#         security_file;
#     };
# };
#
# in your named.conf to provide proper logging

# !!! WARNING !!!
#   Since UDP is connectionless protocol, spoofing of IP and immitation
#   of illegal actions is way too simple.  Thus enabling of this filter
#   might provide an easy way for implementing a DoS against a chosen
#   victim. See
#    http://nion.modprobe.de/blog/archives/690-fail2ban-+-dns-fail.html
#   Please DO NOT USE this jail unless you know what you are doing.
#[named-refused-udp]
#
#enabled  = false
#port     = domain,953
#protocol = udp
#filter   = named-refused
#logpath  = /var/log/named/security.log

[named-refused-tcp]

enabled  = false
port     = domain,953
protocol = tcp
filter   = named-refused
logpath  = /var/log/named/security.log

我刚刚发现的错误fail2ban log

2012-04-25 14:57:29,359 fail2ban.actions.action:错误iptables -N fail2ban-ssh-ddos


操作系统?CentOS / Ubuntu / ...?/etc/init.d/iptables status
Bart De Vos的

@BartDeVos OS Ubuntu 11.04 ...“ /etc/init.d/iptables status” = bash:/etc/init.d/iptables:没有这样的文件或目录...注意:我使用APF来管理iptables!
THpubs 2012年

好吧,可能是您的问题。如何禁用apf,使用iptables设置基本防火墙并查看问题是否仍然存在?
巴特·德沃斯

@BartDeVos就是这个问题...我在VPS系统中...我认为它是openvz ...所以,像UFW之类的某些防火墙在这里不起作用!
THpubs 2012年

你会跑步ufw enable吗?
巴特·德沃斯

Answers:


22

Fail2Ban使用该文件/etc/fail2ban/jail.local并查找该[ssh]部分,您可以在此处更改端口。

[ssh]
enabled  = true
port     = ssh

您可以将port值更改为任何正整数。

如果它不起作用,并且您想进一步查看/etc/fail2ban/jail.conf,请看,应该有类似以下内容:

 logpath = /var/log/auth.log

这就是fail2ban用于检测错误登录的内容。

如果无法正常工作,则可以尝试一些方法来找出问题所在。首先检查是否已安装:

dpkg -l |grep fail   

检查服务是否正在运行:

/etc/init.d/fail2ban status 

检查您的SSH-jail是否已设置:

sudo fail2ban-client status  

检查日志文件:

fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf

检查您的日期/时间:

date && tail -2 /var/log/auth.log

(您应该首先获取日期,然后是中的最后几行auth.log。如果仍然无法查明错误,请将配置文件添加到帖子中。


它似乎不起作用...没有阻止我失败的登录!
THpubs 2012年

1
日志显示我的IP被阻止了...但是我仍然可以登录!
THpubs 2012年

iptables -L -v -n和/或service iptables status
Bart De Vos

我将输出添加到问题中...请检查...
THpubs 2012年

8
这个答案是误导的。尽管fail2ban并不关心要检测的ssh端口,但它只会阻止标准(即错误)端口(22),因此恶意用户仍可以继续连接到非标准端口。此答案包含有关更改阻止的端口的详细信息。
抢劫

35

fail2ban将检测带有日志内容的登录尝试次数。fail2ban不使用端口进行检测,仅用于阻止。
要阻止正确的端口,您必须告诉fail2ban哪个端口才能正确设置iptable。
进入/etc/fail2ban/jail.local

[ssh]
enabled  = true
port     = ssh   <-- just modify this with your port    port = 1234

另一种方法是阻止一切来自违规主机。因此,iptable将丢弃其中的每一个paquet,而不仅仅是ssh。
在开始时/etc/fail2ban/jail.local

banaction = iptables-multiport     <-- regular blocking (one or several ports)
banaction = iptables-allports      <-- block everything

有了iptables-allports您,您不必为端口而烦恼。只需保留默认值即可。


太棒了...它在某种程度上可以正常工作。但是我只是在fail2ban日志中注意到一个错误:“ 2012-04-25 14:57:29,359 fail2ban.actions.action:错误iptables -N fail2ban-ssh-ddos”如果我无法登录,它不会阻止我...只有真正快速地操作时,它才会阻止我。如果我等到SSH说密码错误,它将不会阻止我!
THpubs 2012年

2
恐怕您必须阅读该文档。Fail2ban具有用于在阻止之前调整尝试次数的参数。
Gregory MOUSSAT 2012年

7

简而言之:如果您更改了ssh端口号,则必须将其添加到 jail.local文件中!

例如:(我在端口1234上使用SSH,SFTP)

jail.local

[ssh]

enabled  = true

port     = ssh,sftp,1234

filter   = sshd

logpath  = /var/log/auth.log

maxretry = 6

谢谢,按预期工作,只需要将日志路径修改为/ var / log / secure
AMB 2015年

0

我知道这并不是严格回答问题,但无论如何...

解决问题的另一种方法是,可以考虑将标准端口保留在服务器配置中,然后在路由器中进行NAT。

例如,在我的设置中,我也没有从外部使用ssh的标准端口,但是我的服务器配置是ssh的标准(以及ftp,vpn等),我只是在路由器中打开非标准端口,将它们转发到标准端口。

这样的配置方式为我节省了很多时间。


0

我知道这是一个旧主题,但这是在Google搜索中弹出的主题。我没看到有人给出最正确的答案(imo),就在这里。

要全局更改Linux命名端口定义,请转到 /etc/services

ssh             22/tcp
ssh             22/udp

无需更改fail2ban配置或使用Linux命名端口的任何其他应用程序中的任何内容。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.