RSS

segunda-feira, 10 de junho de 2013

Bloqueando o UltraSurf - [iptables + fail2ban]

[IPTables]
No seu script de firewall adicione a seguinte linha.

iptables -A FORWARD -d 65.49.14.0/24 -j LOG --log-prefix "=UltraSurf= "

Coloque essa regra antes da regra com o modulo state (-m state –state ESTABLISHED, RELATED). Se colocar depois os pacotes das conexões já estabelecidas não irão para o LOG e pode gerar falhas no bloqueio. A rede 65.49.14.0/24 é a primeira a ser contactada pelo UltraSurf quando ele é aberto, a regra acima apenas gera um LOG, não existe bloqueio do acesso.

[Fail2ban]
Instalação Ubuntu: apt-get install fail2ban

Depois de instalado vamos criar um arquivo chamado jail.local
 vi /etc/fail2ban/jail.local

[ultrasurf] 
enabled = true
filter = ultrasurf
port = all
banaction = iptables-ultrasurf
logpath = /var/log/syslog
maxretry = 6 
#Tempo em segundos que o IP fica bloqueado - 15 minutos.
bantime = 900 

Vamos criar o arquivo com a expressão regular que irá filtar o log do iptables.
vi /etc/fail2ban/filter.d/ultrasurf.local

[Definition]
failregex = (.*)=UltraSurf=(.*) SRC= 
ignoreregex = 

Criar o arquivo que irá executar o iptables para o bloqueio e desbloqueio.
vi /etc/fail2ban/action.d/iptables-ultrasurf.local

[Definition]

actionstart =  iptables -N fail2ban- 
                         iptables -A fail2ban- -j RETURN
                         iptables -I INPUT -j fail2ban- 
                         iptables -I FORWARD -j fail2ban- 

actionstop =  iptables -D FORWARD -j fail2ban- 
                         iptables -D INPUT -j fail2ban- 
                         iptables -F fail2ban- 
                         iptables -X fail2ban- 

actioncheck = iptables -n -L FORWARD | grep -q fail2ban- 
                         iptables -n -L INPUT | grep -q fail2ban- 

actionban =     iptables -I fail2ban- 1 -s -j REJECT 
actionunban = iptables -D fail2ban- -s -j REJECT 

[Init]
name = ultrasurf


[Enviar e-mail do bloqueio/desbloqueio]
Instalação Ubuntu: apt-get install mailutils ssmtp

Configuração do sSMTP



$ sudo vi /etc/ssmptp/ssmtp.conf

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=user@gmail.com

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=user@gmail.com

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
UseSTARTTLS=YES
AuthUser=user
AuthPass=password


Teste de envio de e-mail:
$ sudo cat /etc/passwd | mailx -s "Senhas passwd" user@gmail