{"id":2316,"date":"2013-07-15T15:29:35","date_gmt":"2013-07-15T07:29:35","guid":{"rendered":"http:\/\/rmohan.com\/?p=2316"},"modified":"2013-07-15T15:57:56","modified_gmt":"2013-07-15T07:57:56","slug":"proxy-server-with-squid-dansguardian-on-centos-and-cbq","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=2316","title":{"rendered":"Proxy server with Squid, Dansguardian on CentOS and CBQ"},"content":{"rendered":"<p>The advantages of using a proxy server within a company, or even a small network are endless, ranging from bandwidth savings, using the cache to a decent content filter.<br \/>\nIn this tutorial I will show how to configure a proxy using CentOS.<br \/>\nResources used<\/p>\n<p><strong>Hardware<\/strong><br \/>\nIn our specific case, I was fortunate to have a fine machine Parruda to implement the proxy, one Xeon Quad with 4Gb Ram and SAS disks. Recalling that the server has to have at least two network interfaces.<br \/>\nSystem<br \/>\nWe use CentOS 5.7 64bit. Reached our needs very well.<\/p>\n<p><strong>Squid<\/strong><br \/>\nWe use Squid to control users and cache.<br \/>\nDansguardian<br \/>\nWhat is our dansguardian content filter, the great advantage of it&#8217;s own filters by heuristics, which blocks sites based on words within pages.<br \/>\nIptables<br \/>\nUse iptables to control the IPs of servers that do not pass through squid \/ dansguardian<br \/>\nCBQ<br \/>\nWe bandwidth control using the CBQ, existing on CentOS.<br \/>\nInstallation<br \/>\nInstalling the System<br \/>\nDownload here  the latest version of CentOS, always remember to look for a server in Brazil, oo close as possible, there are more chances of having a higher speed. Give preference to the DVD too.<br \/>\nConfigure network interfaces, one pointing to the external network (in our case eth0) and one for the internal (eth1). Edit the resolv.conf for your DNS server. If the server is resolving names normally, we will proceed to the next step.<br \/>\nInstalling Squid<br \/>\nNo mystery here, squid already in the repository for CentOS:<br \/>\nyum install squid<br \/>\nInstalling Dansguardian<br \/>\nThe Dansguardian is not the repository of CentOS, portanta have to compile it on hand.<br \/>\nBefore installing dansguardian, treat some dependencies:<br \/>\nyum install pkgconfig gcc gcc &#8211; c + + libstdc + + &#8211; devel zlib &#8211; devel pcre &#8211; make devel wget bzip2 &#8211; devel<br \/>\nYou may need to include the environment variable pkgconfig:<br \/>\nexport PKG_CONFIG_PATH = \/ usr \/ lib \/ pkgconfig<br \/>\nDownload the latest version here , unzip it on your server and compile:<br \/>\ntar xzfv dansguardian &#8211; 2:10 . 1.1 . tar . gz<br \/>\ncd dansguardian &#8211; 2:10 . 1.1<br \/>\n.\/configure<br \/>\nmake<br \/>\nmake install<br \/>\nConfiguring Squid<br \/>\nBelow is my Squid configuration (\/etc\/squid\/squid.conf). Already prepared for basic authentication of squid directed to file (\/etc\/squid\/passwd). And with the cache settings (\/var\/squid\/cache).<br \/>\n# NAME<br \/>\nvisible_hostname name rmohan.com<\/p>\n<p>PORT #<br \/>\nhttp_port 3128 transparent<\/p>\n<p># CACHE MEMORY<br \/>\ncache_mem 1024 MB<br \/>\nmaximum_object_size_in_memory 20 KB<br \/>\nmemory_replacement_policy heap GDSF<\/p>\n<p>DISCK CACHE #<br \/>\ncache_dir ufs \/var\/squid\/cache 5000  16  256<br \/>\ncache_replacement_policy heap LFUDA<br \/>\nminimum_object_size 15 KB<br \/>\nmaximum_object_size 5 MB<br \/>\ncache_swap_low 90<br \/>\ncache_swap_high 95<\/p>\n<p># LOG<br \/>\naccess_log none<br \/>\ncache_log \/var\/log\/squid\/cache.log<\/p>\n<p>AUTH #<br \/>\nauth_param basic program \/ ??usr \/ lib64 \/ squid \/ ncsa_auth \/ etc \/ squid \/ passwd<br \/>\nauth_param basic children 8<br \/>\nauth_param basic realm Enter your Usu will river and Password<br \/>\nauth_param basic credentialsttl 15 minute<\/p>\n<p># ACLs<br \/>\nacl all src 0.0 . 0.0 \/ 0.0 . 0.0<br \/>\nacl manager proto cache_object<br \/>\nacl localhost src 127.0 . 0.1 \/ 255,255 . 255,255<br \/>\nacl dst to_localhost 127.0 . 0.0 \/ 8<br \/>\nacl SSL_ports port 443<br \/>\nacl Safe_ports port 80  # http<br \/>\nacl Safe_ports port 21  # ftp<br \/>\nacl Safe_ports port 443  # https<br \/>\nacl Safe_ports port 70  # gopher<br \/>\nacl Safe_ports port 210  # wais<br \/>\nacl Safe_ports port 1025 &#8211; 65535  # unregistered ports<br \/>\nacl Safe_ports port 280  # http-mgmt<br \/>\nacl Safe_ports port 488  # gss-http<br \/>\nacl Safe_ports port 591  # filemaker<br \/>\nacl Safe_ports port 777  # http multiling<br \/>\nacl CONNECT method CONNECT<br \/>\nacl-time team SMTWHFA 0 : 00 &#8211; 24 : 00<br \/>\nREQUIRED acl authenticated proxy_auth<\/p>\n<p># HTTP_ACCESES<br \/>\nhttp_access allow authenticated hours<br \/>\nhttp_access allow manager localhost<br \/>\nhttp_access deny manager<br \/>\nhttp_access deny ! Safe_ports<br \/>\nhttp_access deny CONNECT ! SSL_ports<br \/>\n # And finally deny all other access to this proxy<br \/>\nhttp_access allow localhost<br \/>\nhttp_access deny all<br \/>\nicp_access allow all<\/p>\n<p># OTHERS<br \/>\nhierarchy_stoplist cgi &#8211; bin ?<br \/>\naccess_log \/var\/log\/squid\/access . squid log<br \/>\nacl QUERY urlpath_regex cgi &#8211; bin \\?<br \/>\ncache deny QUERY<br \/>\nrefresh_pattern ^ ftp :  in 1440  20 %  10080<br \/>\nrefresh_pattern ^ gopher :  1440  0 %  1440<br \/>\nrefresh_pattern .  0  20 %  4320<br \/>\nacl apache rep_header Server  ^ Apache<br \/>\nbroken_vary_encoding allow apache<br \/>\ncoredump_dir \/ var \/ spool \/ squid<br \/>\nTo start the squid first run the command squid-z , so that it creates cache files, in other times simply call the command squid .<br \/>\nConfiguring Dansguardian<br \/>\nFirst we will update the list of blocked sites, visit this site ( URLBlacklist.com ) and download the newest version. Unzip into \/ usr \/ local \/ etc \/ dansguardian \/ lists \/ blacklists.<br \/>\nLet&#8217;s edit the configuration file Dansguardian (\/ usr \/ local \/ etc \/ dansguardian \/ dansguardian.conf)<br \/>\nREPORT #<br \/>\nreportinglevel =  3<\/p>\n<p># LANGUAGE<br \/>\nlanguagedir =  &#8216;\/ usr \/ local \/ share \/ dansguardian \/ languages&#8217;<br \/>\nlanguage =  &#8216;ptbrazilian&#8217;<\/p>\n<p># Logs<br \/>\nloglevel =  3<br \/>\nlogexceptionhits =  2<br \/>\nlogfileformat =  3<\/p>\n<p>LogLocation =  &#8216;\/ usr \/ local \/ var \/ log \/ dansguardian \/ access.log&#8217;<\/p>\n<p># Network Settings<br \/>\nfilterip =<br \/>\nfilterport =  8080<br \/>\nproxyip =  127.0 . 0.1<br \/>\nproxyPort =  3128<\/p>\n<p>nonstandarddelimiter = on<\/p>\n<p># LOCKED IMAGES<br \/>\nusecustombannedimage = on<br \/>\ncustombannedimagefile =  &#8216;\/ usr\/local\/share\/dansguardian\/transparent1x1.gif&#8217;<\/p>\n<p># Filter groups options<br \/>\nfiltergroups =  1<br \/>\nfiltergroupslist =  &#8216;\/ usr \/ local \/ etc \/ dansguardian \/ lists \/ filtergroupslist&#8217;<\/p>\n<p># Authentication files  <\/p>\n<p># WORDS &#8211; WEIGHT<br \/>\nshowweightedfound = on<br \/>\nweightedphrasemode =  2<\/p>\n<p># Positive (clean) result caching for URLs<br \/>\nurlcachenumber =  1000<\/p>\n<p># Age Before They are stale and Should be ignored in seconds<br \/>\nurlcacheage =  900<\/p>\n<p># Clean cache for content (AV) scan results<br \/>\nscancleancache = on<\/p>\n<p># Smart, Raw and Meta \/ Title phrase content filtering options<br \/>\nphrasefiltermode =  2<\/p>\n<p># Lower casing options \/ 0 = force lower case (default)<br \/>\npreservecase =  0<\/p>\n<p># Hex decoding options<br \/>\nhexdecodecontent = off<\/p>\n<p># Force Quick Search rather than DFA search algorithm<br \/>\nforcequicksearch = off<\/p>\n<p># Reverse lookups for banned site and URLs<br \/>\nreverseaddresslookups = off<\/p>\n<p># Reverse lookups for banned and exception IP lists.<br \/>\nreverseclientiplookups = off<\/p>\n<p># Perform reverse lookups on client IPs for successful requests.<br \/>\nlogclienthostnames = off<\/p>\n<p># Build bannedsitelist bannedurllist and cache files.<br \/>\ncreatelistcachefiles = on<\/p>\n<p># POST protection (web upload and forms)<br \/>\nmaxuploadsize =  &#8211; 1<\/p>\n<p># Max content filter size<br \/>\nmaxcontentfiltersize =  256<\/p>\n<p># Max content ram cache scan size<br \/>\nmaxcontentramcachescansize =  2000<\/p>\n<p># Max content file cache scan size<br \/>\nmaxcontentfilecachescansize =  20000<\/p>\n<p># File cache dir<br \/>\nfilecachedir =  &#8216;\/ tmp&#8217;<\/p>\n<p># Delete file cache after user completes download<br \/>\ndeletedownloadedtempfiles = on<\/p>\n<p># Initial Trickle delay<br \/>\ninitialtrickledelay =  20<\/p>\n<p># Trickle delay<br \/>\ntrickledelay =  10<\/p>\n<p># Download  <\/p>\n<p># Content scanner timeout<br \/>\ncontentscannertimeout =  60<\/p>\n<p># Content scan exceptions<br \/>\ncontentscanexceptions = off<\/p>\n<p># Auth  <\/p>\n<p># Re-check URLs Replaced<br \/>\nrecheckreplacedurls = off<\/p>\n<p># Misc settings<br \/>\nforwardedfor = off<br \/>\nusexforwardedfor = off<br \/>\nlogconnectionhandlingerrors = on<\/p>\n<p># Fork pool options<br \/>\nlogchildprocesshandling = off<br \/>\nmaxchildren =  120<br \/>\nminchildren =  8<br \/>\nminsparechildren =  4<br \/>\npreforkchildren =  6<br \/>\nmaxsparechildren =  32<br \/>\nmaxagechildren =  500<\/p>\n<p># Sets the maximum number client IP addresses allowed to connect at once.<br \/>\nmaxips =  0<\/p>\n<p># IPC filename<br \/>\nipcfilename =  &#8216;\/ tmp \/ .dguardianipc&#8217;<\/p>\n<p># URL list IPC filename<br \/>\nurlipcfilename =  &#8216;\/ tmp \/ .dguardianurlipc&#8217;<\/p>\n<p># IP list IPC filename<br \/>\nipipcfilename =  &#8216;\/ tmp \/ .dguardianipipc&#8217;<\/p>\n<p># PID filename<br \/>\nnodaemon = off<\/p>\n<p># Disable logging process<br \/>\nnologger = off<\/p>\n<p># Enable logging of &#8220;ADs&#8221; category blocks<br \/>\nlogadblocks = on<\/p>\n<p># Enable logging of client User-Agent<br \/>\nloguseragent = off<\/p>\n<p># Soft restart<br \/>\nsoftrestart = off<br \/>\nTwo items that are interesting in this configuration are the access log file (\/ usr \/ local \/ var \/ log \/ dansguardian \/ access.log) and editable HTML page locked (\/ usr \/ local \/ share \/ dansguardian \/ languages) that can be customized.<br \/>\nTo start simply call the command dansguardian  dansguardian , and to recharge the rules:  dansguardian-r.<br \/>\nIptables<br \/>\nFor iptables created a scrip to load the settings, because the rules this should be loaded when the servodor starts.<br \/>\nIn this script:<br \/>\n&#8211; Oblige all incoming connections going to port 8080 (dansguardian);<br \/>\n&#8211; Libero IP network to &#8216;leak&#8217; so do not fall in dansguardian and squid, ideal for network servers;<br \/>\n&#8211; Blocking Ips;<br \/>\n&#8211; Blocking against Ping of Death and DoS;<br \/>\nSave this file with the name  iptables-load , for example and call the sh iptables-load<br \/>\n#! \/ Bin \/ bash<\/p>\n<p># Remaining iptables<br \/>\n\/ etc \/ init . d \/ iptables restart<\/p>\n<p># FUNCTIONS<br \/>\nlibera_ip () {<br \/>\niptables &#8211; t nat &#8211; I PREROUTING &#8211; s $ 1 &#8211; j ACCEPT<br \/>\niptables &#8211; t nat &#8211; I POSTROUTING &#8211; s $ 1 &#8211; eth0 &#8211; j MASQUERADE<br \/>\niptables &#8211; I FORWARD &#8211; s $ 1 &#8211; j ACCEPT<br \/>\n }<\/p>\n<p>bloqueia_ip () {<br \/>\niptables &#8211; A INPUT &#8211; s $ 1 &#8211; j DROP<br \/>\n }<\/p>\n<p># # # # # # # # # # # # # # # # #<\/p>\n<p>BLOCK IPs #<br \/>\n# bloqueia_ip &#8220;192.168.0.199&#8221;<\/p>\n<p># BEARS MODULOS IPTABLES<br \/>\necho 1  &#038; gt ;  \/ proc \/ sys \/ net \/ ipv4 \/ ip_forward<br \/>\nmodprobe iptable_nat<\/p>\n<p># BLOCKS AGAINST DEATH AND PING DoS<br \/>\niptables &#8211; A INPUT &#8211; p icmp &#8211; icmp &#8211; type echo &#8211; request &#8211; m limit &#8211; limit 1 \/ s &#8211; j ACCEPT<br \/>\niptables &#8211; A INPUT &#8211; p icmp &#8211; icmp &#8211; type echo &#8211; reply &#8211; m limit &#8211; limit 1 \/ s &#8211; j DROP<\/p>\n<p># OPEN CONNECTION TO A LOCAL AREA NETWORK AT THE DOOR 8080<br \/>\niptables &#8211; A INPUT &#8211; i eth1 &#8211; p tcp &#8211; dport 8080  &#8211; j ACCEPT<\/p>\n<p># RELEASE FOR NAVIGATION WITHOUT PROXY SERVERS<br \/>\nlibera_ip &#8220;192.168.0.1&#8221;  # SERV1<br \/>\nlibera_ip &#8220;192.168.0.2&#8221;  # Serv2<\/p>\n<p># Ends<br \/>\necho &#8220;Iptables Ready&#8221;<br \/>\nCBQ<br \/>\nWith CBQ do bandwidth control network, first go to the \/ etc \/ sysconfig \/ cbq. For each rule you create a file down and another up. EX:  cbq 0002.geral-in- and  -cbq 0002.geral out.<br \/>\nFor each rule starts numbering from 0002, and continue, noting that this numbering is in hexadecimal.<br \/>\nSee my examples:<br \/>\ncbq-0002.geral-in<br \/>\nDEVICE = eth1 , 1000Mbit , 100Mbit<br \/>\nRATE = 2Mb<br \/>\nWEIGHT = 200Kbit<br \/>\nPRIO = 5<br \/>\nRULE = 192.168 . 0.0<br \/>\nBounded = in<br \/>\nISOLATED = in<br \/>\n0002.geral cbq-out<br \/>\nDEVICE = eth0 , 1000Mbit , 100Mbit<br \/>\nRATE = 100Kbit<br \/>\nWEIGHT = 10Kbit<br \/>\nPRIO = 5<br \/>\nRULE = 192.168 . 0.0 ,<br \/>\nBounded = in<br \/>\nISOLATED = in<br \/>\nCompile the CBQ with the command cbq compile and start with cbd start .<br \/>\nSee this tutorial more information about the CBQ.<br \/>\nStarting all<br \/>\nFinally created a scrip to start all the necessary services, just to facilitate:<br \/>\n#! \/ Bin \/ bash<\/p>\n<p>squid<br \/>\necho &#8220;starting squid&#8221;<\/p>\n<p>dansguardian<br \/>\necho &#8220;Starting dansguardian&#8221;<\/p>\n<p>sh \/ iptables &#8211; load<br \/>\necho &#8220;iptables loading&#8221;<\/p>\n<p>cbq compile<br \/>\necho &#8220;compiling cbq&#8221;<\/p>\n<p>cbq start<br \/>\necho &#8220;starting cbq&#8221;<\/p>\n<p>echo &#8220;READY&#8221;<br \/>\nAdditional<br \/>\nTo facilitate the administration of the server, I recommend installing webmin (a web interface facing server administration, now with the Squid module installed, great for keeping the users) and sarg with the  webalizer. Reports To log dansguardian .<br \/>\nFinishing<br \/>\nI hope this article is helpful, of course does not answer all questions, and each installation will have their particular problems, but hopefully it will be a base for administrators. Leave your comments with questions and suggestions.<br \/>\nThanks for visiting and sharing this post!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The advantages of using a proxy server within a company, or even a small network are endless, ranging from bandwidth savings, using the cache to a decent content filter. In this tutorial I will show how to configure a proxy using CentOS. Resources used<\/p>\n<p>Hardware In our specific case, I was fortunate to have a [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2316"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2316"}],"version-history":[{"count":16,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2316\/revisions"}],"predecessor-version":[{"id":2332,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2316\/revisions\/2332"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}