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


OS? CentOS / 우분투 / ...? /etc/init.d/iptables status
— Bart De Vos의

@BartDeVos OS Ubuntu 11.04 ... "/etc/init.d/iptables status"= bash : /etc/init.d/iptables : 해당 파일 또는 디렉토리가 없습니다 ... 참고 : iptables를 관리하기 위해 APF를 사용합니다!
— THpubs

글쎄, 아마도 당신의 문제가있을 것입니다. apf를 비활성화하고 iptables로 기본 방화벽을 설정하고 문제가 지속되는지 확인하십시오.
— Bart De Vos

@BartDeVos 그게 문제 야 ... VPS 시스템에서 나는 ... openvz라고 생각한다 ... 그래서, UFW와 같은 일부 방화벽은 여기서 작동하지 않을 것이다!
— THpubs

당신은 실행할 수 있습니까 ufw enable?
— Bart De Vos

답변:


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

1
로그에 내 IP가 차단 된 것으로 표시되지만 여전히 로그인 할 수 있습니다!
— THpubs

의 출력 무엇입니까 iptables -L -v -n및 / 또는service iptables status
— 바트 드 보스

질문에 출력을 추가했습니다 ... 확인하십시오 ...
— THpubs

8
이 답변은 잘못된 것입니다. fail2ban은 감지를 위해 ssh 포트를 신경 쓰지 않지만 표준 (즉, 잘못된) 포트 (22) 만 차단하므로 악의적 인 사용자는 계속 비표준 포트에 연결할 수 있습니다. 이 답변 에는 차단 된 포트 변경에 대한 세부 정보가 있습니다.
— 강탈

35

fail2ban은 로그 내용으로 로그인 시도를 감지합니다. fail2ban은 탐지를 위해 포트를 사용하지 않고 차단하기 만합니다.
올바른 포트를 차단하려면 iptable을 올바르게 설정하기 위해 fail2ban에 어떤 포트를 지정해야합니다.
로 /etc/fail2ban/jail.local:

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

또 다른 방법은 문제가있는 호스트의 모든 것을 차단하는 것입니다. 따라서 iptable은 ssh뿐만 아니라 모든 paquets를 삭제합니다.
시작 부분 /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 : ERROR iptables -N fail2ban-ssh-ddos" 로그인에 실패하면 나를 차단하지 않습니다 ... 정말 빠른 경우에만 차단됩니다. SSH가 비밀번호가 틀렸다고 말할 때까지 기다리면 나를 차단하지 않습니다!
— THpubs

2
나는 당신이 그 문서를 읽어야 할 것을 두려워합니다. Fail2ban에는 차단되기 전에 시도 횟수를 조정하는 매개 변수가 있습니다.
— Gregory MOUSSAT

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

0

나는 이것이 엄격하게 질문에 대답하는 것이 아니라 어쨌든 ...

문제를 해결하는 또 다른 방법으로 서버 구성에서 표준 포트를 유지 한 다음 라우터에서 NAT를 수행하는 것을 고려할 수 있습니다.

예를 들어, 설정에서 외부의 ssh에 표준 포트를 사용하지 않지만 서버 구성은 ssh에 대한 표준입니다 (ftp, vpn 등) 표준은 라우터에서 비표준 포트를 열고 표준 포트로 전달하십시오.

이렇게하면 설정을 구성 할 때 많은 시간을 절약 할 수 있습니다.


0

나는 이것이 오래된 스레드라는 것을 알고 있지만 이것이이 주제에 대한 Google 검색에서 나타나는 것입니다. 나는 누군가가 가장 정확한 답변을주는 것을 보지 못했습니다.

리눅스로 명명 된 포트 정의를 전체적으로 변경하려면 /etc/services

ssh             22/tcp
ssh             22/udp

fail2ban 구성이나 Linux 명명 된 포트를 사용하는 다른 응용 프로그램에서는 아무것도 변경할 필요가 없습니다.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.