Preface:
I remember when I discovered port forwarding for the first time 16 years ago - I was amazed I could open a port on my router and point it to a device on the LAN side of my router. I had many services I was running on my LAN which I wanted to access, I even ran my own web server, opening port 80 for the web server and port 22 so I could ssh in and administer it. I then opened my routers port for web administration. My defining achievement and ultimately my undoing was opening my RealVNC(password protected) port on my home PC so I could control my pc from elsewhere!
Then one day when using my PC at home - my mouse moved on screen without me moving it, then notepad opened and someone typed âI can see what you seeâ ! crapping myself I immediately turned off my PC. after crawling out from under my bed I took a look at my routers log file, surly enough someone had accessed my VNC service, I closed all the ports down on my router. It turned out that someone had scanned my public IP for open ports and accessed my VNC service via an exploit that had been exposed on the website Milw0rm. I thought how could this happen - how could someone find my IP and find open ports, but as my research expanded it was clear how easy it was. Would-be hackers had used NMAP to scan a range of public IP addresses for the default open VNC ports - found mine, used the exploit and they were in.
This taught me to be careful what you expose to your public IP - even password protected didnât protect me from holes in software and services I was running.
How to check your own network security
Be savy on network security by checking your router log file - checking what ports you have forwarded and how your router is configued
-
SSH to your server and install NMAP and xsltproc
-
Run the following for your LAN address range
nmap -oX output.xml -T4 -A 192.168.0.1-254
xsltproc output.xml -o network.html
network.html is the report you can check for your LANâs devices - just read though it and check for things that you are not happy with
-
Run Shields Up! on your network to scan your network from the outside https://www.grc.com/x/ne.dll?bh0bkyd2
Base Knowledge:
Before I get into the technology of how to setup OpenVPN - Iâm going to describe a bit of network topology -starting VERY basic - sorry seasoned techies.
The internet/cloud/web - whatever you want to call it is just a load of servers or PCâs connecting to each other and providing information from one device to another. When your router connects to the internet it gets a public IP address - the other thing it does is hands out(by default) private IP addresses to the local devices on your LAN. Anything on your LAN side can see each other by default unless the other device has some kind of firewall or blocking mechanism. Hence why you can access something like Home Assistant on your LAN via your Laptop.
By default devices on the LAN side can access the internet - but the internet devices canât access LAN devices - unless you tell your router to allow it via UPnP, a DMZ, port forwarding or a static route though. I wonât cover disabling UPnP but I highly recommend it - (It opens ports on your router automatically from the LAN side requesting device on your router - allowing the outside in to that device !)
The devices on the LAN side can run any number of services and expose them to other devices, LAN side or WAN side (if ports are open on your router) via ports 0 to 65535 (0-1024 are reserved for known services) some examples include SSH on port 22 (reserved) and Home Assistant default port 8123 (unreserved)
An example of this(Looking at the diagram above) is if Home Assistant is running on Server (192.168.0.14) to access Home Assistant you would navigate to http://192.168.0.14:8123 LAN side and if you opened port 8123 on the router to point to the Server you could, from the WAN side, access http://56.34.155.66:8123. When this happens you router knows to forward traffic from the requesting WAN device to the internal server (Port Forwarded)
Great, but you just exposed Home Assistant to the internet - As amazing as Home Assistant is, itâs purpose is Home Automation and control - not military grade security. I hear you saying well I could password protect it, or encrypt the connection - fair enough but think about it like this - if a hacker accesses your password protected Home Assistant - they already successfully connected to a service on your LAN(be it on the password page, making exploration easier) and encryption only helps prevent tapping of the information from one end point to another (there are devices that can sniff encrypted SSL traffic if they are in the right place Blue Coat Proxy)
An alternative to port forwarding is SSH tunneling - This is good because it is encrypted - but you need to have ssh open to the world and running on a server and you need to tunnel each port you want to connect too.
The recommendation is to run a service which has been designed for managing and allowing connections in a secure manner, a VPN.
What is a VPN
VPN stands for Virtual Private Network - itâs a service you connect to on your LAN from the internet, you get an IP and if configured in such a way the device you connected from thinks itâs on your LAN at home, Thus allowing you to connect to any local services you are running.
e.g. I use my iPhone to VPN into my home LAN and itâs like connecting to my home WIFI - I get all the benefits of being secure on my LAN with the convenience I would get from being on my LAN at home
The other benefit is you can browse the internet and itâs as if the traffic is originating from your home LAN not from the internet - useful if you want to appear as if you are at home - or encrypt all the traffic you are browsing, from the originating network - useful on public WI-FI - who knows which IT admin is logging traffic you are using in Starbucks from your device - connect to OpenVPN and you just made it a ton harder for that admin to tap that information.
e.g I have my iPhone on LTE or a public WIFI and search for show my ip in Google - it will show my LTE or public wifi IP but if I connect to my VPN I will see my home routers external IP
It encrypts all traffic over the connection using certificates that have been exchanged prior to forming the connection - not at the time of the connection. These certificates are the âkeys to the vaultâ and can even be password protected themselves. If thatâs not enough you create a user on the VPN server which can have 2FA / Key added to the password (Like an RSA key) to further make the connection more secure.
How to setup OpenVPN
Your going to need a Linux server - I run mine on Ubuntu 16.04.4 LTS - but you could run it on a spare Raspberry Pi - itâs going to take you 1-2 hours to complete this, buckle up and MAKE SURE YOU READ EVERYTHING IN THE FIRST GUIDE - as tempting as it maybe donât just copy paste the commands - understand them and ask questions here if you need to.
There are three guides to follow(these are the best guides I have found) - each can be done separately
Follow the guide here
I would advise running OpenVPN on a different port to the default one and this is the port you will need to open on your router to allow VPN connections inbound to your network. Also choose the build-key-pass path route for the client.
There are further guides to Harden OpenVPN - pick and choose which you wish to do
Configuring 2FA with OpenVPN
Once complete go to somewhere remote and connect to your new VPN, open Home Assistants local IP address or any other service on your LAN - you can also use show my IP to test your public IP of your device changes to you home LANs IP
Good Luck!