Unix, Código, Software Libre, Geeks… Cervezas & Tequila
Filtrando contenido web con proxy transparente (dhcp3-server + squid + dansguardian + iptables)
El objetivo de este pequeño tutorial es tener un equipo que filtre el contenido de internet hacia nuestra red local, aparte de eso debemos dicho equipo se encargara de routear los paquetes para acceder a internet por medio de él.
Nota: Las ips de la red local son del tipo: 192.168.50.x y el equipo que hará de router tendrá dos interfaces de red, eth0: 189.204.25.132 y eth1: 192.168.50.254
Requerimientos:
- Equipo con GNU/Linux instalado (Debian)
- Equipo con dos (2) tarjetas de red
- Conexión a internet
Instalación:
Instalamos los programas que vamos a necesitar para hacer el trabajo:
# apt-get install iptables squid dansguardian dhcp3-serverConfiguración del servidor DHCP (dhcp3-server)
Para configurar dhcp3-server debemos de editar el archivo localizado en: /etc/dhcp3/dhcpd.conf
# # Sample configuration file for ISC dhcpd for Debian # ddns-update-style none; # option definitions common to all supported networks... option domain-name "example.org"; option domain-name-servers ns1.example.org, ns2.example.org; default-lease-time 600; max-lease-time 7200; authoritative; log-facility local7; subnet 192.168.50.0 netmask 255.255.255.0 { range 192.168.50.10 192.168.50.50; option domain-name-servers 200.57.64.67; option domain-name-servers 200.85.0.107; option domain-name “k4ch0.netâ€; option routers 192.168.50.254; option broadcast-address 192.168.50.255; default-lease-time 600; max-lease-time 7200; }
Explicación:
Con esta configuración, lo que estamos haciendo es que el servidor DHCP se encargara de asignar IP’s de manera dinámicas que entren en el rango 192.168.50.10 – 192.168.50.50, con una máscara de red 255.255.255.0, y Servidores DNS 200.57.64.67 y 200.85.0.107, una puerta de enlace definida en la IP 192.168.50.254 y un broadcast de 192.168.50.255.
Configuración del Servidor Proxy (squid)
Procedemos a editar el archivo /etc/squid/squid.conf
visible_hostname proxy http_port 3128 transparent #We recommend you to use at least the following line. hierarchy_stoplist cgi-bin ? #We recommend you to use the following two lines. acl QUERY urlpath_regex cgi-bin \? cache deny QUERY # Apache to signal ETag correctly on such responses acl apache rep_header Server ^Apache broken_vary_encoding allow apache # To log the request via syslog specify a filepath of “syslog†access_log /var/log/squid/access.log squid #Default: hosts_file /etc/hosts hosts_file /etc/hosts #Suggested default: refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern . 0 20% 4320 #Recommended minimum configuration: <strong>acl red_local src 192.168.0.0 192.168.50.0/255.255.255.0 #agregamos nuestras redes</strong> acl all src 0.0.0.0/0.0.0.0 acl manager proto cache_object acl localhost src 127.0.0.1/255.255.255.255 acl to_localhost dst 127.0.0.0/8 acl SSL_ports port 443 # https acl SSL_ports port 563 # snews acl SSL_ports port 873 # rsync acl Safe_ports port 80 # http acl Safe_ports port 21 # ftp acl Safe_ports port 443 # https acl Safe_ports port 70 # gopher acl Safe_ports port 210 # wais acl Safe_ports port 1025-65535 # unregistered ports acl Safe_ports port 280 # http-mgmt acl Safe_ports port 488 # gss-http acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl Safe_ports port 631 # cups acl Safe_ports port 873 # rsync acl Safe_ports port 901 # SWAT acl purge method PURGE acl CONNECT method CONNECT # Only allow cachemgr access from localhost <strong>http_access allow red_local #damos acceso a nuestras redes</strong> http_access allow localhost http_access allow manager localhost http_access deny manager http_access deny all # Only allow purge requests from localhost http_access allow purge localhost http_access deny purge # Deny requests to unknown ports http_access deny !Safe_ports # Deny CONNECT to other than SSL ports http_access deny CONNECT !SSL_ports #http_access allow our_networks http_access allow localhost # And finally deny all other access to this proxy http_access deny all # and finally allow by default http_reply_access allow all #Allow ICP queries from everyone icp_access allow all #TAG: cache_effective_group #If you want Squid to run with a specific GID regardless of #the group memberships of the effective user then set this #to the group (or GID) you want Squid to run as. When set #all other group privileges of the effective user is ignored #and only this GID is effective. If Squid is not started as #root the user starting Squid must be member of the specified #group. #cache_effective_group proxy <strong># Nota: debemos de crear un usuario squid y un grupo squid</strong> <strong>#agregamos el usuario y el grupo con que va a trabajar</strong> cache_effective_user squid cache_effective_group squid
Y con eso ya tenemos configurado squid en nuestro equipo.
Configuración de Dansguardian
Ahora nos dedicamos a configurar Dansguardian, /etc/dansguardian/dansguardian.conf y lo primero es comentar o borrar esta linea:
#UNCONFIGURED - Please remove this line after configuration :DLuego continuamos:
reportinglevel = 0 language = "spanish" loglocation = "/var/log/dansguardian/access.log" filterip = 192.168.50.254 #La IP de la interfaz en la que el Dansguardian escuchará! filterport = 8080 #El puerto en el que trabajara Dansguardian proxyip = 192.168.50.254 proxyport = 3128 #El puerto en el que funciona Squid
Importante definir las listas de “cosas” prohibidas, como formatos de archivos, urls y demás, aunque estas opciones por defecto vienen activadas al igual que otras más.
bannedextensionslist = "/etc/dansguardian/bannedextensionslist" bannediplist = "/etc/dansguardian/bannediplist" banneduserlist = "/etc/dansguardian/banneduserlist"
Evito hacer mención de un montón de opciones mas que cada quien puede configurar a su gusto en dichos archivos de configuración al igual que la utilización de blacklists adicionales, pero como ejemplo nos descargarnos la blacklist de http://urlblacklist.com/ y la descomprimimos en /etc/dansguardian/
Y con esto estará configurado Dansguardian.
Configuración de Iptables
A continuación va es script con iptables que funciona para bloquear puertos y redireccionar el trafico.
#!/bin/sh # squid server IP SQUID_SERVER="192.168.50.254" # Interface connected to Internet INTERNET="eth0" # Interface connected to LAN LAN_IN="eth1" # Squid port SQUID_PORT="3128" # DO NOT MODIFY BELOW # Clean old firewall iptables -F iptables -X iptables -t nat -F iptables -t nat -X iptables -t mangle -F iptables -t mangle -X # Load IPTABLES modules for NAT and IP conntrack support modprobe ip_conntrack modprobe ip_conntrack_ftp # For win xp ftp client #modprobe ip_nat_ftp echo 1 > /proc/sys/net/ipv4/ip_forward # Setting default filter policy iptables -P INPUT DROP iptables -P OUTPUT ACCEPT # Unlimited access to loop back iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT # Allow UDP, DNS and Passive FTP iptables -A INPUT -i $INTERNET -m state ESTABLISHED,RELATED -j ACCEPT # set this system as a router for Rest of LAN iptables table nat append POSTROUTING out-interface $INTERNET -j MASQUERADE iptables append FORWARD in-interface $LAN_IN -j ACCEPT # unlimited access to LAN iptables -A INPUT -i $LAN_IN -j ACCEPT iptables -A OUTPUT -o $LAN_IN -j ACCEPT # DNAT port 80 request comming from LAN systems to squid 3128 ($SQUID_PORT) aka #transparent proxy iptables -t nat -A PREROUTING -i $LAN_IN -p tcp dport 80 -j DNAT to $SQUID_SERVER:8080 # if it is same system iptables -t nat -A PREROUTING -i $INTERNET -p tcp dport 80 -j REDIRECT to-port $SQUID_PORT # DROP everything and Log it iptables -A INPUT -j LOG iptables -A INPUT -j DROP
Lo guardamos proxy.sh y le asignamos permisos de ejecucion
# chmod +x proxy.shpara correrlo seria
#sh proxy.shY con esto tenemos configurado nuestro proxy web transparente con dhcp3-server + squid + dansguardian + iptables.
Espero les sirva, cualquier corrección pueden mandarme un correo a la dirección: lcacho [at] k4ch0.net
También te puede interesar :
| This entry was posted by k4ch0 on July 7, 2009 at 5:27 pm, and is filed under How-to, Redes, Sysadmin. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

about 3 weeks ago
El script de iptables devuelve un error:
Bad argument `ESTABLISHED,RELATED’
Try `iptables -h’ or ‘iptables –help’ for more information.
Bad argument `table’
Try `iptables -h’ or ‘iptables –help’ for more information.
Bad argument `append’
Try `iptables -h’ or ‘iptables –help’ for more information.
Bad argument `dport’
Try `iptables -h’ or ‘iptables –help’ for more information.
about 3 weeks ago
Ya compuse el script, te lo anexo aquí, gracias por la observación:
#!/bin/sh
# squid server IP
SQUID_SERVER=”192.168.50.254″
# Interface connected to Internet
INTERNET=”eth0″
# Interface connected to LAN
LAN_IN=”eth1″
# Squid port
SQUID_PORT=”3128″
# DO NOT MODIFY BELOW
# Clean old firewall
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
# Load IPTABLES modules for NAT and IP conntrack support
modprobe ip_conntrack
modprobe ip_conntrack_ftp
# For win xp ftp client
#modprobe ip_nat_ftp
echo 1 > /proc/sys/net/ipv4/ip_forward
# Setting default filter policy
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
# Unlimited access to loop back
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# Allow UDP, DNS and Passive FTP
iptables -A INPUT -i $INTERNET -m state –state ESTABLISHED,RELATED -j ACCEPT
# set this system as a router for Rest of LAN
iptables -t nat –append POSTROUTING –out-interface $INTERNET -j MASQUERADE
iptables –append FORWARD –in-interface $LAN_IN -j ACCEPT
# unlimited access to LAN
iptables -A INPUT -i $LAN_IN -j ACCEPT
iptables -A OUTPUT -o $LAN_IN -j ACCEPT
# DNAT port 80 request comming from LAN systems to squid 3128 ($SQUID_PORT) aka #transparent proxy
iptables -t nat -A PREROUTING -i $LAN_IN -p tcp –dport 80 -j DNAT –to-destination $SQUID_SERVER:8080
# if it is same system
iptables -t nat -A PREROUTING -i $INTERNET -p tcp –dport 80 -j REDIRECT –to-port $SQUID_PORT
# DROP everything and Log it
iptables -A INPUT -j LOG
iptables -A INPUT -j DROP