[root@guardiao ~]# hostname
guardiao.home
[root@guardiao ~]# ifconfig eth0 | grep "inet addr" inet addr:192.168.122.97 Bcast:192.168.122.255 Mask:255.255.255.0
[root@guardiao ~]#
- Objetivo: Permitir apenas 1 conexão via SSH no período de 2 minutos por origem
[root@guardiao ~]# iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
[root@guardiao ~]# iptables -N SSH
[root@guardiao ~]# iptables -A INPUT -p tcp –dport 22 -j SSH
[root@guardiao ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all – 0.0.0.0/0 0.0.0.0/0
ACCEPT all – 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
SSH tcp – 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT)
target prot opt source destination
target prot opt source destination
Chain SSH (1 references)
target prot opt source destination
[root@guardiao ~]#
Chain SSH (1 references)
target prot opt source destination
all – 0.0.0.0/0 0.0.0.0/0 recent: SET name: conexoes_ssh side: source
[root@guardiao ~]# iptables -A SSH -m recent –name conexoes_ssh –update –seconds 120 –hitcount 2 -j REJECT
[root@guardiao ~]# iptables -vnL SSH
Chain SSH (1 references)
pkts bytes target prot opt in out source destination
0 0 all – * &nbs
p; * 0.0.0.0/0 0.0.0.0/0 recent: SET name: conexoes_ssh side: source
0 0 REJECT all – * * 0.0.0.0/0 0.0.0.0/0 recent: UPDATE seconds: 120 hit_count: 2 name: conexoes_ssh side: source reject-with icmp-port-unreachable
Após a execução da regra, nosso firewall já esta monitorando tentativas de conexão via SSH e irá permitir a primeira tentativa de cada IP de origem. Vejamos:
Wed Dec 9 01:34:11 BRST 2009
root@guardiao’s password:
Last login: Wed Dec 9 01:30:24 2009 from 192.168.122.1
[root@guardiao ~]#
Chain SSH (1 references)
pkts bytes target prot opt in out source destination
1 60 all – * * 0.0.0.0/0 0.0.0.0/0 recent: SET name: conexoes_ssh side: source
0 0 REJECT all – * * 0.0.0.0/0 0.0.0.0/0 recent: UPDATE seconds: 120 hit_count: 2 name: conexoes_ssh side: source reject-with icmp-port-unreachable
[root@guardiao ~]# cat /proc/net/xt_recent/conexoes_ssh
src=192.168.122.1 ttl: 64 last_seen: 6084651 oldest_pkt: 1 6084651
[root@guardiao ~]#
Wed Dec 9 01:34:45 BRST 2009
ssh: connect to host guardiao port 22: Connection refused
[marcelo@mmello vms]$
[root@guardiao ~]# iptables -nvL SSH
Chain SSH (1 references)
pkts bytes target prot opt in out source destination
2 120 all – * * 0.0.0.0/0 0.0.0.0/0 recent: SET name: conexoes_ssh side: source
1 60 REJECT all – * * 0.0.0.0/0 0.0.0.0/0 recent: UPDATE seconds: 120 hit_count: 2 name: conexoes_ssh side: source reject-with icmp-port-unreachable
[root@guardiao ~]# cat /proc/net/xt_recent/conexoes_ssh
src=192.168.122.1 ttl: 64 last_seen: 6119177 oldest_pkt: 3 6084651, 6119177, 6119177
[root@guardiao ~]
Wed Dec 9 01:37:19 BRST 2009
root@guardiao’s password:
Last login: Wed Dec 9 01:34:11 2009 from 192.168.122.1
[root@guardiao ~]#
- echo +192.168.122.15 > /proc/net/xt_recent/conexoes_ssh para adicionarmos um endereço na tabela
- echo -192.168.122.1 > /proc/net/xt_recent/conexoes_ssh para removermos um endereço da tabela
- echo / > /proc/net/xt_recent/conexoes_ssh para limparmos a tabela
src=192.168.122.1 ttl: 64 last_seen: 6273353 oldest_pkt: 4 6084651, 6119177, 6119177, 6273353
[root@guardiao ~]# echo +192.168.122.15 > /proc/net/xt_recent/conexoes_ssh
[root@guardiao ~]# cat /proc/net/xt_recent/conexoes_ssh
src=192.168.122.1 ttl: 64 last_seen: 6273353 oldest_pkt: 4 6084651, 6119177, 6119177, 6273353
src=192.168.122.15 ttl: 0 last_seen: 6847221 oldest_pkt: 1 6847221
[root@guardiao ~]# echo -192.168.122.1 > /proc/net/xt_recent/conexoes_ssh
[root@guardiao ~]# cat /proc/net/xt_recent/conexoes_ssh
src=192.168.122.15 ttl: 0 last_seen: 6847221 oldest_pkt: 1 6847221
[root@guardiao ~]# echo /> /proc/net/xt_recent/conexoes_ssh
[root@guardiao ~]# cat /proc/net/xt_recent/conexoes_ssh
[root@guardiao ~]#
E finalizando, um ponto importante que precisa ser colocado é que se você for trabalhar com um firewall com grande número de conexões, por padrão o recent irá armazenar apenas 100 endereços nas tabelas criadas. Para alterar esse valor por exemplo para
1024, precisamos passar para o módulo xt_recent algumas flags:
filename: /lib/modules/2.6.29.4-167.fc11.i686.PAE/kernel/net/netfilter/xt_recent.ko
alias: ip6t_recent
alias: ipt_recent
license: GPL
description: Xtables: “recently-seen” host matching for IPv4
author: Jan Engelhardt
author: Patrick McHardy
srcversion: 0CA8710587603DFF5C5923B
depends:
vermagic: 2.6.29.4-167.fc11.i686.PAE SMP mod_unload 686
parm: ip_list_tot:number of IPs to remember per list (uint)
parm: ip_pkt_list_tot:number of packets per IP to remember (max. 255) (uint)
parm: ip_list_hash_size:size of hash table used to look up IPs (uint)
parm: ip_list_perms:permissions on /proc/net/xt_recent/* files (uint)
parm: ip_list_uid:owner of /proc/net/xt_recent/* files (uint)
parm: ip_list_gid:owning group of /proc/net/xt_recent/* files (uint)
Ah!! Para finalizar não esqueça de salvar suas regras e ativar o serviço de firewall persistente ao reboot
[root@guardiao ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@guardiao ~]# chkconfig iptables on
















Tutorial muito bom! Fazia tempo que não lia algo bem feito.