Generating ssl certificates for squid transparent proxying, named (Http/HTTPS) on a centOS 6.6system

Introduction:

squid is set up between browsers and servers, retrieving documents from servers for browsers. It is also created to accelerate caching and serving them to clients. It can filter pop-up ads and malware as well as to enforce access control (which clients may request). Proxying can be an optional component or browsers can be configured to actively make use of the proxy. Transparent proxying is passing data traffic over the proxy from the host server to the clients without the clients knowledge.

Because the destination address is translated for such connections, the proxy needs to operate behind a firewall. The squid proxy also needs some option to find the intended web-server’s destination address. Older clients don’t display a Host: header, whereby squid can query the packet filter original destination address in the redirected connection.   The next instructions describe how you can configure squid and a firewall itself, while generating ssl certificates.

STEP 1 – Installing Base system , upgrading it and disabling IPtables , SeLINUX

Download tthe  CentOS  6.5  iso from CentOS website (x86 or x64) : CentOS 6.5 ISO’s , install no more packages other than Base system. . If you are using squid, it is better to create a hostname instead of the normal “localhost” we will use the following information as examples :

hostname: proxy.home.lan

 ip address is : 192.168.201.250 .

chkconfig iptables off
chkconfig ip6tables off

vi /etc/selinux/config

Press i , look for SELINUX= line and change it  to  SELINUX=disabled

Once the system is rebooted , type into console :

yum upgrade

STEP 2 – Enable EPEL repo, install SQUID

CentOS provided SQUID does not have ssl_crtd for some reason, thus it  cannot  be configured to generate certificates for https sites.Type the following into console :

cd /home
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6.8.noarch.rpm
cd /etc/yum.repos.d
vi SQUID.repo

Enter the following text into this file :

[Squid]
name=SQUID repo for CentOS Linux 6 - $basearch
#IL mirror
baseurl=http://www1.ngtech.co.il/rpm/centos/6/i686
failovermethod=priority
enabled=1
gpgcheck=0

Once done we update the system again and install SQUID, type into terminal :

yum update
yum install perl-Crypt-OpenSSL-X509
yum install squid

Now we initialize SQUID ssl_db directory , type this into console :

/usr/lib/squid/ssl_crtd -c -s /var/lib/ssl_db
chown -R squid.squid /var/lib/ssl_db

Finally edit your Squid.conf config file with these configs :

/etc/squid/squid.conf
acl localnet src 192.168.201.0/24

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
acl CONNECT method CONNECT

http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports

http_access allow all

http_port 3130

http_port 3128 intercept
https_port 3129 intercept ssl-bump generate-host-certificates=on dynamic_cert_mem_cache_size=4MB cert=/etc/squid/ssl_cert/myca.pem key=/etc/squid/ssl_cert/myca.pem

#always_direct allow all
ssl_bump server-first all
#sslproxy_cert_error deny all
#sslproxy_flags DONT_VERIFY_PEER

sslcrtd_program /usr/lib/squid/ssl_crtd -s /var/lib/ssl_db -M 4MB
sslcrtd_children 8 startup=1 idle=1

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


shutdown_lifetime 1 second

STEP 3 – Generate Certificate for SQUID using OpenSSL

We will first edit our /etc/pki/tls/openssl.conf file , and alter only the following

default_days    = 1365           # How long to certify for
...

[ req_distinguished_name ]
countryName                     = Country Name (code)
countryName_default             = DE
countryName_min                 = 2
countryName_max                 = 2

stateOrProvinceName             = State or Province Name (full name)
stateOrProvinceName_default     = NRW

localityName                    = Locality Name (eg, city)
localityName_default            = Paderborn

0.organizationName              = Organization Name (eg, company)
0.organizationName_default      = Home.LAN

# we can do this but it is not needed normally :-)
#1.organizationName             = Second Organization Name (eg, company)
#1.organizationName_default     = World Wide Web Pty Ltd

organizationalUnitName          = Organizational Unit Name (eg, section)
organizationalUnitName_default  = Proxy Server

commonName                      = Common Name (eg, your name or your server's hostname)
# (Very Important, in order to keep mail clients and other user agents from complaining, this name must
# match exactly the name that the user will be entering into their client settings.  Whether that be
# domain.extension or mail.domain.extension or what.  It must be a valid DNS name pointing at your
# server.
commonName_default              = proxy.home.lan   # this line you need to add
commonName_max                  = 64

emailAddress                    = Email Address
emailAddress_default            = robert@proxy.home.lan  # this line you need to add
emailAddress_max                = 64

Once this is done we go to SQUID directory , create the certificate folder and generate the keys :

mkdir /etc/squid/ssl_cert
chown -R squid.squid /etc/squid/ssl_cert
cd /etc/squid/ssl_cert

For SQUID server the key :

openssl req -new -newkey rsa:1024 -days 1365 -nodes -x509 -keyout myca.pem -out myca.pem

For Windows clients the key :

openssl x509 -in myca.pem -outform DER -out myca.der

STEP 4 – install our own DNS server

Enter the following into console :

yum install bind

Then we go and modify the /etc/named.conf to look like this :

//
// named.conf
//
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//

acl mynet {
    192.168.201.0/24; # test network
    127.0.0.1; # localhost
    };

options {
    listen-on { 
        mynet;
        };
    listen-on-v6 port 53 { ::1; };
    directory     "/var/named";
    dump-file     "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
    allow-query     { mynet; };
    recursion yes;
    
    forward only;
    forwarders {
        8.8.8.8;
        };

    dnssec-enable yes;
    dnssec-validation yes;
    dnssec-lookaside auto;

    /* Path to ISC DLV key */
    bindkeys-file "/etc/named.iscdlv.key";

    managed-keys-directory "/var/named/dynamic";
};

logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};

zone "." IN {
    type hint;
    file "named.ca";
};

include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

#############################################
#    home.lan
#############################################

zone "home.lan" IN {
    type master;
    file "/var/named/home.lan/db.home";
    allow-query {
    mynet;
    };
    };

Type this into terminal :

mkdir /var/named/home.lan
touch /var/named/home.lan/db.home
chown -R named.named /var/named/home.lan

 

Then we add the following to the file /var/named/home.lan/db.home

$ORIGIN home.lan.
$TTL 86400
@    IN    SOA    proxy.home.lan.    proxy.home.lan. (
    2014032801 ; Serial
    28800 ; Refresh
    7200 ; Retry
    604800 ; Expire
    86400 ; Negative Cache TTL
    )
@    IN    NS    proxy.home.lan.
proxy    IN    A    192.168.201.250

 

Next we make sure that both SQUID and NAMED are automatically

started every time we reboot. Type this into terminal :

chkconfig squid on
chkconfig named on
/etc/init.d/named start
/etc/init.d/squid start

 

Then we alter /etc/resolv.conf to use local DNS server :

search localdomain home.lan
nameserver 127.0.0.1

 

it is highly recommended to sync it’s time with the hosts  internal NTP server or a

external one. We can setup a cronjob for it like this :

vi /home/timesync

Then add the following into this file;

0 3,9,15,21 * * * /usr/sbin/ntpdate -u 134.130.4.17

And finally add it to the crontab;

crontab /home/timesync

This way it will sync the time regularly with our own internal or in this case with this external NTP server.

STEP 5 – Redirect HTTP and HTTPS traffic on our router to Proxy

add the following rules to IPtables :

iptables -t nat -A PREROUTING -p tcp -s 192.168.201.0/24 --dport 80 -j DNAT --to 192.168.201.250:3128
iptables -t nat -A PREROUTING -p tcp -s 192.168.201.0/24 --dport 443 -j DNAT --to 192.168.201.250:3129

STEP 6 – Client Configuration

Then the last step is to copy  the /etc/squid/ssl_cert/myca.der file to Windows  clients  ,  you  can  use  WinSCP  .  Installation  and configuration  is  straightforward  so  I wont go into detail

here .

For Internet Explorer :

Tools -> Internet Options -> Content -> Certificates Click on
Import , select  myca.der file , make sure that you import to
Root Trusted Certificates  ,  close  down the browser and try

www.grc.com for example.

For Mozilla Firefox :

Tools-> Options-> Advanced -> Certificates - > View Certificates
Import
(x) Trust this CA to identify websites
(x) Trust this CA to identify email users
(x) Trust this CA to identify software developers
Click OK you are done.

For Google Chrome same as Internet Explorer.

NOTE : If the main certificate expires for SQUID , and you generate a  new  one ,  don’t  forget  to  delete  the  old  certificates , /var/lib/ssl_db/certs also empty the file /var/lib/ssl_db/index.txt, and set the number inside the file /var/lib/ssl_db/size to 0 .

 

 

Advertisements