DansGuardian Filtering Proxy unter Debian / Ubuntu Linux

Aus Laub-Home Wiki

Dies ist eine Anleitung wie man mittels DansGuardian, Squid3 und ClamAV einen Content Scanner Proxy baut. DansGuardian greift hierfür auf den Standard Port (TCP 3128) des Squids zu und bietet via Port 8080 dem Client seine Dienste als Proxy an. Zusätzlich scannt ClamAV den Content auf Viren. Als Voraussetzung dient eine Ubuntu Linux oder Debian Linux Grundinstallation. Ich habe das Ganze mit einer Ubuntu 12.04 LTS Installation durchgeführt. Es sollte aber auch kein Problem sein das ganze unter Debian Squeeze nachzubauen.
Anleitungen zur Grundinstallation der beiden Linux findet ihr hier:

Installation

Als erstes holen wir uns alle benötigten Pakete:

aptitude install squid3 squid3-common dansguardian clamav-daemon clamav-freshclam

Nun aktualisieren wir den ClamAV mittels freshclam:

freshclam

Freshclam aktualisiert standardmäßig die Virusdatenbank alle Stunde, will man dies ändern, macht man dies in folgender Konfigurationsdatei:
/etc/clamav/freshclam.conf

# Check for new database 24 times a day
Checks 24

Squid3 Konfiguration

Die Squid Konfiguration findet in folgender Datei statt: /etc/squid3/squid.conf
Als erstes sollte man dafür sorgen das niemand aus dem Lokalen Netz den Squid direkt als Proxy verwenden kann:

# Squid normally listens to port 3128
http_port 127.0.0.1:3128

#acl localhost src 127.0.0.1/32 ::1
#acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
acl localhost src 127.0.0.1/32
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
#acl localnet src 10.0.0.0/8    # RFC1918 possible internal network
#acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
#acl localnet src 192.168.0.0/16        # RFC1918 possible internal network
#acl localnet src fc00::/7       # RFC 4193 local private network range
#acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines

Nun sorgen wir noch für ein bisschen Performance in dem wir den Cache einrichten:
Zuerst geben wir die Größe des Memory Caches an. Ich denke ein guter Wert ist 30-40% des Arbeitsspeichers, sofern keine anderen Dienste auf dem System laufen (ich habe hier 8GB Ram):

cache_mem 3 GB
maximum_object_size_in_memory 64 MB
memory_replacement_policy lru

Nun stellen wir noch die Größe des Disk Caches ein:
8GB Festplattenplatz, 32 Level1 Folder und 512 Level2 Folder

cache_replacement_policy lru
cache_dir ufs /var/spool/squid3 8192 32 512
minimum_object_size 0 KB
maximum_object_size 256 MB
cache_swap_low 90
cache_swap_high 95
# Tuning Tipp:
quick_abort_min -1
range_offset_limit -1

Nun noch das Logging für eventuelle Awstats Auswertungen anpassen:

logformat combined %>a %{X-Forwarded-For}>h %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
access_log /var/log/squid3/access.log combined
emulate_httpd_log on

Weitere Konfigurationsinformationen findet ihr hier:

Zu guter Letzt starten wir den Dienst neu:

service squid3 restart

Ob er läuft sieht man mittels:

netstat -an | grep 3128
tcp        0      0 127.0.0.1:3128          0.0.0.0:*               LISTEN

oder

ps ax |grep squid3
24109 ?        Ss     0:00 /usr/sbin/squid3 -N -YC -f /etc/squid3/squid.conf

DansGuardian Konfiguration

Die Konfiguration des DansGuardian spielt sich in der /etc/dansguardian/dansguardian.conf Konfigurationsdatei ab. In dieser muss als erstes ein "#" vor "UNCONFIGURED" geschrieben werden:

#UNCONFIGURED - Please remove this line after configuration

Als nächstes sagen wir ihm wo er den Squid und den ClamAV findet, stellen die Sprache auf Deutsch und richten den Cache ein:

language = 'german'
proxyip = 127.0.0.1
proxyport = 3128
maxcontentfiltersize = 0
maxcontentramcachescansize = 501200
maxcontentfilecachescansize = 1024000
filecachedir = '/var/spool/dansguardian'
contentscanner = '/etc/dansguardian/contentscanners/clamav.conf'
contentscanner = '/etc/dansguardian/contentscanners/clamdscan.conf'
forwardedfor = on

Nun noch das Cache Dir erstellen

mkdir /var/spool/dansguardian
chown dansguardian.dansguardian /var/spool/dansguardian

Und auch hier zu guter Letzt den Dienst neu starten:

service dansguardian restart

Nun sollte man prüfen ob er läuft:

netstat -an | grep 8080
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN

Nun noch den clamav Benutzer in die dansguardian Gruppe schmeißen:

usermod -G dansguardian clamav

Achtung!!! Unter Ubuntu muss noch dem ClamAV via Apparmor erlaubt werden das er auf das Cache Verzeichniss zugreifen darf:
/etc/apparmor.d/usr.sbin.clamd

...
  /tmp/ rw,
  /tmp/** krw,

  /var/spool/dansguardian/ rw,
  /var/spool/dansguardian/** krw,

  /var/lib/clamav/ r,
...

zum aktivieren beide Dienste neustarten:

service apparmor restart
service clamav-daemon restart

Kontentfilter Einstellungen

Hier kann man die ganzen Filter an und aus stellen:
/etc/dansguardian/dansguardianf1.conf
Hier werden die Dateierweiterungen konfiguriert:
/etc/dansguardian/lists/bannedextensionlist
Hier werden die Mime Types konfiguriert:
/etc/dansguardian/lists/bannedmimetypelist
Seiten Whitlist:
/etc/dansguardian/lists/exceptionsitelist
/etc/dansguardian/lists/exceptionurllist
Wörterlisten:
/etc/dansguardian/lists/weightedphraselist

Will man das Restrictions Level erhöhen, da zu viel gefiltert und geblockt wird, kann man dies durch das "naughtynesslimit" machen.
/etc/dansguardian/dansguardianf1.conf

# 50 is for young children,  100 for old children,  160 for young adults.
naughtynesslimit = 160

Als Transparenten Proxy nutzen

iptables -t nat -A PREROUTING -m tcp -p tcp -s ! 192.168.xx.10 --dport 80 -j DNAT --to 192.168.xx.10:8080
iptables -t nat -A PREROUTING -m tcp -p tcp -s ! 192.168.xx.10 -d ! 192.168.xx.0/24 --dport 80 -j DNAT --to 192.168.xx.10:8080
iptables -t nat -A PREROUTING -i br0 -m tcp -p tcp -s ! 192.168.xx.10 -d ! 192.168.xx.0/24 --dport 80 -j DNAT --to 192.168.xx.10:8080
Step 0: Configure and compile Squid with the "--enable-linux-netfilter" option
Step 1: Set up DG and Squid so that they work in normal proxy mode.
Step 2: Add or update the following lines in Squid.conf
httpd_accel_host virtual
httpd_accel_port 80
httpd_accel_single_host off
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
Step 3: Run the following command as root
iptables -t nat -A PREROUTING -m tcp -p tcp --dport 80 -j REDIRECT 
--to-port 8080
Where 8080 is the port that DG is running on. You may also want to add an 
exclusion for you own internal servers, etc. Do a "man iptables" for more 
help, or visit http://www.netfilter.org/.

If you want, you can set up a separate machine to handle the redirect...
iptables -t nat -A PREROUTING -m tcp -p tcp -s ! {DG's IP ADDRESS} --dport 
80 -j DNAT --to {DG's IP ADDRESS}:8080
The "!" is important, because you don't want to redirect traffic from the 
filter server (infinite loop).

Step 3b: Run the following command as root
iptables -A INPUT -m tcp -p tcp -s ! 127.0.0.1 --dport 3128 -j DROP
This will block your kids/customers/employees from connecting to Squid 
directly in order to bypass the filter.

Step 3c: Run the following command as root
/etc/init.d/iptables save
"So let it be written, so let it be done..."

Step 4: Add or update these lines in /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
You will want to add lines for each interface.

Step 5: Forward traffic through your filter box (or whichever box is 
handling the redirect) by changing the default gateway on any filtered 
machines.

This works on my machine (DG 2.5, Redhat 8.0), it should work on yours. I 
am not responsible if you screw up, blah, blah, blah. Good luck :) Also, 
you've made enough kernel-level changes, etc that it would be a good idea 
to reboot and make sure everything comes up running.

Testen der Einstellungen

Um das Ganze nun zu testen muss man den Server als Proxy im Browser eintragen, also die IP und den Port (hier 8080). Um den Virenscanner zu testen kann man auf der folgenden Seite die Test Viren nutzen:

Adzu muss man allerdings in den oben stehenden Dateien die Endung zip/tgz oder com und den dazugehörigen Mime Type erlauben. Den Content Filter kann man recht gut mittels google testen, in dem man hier einfach mal nach bösen Seiten sucht und diese versucht zu öffnen.
Die Fehlerseite kann man im übrigen individuell anpassen in dem man einfach die folgende datei editiert und hier zum Beispiel "MEINE FIRMA" ersetzt:
/etc/dansguardian/languages/german/template.html

Und nun viel Spaß beim Safer Surfing!

Konfiguration via Webmin

Will man eine schöne grafische Oberfläche für die Administration haben, kann man sich diese mittels Webmin bereitstellen.
Die Grundinstallation von Webmin ist hier beschrieben:

Nach erfolgreichem Login laden wir uns bei Sourceforge das Passende Dansguardian Modul herunter:

Nun kann es installiert werden:
Webmin --> Webmin Configuration --> Webmin Modules --> From uploaded file
Nun den Browser refreshen und das Modul starten. Man sollte dieses nun Konfigurieren:

Full path to DG config(etc) directory: /etc/dansguardian   (default)
Full path to DG pid file: /var/run/dansguardian.pid  (default)
Full path to DG binary: /usr/sbin/dansguardian    (CHANGE THIS)
Full path to DG log directory: /var/log/dansguardian  (default)
Full path to DG messages file (or literal 'followDansGuardian'): followDansGuardian  (default)
Command to restart DG (if allowed): service dansguardian restart  (CHANGE THIS)
Command to start DG (if allowed): service dansguardian start  (CHANGE THIS)
Command to stop DG (if allowed): service dansguardian stop  (CHANGE THIS)

Quellen