Enviado em 25/09/2013 - 09:06h
Recentemente tive que bloquear o facebook via https aqui onde trabalho. Utilizo o Ubuntu 12.04 x64, squid3 transparente na porta 3128 e iptables. Achei o seguinte código que me atendeu:
#!/bin/bash
FACEBOOK_IP_RANGE="31.13.64.0-31.13.127.255 31.13.24.0-31.13.31.255 74.119.76.0-74.119.79.255 69.63.176.0-69.63.191.255 69.171.224.0-69.171.255.255 66.220.144.0-66.220.159.255 204.15.20.0-204.15.23.255 173.252.64.0-173.252.127.255"
iptables -N FACEBOOK
## FACEBOOK DENY
for face in $FACEBOOK_IP_RANGE; do
iptables -I FORWARD -m tcp -p tcp -m iprange --dst-range $face --dport 443 -j FACEBOOK
iptables -I FORWARD -m tcp -p tcp -m iprange --dst-range $face --dport 80 -j FACEBOOK
iptables -I FORWARD -m tcp -p tcp -m iprange --dst-range $face --dport 3128 -j FACEBOOK
iptables -I FORWARD -m tcp -p tcp -m iprange --dst-range $face --dport 80:8080 -j FACEBOOK
done
iptables -A FACEBOOK -j REJECT
# Funciona somente no transparente
# iptables -A FORWARD -i eth0 -d 192.168.0.0/24 -m string --algo bm --string facebook -j DROP
# iptables -I FORWARD -i eth0 -d 192.168.0.0/24 -m string --algo bm --string facebook -j DROP
# funciona somente no transparente
# iptables -I FORWARD -m string --algo bm --string facebook.com -j DROP
# iptables -I FORWARD -m string --algo bm --string facebook -j DROP
# bloqueia tudo, transparente ou nao e os que tem referencia ao face também
# iptables -A FORWARD -i eth0 -d 192.168.0.0/24 -m string --algo bm --string facebook.com -j DROP
# iptables -I FORWARD -i eth0 -d 192.168.0.0/24 -m string --algo bm --string facebook.com -j DROP
# bloqueia redirecionamento (com ref tb) mas nao no transparente
# iptables -I OUTPUT -m string --algo bm --string facebook.com -j DROP
# Funciona somente no transparente
# iptables -I FORWARD -m string --string 'facebook' --algo bm -j DROP
# iptables -I FORWARD -m string --string facebook --algo bm -j DROP
# iptables -A FORWARD -m string --string facebook --algo bm -j DROP
# não funciona
# iptables -A FORWARD -i eth1 -d 192.168.0.0/24 -m string --algo bm --string facebook.com -j DROP
# iptables -I FORWARD -i eth1 -d 192.168.0.0/24 -m string --algo bm --string facebook.com -j DROP
# iptables -A FORWARD -i eth1 -d 192.168.0.0/24 -m string --algo bm --string facebook.com -j DROP
# iptables -I FORWARD -i eth1 -d 192.168.0.0/24 -m string --algo bm --string facebook.com -j DROP
# Generated by iptables-save v1.4.12 on Thu Jun 6 10:05:25 2013
*mangle
:PREROUTING ACCEPT [349:44378]
:INPUT ACCEPT [275:24210]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [186:22459]
:POSTROUTING ACCEPT [186:22459]
COMMIT
# Completed on Thu Jun 6 10:05:25 2013
# Generated by iptables-save v1.4.12 on Thu Jun 6 10:05:25 2013
*filter
:INPUT ACCEPT [201:20340]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [181:22112]
-A INPUT -s 74.125.234.61/32 -j DROP
-A INPUT -i eth1 -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p udp -m udp --sport 53 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 3128 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 2230 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 2231 --tcp-flags FIN,SYN,RST,ACK SYN -m limit --limit 1/min --limit-burst 3 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 2231 --tcp-flags FIN,SYN,RST,ACK SYN -j DROP
-A INPUT -i eth1 -p udp -m udp --dport 33435:33525 -j DROP
-A INPUT -i eth2 -p udp -m udp --dport 33435:33525 -j DROP
-A INPUT -p tcp -m tcp --dport 8017 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 82 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 6881:6889 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p udp -m udp --dport 6881:6889 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -m tcp --dport 4662 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -m tcp --dport 4666 -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -d 74.125.234.61/32 -j DROP
-A FORWARD -p udp -m udp --sport 53 -j ACCEPT
-A FORWARD -d 213.248.112.0/24 -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -p tcp -m tcp --dport 1214 -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -p tcp -m tcp --dport 6881:6889 -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -p udp -m udp --dport 6881:6889 -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -p tcp -m tcp --dport 4672 -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -p tcp -m tcp --dport 4666 -j REJECT --reject-with icmp-port-unreachable
-A OUTPUT -p tcp -m tcp --dport 6881:6889 -j REJECT --reject-with icmp-port-unreachable
-A OUTPUT -p udp -m udp --dport 6881:6889 -j REJECT --reject-with icmp-port-unreachable
-A OUTPUT -p tcp -m tcp --dport 6346 -m state --state NEW -j REJECT --reject-with icmp-port-unreachable
-A OUTPUT -p tcp -m tcp --dport 4632:4732 -m state --state NEW -j REJECT --reject-with icmp-port-unreachable
-A OUTPUT -p tcp -m tcp --dport 4666 -m state --state NEW -j REJECT --reject-with icmp-port-unreachable
-A OUTPUT -d 74.125.234.61/32 -j DROP
COMMIT
# Completed on Thu Jun 6 10:05:25 2013
# Generated by iptables-save v1.4.12 on Thu Jun 6 10:05:25 2013
*nat
:PREROUTING ACCEPT [88:22592]
:INPUT ACCEPT [14:2424]
:OUTPUT ACCEPT [4:288]
:POSTROUTING ACCEPT [0:0]
-A PREROUTING -s 192.168.0.0/24 -d 200.201.173.68/32 -p tcp -m tcp --dport 80 -j RETURN
-A PREROUTING -s 192.168.0.0/24 -d 200.201.174.0/24 -p tcp -m tcp --dport 80 -j RETURN
-A PREROUTING -s 192.168.0.0/24 -d 186.226.17.28/32 -p tcp -m tcp --dport 80 -j RETURN
-A PREROUTING -s 192.168.0.0/24 -d 201.63.119.0/24 -p tcp -m tcp --dport 80 -j RETURN
-A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128
-A PREROUTING -i eth1 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128
-A PREROUTING -i eth1 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.0.9:3128
-A PREROUTING -i eth1 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128
-A POSTROUTING -o eth1 -j MASQUERADE
-A POSTROUTING -o eth0 -j MASQUERADE
-A POSTROUTING -s 192.168.0.0/24 -o eth0 -j MASQUERADE
COMMIT
# Completed on Thu Jun 6 10:05:25 2013