Squid

Allgemein

Squid ist ein quelloffener und freier Proxy-Server speziell für die Protokolle HTTP/HTTPS und FTP. Zudem unterstützt er weitere Protokolle wie SSL/TLS und Gopher. Er kann sowohl als Transparent Proxy, aber auch als Forward Proxy oder Reverse Proxy eingesetzt werden.

Eine Besonderheit von squid ist, das er sehr flexibel und auch den eigenen Bedürfnissen entsprechend anpassbar ist. Dies ist auch eines der Gründe, warum squid einer der meist genutzten HTTP Proxies ist.

Features

Squid bietet sehr viele Features & Funktionen an. Hier wird nur auf die wesentlichen davon eingegangen.

ACLs

ACLs, kurz für Access Control Lists (Zugangskontrolliste), ermöglichen bestimmte Verbindungen anhand von wohldefinierter Bedingungen zuzulassen oder abzulehnen.

acl <acl_name-1>   <src | dst>                       <IP_ADDR/MASK>
acl <acl_name-2>   port                               <port_no1> [port_no2 ...]
acl <acl_name-3>   <srcdomain | <dstdomain>           <.domain.example>         # .example.org for matching '*.example.org'
acl <acl_name-4>   <src_as | dst_as>                  <AS_NO>  [AS_NO2 ...]     # rules based on AS number

acl <acl_name-5>   proto                              <HTTP | FTP | ...>  [HTTP | FTP | ...]
acl <acl_name-6>   method                             <GET | POST | ...>  [GET | POST | ...]

Caching

# Allow establishing connection to the ports
acl Safe_ports port 80
acl Safe_ports port 443

# cache_dir Type Directory-Name Fs-specific-data [options]
# Types: ufs (classic), aufs (async ufs), diskd, rockd
# here: ufs <cache dir> 8GB 16-L1 256-L2 direcotories
cache_dir ufs /var/spool/squid/cache 8000 16 256

cache_mem 1 GB   # memory usage for in-transit and hot objects
minimum_object_size 0 bytes
maximum_object_size 200 MB
maximum_object_size_in_memory 512 KB

# refresh_pattern [-i] regex min_expiry percent max_expiry [options]
# add -i if case-insensitive matching required
# Below for forced cache:

# pictures & images
refresh_pattern -i \.(gif|png|jpeg|jpg|bmp|tif|tiff|ico)$ 10080 50% 43200 ignore-no-cache ignore-auth ignore-private store-stale
refresh_pattern -i \.(gif|png|jpeg|jpg|bmp|tif|tiff|ico)\? 10080 50% 43200 ignore-no-cache ignore-auth ignore-private store-stale

# website
#refresh_pattern -i \.(xml|html|htm|js|jsp|txt|css|php|asp)$ 10080 50% 43200 ignore-no-cache ignore-auth ignore-private store-stale
refresh_pattern -i \.(xml|js|jsp|txt|css)$ 10080 50% 43200 ignore-no-cache ignore-auth ignore-private store-stale
refresh_pattern -i \.(xml|js|jsp|txt|css)\? 10080 50% 43200 ignore-no-cache ignore-auth ignore-private store-stale

#sound, video multimedia
refresh_pattern -i \.(flv|x-flv|mov|avi|qt|mpg|mpeg|swf|wmv)$ 10080 50% 43200 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache store-stale
refresh_pattern -i \.(wav|mp3|mp4|au|mid)$ 10080 50% 43200 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-auth ignore-private store-stale

# files
refresh_pattern -i \.(iso|deb|rpm|zip|tar|tgz|ram|rar|bin|ppt|doc)$ 10080 90% 43200 ignore-no-cache ignore-auth store-stale
refresh_pattern -i \.(zip|gz|arj|lha|lzh)$ 10080 100% 43200 override-expire ignore-no-cache ignore-auth store-stale
refresh_pattern -i \.(rar|tgz|tar|exe|bin)$ 10080 100% 43200 override-expire ignore-no-cache ignore-auth ignore-reload ignore-no-cache store-stale
refresh_pattern -i \.(hqx|pdf|rtf|doc|swf)$ 10080 100% 43200 override-expire ignore-no-cache ignore-auth store-stale
refresh_pattern -i \.(inc|cab|ad|txt|dll)$ 10080 100% 43200 override-expire ignore-no-cache ignore-auth store-stale

SSL Bump

Da dies ein erweiterter Feature ist, der für gewöhnlich nicht genutzt wird, werden hier nur grundlegende Informationen mitgeteilt. Details unter: https://wiki.squid-cache.org/Features/SslPeekAndSplice

Mit SSL Bump kann squid auch TLS-verschlüsselte Verbindungen mitschneiden und cachen oder andere Operationen hierauf durchführen. Dabei werden diverse ssl_bump Aktionen verwendet.

Anhand der peek und splice Aktionen schaut squid dabei in den TLS-Handshake, startend mit dem Client Hello Message. Dabei wird die Nachricht des Clients möglichst identisch oder ähnlich an den Server übergeben. Die Entscheidung zum Splice, Bump oder zur Beendigung der Verbindung in jedem Schritt erfolgen und beeinflusst entsprechend das Vorgehen im nächsten Schritt.

ssl_bump Aktionen:

  • peek: im nächsten Schritt wird bspw. die SNI Information (step 1) oder das Server Zertifikat (step 2) extrahiert
  • splice: Die Verbindung zwischen Client und Server ohne Manipukation oder Mittschnitt aufbauen
  • bump: Zwischen Server und Proxy TLS-Verbindung aufbauen, und zwischen Client und Proxy mit einem nachgeahmten Zertifikat eine separate Verbindung aufbauen
  • terminate: DIe Verbindung zwischen Client und Server beenden

Antivirus (mit ClamAV)

Es ist möglich, auf dem Server einen Netzwerkantivirus zwischen Client und Server zu konfigurieren. Dabei können die ClamAV Datenbanken, aber auch Google Safebrowsing Datenbanken verwendet werden. So werden etwaige Clients vor dem Download vor schädlichen Dateien oder vor dem Öffnen schädlicher bzw. betrügerischer Webseiten gewahrt.

Weitere Details unter:

Modifizierung von Headern

Mit Hilfe der Direktiven request_header_replace und request_header_access können die Header eines Client-Requests manipuliert werden.

Bsp.:

request_header_replace User-Agent Mozilla/2.02Gold (Win95; I)

request_header_access Referer deny all
request_header_access Authorization allow all
request_header_access Proxy-Authorization allow all
request_header_access Cache-Control allow all
request_header_access All deny all

Authentifizierung

Es ist möglich, dass ein Nutzer authentifiziert wird. Dabei kann dies mittels klassischem Basic Auth erfolgen, oder auch via OAuth, und sogar Negotiate, einem von Microsoft entwickelten Wrapper zum Wrapper für Kerberos und NTLM.

Nachfolgend ein Beispiel für Basic Auth mit einer lokalen Datei als Datenbank:

printf "<username>:$(openssl passwd -crypt '<password>')\n" | sudo tee -a /etc/squid/httpauth
auth_param basic program /usr/lib/squid3/basic_ncsa_auth /etc/squid/htpasswd
auth_param basic realm proxy
acl my_auth proxy_auth REQUIRED
acl google_users proxy_auth user1 user2 user3
acl google dstdomain .google.com
http_access deny google !google_users
http_access allow my_auth
http_access deny all

Konfiguration

Konfigurationsdatei

Die Konfigurationsdatei für Squid ist unter /etc/squid/squid.conf zu finden. Es können jedoch weitere Konfigurationsdateien mittels 'include' geladenw erden, so bspw. alle Dateien in /etc/squid/squid.conf/conf.d/.

Beispielkonfiguration

Bei einer Neuinstallation hat man bereits eine Beispielkonfiguration vorliegen. Diese sieht zusammengefasst in etwa so aus, wobei ca. 9.100 Zeilen an Kommentare entfernt wurden:

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 0.0.0.1-0.255.255.255  # RFC 1122 "this" network (LAN)
acl localnet src 10.0.0.0/8     # RFC 1918 local private network (LAN)
acl localnet src 100.64.0.0/10      # RFC 6598 shared address space (CGN)
acl localnet src 169.254.0.0/16     # RFC 3927 link-local (directly plugged) machines
acl localnet src 172.16.0.0/12      # RFC 1918 local private network (LAN)
acl localnet src 192.168.0.0/16     # RFC 1918 local private network (LAN)
acl localnet src fc00::/7           # RFC 4193 local private network range
acl localnet src fe80::/10          # RFC 4291 link-local (directly plugged) machines
acl SSL_ports port 443
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

#
# Recommended minimum Access Permission configuration:
#

# Deny requests to certain unsafe ports
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
http_access deny CONNECT !SSL_ports

# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
include /etc/squid/conf.d/*.conf

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
#http_access allow localnet
http_access allow localhost

# And finally deny all other access to this proxy
http_access deny all

# Squid normally listens to port 3128
http_port 3128

# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid

#
# Add any of your own refresh_pattern entries above these.
#
refresh_pattern ^ftp:       1440    20% 10080
refresh_pattern ^gopher:    1440    0%  1440
refresh_pattern -i (/cgi-bin/|\?) 0 0%  0
refresh_pattern .       0   20% 4320