Saturday, January 26, 2013

Howto Raspberry Pi - Use your Pi as a secure Reverse Proxy gateway to your Web internal Sites and Services



Last update 02/01/2013





The Goal: 


You have a Raspberry Pi and want to use it as your secure Web reverse proxy gateway to access to your various Internal services through your main fully qualified domain name or IP.

Let's say:
  • You have a main router or ISP Box
  • Your Rpi will be in front of the Internet by redirecting http/https por
  • For this configuration to work from both inside and outside your home network, your domain name (here "myowndomain.com") must be associated with your public IP
  • ts from your router to your Rpi
  • You have or not internal servers providing Web sites or services your want to access from your public IP / domain name
We will use:
  • nginx as the great secure reverse proxy instance
  • SSL with auto signed or officially signed certificate to secure our web traffic
  • htpasswd to password protect your shellinbox from being visible and accessible whitout credentials
  • shellinabox to host a nice Web SSH frontend

Summary of steps: 

Step 1: OPTIONAL - Get a fully Qualified Domain Name (FQDN)
Step 2: Manage your SSL certificate
Step 3: Put a Shellinabox in your Pi ^^
Step 4: Install and configure Nginx



Step 1: OPTIONAL - Get a Fully Qualified Domain Name  

This is absolutely optional, but you could think about getting a qualified domain name to access to your home network. (a domain costs very few per year, and your can dynamically associate it with your public IP)

In many cases, when you connect from secure places (such as your company site), trying to access to a web site using its public IP will be prohibited by internals web proxies and firewalls.
By using a fqdn to associate your public IP to a real Internal domain name, your site is as official as any Internet company web site :-)

As an alternative to buy your own domain name, you can also use dynamic free domain name services such as no-ip.org, but most of company proxy will also block them.

And finally, this is just clean and beautiful ^^

In this post, i will assume for the example purpose that your domaine name is "myowndomain.com". (still the fqdn is optional)
  
Step 2: Manage your SSL certificate

Off course, we will want to secure our Web traffic using an SSL certificate, there is 2 ways to achieve this:

1. Generating an "auto-signed" SSL certificate

You can very easily generate an auto-signed SSL certificate, you will have exactly the same security and encrypting level than any official certificate but this certificate won't be officially recognized over the Internet.

That means that when connecting to your site, your Web browser will warn you about the impossibility to guaranty your security connecting to this site, and you have to accept this.

I personally prefer having an official SSL certificate :-)

2. Buy and generate an Officially signed SSL certificate

You can also buy an official SSL certificate for very few, in this case your browser will automatically recognize your certificate as valid and you won't get any warn.

There is some places where you can get a free official SSL certificate for personal use. (look for "startssl")

In both cases, Google is your friend ^^

How to generate an auto signed certificate:

Install OpenSSL:
$ sudo apt-get install openssl

Generate your self signed certificate:
sudo mkdir -p /etc/ssl/localcerts
openssl req -new -x509 -days 3650 -nodes -out /etc/ssl/localcerts/autosigned.crt -keyout /etc/ssl/localcerts/autosigned.key
chmod 600 /etc/ssl/localcerts/*

Note: Respond to OpenSSL questions as you wish, it does not really mind as your certificate is a self-signed anyway


Step 3: Put a shellinabox in your Pi ^^

As explained before, shellinabox is a wonderfull web frontend to SSH, this way you will access to your SSH server without having to deal with an SSH client.

By the past, i wrote an article about an other SSH web frontend "ajaxterm" which is nice too, but in my opinion much more limited and low.
So i recommend to use shellinabox instead.

You will be able to access to your SSH server using standard Web ports even when connecting from places where external SSH traffic is prohibited :-) 

To install:
# sudo apt-get install shellinabox

By default, shellinabox uses the port "4200" to listen to, you can let that as it is as your nginx reverse proxy take care about redirecting our request to this internal service. 

If you want to manage your shellinabox configuration, take a look at main config files:
  • /etc/default/shellinabox
  • /etc/shellinabox/*
Default configuration is ok for us, test your shellinabox by connecting from a browser inside your network: http://<mypiserver>:4200

Note that even if we won't use it, shellinabox comes with embeded SSL auto-signed certificate configuration to redirect http to https and secure your web traffic.


Step 4: Install and configure Nginx

Ok, serious things now, let's install and configure nginx.

Nginx is an extremely powerful Opensource Web server, light secure and fast, that can be used as reverse proxy instance gateway to your internal Web services.

It is more and more used by many companies web site with high load Web Sites, do not hesitate to take a look at official sites:
I used by the past Apache running as a reverse proxy to do this job, but nginx assumes this job with great success, it's very modular and easy to maintain, this is why i recommend your Nginx.

To install:
# sudo apt-get install nginx-full


Now let's configure the beast:

First, some configuration in main config file "/etc/nginx/nginx.conf", here is a sample config file:
# /etc/nginx/nginx.conf

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
 worker_connections 768;
}

http {

 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout 65;
 types_hash_max_size 2048;

 include /etc/nginx/mime.types;
 default_type application/octet-stream;

 access_log /var/log/nginx/access.log;
 error_log /var/log/nginx/error.log;

 gzip on;
 gzip_disable "msie6";

 include /etc/nginx/conf.d/*.conf;
 include /etc/nginx/sites-enabled/*;
}

Please note that as Apache configuration style under Debian/Ubuntu, any configuration file (for site or module) included in conf.d or sites-enabled will be loaded at Nginx start

A good practice is to create a symbolic link from "sites-available" to "sites-enabled".

Let's deactivate the default web site we won't use by removing its symbolic link:
$ sudo rm /etc/nginx/sites-enable/default 

Create an htpasswd file that will contain your credentials (adapt <username>)
$ sudo htpasswd -c /etc/nginx/.htpasswd <username>

Now create your main web site configuration file, example:
  • /etc/nginx/sites-available/main
Here is a sample secured configuration:
access_log off;
add_header Cache-Control public;
server_tokens off;


# HTTP 80
server {
 listen         80;
 server_name _;
 rewrite ^ https://myowndomain.com$request_uri? permanent;
}

# HTTPS 443
server  {

 include    /etc/nginx/proxy.conf;

 listen 443 ssl;
 keepalive_timeout 70;

 server_name myowndomain.com;

 # SSL config
 ssl on;
 ssl_certificate /etc/ssl/localcerts/autosigned.crt;
 ssl_certificate_key /etc/ssl/localcerts/autosigned.key;

 ssl_session_timeout 5m;
 ssl_protocols SSLv3 TLSv1.2;
 ssl_ciphers RC4:HIGH:!aNULL:!MD5;
 ssl_prefer_server_ciphers on;
 ssl_session_cache shared:SSL:10m;

 add_header X-Frame-Options DENY;

 # DDOS protection - Tune Values or deactivate in case of issue
 # limit_conn conn_limit_per_ip 20;
 # limit_req zone=req_limit_per_ip burst=20 nodelay;

 # status for ngxin auditing
 location /nginx-status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
  }

 location / {
  rewrite ^ https://myowndomain.com/shellinabox/ permanent;
  }

 location /shellinabox/ {
  proxy_pass http://localhost:4200;
                auth_basic            "Access Restricted";
                auth_basic_user_file  "/etc/nginx/.htpasswd";
                access_log /var/log/nginx/shellinabox.access.log;
                error_log /var/log/nginx/shellinabox.error.log;
  }
}

Create the file "/etc/nginx/proxy.conf" with following content:
proxy_redirect          off;
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffers           32 4k;

Activate your nginx web site and restart:
sudo ln -s /etc/nginx/sites-enables/main /etc/nginx/sites-available/main
sudo service nginx restart

NOTE:

For this configuration to work from both inside and outside your home network, your domain name (here "myowndomain.com") must be associated with your public IP


Now test accessing to your Web site from both internal and external access :-)

As you understood, you can manage as many internal Web sites as you need through a unique Web instance and virtual hosts. (called location in Nginx)

In the sample config, shellinabox is the default site accessible with your domain name, but you change it and/or add any other internal web sites very easily.

Just add a new location related to your internal Web site you want to be able to access and you're done :-)



11 comments:

  1. Hi,
    can you help me ?

    my ssl is not working :
    SSL a reçu un enregistrement qui dépasse la longueur maximale autorisée.
    (Code d'erreur : ssl_error_rx_record_too_long

    my pi say that it don't understand:
    limit_req zone=req_limit_per_ip

    (sudo sudo nginx restart would be better with sudo service nginx restart).

    please don't tack this comment as an attack, i'me really asking questions !

    Sincerly,

    ReplyDelete
  2. Hi Olivier,

    No problem with pleasure :-)

    My mistake, a 2048 bits SSL key length requires a change in the main "Listen" directive.

    See:
    http://www.robinverlangen.nl/index/view/50866e733c3ba-1227f8/ssl-error-rx-record-too-long-on-nginx.html

    listen 443;

    change it into

    listen 443 ssl;

    I use an external official certificate with a 1024 keys so i didn't had this problem.

    About: limit_req zone=req_limit_per_ip
    It's a DDOS protection, it's optional if you have an issue just comments these 2 lines:

    # DDOS protection - Tune Values or deactivate in case of issue
    limit_conn conn_limit_per_ip 20;
    limit_req zone=req_limit_per_ip burst=20 nodelay;

    Thanks for the correction.

    Guilhem

    ReplyDelete
  3. Thanks a lot ! here is my nginx.conf improved with what i find about more ddos protection.
    /etc/nginx $ cat nginx.conf

    user www-data;
    worker_processes 4;
    pid /var/run/nginx.pid;

    events {
    worker_connections 768;
    # multi_accept on;
    }

    http {
    ##
    # Prevent Ddos attaque
    ##

    limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
    limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=5r/s;

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
    }


    #mail {
    # # See sample authentication script at:
    # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
    #
    # # auth_http localhost/auth.php;
    # # pop3_capabilities "TOP" "USER";
    # # imap_capabilities "IMAP4rev1" "UIDPLUS";
    #
    # server {
    # listen localhost:110;
    # protocol pop3;
    # proxy on;
    # }
    #
    # server {
    # listen localhost:143;
    # protocol imap;
    # proxy on;
    # }
    #}

    ReplyDelete
  4. Hello and here is a full-cleaned sample about what i have (i've clean everything but let the shellinabox basic config)

    /etc/nginx/sites-available $ cat rpi_siab
    access_log off;
    add_header Cache-Control public;
    server_tokens off;


    # HTTP 80
    server {
    listen 80;
    server_name _;
    rewrite ^ https://myowndomain.uk$request_uri? permanent;
    }

    # HTTPS 443
    server {

    include /etc/nginx/proxy.conf;

    listen 443 ssl;
    keepalive_timeout 70;

    server_name myowndomain.uk;

    ##
    # SSL config
    ##

    ssl on;
    ssl_certificate /etc/ssl/localcerts/autosigned.crt;
    ssl_certificate_key /etc/ssl/localcerts/autosigned.key;

    ssl_session_timeout 5m;
    ssl_protocols SSLv3 TLSv1.2;
    ssl_ciphers RC4:HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;

    add_header X-Frame-Options DENY;

    ##
    # DDOS protection - Tune Values or deactivate in case of issue
    ##

    limit_conn conn_limit_per_ip 10;
    limit_req zone=req_limit_per_ip burst=10 nodelay;

    ##
    # status for ngxin auditing
    ##

    location /nginx-status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
    }

    location / {
    rewrite ^ https://myowndomain.uk/shellinabox/ permanent;
    }

    location /shellinabox/ {
    proxy_pass http://localhost:4200;
    auth_basic "Access Restricted";
    auth_basic_user_file "/etc/nginx/.htpasswd";
    access_log /var/log/nginx/shellinabox.access.log;
    error_log /var/log/nginx/shellinabox.error.log;
    }
    }

    ReplyDelete
  5. /!\ Still doesn't work!:
    (Code d'erreur : ssl_error_rx_record_too_long
    So i'v try some changes see on another blog but still doesn't work

    ##
    # SSL config
    ##

    ssl on;
    ssl_certificate /etc/ssl/localcerts/autosigned.crt;
    ssl_certificate_key /etc/ssl/localcerts/autosigned.key;

    ssl_session_timeout 5m;
    ssl_protocols SSLv3 SSLv2 TLSv1.2 TLSv1;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;

    add_header X-Frame-Options DENY;

    ReplyDelete
    Replies
    1. Hi,

      Well quite strange... Do you have this message at nginx startup ?

      Some little things to test i think:

      1. Ensure nginx is compiled with SSL (using 'nginx -V')

      2. Try to generate a new certificate with a 1024 bits key and test

      3. If nginx starts, try a simple location containing an test example web page

      4. After a few search, it seems it could also be a problem related with your host config, ensure the host you mentionned on your nginx config exist in /etc/hosts (ensure your have an entry like "myowndomain.com XXX.XXX.XXX.XXX"

      5. Ensure nothing is already listening to 443 local port (netstat -an | grep LISTEN) before starting nginx

      6. Doest nginx works in http ? (for testing purposes)

      Delete
    2. First and before all double check your local host configuration, it seems there is a good luck to be the root cause!

      In "/etc/hosts", add:

      xxx.xxx.xxx.xxx mydomain.com

      Where the first field is the local IP of your nginx instance (not the loopback) and the same domain name set in your nginx config.

      Let me know ^^

      Delete
  6. "Your Rpi will be in front of the Internet by redirecting http/https por"

    What do you mean by this statement?
    Where will the Raspi be located in the network architecture?
    Directly in between the Router and the webserver?

    ReplyDelete
    Replies
    1. Hi,

      I mean that web requests coming from the Internet will be redirected from your router to your Raspberry and not the web-server itself.

      Then, reverse proxy instances running in your Raspberry Pi will "talk" to your internal web-server and serve requested web pages.

      The goal of a such config is to prevent your Web-server from being directly in front of an external unsecured Network such as the Internet.

      In term of architecture view (and company context), a best practice would be to localize reverse proxy in a DMZ (or VLAN) to control which kind of traffic will be authorized between reverse proxies and other servers.

      Still, preventing your Web-Server from being directly exposed to the Internet by redirecting Web ports from your router to your reverse proxy (even if both reverse proxy and web-servers are on same physical networks) drastically improves your security.

      Hope my explanation will help ^^

      Delete
  7. Am I correct in assuming that you will have to Portforward both 443 and 80? I would prefer not to use nor portforward 80 so bots can't receive http requests signalling I got a website for them to hack.

    -Rich

    ReplyDelete
    Replies
    1. Hi,

      I guess i forgot to answer, sorry ^^

      No, there is no obligation to forward both 80 and 443, if you just want https (and no http automatic redirection), just open 443 and ensure to always access to your site using "https://"

      Nothing more

      Delete

Please feel free to comment ^^