Bloqueio de repetidas tentativas de login ao seu Linux

Esse artigo mostra como implementar um sistema de segurança para bloqueio de ataques brute-force em seu servidor SSH. Ao mesmo tempo também mostra como criar um esquema para permitir acesso remoto (para manutenção) a partir de qualquer IP da internet.

[ Hits: 105.132 ]

Por: julio henrique maschio em 10/11/2005


Configurando o fail2ban



O arquivo de configuração do fail2ban até o presente momento é o /etc/fail2ban.conf.

Para um funcionamento básico, não é necessário editar o arquivo, ele já vem com tudo configurado e pronto para usar.

As funcionalidades que eu achei interessantes são:
  • Possibilidade de enviar um email para o root dizendo que um IP foi bloqueado;
  • Possibilidade de analisar também os logs do Apache atrás de falhas repetidas de autenticação;
  • Personalização de quantas falhas e de quanto tempo entre elas antes de se definir um bloqueio;
  • Faixas de IPs a serem ignoradas no monitoramento;
  • Expressões regex podem ser editadas de acordo com a necessidade.

Apenas a título de curiosidade, segue abaixo o meu /etc/fail2ban.conf com os comentários originais, para se ter uma noção do que ele consegue fazer:

# Fail2Ban configuration file
# $Revision: 1.8.2.13 $
# 2005.06.21  modified for readability  Iain Lea  iain@bricbrac.de
[DEFAULT]
# Option:  background
# Notes.:  start fail2ban as a daemon. Output is redirect to logfile.
# Values:  [true | false]  Default:  false
background = false
# Option:  debug
# Notes.:  enable debug mode. More verbose output and bypass root user test.
# Values:  [true | false]  Default:  false
debug = false
# Option:  logtargets
# Notes.:  log targets. Space separated list of logging targets.
# Values:  STDERR SYSLOG file  Default:  /var/log/fail2ban.log
logtargets = /var/log/fail2ban.log
# Option:  syslog-target
# Notes.:  where to find syslog facility if logtarget SYSLOG.
# Values:  SOCKET HOST HOST:PORT  Default: /dev/log
syslog-target = /dev/log
# Option:  syslog-facility
# Notes.:  which syslog facility to use if logtarget SYSLOG.
# Values:  NUM  Default: 1
syslog-facility = 1
# Option:  pidlock
# Notes.:  path of the PID lock file (must be able to write to file).
# Values:  FILE  Default:  /var/run/fail2ban.pid
pidlock = /var/run/fail2ban.pid
# Option:  maxfailures
# Notes.:  number of failures before IP gets banned.
# Values:  NUM  Default:  5
maxfailures = 5
# Option:  bantime
# Notes.:  number of seconds an IP will be banned.
# Values:  NUM  Default:  600
bantime = 600
# Option:  findtime
# Notes.:  lifetime in seconds of a "failed" log entry.
# Values:  NUM  Default:  600
findtime = 600
# Option:  ignoreip
# Notes.:  space separated list of IP's to be ignored by fail2ban.
#          You can use CIDR mask in order to specify a range.
#          Example:  ignoreip = 192.168.0.1/24 123.45.235.65
# Values:  IP  Default:  192.168.0.0/16
ignoreip = 192.168.0.0/16
# Option:  cmdstart
# Notes.:  command executed once at the start of Fail2Ban
# Values:  CMD  Default:
cmdstart =
# Option:  cmdend
# Notes.:  command executed once at the end of Fail2Ban.
# Values:  CMD  Default:
cmdend =
# Option:  polltime
# Notes.:  number of seconds fail2ban sleeps between iterations.
# Values:  NUM  Default:  1
polltime = 1
[MAIL]
# Option:  enabled
# Notes.:  enable mail notification when banning an IP address.
# Values:  [true | false]  Default:  false
enabled = false
# Option:  host
# Notes.:  host running the mail server.
# Values:  STR  Default:  localhost
host = localhost
# Option:  port
# Notes.:  port of the mail server.
# Values:  INT  Default:  25
port = 25
# Option:  from
# Notes.:  e-mail address of the sender.
# Values:  MAIL  Default:  fail2ban
from = fail2ban
# Option:  to
# Notes.:  e-mail addresses of the receiver. Addresses are space separated.
# Values:  MAIL  Default:  root
to = root
# Option:  subject
# Notes.:  subject of the e-mail.
# Tags:    <ip>  IP address
#          <failures>  number of failures
#          <failtime>  unix timestamp of the last failure
# Values:  TEXT  Default:  [Fail2Ban] Banned <ip>
subject = [Fail2Ban] Banned <ip>
# Option:  message
# Notes.:  message of the e-mail.
# Tags:    <ip>  IP address
#          <failures>  number of failures
#          <failtime>  unix timestamp of the last failure
#          <br>  new line
# Values:  TEXT  Default:
message = Hi,<br>
          The IP <ip> has just been banned by Fail2Ban after
          <failures> attempts.<br>
          Regards,<br>
          Fail2Ban
# You can define a new section for each log file to check for
# password failure. Each section has to define the following
# options: logfile, fwban, fwunban, timeregex, timepattern,
# failregex.
[Apache]
# Option:  enabled
# Notes.:  enable monitoring for this section.
# Values:  [true | false]  Default:  false
enabled = false
# Option:  logfile
# Notes.:  logfile to monitor.
# Values:  FILE  Default:  /var/log/httpd/access_log
logfile = /var/log/httpd/access_log
# Option:  fwstart
# Notes.:  command executed once at the start of Fail2Ban
# Values:  CMD  Default:
fwstart = iptables -N fail2ban-http
          iptables -I INPUT -p tcp --dport http -j fail2ban-http
          iptables -A fail2ban-http -j RETURN
# Option:  fwend
# Notes.:  command executed once at the end of Fail2Ban
# Values:  CMD  Default:
fwend = iptables -D INPUT -p tcp --dport http -j fail2ban-http
        iptables -D fail2ban-http -j RETURN
        iptables -X fail2ban-http
# Option:  fwban
# Notes.:  command executed when banning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    <ip>  IP address
#          <failures>  number of failures
#          <failtime>  unix timestamp of the last failure
#          <bantime>  unix timestamp of the ban time
# Values:  CMD
# Default: iptables -I INPUT 1 -s <ip> -j DROP
fwban = iptables -I fail2ban-http 1 -s <ip> -j DROP
# Option:  fwunban
# Notes.:  command executed when unbanning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    <ip>  IP address
#          <bantime>  unix timestamp of the ban time
#          <unbantime>  unix timestamp of the unban time
# Values:  CMD
# Default: iptables -D INPUT -s <ip> -j DROP
fwunban = iptables -D fail2ban-http -s <ip> -j DROP
# Option:  timeregex
# Notes.:  regex to match timestamp in Apache logfile.
# Values:  [Wed Jan 05 15:08:01 2005]
# Default: S{3} S{3} d{2} d{2}:d{2}:d{2} d{4}
timeregex = S{3} S{3} d{2} d{2}:d{2}:d{2} d{4}
# Option:  timepattern
# Notes.:  format used in "timeregex" fields definition. Note that '%' must be
#          escaped with '%' (see http://rgruet.free.fr/PQR2.3.html#timeModule)
# Values:  TEXT  Default:  %%a %%b %%d %%H:%%M:%%S %%Y
timepattern = %%a %%b %%d %%H:%%M:%%S %%Y
# Option:  failregex
# Notes.:  regex to match the password failure messages in the logfile.
# Values:  TEXT  Default:  authentication failure|user .* not found
failregex = authentication failure|user .* not found
[SSH]
# Option:  enabled
# Notes.:  enable monitoring for this section.
# Values:  [true | false]  Default:  true
enabled = true
# Option:  logfile
# Notes.:  logfile to monitor.
# Values:  FILE  Default:  /var/log/secure
logfile = /var/log/secure
# Option:  fwstart
# Notes.:  command executed once at the start of Fail2Ban
# Values:  CMD  Default:
fwstart = iptables -N fail2ban-ssh
          iptables -I INPUT -p tcp --dport ssh -j fail2ban-ssh
          iptables -A fail2ban-ssh -j RETURN
# Option:  fwend
# Notes.:  command executed once at the end of Fail2Ban
# Values:  CMD  Default:
fwend = iptables -D INPUT -p tcp --dport ssh -j fail2ban-ssh
        iptables -D fail2ban-ssh -j RETURN
        iptables -X fail2ban-ssh
# Option:  fwbanrule
# Notes.:  command executed when banning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    <ip>  IP address
#          <failures>  number of failures
#          <failtime>  unix timestamp of the last failure
#          <bantime>  unix timestamp of the ban time
# Values:  CMD
# Default: iptables -I INPUT 1 -s <ip> -j DROP
fwban = iptables -I fail2ban-ssh 1 -s <ip> -j DROP
# Option:  fwunbanrule
# Notes.:  command executed when unbanning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    <ip>  IP address
#          <bantime>  unix timestamp of the ban time
#          <unbantime>  unix timestamp of the unban time
# Values:  CMD
# Default: iptables -D INPUT -s <ip> -j DROP
fwunban = iptables -D fail2ban-ssh -s <ip> -j DROP
# Option:  timeregex
# Notes.:  regex to match timestamp in SSH logfile.
# Values:  [Mar  7 17:53:28]
# Default: S{3}s{1,2}d{1,2} d{2}:d{2}:d{2}
timeregex = S{3}s{1,2}d{1,2} d{2}:d{2}:d{2}
# Option:  timepattern
# Notes.:  format used in "timeregex" fields definition. Note that '%' must be
#          escaped with '%' (see http://rgruet.free.fr/PQR2.3.html#timeModule)
# Values:  TEXT  Default:  %%b %%d %%H:%%M:%%S
timepattern = %%b %%d %%H:%%M:%%S
# Option:  failregex
# Notes.:  regex to match the password failures messages in the logfile.
# Values:  TEXT  Default:  Authentication failure|Failed password|Invalid ser
failregex = Authentication failure|Failed password|Invalid user
Página anterior     Próxima página

Páginas do artigo
   1. O problema
   2. Bloqueio de repetidas tentativas por regras de iptables
   3. Considerações sobre fail2ban
   4. Instalação do fail2ban - Debian like
   5. Configurando o fail2ban
   6. fail2ban.conf - comentários
   7. Afinando o seu sistema....
   8. Considerações finais
Outros artigos deste autor

Acelere o seu Squid com squid-prefetch - baixe as páginas antes dos seus usuários!

Leitura recomendada

Quando próximo, finja estar longe; quando longe, finja estar próximo

Como assinar digitalmente um documento criado no Br/OpenOffice

From Deploy WAR (Tomcat) to Shell (FreeBSD)

VPN entre servidores CentOS 6 e Windows 7

Melhorando a segurança do seus servidores em FreeBSD

  
Comentários
[1] Comentário enviado por fernoliv em 10/11/2005 - 01:53h

Caro Julio, parabéns pelo artigo!

Eu gostaria de citar outros dois softwares que utilizo para bloquear IP´s de atacantes em meu servidor:

APF Firewall
http://www.rfxnetworks.com/apf.php
Descrição: Firewall baseado em IPTables de fácil configuração e muito eficiente. A configuração é feita através de um .conf simples onde são informadas as portas a serem liberadas/bloqueadas, além de outras funcionalidades tanto para portas tcp como udp, além de bloquear ataques via ICMP (Denial of Service por exemplo) pois possui embutido um "anti-DOS". Utiliza a lista negras de IP´s que é atualizada frequentemente através do site www.dshiel.org (Distributed Intrusion Detection System), onde constam os IP´s utilizados com maior frequência para fins ilícitos.
-------------------------------------------
BFD - Brute Force Detect
http://www.rfxnetworks.com/bfd.php
Descrição: Em conjunto com o APF Firewall, detecta tentativas de acesso não autorizada a serviços como SSH, Apache, EXIM, FTP, etc. Após 5 tentativas de acesso sem sucesso, o IP de origem é "dropado" no IpTables e o administrador do servidor pode ser avisado por e-mail.

Ambos são desenvolvidos pelo RFX Networks e são free e de uso irrestrito. Não é preciso manjar de IpTables, basta saber quais portais você quer liberar/fechar e eles fazem o trabalho.

É isso ai!

Abraço, Fernando.

[2] Comentário enviado por jmhenrique em 10/11/2005 - 08:37h

Apenas em tempo! desde quando escrevi o artigo, já sairam importantes modificações nas configurações com relação a failregex do ssh. No caso, a versão anterior apenas fazia um 'match case' simples, mas agora está muito mais sofisticada, e tende a ficar cada vez mais.
Caso você tenha seguido os passos e desabilitado a listagem do ssh na interface ipv6, você terá que editar também a failregex e retirar o bloco (?:::f{4,6}:)? dela, pois senão ele não irá casar a regra. Ficaria então algo como:

failregex = : (?:(?:Authentication failure|Failed [-/\w+]+) for(?: [iI](?:llegal|nvalid) user)?|[Ii (?:llegal|nvalid) user).* from (?P<host>\S*)

[3] Comentário enviado por technomancer em 10/11/2005 - 09:31h

Muito obrigado! Eu tenho um servidor aqui na empresa que disponibiliza acesso via ssh e estou tendo logs cheios também.

Dam kidos!

[4] Comentário enviado por lennon.jesus em 10/11/2005 - 09:34h

Grande dica! As configurações de segurança dessa ferramenta são fundamentais para qualquer Sistema que precise ser acessado remotamente.

Parabéns pelo artigo!
XD

[5] Comentário enviado por technomancer em 10/11/2005 - 10:02h

Voltando a comentar... hoje fui dar uma checada no log, e me deparei com o registro de um possivel ataque de dicionario via ssh.

Quero agradecer novamente. Mais um ponto contra os kidos!

[6] Comentário enviado por jmhenrique em 10/11/2005 - 10:52h

technomancer, se você quer dar uma aliviada no fail2ban fazendo ele bloquear somente as coisas NECESSÁRIAS, eu incrementei as regras do hosts.allow e do hosts.deny...
segue abaixo:
1 - Imagino que você só irá acessar da rede brasileira, fiz um script que até o presente momento funciona a contento. Ele analisa via whois o ip da pessoa que está tentando acessar, e dependendo da resposta, automaticamente atualiza o hosts.allow ;-)
2 - edite o arquivo /etc/hosts.allow e coloque somente o seguinte:
#exemplo da sua rede:
sshd: 172.22.34.
#linha de comando que irá enviar o email, rodar
#o script que verifica se o ip pertence ao
#Brasil e se for o caso, re-escrever o hosts.allow sozinho.

sshd: ALL : spawn (echo | /usr/bin/mail -s "[ SSH ] - %c - %u %h %s" root ; /usr/local/bin/verifica_ip %a )

salve e coloque este script em /usr/local/bin/

--corte-----
#!/bin/sh
#Script de Dominio Público criado por
#Julio Henrique Maschio -
#Dominio Publico - a única e verdadeira LIBERDADE
#verifica_ip - verifica se o ip pertence ao brasil

IP_EXTERNO=$1
if [ "$IP_EXTERNO" == "" ] ; then

echo Uso: $0 xxx.www.yyy.zzz
echo
exit 1
fi

#SE O IP PERTENCE AO BRASIL, $VERIFICA_BRASIL DEVERÁ SER
#UMA STRING VAZIA
VERIFICA=`whois $IP_EXTERNO | grep -i \\.br`
if [ $? != 0 ] ; then
#o ip nao é do brasil, pode ser bloqueado tranquilamente.
#iremos bloquear logo a rede inteira...
REDE=`echo $IP_EXTERNO | cut -d\. -f1,2,3`
REDE="$REDE."
echo "sshd: $REDE" : DENY >>/tmp/$$listagem$$
cat /etc/hosts.allow >>/tmp/$$listagem$$
cat /tmp/$$listagem$$ >/etc/hosts.allow
rm -f /tmp/$$listagem$$

else
echo "nada a fazer"
fi

---corta---

este script verifica se o ip é do Brasil, se não for, adiciona ele no hosts.allow com uma instrução de DENY.
Ou seja, voc vai ver ele somente uma vez nos logs...hehehe
(não se esqueça de dar um chmod 755 nele, né?)

pronto, mais uma camada de segurança ao seu linux. :-D

[7] Comentário enviado por Herr_Filip em 11/11/2005 - 08:29h

show o artigo!
parabens!!

[8] Comentário enviado por removido em 11/11/2005 - 20:39h

parabéns.. Muito bom o artigo
!!!


[9] Comentário enviado por Century_Child em 15/11/2005 - 19:52h

Interessante. Tem alguém que fica "poluindo" minha conexão (que já não é lá essas maravilhas) com tentativas de login via SSH.

[10] Comentário enviado por diogofsr em 10/06/2006 - 06:58h

Belíssimo artigo meu amigo.
Me ajudou horrores.
Valeu mesmo.

[11] Comentário enviado por cold_feelings em 14/09/2006 - 07:57h

Vlw ae pelo artigo, era disso q eu tava precisando .. :)

[12] Comentário enviado por pontejunior em 16/03/2009 - 20:05h

Gostei...Muito interessante...São poucos que merecem entrar nos favoritos e este é um deles.

[13] Comentário enviado por perucio em 12/09/2012 - 09:23h

no meu jail.conf não funcionou a função de e-mail, se mando e-mail via console ele vai normalmente, quando restarto o serviço ele da mensagem de erro!


[MAIL]
enabled = true
host = localhost
port = 25
from = root
to = MEU_EMAIL@EMPRESA.COM.BR
subject = [Fail2Ban] Bloqueio <ip>
message = Hi,<br>
The IP <ip> has just been banned by Fail2Ban after
<failures> attempts.<br>
Regards,<br>
Root


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts