Fabriciojf.com Wiki
  • FAQ
    • APIs
      • FAQs Favoritos

        Qual é a sua dúvida?

        •  Página inicial do FAQ
        • Asterisk
        • Instalando Fail2ban

        Instalando Fail2ban

        Instalando o Fail2ban

        $ sudo apt install fail2ban

        Configurando o fail2ban

        Edite o arquivo /etc/fail2ban/jail.conf e insira o conteúdo abaixo:

        $ nano /etc/fail2ban/jail.conf
        #
        # WARNING: heavily refactored in 0.9.0 release. Please review and
        # customize settings for your setup.
        #
        # Changes: in most of the cases you should not modify this
        # file, but provide customizations in jail.local file,
        # or separate .conf files under jail.d/ directory, e.g.:
        #
        # HOW TO ACTIVATE JAILS:
        #
        # YOU SHOULD NOT MODIFY THIS FILE.
        #
        # It will probably be overwritten or improved in a distribution update.
        #
        # Provide customizations in a jail.local file or a jail.d/customisation.local.
        # For example to change the default bantime for all jails and to enable the
        # ssh-iptables jail the following (uncommented) would appear in the .local file.
        # See man 5 jail.conf for details.
        #
        # [DEFAULT]
        # bantime = 3600
        #
        # [sshd]
        # enabled = true
        #
        # See jail.conf(5) man page for more information



        # Comments: use '#' for comment lines and ';' (following a space) for inline comments


        [INCLUDES]

        #before = paths-distro.conf
        before = paths-debian.conf

        # The DEFAULT allows a global definition of the options. They can be overridden
        # 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 (and/or comma) separator.
        ignoreip = 127.0.0.1 191.252.60.252 191.252.220.11 200.131.56.209 191.252.219.215 189.124.21.2 200.201.212.100 189.124.21.2

        # "bantime" is the number of seconds that a host is banned.
        bantime = 86400

        # A host is banned if it has generated "maxretry" during the last "findtime"
        # seconds.
        findtime = 600

        # "maxretry" is the number of failures before a host get banned.
        maxretry = 3

        # "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
        #

        # Some options used for actions

        # Destination email address used solely for the interpolations in
        # jail.{conf,local,d/*} configuration files.
        destemail = douglas.rezende@engenharia.ufjf.br

        # Sender email address used solely for some actions
        sender = suporte1@bemmelhor.com.br

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

        # Default protocol
        protocol = tcp

        # Specify chain where jumps would need to be added in iptables-* actions
        chain = INPUT

        # Ports to be banned
        # Usually should be overridden in a particular jail
        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"]

        # 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

        [dropbear]

        enabled = false
        port = ssh
        filter = sshd
        logpath = /var/log/dropbear
        maxretry = 6

        # 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 = ufw-allports
        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/authfail.log
        maxretry = 5

        #
        # HTTP servers
        #

        [apache]

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

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

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

        [apache-noscript]

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

        [apache-overflows]

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

        #
        # 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


        [pure-ftpd]

        enabled = false
        port = ftp,ftp-data,ftps,ftps-data
        filter = pure-ftpd
        logpath = /var/log/auth.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/mail.warn instead if you are
        # running postfix since it would provide the same log lines at the
        # "warn" level but overall at the smaller filesize.
        logpath = /var/log/mail.log

        [dovecot]

        enabled = false
        port = smtp,ssmtp,imap2,imap3,imaps,pop3,pop3s
        filter = dovecot
        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 connection-less protocol, spoofing of IP and imitation
        # 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

        [asterisk]

        enabled = true
        filter = asterisk
        action = iptables-multiport[name=asterisk-tcp, port="5059,5060,5061,15060,15061", protocol=tcp]
        iptables-multiport[name=asterisk-udp, port="5059,5060,5061,15060,15061", protocol=udp]
        sendmail-whois[name=Asterisk, dest=maxcontatosserv@gmail.com, sender=fail2ban@localhost]
        logpath = /var/log/asterisk/messages
        maxretry = 4
        bantime = 600100

        [asterisk-iptables]
        enabled = true
        filter = asterisk
        action = iptables-allports[name=ASTERISK, protocol=all]
        sendmail-whois[name=ASTERISK, dest=root, sender=fabriciojf@gmail.com]
        logpath = /var/log/asterisk/security
        maxretry = 4
        bantime = 600100

        Libere o Security no Asterisk (Acima do asterisk 10)

        $ nano /etc/asterisk/logger.conf

        localize a linha security e descomente-a

        security => security

        ;; opcional, mantenha os logs do security no console para analise em tempo real

        full => debug,error,notice,verbose,warning,security
        console => debug,error,notice,verbose,warning,security

        Ajuste o parâmetro de data no asterisk logger

        Localize o parametro dateformat dentro do arquivo logger  descomente a linha dateformat=%F %T

        $ nano /etc/asterisk/logger.conf
        [general]
        ;
        ; Customize the display of debug message time stamps
        ; this example is the ISO 8601 date format (yyyy-mm-dd HH:MM:SS)
        ;
        ; see strftime(3) Linux manual for format specifiers. Note that there is also
        ; a fractional second parameter which may be used in this field. Use %1q
        ; for tenths, %2q for hundredths, etc.
        ;
        dateformat=%F %T

        Confirurando o Fail2ban com o Security do Asterisk

        Crie o arquivo /etc/fail2ban/filter.d/asterisk.conf

        $ touch /etc/fail2ban/filter.d/asterisk.conf

        Insira o conteúdo abaixo

        # Fail2Ban configuration file
        #
        #
        # $Revision: 250 $
        #

        [INCLUDES]

        # Read common prefixes. If any customizations available -- read them from
        # common.local
        #before = common.conf


        [Definition]

        #_daemon = asterisk

        # Option: failregex
        # Notes.: regex to match the password failures messages in the logfile. The
        # host must be matched by a group named "host". The tag "<HOST>" can
        # be used for standard IP/hostname matching and is only an alias for
        # (?:::f{4,6}:)?(?P<host>\S+)
        # Values: TEXT
        #

        failregex = SECURITY.* SecurityEvent="FailedACL".*RemoteAddress=".+?/.+?/<HOST>/.+?".*
        SECURITY.* SecurityEvent="InvalidAccountID".*RemoteAddress=".+?/.+?/<HOST>/.+?".*
        SECURITY.* SecurityEvent="ChallengeResponseFailed".*RemoteAddress=".+?/.+?/<HOST>/.+?".*
        SECURITY.* SecurityEvent="InvalidPassword".*RemoteAddress=".+?/.+?/<HOST>/.+?".*

        # Option: ignoreregex
        # Notes.: regex to ignore. If this regex matches, the line is ignored.
        # Values: TEXT
        #
        ignoreregex =

        Reinicie o Fail2ban e o logger do asterisk

        $ asterisk -rx reload
        $ service fail2ban restart
         Página inicial do FAQ
        • FAQ
          • APIs
            • FAQs Favoritos

              © Copyright 2026 Fabriciojf.com  Criado por FábioCostaMKT   Pastes   Administração