반응형
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y fail2ban fail2ban-systemd
라이브러리 설치
chkconfig fail2ban on
fail2ban을 시작프로그램으로 등록
systemctl enable fail2ban
fail2ban을 자동실행 설정
systemctl start fail2ban
fail2ban 서비스 시작
fail2ban 설정
"/etc/fail2ban/jail.conf" 파일을 복사하여 "/etc/fail2ban/jail.local" 파일을 생성하여 설정을 적용해야 한다.
jail.conf 파일은 업데이트 등으로 인해 파일에 적용한 설정들이 초기화되거나 할 수 있으므로 jail.local 파일을 이용해 설정을 하라고 jail.conf 파일에 적혀있다.
기본 설정 [DEFAULT]
[DEFAULT]
# 차단하지 않을 IP
# 여러 IP를 적용하려면 ' ' 또는 ','를 구분자로 사용하여 적용할 수 있다.
ignoreip = 127.0.0.1/8 172.30.1.0/8
# External command that will take an tagged arguments to ignore, e.g. <ip>,
# and return true if the IP is to be ignored. False otherwise.
#
# ignorecommand = /path/to/command <ip>
ignorecommand =
# 해당 IP가 특정 횟수 이상 실패 시 몇 초 동안 차단할 것인지 (-1: 영구차단)
bantime = 3600
# findtime(초단위)내 maxretry 만큼 실패하면 차단
# ex) 600초 사이에 5번 실패하면 3600초 동안 차단한다.
findtime = 600
# 최대 재시도 가능 횟수
maxretry = 5
# "backend" specifies the backend used to get files modification.
# Available options are "pyinotify", "gamin", "polling", "systemd" and "auto".
# This option can be overridden in each jail as well.
#
# pyinotify: requires pyinotify (a file alteration monitor) to be installed.
# If pyinotify is not installed, Fail2ban will use auto.
# gamin: requires Gamin (a file alteration monitor) to be installed.
# If Gamin is not installed, Fail2ban will use auto.
# polling: uses a polling algorithm which does not require external libraries.
# systemd: uses systemd python library to access the systemd journal.
# Specifying "logpath" is not valid for this backend.
# See "journalmatch" in the jails associated filter config
# auto: will try to use the following backends, in order:
# pyinotify, gamin, polling.
#
# Note: if systemd backend is chosen as the default but you enable a jail
# for which logs are present only in its own log files, specify some other
# backend for that jail (e.g. polling) and provide empty value for
# journalmatch. See
https://github.com/fail2ban/fail2ban/issues/959#issuecomment-74901200
backend = auto
# "usedns" specifies if jails should trust hostnames in logs,
# warn when DNS lookups are performed, or ignore all hostnames in logs
#
# yes: if a hostname is encountered, a DNS lookup will be performed.
# warn: if a hostname is encountered, a DNS lookup will be performed,
# but it will be logged as a warning.
# no: if a hostname is encountered, will not be used for banning,
# but it will be logged as info.
# raw: use raw value (no hostname), allow use it for no-host filters/actions (example user)
usedns = warn
# "logencoding" specifies the encoding of the log files handled by the jail
# This is used to decode the lines from the log file.
# Typical examples: "ascii", "utf-8"
#
# auto: will use the system locale setting
logencoding = auto
# "enabled" enables the jails.
# By default all jails are disabled, and it should stay this way.
# Enable only relevant to your setup jails in your .local or jail.d/*.conf
#
# true: jail will be enabled and log files will get monitored for changes
# false: jail is not enabled
enabled = false
# "filter" defines the filter to use by the jail.
# By default jails have names matching their filter name
#
filter = %(__name__)s
#
# ACTIONS
#
# 메일 수신자와 발신자는 하나만 지정 가능
# 메일 수신자 주소
destemail = root@localhost
# 메일 발신자 주소
sender = root@localhost
# 메일 발송에 사용할 agent
# sendmail 이 없다면 따로 설치해야한다
mta = sendmail
# Default protocol
protocol = tcp
# Specify chain where jumps would need to be added in iptables-* actions
chain = INPUT
# 차단 대상 포트
port = 0:65535
# Format of user-agent
https://tools.ietf.org/html/rfc7231#section-5.5.3
fail2ban_agent = Fail2Ban/%(fail2ban_version)s
#
# Action shortcuts. To be used to define action parameter
# Default banning action (e.g. iptables, iptables-new,
# iptables-multiport, shorewall, etc) It is used to define
# action_* variables. Can be overridden globally or per
# section within jail.local file
banaction = iptables-multiport
banaction_allports = iptables-allports
# The simplest action to take: ban only
action_ = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
# ban & send an e-mail with whois report to the destemail.
action_mw = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
%(mta)s-whois[name=%(__name__)s, sender="%(sender)s", dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s"]
# ban & send an e-mail with whois report and relevant log lines
# to the destemail.
action_mwl = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
%(mta)s-whois-lines[name=%(__name__)s, sender="%(sender)s", dest="%(destemail)s", logpath=%(logpath)s, chain="%(chain)s"]
# See the IMPORTANT note in action.d/xarf-login-attack for when to use this action
#
# ban & send a xarf e-mail to abuse contact of IP address and include relevant log lines
# to the destemail.
action_xarf = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
xarf-login-attack[service=%(__name__)s, sender="%(sender)s", logpath=%(logpath)s, port="%(port)s"]
# ban IP on CloudFlare & send an e-mail with whois report and relevant log lines
# to the destemail.
action_cf_mwl = cloudflare[cfuser="%(cfemail)s", cftoken="%(cfapikey)s"]
%(mta)s-whois-lines[name=%(__name__)s, sender="%(sender)s", dest="%(destemail)s", logpath=%(logpath)s, chain="%(chain)s"]
# Report block via blocklist.de fail2ban reporting service API
#
# See the IMPORTANT note in action.d/blocklist_de.conf for when to
# use this action. Create a file jail.d/blocklist_de.local containing
# [Init]
# blocklist_de_apikey = {api key from registration]
#
action_blocklist_de = blocklist_de[email="%(sender)s", service=%(filter)s, apikey="%(blocklist_de_apikey)s", agent="%(fail2ban_agent)s"]
# Report ban via badips.com, and use as blacklist
#
# See BadIPsAction docstring in config/action.d/badips.py for
# documentation for this action.
#
# NOTE: This action relies on banaction being present on start and therefore
# should be last action defined for a jail.
#
action_badips = badips.py[category="%(__name__)s", banaction="%(banaction)s", agent="%(fail2ban_agent)s"]
#
# Report ban via badips.com (uses action.d/badips.conf for reporting only)
#
action_badips_report = badips[category="%(__name__)s", agent="%(fail2ban_agent)s"]
# 차단 했을때 실행할 액션. 기본 값인 "%(action_)s"는 아무 행동을 안하는 것이다.
# action_mw: whois로 IP 정보 조회 결과를 메일로 전송
# action_mwl: whois로 IP 정보 조회 결과 및 로그를 메일로 전송
action = %(action_)s
SSH 차단 설정 [sshd]
[sshd]
# fail2ban을 적용하려면 true로 설정
enabled = true
# To use more aggressive sshd filter (inclusive sshd-ddos failregex):
#filter = sshd-aggressive
# SSH 차단 적용 포트
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s
다른 포트(ex:test)를 차단하고 싶으면 [test] 입력 후 enabled=true, port=?? 등의 설정을 해주면 된다.
fail2ban 설정 적용
sudo systemctl restart fail2ban
서비스를 재시작 함으로써 설정을 적용할 수 있다.
fail2ban IP 차단/차단 해제
# IP 차단
fail2ban-client set sshd banip <IP>
# IP 차단 해제
fail2ban-client set sshd unbanip <IP>
반응형
'Linux' 카테고리의 다른 글
CentOS - sendmail 설정 (0) | 2022.04.28 |
---|---|
CentOS - whois 설정 (0) | 2022.04.27 |
npm 모듈 보안취약점 점검 (0) | 2022.04.19 |
CentOS 무선 랜 설정 (0) | 2022.04.17 |
외부 저장 장치에 CentOS 7 설치하기 (0) | 2022.04.10 |