{"id":62,"date":"2022-03-30T14:00:48","date_gmt":"2022-03-30T13:00:48","guid":{"rendered":"https:\/\/steambot.ch\/blog\/?p=62"},"modified":"2022-04-01T13:35:54","modified_gmt":"2022-04-01T12:35:54","slug":"setup-a-home-vpn-server-and-access-your-private-network-from-anywhere","status":"publish","type":"post","link":"https:\/\/steambot.ch\/blog\/?p=62","title":{"rendered":"Setup a home VPN server and access your private network from anywhere"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hosting a VPN at home offers several benefits such as allowing you to access all of your devices and personal web services from anywhere and protecting your communications when connecting from an untrusted or low-security infrastructure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Raspberry Pi is well suited to the task with its low-energy consumption and high reliability.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">OpenVPN installation and setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I suggest to follow the instruction from Digital Ocean available <a rel=\"noreferrer noopener\" href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-openvpn-server-on-debian-10\" target=\"_blank\">in this post<\/a> that will allow to deploy a VPN server on a Raspberry Pi.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dealing with changing IPs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One major problem you may encounter is that your ISP might randomly (and will probably at some point) change the public IP of your home router. As a result, when a devices tries to connect to your VPN server, it is unable to do so as it cannot find the server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A simple fix is to register a domain name that you can use to point to your current IP address. Any change in IP address will then be reflected in the DNS record to make sure it always points to the correct and latest valid IP address. If you do not want to pay for such a service, I recommend using <a rel=\"noreferrer noopener\" href=\"https:\/\/www.duckdns.org\/\" target=\"_blank\">DuckDNS<\/a> to register a subdomain of <code>duckdns.org<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can automate the check and IP updates by using the script provided on the duckdns website (just follow the instructions on the duckdns website in the &#8220;install&#8221; tab when you&#8217;re logged in).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring Pi-Hole<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;re using <a href=\"https:\/\/pi-hole.net\/\" target=\"_blank\" rel=\"noreferrer noopener\">Pi-Hole<\/a> as your home DNS server, follow <a href=\"https:\/\/docs.pi-hole.net\/guides\/vpn\/openvpn\/overview\/\" target=\"_blank\" rel=\"noreferrer noopener\">the instructions from pi-hole&#8217;s website<\/a> to make it your OpenVPN DNS resolver. This will make all DNS requests pass through your Pi-Hole and clean-up your internet traffic, wherever you&#8217;re connected. Pretty neat!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adding security<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The installation of OpenVPN gives you access to your home network but can potentially open a breach for attackers directly into your home. Be careful with what you do and make regular security checks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below are a few tools you can use to protect yourself against attacks and to monitor the successful and failed access to your VPN.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">fail2ban<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">fail2ban is a python-based intrusion prevention software framework that protects computers from brute-force attacks. You can configure it to log and block every failed attempt to connect to OpenVPN. In order to do this, you need to create a new rule.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create the new file <code>openvpn.local<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/etc\/fail2ban\/filter.d\/openvpn.local<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and add the following lines inside it. This will allow fail2ban to scan your OpenVPN logs and recognize failed connection attempts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Fail2Ban filter for selected OpenVPN rejections\n\n&#91;Definition]\n\nfailregex = &#91;a-b]*ovpn-server.*:.&lt;HOST&gt;:&#91;0-9]{4,5} TLS Auth Error:.*\n            &#91;a-b]*ovpn-server.*:.&lt;HOST&gt;:&#91;0-9]{4,5} VERIFY ERROR:.*\n            &#91;a-b]*ovpn-server.*:.&lt;HOST&gt;:&#91;0-9]{4,5} TLS Error: TLS handshake failed.*\n            &#91;a-b]*ovpn-server.*:.&lt;HOST&gt;:&#91;0-9]{4,5} WARNING: Bad encapsulated packet length from peer\n            #&#91;a-b]*ovpn-server.*:.&lt;HOST&gt;:&#91;0-9]{4,5} Connection reset, restarting \\&#91;&#91;0-9]{1,2}\\]\n\nignoreregex =\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">then edit the fail2ban configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/etc\/fail2ban\/jail.local<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and edit it to contain these lines to filter for failed ssh connections and openvpn connections:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;DEFAULT]\n# Ban hosts for 1 hour after they perform 3 failed login attempts within 20 minutes\nbantime = 3600\nfindtime = 1200\nmaxretry = 3\n\n&#91;sshd]\n# Enables the sshd jail\nenabled = true\n\n&#91;openvpn]\n# Fail2Ban configuration fragment for OpenVPN\nenabled  = true\nport     = 655\nprotocol = tcp\nfilter   = openvpn\nlogpath  = \/var\/log\/syslog\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the default section, you can choose how long you want to block potential attackers, how long fail2ban should keep track of failed attempts and how many retries a user gets. Beware that you might also unexpectedly block yourself out of your server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">logwatch<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Logwatch is a powerful and versatile log parser and analyzer designed to give a unified report of all activity on a server, which can be delivered through the command line or email. By default logwatch will read your openvpn log and report every failed and successful connection attempts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can install it directly from the command line using the package manager:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install logwatch<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">to run a logwatch job on a daily basis, create a file in your cron.daily repository:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/etc\/cron.daily\/00logwatch<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and add the following content, customizing the level of details you wish and the location of the output file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n#execute\n\/usr\/sbin\/logwatch --detail high --format html --range \"since 4 days ago\" --filename &lt;PATH_TO_YOUR_FAVORITE_LOCATION&gt;\/logwatch_output.html<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will configure logwatch to generate an html file every morning containing a summary of your logs for the last 4 days and allow you to survey what is happening on your server.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"134\" src=\"https:\/\/steambot.ch\/blog\/wp-content\/uploads\/2022\/04\/logwatch_fail2ban.jpg\" alt=\"\" class=\"wp-image-92\" srcset=\"https:\/\/steambot.ch\/blog\/wp-content\/uploads\/2022\/04\/logwatch_fail2ban.jpg 806w, https:\/\/steambot.ch\/blog\/wp-content\/uploads\/2022\/04\/logwatch_fail2ban-300x50.jpg 300w, https:\/\/steambot.ch\/blog\/wp-content\/uploads\/2022\/04\/logwatch_fail2ban-768x128.jpg 768w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\" \/><figcaption>Example logwatch output for fail2ban<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"956\" height=\"291\" src=\"https:\/\/steambot.ch\/blog\/wp-content\/uploads\/2022\/03\/logwatch_openvpn.jpg\" alt=\"\" class=\"wp-image-95\" srcset=\"https:\/\/steambot.ch\/blog\/wp-content\/uploads\/2022\/03\/logwatch_openvpn.jpg 956w, https:\/\/steambot.ch\/blog\/wp-content\/uploads\/2022\/03\/logwatch_openvpn-300x91.jpg 300w, https:\/\/steambot.ch\/blog\/wp-content\/uploads\/2022\/03\/logwatch_openvpn-768x234.jpg 768w\" sizes=\"auto, (max-width: 956px) 100vw, 956px\" \/><figcaption>Example logwatch output for OpenVPN<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Optional: Connecting to a second VPN<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you would like another layer of privacy and use an outgoing VPN connection from your OpenVPN server (as opposed to your normal ISP network connection), have a look at the detailed instructions in <a href=\"https:\/\/steambot.ch\/blog\/?p=9\">this previous post<\/a>, section &#8220;Add rules to forward a second OpenVPN traffic&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hosting a VPN at home offers several benefits such as allowing you to access all of your devices and personal web services from anywhere and protecting your communications when connecting from an untrusted or low-security infrastructure. A Raspberry Pi is well suited to the task with its low-energy consumption and high reliability. OpenVPN installation and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9,10,11,5,4],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-vpn","tag-networking","tag-openvpn","tag-pihole","tag-raspberrypi","tag-vpn"],"_links":{"self":[{"href":"https:\/\/steambot.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/steambot.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/steambot.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/steambot.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/steambot.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=62"}],"version-history":[{"count":31,"href":"https:\/\/steambot.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/steambot.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/62\/revisions\/100"}],"wp:attachment":[{"href":"https:\/\/steambot.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/steambot.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/steambot.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}