{"id":254,"date":"2012-09-06T19:24:47","date_gmt":"2012-09-06T23:24:47","guid":{"rendered":"https:\/\/infotechguy.net\/?p=254"},"modified":"2025-02-22T13:00:18","modified_gmt":"2025-02-22T18:00:18","slug":"network-adblocking-using-squid-squidguard-and-iptables","status":"publish","type":"post","link":"https:\/\/infotechguy.net\/?p=254","title":{"rendered":"Linux &#8212; SquidProxy Network Adblocking using Squid1.4"},"content":{"rendered":"<p>I originally discovered <a href=\"https:\/\/addons.mozilla.org\/en-US\/firefox\/addon\/adblock-plus\/\" target=\"_blank\" rel=\"noopener noreferrer\">Adblock Plus<\/a> when I first downloaded Firefox many years ago. Since then I&#8217;ve installed the Adblock plugin right after Firefox, etc. It&#8217;s become so standard that I almost think Firefox should just bundle them together. Including it in it&#8217;s default install exe.<\/p>\n<p>Adblock Plus works as if it were a local content policy,\u00a0 filtering each request you make with Firefox. Each URL, each domain, each link you navigate to is check based on a static blacklist of expressions and URLs. If a match is found, Adblock Plus simply discards the content from rendering. The discarding and allowing content to load is managed by the Content Policy engine within Firefox. Adblock Plus simply utilizes this in order to block the unwanted contents. Or at least this is my comprehension of how it works. :-p<\/p>\n<h3>Setting up your own Network wide Adblocker<\/h3>\n<p>The purpose of this guide and tutorial is to instruct you on how to set up your own network based adblocker. Expections after completion is every client browser on the network will benefit from adblocking. I will include as much as possible, and feel free to ping me with questions or comment down below.<\/p>\n<p>You will need:<\/p>\n<ol>\n<li>Computer that will be running the Web Proxy. (For this article, see specs below)<\/li>\n<li>OS that will host the Proxy Software. (For this article, <a href=\"http:\/\/www.ubuntu.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Ubuntu<\/a> 12.04 32-bit Server)<\/li>\n<li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Proxy_server\" target=\"_blank\" rel=\"noopener noreferrer\">Proxy software<\/a> that allows rewrite engines\/programs. (<a href=\"http:\/\/www.squidguard.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">squidGuard<\/a>)<\/li>\n<li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Content-control_software\" target=\"_blank\" rel=\"noopener noreferrer\">Content-Control-Software<\/a> or URL Redirect Application(This will consume your blacklists)<\/li>\n<li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Blacklist_%28computing%29\" target=\"_blank\" rel=\"noopener noreferrer\">URL and RegExp Blacklists<\/a> consumable by your Content-Control-Software (<a href=\"http:\/\/www.squidguard.org\/blacklists.html\" target=\"_blank\" rel=\"noopener noreferrer\">Here<\/a> are some free ones)<\/li>\n<li>Optional: ipTables for transparent proxy redirection<\/li>\n<li>Patients and enthusiasm :-p<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<h4><strong>Step 1: Installling OS and software<\/strong><\/h4>\n<ol>\n<li>Spec&#8217;ing out your hardware. Keep in mind your sizing and scope of clients you will be serving<br \/>\n<em>For this tutorial I used the following:<br \/>\n&#8211;Dual Core -Intel(R) Xeon(TM) CPU 2.66GHz 32-bit<br \/>\n&#8211;4GB DDR RAM<br \/>\n&#8211;320GB SATA HD<br \/>\n&#8211;Dual 10\/100\/1000 NIC<\/em><\/li>\n<li>Download a ISO copy of Ubuntu or Debian.<\/li>\n<li>Install Ubuntu, for tutorial see <a href=\"http:\/\/ubuntuserverguide.com\/2012\/05\/how-to-install-ubuntu-server-12-04-lts-precise-pangolin-included-screenshot.html\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/li>\n<li>Make sure you are a sudo users or have root access, needed for the next steps.<\/li>\n<li>After Install update your system to the latest via\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo apt-get update &amp;&amp; sudo apt-get upgrade -y<\/pre>\n<\/li>\n<li>Next install squidProxy\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo apt-get install squid3 -y<\/pre>\n<\/li>\n<li>Install squidGuard from the repositories\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo apt-get install squidGuard -y<\/pre>\n<\/li>\n<li>Setting static IP address on the server. Do a <code>ifconfig eth0<\/code> to figure out what your current IP is. Mine was <strong>192.168.0.113<\/strong>. We will assume this is a \/24 network, so pick a lower number for the last octet. I chose <strong> 192.168.0.5<\/strong>. Reason for this is out side the scope of this article. Perform the static ip address config.<br \/>\nAdd the following in<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo vi \/etc\/network\/interfaces \r\n\r\nauto eth1 \r\niface eth1 inet static \r\n      address 192.168.0.5 \r\n      netmask 255.255.255.0<\/pre>\n<\/li>\n<li>Restart network interfaces. Make sure you are local, you will lose remote access!!.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo \/etc\/init.d\/networking restart<\/pre>\n<\/li>\n<li>Check <code>ifconfig eth0<\/code> you should have <strong>192.168.0.5<\/strong>(or the IP you picked).<\/li>\n<\/ol>\n<p>***The base installation is now complete and we are now ready to configure our service applications!<\/p>\n<h4><strong>Step 2: Configuring squidProxy<\/strong><\/h4>\n<ol>\n<li>Make a copy of your default config file.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo cp \/etc\/squid3\/squid.conf \/etc\/squid3\/squid.conf.bak<\/pre>\n<\/li>\n<li>Null the squid.conf file.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo su - cat \/dev\/null &gt; \/etc\/squid3\/squid.conf<\/pre>\n<\/li>\n<li>Edit the squid.conf vith vi and paste the following lines.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo vi \/etc\/squid3\/squid.conf\r\n#acl lists\r\nacl manager proto cache_object\r\nacl localhost src 127.0.0.1\/32 ::1\r\nacl to_localhost dst 127.0.0.0\/8 0.0.0.0\/32 ::1\r\nacl localnet src 192.168.0.0\/24 # RFC1918 possible internal network\r\n\r\n#port connections\r\nacl SSL_ports port 443\r\nacl SSL method CONNECT\r\nacl Safe_ports port 80          # http\r\nacl Safe_ports port 21          # ftp\r\nacl Safe_ports port 443         # https\r\nacl Safe_ports port 70          # gopher\r\nacl Safe_ports port 210         # wais\r\nacl Safe_ports port 1025-65535  # unregistered ports\r\nacl Safe_ports port 280         # http-mgmt\r\nacl Safe_ports port 488         # gss-http\r\nacl Safe_ports port 591         # filemaker\r\nacl Safe_ports port 777         # multiling http\r\nacl CONNECT method CONNECT\r\n\r\n#allow\/deny\r\nhttp_access allow manager localhost\r\nhttp_access deny manager\r\nhttp_access allow localnet\r\n\r\n# Deny requests to certain unsafe ports\r\nhttp_access deny !Safe_ports\r\n\r\n# Deny CONNECT to other than secure SSL ports\r\nhttp_access deny CONNECT !SSL_ports\r\n\r\n# Example rule allowing access from your local networks.\r\nhttp_access allow localnet\r\nhttp_access allow localhost\r\n\r\n# And finally deny all other access to this proxy\r\nhttp_access deny all\r\n\r\n#bind address default port is 3128\r\nhttp_port 192.168.0.5:8080\r\n\r\n#cache directory\r\ncache_dir ufs \/home\/serveruser\/squidcache\/ 512 16 128\r\ncache_mem 2048MB\r\n#coredump_dir \/home\/serveruser\/squidcache\/\r\n\r\n#log\r\ncache_store_log \/var\/log\/squid3\/store.log\r\n<\/pre>\n<\/li>\n<li>Next start the service up.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo service squid3 restart<\/pre>\n<\/li>\n<li>If you encounter any issues or error message, check the syslog\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">less \/var\/log\/syslog<\/pre>\n<\/li>\n<li>TEST with Firefox. Go into Firefox settings &#8211;&gt; Connection Settings. Enter in the IP address and Port of your proxy server. Similar to this <a href=\"http:\/\/s6.postimg.org\/55zk80fi9\/Screenshot_from_2012_09_06_190815.png\"><img decoding=\"async\" class=\"aligncenter wp-image-284\" title=\"Firefox Connection Settings\" src=\"https:\/\/s6.postimg.org\/55zk80fi9\/Screenshot_from_2012_09_06_190815.png\" alt=\"\" \/><\/a> Try to browse to some sites like Google, MSN, Facebook, etc. Ensure you can get to them. Next, to make this a thorough test and ensure you are connecting through the proxy. Go back to your Proxy server&#8217;s terminal. Enter the following to turn off the proxy service.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo service squid3 stop<\/pre>\n<p>After that completes try to navigate and browse on the client machines. You should get an error.<\/li>\n<\/ol>\n<p>***Congratulations you have successfully set up your first Proxy Server with squidProxy!!!<\/p>\n<h4><strong>Step 3: Configuring squidGuard and Blacklists<\/strong><\/h4>\n<ol>\n<li>Make a backup of the squidGuard config file.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo cp \/etc\/squid3\/squidGuard.conf \/etc\/squid3\/squidGuard.conf.bak<\/pre>\n<\/li>\n<li>Grab a blacklist from <a href=\"http:\/\/www.squidguard.org\/blacklists.html\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cd ~ &amp;&amp; wget http:\/\/squidguard.mesd.k12.or.us\/blacklists.tgz<\/pre>\n<\/li>\n<li>Uncompress.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">tar xzvf blacklists.tgz<\/pre>\n<\/li>\n<li>This Uncompresses the folder structure for all the blacklists categories, i.e. Ads, Porn, Gambling, etc. We are only concerned with Ads, so grab it&#8217;s path.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cd blacklists\/ads\/ \r\npwd<\/pre>\n<p>COPY this path, need it for later.<\/li>\n<li>We have to make these blacklists readable by the user that squid runs as.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cd ~ &amp;&amp; sudo chown -R proxy.proxy blacklists \r\nsudo chmod -R 750 blacklists<\/pre>\n<p>I had to use the permission bits of 750, it was the only permissions that would allow squid to read the blacklists. I put a 0 at the end because I don&#8217;t want the list readable by any other users on the system.<\/li>\n<li>Now time to edit the squidGuard.conf file.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo vi \/etc\/squid3\/squidGuard.conf\r\ndbhome \/home\/serveruser\/squidGuard\r\nlogdir \/var\/log\/squid3\r\n#create ads category\r\ndest ads {\r\n        #location of blacklists, domains, urls, expressions. \r\n        domainlist blacklists\/ads\/domains\r\n        urllist blacklists\/ads\/urls\r\n        expressionlist blacklists\/ads\/expressions\r\n}\r\nacl {\r\n        default {\r\n                #allow except 'ads'\r\n                pass !ads all\r\n                #redirect to transparent gif\r\n                redirect http:\/\/localhost\/blank.gif\r\n\r\n        }\r\n}<\/pre>\n<\/li>\n<li>Now add the squidGuard specific&#8217;s to the squid.conf file, so the main squid process is aware of squidGuard. Add these lines to the end of your squid.conf file.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo vi \/etc\/squid3\/squid.conf \r\n\r\n#rewrite program squidGuard \r\nurl_rewrite_program \/usr\/bin\/squidGuard -c \/etc\/squid3\/squidGuard.conf\r\nurl_rewrite_children 5 #threads \r\nurl_rewrite_concurrency 0 #jobs per threads<\/pre>\n<\/li>\n<li>Initialize the squidGuard database files, to consume the blacklists you just downloaded. Needs to be done everytime you update the list.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo squidGuard -C all<\/pre>\n<p>Should be fairly quick, if it hangs, squidGuard probably cannot read the blacklist directory. Check your syslog.<\/li>\n<li>For the next step I recommend having two terminal windows open to your proxy server. This will make it easier to tail the logs when you try to start it for the first time.<\/li>\n<li>Restart squid3 to pick up the new configuration items(i.e. squidGuard) Make sure you tail your syslog with the other window.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo service squid3 restart<\/pre>\n<p>In your squidGuard log you should see the following lines:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">less \/var\/log\/squid3\/squidGuard \r\nsquidGuard 1.4 started \r\nsquidGuard ready for requests<\/pre>\n<p>You will also see in the log how it loaded the dbfiles you defined in your squidGuard.conf file.<\/li>\n<li>Testing. Ensure your client&#8217;s firefox still has Connection Settings pointing at your proxy server. Now browse to a website that has a lot of ads. I suggest p2p sites, usually utilize a crap load of ads.<\/li>\n<\/ol>\n<p>***Congratulations you have protected your network from ADs!!!<\/p>\n<h4><strong>Optional Step 4: Transparent Proxy Redirection via iptables (optional)<\/strong><\/h4>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>This next section assumes you have a working IPTables setup, with a Router\/Firewall at <strong>192.168.0.1<\/strong> and a Proxy Server at <strong>192.168.0.5<\/strong><\/li>\n<li>Need to add two statements.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo iptables -A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.0.5:8080<\/pre>\n<\/li>\n<li>***NOTICE We are only specifying port 80, not 443!<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>Port 443 bypasses the proxy as we are not doing SSL Interception.<\/p>\n<ul>\n<li>Now we must tell squidProxy about the transparency. Find the line <strong>http_port<\/strong> and add <strong>transparent<\/strong> to the end of it.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">http_port 192.168.0.5:8080 intercept<\/pre>\n<\/li>\n<li>Remove Firefox proxy settings in Connection Settings.<br \/>\n<a href=\"https:\/\/infotechguy.net\/wp-content\/uploads\/2021\/03\/Screenshot-from-2012-09-06-190815.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3578\" src=\"https:\/\/infotechguy.net\/wp-content\/uploads\/2021\/03\/Screenshot-from-2012-09-06-190815-297x300.png\" alt=\"\" width=\"599\" height=\"605\" srcset=\"https:\/\/infotechguy.net\/wp-content\/uploads\/2021\/03\/Screenshot-from-2012-09-06-190815-297x300.png 297w, https:\/\/infotechguy.net\/wp-content\/uploads\/2021\/03\/Screenshot-from-2012-09-06-190815.png 546w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/a><\/li>\n<\/ul>\n<p>***Congrats you are now transparently redirecting all port 80 calls to your proxy server.<br \/>\nSources:<\/p>\n<ul>\n<li>Adblocker FAQs <a href=\"http:\/\/adblockplus.org\/en\/faq_internal\">http:\/\/adblockplus.org\/en\/faq_internal<\/a><\/li>\n<li>Ubuntu Server Installation Guide<a href=\"http:\/\/ubuntuserverguide.com\/2012\/05\/how-to-install-ubuntu-server-12-04-lts-precise-pangolin-included-screenshot.html\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/ubuntuserverguide.com\/2012\/05\/how-to-install-ubuntu-server-12-04-lts-precise-pangolin-included-screenshot.html<\/a><\/li>\n<li>Installing Squid <a href=\"http:\/\/www.server-world.info\/en\/note?os=Ubuntu_12.04&amp;p=squid&amp;f=3\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.server-world.info\/en\/note?os=Ubuntu_12.04&amp;p=squid&amp;f=3<\/a><\/li>\n<li>squidGuard Config site <a href=\"http:\/\/www.squidguard.org\/Doc\/configure.html\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.squidguard.org\/Doc\/configure.html<\/a><\/li>\n<li>squidGuard Examples <a href=\"http:\/\/www.squidguard.org\/Doc\/examples.html\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.squidguard.org\/Doc\/examples.html<\/a><\/li>\n<li>Ubuntu Specific squidGuard Config <a href=\"https:\/\/help.ubuntu.com\/community\/SquidGuard\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/help.ubuntu.com\/community\/SquidGuard<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I originally discovered Adblock Plus when I first downloaded Firefox many years ago. Since then I&#8217;ve installed the Adblock plugin right after Firefox, etc. It&#8217;s become so standard that I almost think Firefox should just&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":4240,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[112,139],"class_list":["post-254","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-security-2","tag-squidproxy"],"_links":{"self":[{"href":"https:\/\/infotechguy.net\/index.php?rest_route=\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/infotechguy.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/infotechguy.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/infotechguy.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/infotechguy.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=254"}],"version-history":[{"count":3,"href":"https:\/\/infotechguy.net\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":4264,"href":"https:\/\/infotechguy.net\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions\/4264"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/infotechguy.net\/index.php?rest_route=\/wp\/v2\/media\/4240"}],"wp:attachment":[{"href":"https:\/\/infotechguy.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/infotechguy.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/infotechguy.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}