Filtrando contenido web por medio de proxy transparente

Posted by on July 18, 2007

Autor:  Carlos Torrento

Objetivo: Tener un servidor dhcp que filtre contenido web y que sirva como router para accesar a internet a través de el.
lo obtendrá al final seria un servidor dhcp con proxy transparente.

Nota: tengo eth0:192.168.0.3 # acceso a mi red con internet
eth1:192.168.2.1 # acceso de mi red
-> Que necesito.
1) acceso a internet (obvio), 2 tarjetas de red y debían por su puesto.

2) Instalar dhcp3-server, squid, dansguardian, iptables

como?

# apt-get install squid
# apt-get install dansguardian
# apt-get install iptables
# apt-get install dhcp3-server

y ahora?

Lo primero que hice y lo más fácil es configurar mi /etc/dhcp/dhcpd.conf
ddns-update-style none;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;

subnet 192.168.2.0 netmask 255.255.255.0 {
range 192.168.2.3 192.168.2.250;
option domain-name-servers 200.85.0.104;
option domain-name-servers 200.85.0.107;
option domain-name “tuxito.net”;
option routers 192.168.2.1;
option broadcast-address 192.168.2.255;
default-lease-time 600;
max-lease-time 7200;
}
Con esta configuración lo que estamos haciendo es que estamos asignando ip´s dinámicas desde la 192.168.0.3 hasta la 192.168.2.250 con netmask 255.255.255.0,
DNS 200.85.0.104 y 200.85.0.107, como puerta de enlace la 192.168.2.1 y un broadcast de 192.168.2.255.

3) Ahora procedemos a configurar el /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:
acl mired src 192.168.0.0 192.168.2.0/255.255.255.0 #agregamos nuestras redes
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
http_access allow mired #damos acceso a nuestras redes
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
# Nota: debo de crear un usuario squid y un grupo squid
#agregamos el usuario y el grupo con que va a trabajar
cache_effective_user squid
cache_effective_group squid
Con esto tendríamos configurado en dhcp y el squid

4) ahora vamos a configurar el dansguardian :)
Como son un montón de líneas solo mostrare las de configuración:
# DansGuardian config file for version 2.8.0 with Anti-Virus plug-in 6.4.3
# **NOTE** as of version 2.7.5 most of the list files are now in dansguardianf1.conf
#UNCONFIGURED - Please remove this line after configuration
reportinglevel = 3
languagedir = ‘/etc/dansguardian/languages’
language = ’spanish’
loglevel = 3
logexceptionhits = on
logfileformat = 1
loglocation = ‘/var/log/dansguardian/access.log’
filterip = 192.168.0.3
filterport = 8080
proxyip = 192.168.0.3
proxyport = 3128
accessdeniedaddress = ‘http://localhost/’
nonstandarddelimiter = on
usecustombannedimage = 1
custombannedimagefile = ‘/etc/dansguardian/transparent1×1.gif’
filtergroups = 1
filtergroupslist = ‘/etc/dansguardian/filtergroupslist’
bannediplist = ‘/etc/dansguardian/bannediplist’
exceptioniplist = ‘/etc/dansguardian/exceptioniplist’
banneduserlist = ‘/etc/dansguardian/banneduserlist’
exceptionuserlist = ‘/etc/dansguardian/exceptionuserlist’
bannedurllist = ‘/etc/dansguardian/bannedurllist’
exceptionurllist = ‘/etc/dansguardian/bannedurllist’
showweightedfound = on
weightedphrasemode = 2
urlcachenumber = 3000
urlcacheage = 900
phrasefiltermode = 2
preservecase = 0
hexdecodecontent = 0
forcequicksearch = 0
reverseaddresslookups = off
reverseclientiplookups = off
createlistcachefiles = on
maxuploadsize = -1
maxcontentfiltersize = 256
usernameidmethodproxyauth = on
usernameidmethodntlm = off # **NOT IMPLEMENTED**
usernameidmethodident = off
preemptivebanning = on
forwardedfor = off
usexforwardedfor = off
logconnectionhandlingerrors = on
maxchildren = 120
minchildren = 8
minsparechildren = 4
preforkchildren = 6
maxsparechildren = 32
maxagechildren = 500
ipcfilename = ‘/tmp/.dguardianipc’
urlipcfilename = ‘/tmp/.dguardianurlipc’
nodaemon = off
nologger = off
softrestart = off
# ANTIVIRUS SETTINGS
# ——————–
virusscan = on
virusengine = ‘clamav’
tricklelength = 32768
forkscanlength = 32768
firsttrickledelay = 10
followingtrickledelay = 10
maxcontentscansize = 41904304
virusscanexceptions = on
urlcachecleanonly = on
virusscannertimeout = 60
notify = 0
emaildomain = ‘your.domain.com’
postmaster = ‘postmaster@your.domain.com’
emailserver = ‘127.0.0.1:25′
downloaddir = ‘/tmp/dgvirus’
# CLAMAV SETTINGS
# ——————–
clmaxfiles = 1500
clmaxreclevel = 3
clmaxfilesize = 10485760
clblockencryptedarchives = off
cldetectbroken = off
# CLAMDSCAN SETTINGS
# ——————–
clamdsocket = ‘/tmp/clamd’
# KASPERSKY 5 SETTINGS
# ——————–
avesocket = ‘/var/run/aveserver’
# TROPHIE SETTINGS
# ——————–
trophiesocket = ‘/var/run/trophie’
# SOPHIE SETTINGS
# ——————–
sophiesocket = ‘/var/run/sophie’
# ICAP SETTINGS (experimental)
# —————————-
icapsocket = ‘localhost:1344′

# OPTION: icapservice
icapservice = ‘icap://localhost/avscan’
daemonuser = ’squid’
daemongroup = ’squid’
bueno ya con eso tendriamos el dansguardian configurado(nota:_ aquí le agregue el usuario y grupo que tuve que haber creado “squid”)
5) acontinuacion les muestro el script con iptables que funciona para bloquear puertos y redireccionar el trafico.
#!/bin/sh

# squid server IP
SQUID_SERVER=”192.168.0.3″

# 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 –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.sh
para correrlo seria #sh proxy.sh

6) como ultimo paso para bloquear las urls o expresiones, nos descargarnos la blacklist de http://urlblacklist.com/ la descomprimimos en /etc/dansguardian/
Y eso seria todo ya tenemos un servidor dhcp con proxy transparente.
Espero que les sirva tanto como a mi.

Cualquier corrección hacérmela saber a cltorrento AT gmail.com o tuxito AT linuxmail.org

Comments

Respond | Trackback

  1. Buenos días.
    Fiajte que te quiero comentar que tengo problemas con el unicode de este sitio, ya que presisamene en este articulo tengo errores con los acentos.
    No se si es de mi configuracion de iceweasel o es en la forma que agregastes el post y seria bueno que corrigieras esto (de ser de parte del glog).

    Muy bueno este post.

    Saludos brother.

  2. Ruben Ventura March 21, 2008 9:28 am

    muy buen articulo, justo lo que necesito, estoy por realizar algo semejante.

    Saludos desde México.

  3. edwin leoanrdo March 31, 2008 1:52 pm

    Hola como estas, una pregunta cuando doy apt-get install squid, luego de bajarlo y desempaquetar todo, y luego lo intala da el siguiente error:

    Leyendo lista de paquetes… Hecho
    Creando árbol de dependencias… Hecho
    Paquetes sugeridos:
    squidclient squid-cgi logcheck-database resolvconf
    Se instalarán los siguientes paquetes NUEVOS:
    squid
    0 actualizados, 1 se instalarán, 0 para eliminar y 0 no actualizados.
    Se necesita descargar 0B/710kB de archivos.
    Se utilizarán 1749kB de espacio de disco adicional después de desempaquetar.
    Preconfigurando paquetes …
    Seleccionando el paquete squid previamente no seleccionado.
    (Leyendo la base de datos …
    83996 ficheros y directorios instalados actualmente.)
    Desempaquetando squid (de …/squid_2.6.5-6etch1_amd64.deb) …
    Configurando squid (2.6.5-6etch1) …
    Creating squid spool directory structure
    FATAL: Could not determine fully qualified hostname. Please set ‘visible_hostna me’

    Squid Cache (Version 2.6.STABLE5): Terminated abnormally.
    CPU Usage: 0.004 seconds = 0.004 user + 0.000 sys
    Maximum Resident Size: 0 KB
    Page faults with physical i/o: 0
    /var/lib/dpkg/info/squid.postinst: line 133: 6984 Abortado /usr/ sbin/squid -z
    Restarting Squid HTTP proxy: squid* Creating squid spool directory structure
    FATAL: Could not determine fully qualified hostname. Please set ‘visible_hostna me’

    Squid Cache (Version 2.6.STABLE5): Terminated abnormally.
    CPU Usage: 0.004 seconds = 0.004 user + 0.000 sys
    Maximum Resident Size: 0 KB
    Page faults with physical i/o: 0
    /etc/init.d/squid: line 74: 7018 Abortado $DAEMON -z
    FATAL: Could not determine fully qualified hostname. Please set ‘visible_hostna me’

    Squid Cache (Version 2.6.STABLE5): Terminated abnormally.
    CPU Usage: 0.008 seconds = 0.004 user + 0.004 sys
    Maximum Resident Size: 0 KB
    Page faults with physical i/o: 0
    /etc/init.d/squid: line 74: 7020 Abortado start-stop-daemon –qu iet –start –pidfile $PIDFILE –chuid $CHUID –exec $DAEMON — $SQUID_ARGS

Comments

Comments: