create account

MasterNode VPS Security - The Basics - Part 2 by docdagbjort

View this thread on: hive.blogpeakd.comecency.com
· @docdagbjort ·
$1.86
MasterNode VPS Security - The Basics - Part 2
<html>
<h3>Introduction</h3>
<p><br></p>
<p>In 'Part 1' we covered changing the root password, changing the default SSH port and enabling key-based SSH authentication. &nbsp;</p>
<p>If you haven't already done these things, you should go back and <a href="https://steemit.com/crypto/@docdagbjort/masternode-vps-security-the-basics-part-1">work through 'Part 1' first</a> before moving on to this section.</p>
<p><br></p>
<h3>Step 4 - Turn Off Password-based SSH Logins - 'Brick Up The Old Front Door'</h3>
<p><br></p>
<p>There's no point in having enabled key-based SSH logins if an attacker can still attempt password-based logins. &nbsp;At this point in this guide, both systems are currently enabled on &nbsp;your VPS. &nbsp;So, let's turn off password-based SSH logins.</p>
<p>SSH into your VPS (if you haven't already) and use nano to open the SSH configuration file (just like we did when changing the default port above):</p>
<pre><code>nano /etc/ssh/sshd_config</code></pre>
<p><br></p>
<p>Find the line:</p>
<p><code>#PasswordAuthentication yes</code></p>
<p>(it should be on the third page). &nbsp;Change it to read:</p>
<p><code>PasswordAuthentication no</code></p>
<p>Note the removal of the '#' character at the beginning of the line (a '#' means a line is a comment and should be ignored) as well as the change from 'yes' to 'no'.</p>
<p>Hit Ctrl-O to save the file and Ctrl-X to exit nano.</p>
<p>Now, restart SSH on the VPS:</p>
<pre><code>service ssh restart</code></pre>
<p><br></p>
<p>Finally, check you can still log in via SSH (with a new SSH session - leave the existing login open in case you need to revert the changes above).</p>
<p>If you can, all is well. &nbsp;Now would be a good time to take a snapshot of your VPS if your provider makes that capability available to you.</p>
<p>The steps in the rest of the guide can, if carried out incorrectly, deny you the ability to access your VPS - being able to restore a snapshot to regain access without losing all of the work above might be convenient.</p>
<p>Don't forget that your provider probably has a shell available on their web-based control panel that you can use instead of SSH in an emergency (such as locking yourself out with a badly configured firewall).</p>
<p><br></p>
<h3>Step 5 - Install A Firewall - 'Brick Up The Windows And Add CCTV Cameras'&nbsp;</h3>
<p><br></p>
<p>Unless your VPS provider supplied you with a firewall (most don't) your VPS is sat out naked on the Internet, ports exposed to the world.</p>
<p>An (automated) attacker can drive by your VPS and check to see which ports are open, and what services you're running, with a view to exploiting any known vulnerabilities in those services. &nbsp;Like a burglar peering in through your windows and giving them a little tug to see if they're locked.</p>
<p>Let's get a firewall installed and brick up those windows - our aim here is to achieve as close to 'silent running' as we can manage.</p>
<p>That is, want to<strong> a)</strong> reduce the probability of the VPS being detected <strong>b)</strong> reduce the available attack surface to the absolute minimum when/if it is detected.</p>
<p>We're going to use '<a href="https://configserver.com/cp/csf.html">ConfigServer Security &amp; Firewall</a>' (csf). &nbsp;It not only provides an easy interface to the 'iptables' system in Linux (which has a pretty steep learning curve but is very capable) it also comes with a Login Failure Daemon (lfd) to watch out for, and block, the originators of multiple failed login attempts.</p>
<p>Make sure you check the software licensing terms on their site above before continuing to installing the software below (it's free) and that you agree with those terms.</p>
<p>SSH into your VPS as root and make sure you're back in your home directory:</p>
<pre><code>cd ~</code></pre>
<p><br></p>
<p>or</p>
<pre><code>cd /root/</code></pre>
<p><br></p>
<p>and then type the following to download csf:</p>
<pre><code>wget https://download.configserver.com/csf.tgz</code></pre>
<p><br></p>
<p>You can do:</p>
<pre><code>ls -lahtr</code></pre>
<p><br></p>
<p>afterwards to make sure the file is there as per:</p>
<p><img src="https://www.bollock.stream/images/12csf.png" width="905" height="338"/></p>
<p><br></p>
<p>Now, decompress the file (it's a gzip'd tar archive) with:</p>
<pre><code>tar xzf csf.tgz</code></pre>
<p><br></p>
<p>If you do another:</p>
<pre><code>ls -lahtr</code></pre>
<p><br></p>
<p>You'll see a new directory called 'csf'.</p>
<p>Let's get into that directory:</p>
<pre><code>cd csf</code></pre>
<p><br></p>
<p>Now do:</p>
<pre><code>ls</code></pre>
<p><br></p>
<p>and you'll see lots and lots of files. &nbsp;Of note are '<code>readme.txt</code>' which describes in detail how 'csf' and 'lfd' work and '<code>install.txt</code>' which describes how to install it.</p>
<p>Firstly, let's get some standard perl libraries installed which csf may make use of. &nbsp;On Ubuntu, use:</p>
<pre><code>apt-get install libwww-perl</code></pre>
<p><br></p>
<p>Or on CentOS, it's:</p>
<pre><code>yum install libwww-perl</code></pre>
<p><br></p>
<p>And also some other prerequisites:</p>
<pre><code>apt-get install e2fsprogs</code></pre>
<p><br></p>
<p>or</p>
<pre><code>yum install e2fsprogs</code></pre>
<p><br></p>
<p>Now, using '<code>install.txt</code>' for guidance, let's go ahead and get csf installed by typing:</p>
<pre><code>sh install.sh</code></pre>
<p><br></p>
<p>A lot of information will scroll past really quickly, and should end with 'Installation Completed'.</p>
<p>Now we check if all the required iptables modules are installed:</p>
<pre><code>perl /usr/local/csf/bin/csftest.pl</code></pre>
<p><br></p>
<p>We're looking for '<code>RESULT: csf should function on this server</code>' as per:</p>
<p><img src="https://www.bollock.stream/images/13csf.png" width="554" height="489"/></p>
<p><br></p>
<p>If you don't get those results, consult the csf documentation for guidance.</p>
<p>Having completed these steps, csf is now running, but in 'TESTING' mode only. &nbsp;That is, it's not actually doing anything (or rather, it'll stop doing being useful after 5 minutes in case you lock yourself out).</p>
<p>Before we turn it on for real, let's tweak the configuration file. &nbsp;You can open this in nano with:</p>
<pre><code>nano /etc/csf/csf.conf</code></pre>
<p><br></p>
<p>Head on down to the lines that read:</p>
<p><code># Allow incoming TCP ports</code></p>
<p><code>TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995,9707"</code></p>
<p>It should be about 138 lines down (you can use '<code>Pg Dn</code>' on your keyboard to move more quickly through the config file).</p>
<p>This line controls which ports are open to incoming connections from the outside world.</p>
<p>Note that csf has been smart enough to append your new, non-standard, SSH port number to the end of this line already - 9707 - set in 'Step 2' above. &nbsp;At least, it should have. &nbsp;If it hasn't, add this now to the end of the list taking care to separate the values with commas and to end the line with a double-quote.</p>
<p>Most of the open port numbers above have no business being open on a masternode and are for services we're either not running, or don't want to be running.</p>
<p>Really, the only open ports we want are the one you're going to be SSH-ing in on, and the one your masternode uses for whichever cryptocurrency you're working with. &nbsp;So, for example, PIVX uses port 51470, meaning you'd be looking to change that line to:</p>
<p><code>TCP_IN = "9707,51470"</code></p>
<p>Slightly further down, we can also edit the line:</p>
<p><code>TCP_OUT = "20,21,22,25,53,80,110,113,443,587,993,995"</code></p>
<p>to:</p>
<p><code>TCP_OUT = "25,53,587,80,443,51470"</code></p>
<p>(meaning the VPS can now only make outgoing connections for four things - sending e-mail, doing DNS lookups, HTTP connections for downloading software updates, and outgoing masternode connections).</p>
<p>Change the line:</p>
<p><code>UDP_IN = "20,21,53"</code></p>
<p>to:</p>
<p><code>UDP_IN = ""</code></p>
<p>as there's likely nothing we need incoming UDP connections for.</p>
<p>Now, if your VPS comes with IPv6 support (which is increasingly likely) we also have to make sure csf is operational for that too.</p>
<p>If you don't know if IPv6 is enabled on your VPS, back on the commandline type:</p>
<pre><code>ifconfig</code></pre>
<p><br></p>
<p>If you can see any line beginning:</p>
<p><code>'inet6 addr:'</code></p>
<p>(note the '6') then IPv6 is probably enabled on your VPS and you need to adjust csf accordingly. &nbsp;If not, you can likely skip this next section.</p>
<p><strong>Enabling csf for IPv6</strong></p>
<p>Back in nano in /etc/csf/csf.conf find the line:</p>
<p><code>IPV6 = "0"</code></p>
<p>Change it to:</p>
<p><code>IPV6 = "1"</code></p>
<p>to enable IPv6 firewall support.</p>
<p>Now, find the line:</p>
<p><code>TCP6_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995,9707"</code></p>
<p>and change it to match your equivalent 'TCP_IN' line above, for example:</p>
<p><code>TCP6_IN = "9707,51470"</code></p>
<p>Similarly, change:</p>
<p><code>TCP6_OUT = "20,21,22,25,53,80,110,113,443,587,993,995"</code></p>
<p>to:</p>
<p><code>TCP6_OUT = "25,53,587,80,443,51470"</code></p>
<p>And then find:</p>
<p><code>UDP6_IN = "20,21,53"</code></p>
<p>and change it to:</p>
<p><code>UDP6_IN = ""</code></p>
<p>All of this essentially duplicates the firewall configuration so that it's also active for IPv6 connections (as well as IPv4).</p>
<p><strong>Enabling The Firewall</strong></p>
<p>Finally, it's time to enable the firewall for real, so go back up to the top of the configuration file in nano and find the line that reads:</p>
<p><code>TESTING = "1"</code></p>
<p>Change it to:</p>
<p><code>TESTING = "0"</code></p>
<p>Hit <code>Ctrl-O</code> to save, and <code>Ctrl-X</code> to exit nano.</p>
<p>Now, back on the commandline let's reload the firewall with:</p>
<pre><code>csf -r</code></pre>
<p><br></p>
<p>You'll see many, many, lines scrolling past. &nbsp;It'll probably end with:</p>
<p><img src="https://www.bollock.stream/images/14csf.png" width="748" height="75"/></p>
<p><br></p>
<p>(you can ignore the warning unless you've got maliciously-minded third parties SSH-ing into your VPS on a regular basis - which you hopefully don't if you've been following this guide).</p>
<p>Lastly, without closing your existing SSH window, check that you can still SSH in by opening a new SSH connection to the VPS.</p>
<p>If you can, all is well and the firewall should be doing its job - on to Step 6! &nbsp;If you happen to speak iptables, you can verify the firewall is in place with:</p>
<pre><code>iptables -L --verbose</code></pre>
<p><br></p>
<p>(which will list all active firewall rules).</p>
<p><strong>Oops - Now You Can't SSH In!</strong></p>
<p>If you can't log in, you've likely made a mistake somewhere in /etc/csf/csf.conf. &nbsp;Back on the SSH connection you left open (or using the emergency commandline on your VPS provider's control panel) open the csf configuration file with nano:</p>
<pre><code>nano /etc/csf/csf.conf</code></pre>
<p><br></p>
<p>and set:</p>
<p><code>TESTING = "1"</code></p>
<p>back to:</p>
<p><code>TESTING = "0"</code></p>
<p>Save the file (<code>Ctrl-O</code>) exit nano (<code>Ctrl-X</code>) and do:</p>
<pre><code>csf -r</code></pre>
<p><br></p>
<p>to reload the firewall in 'testing' mode.</p>
<p>Now check if you can log in via SSH - it may take up to 5 minutes for the firewall to drop automatically, so be patient and keep retrying.</p>
<p>If eventually you can, that likely proves something is wrong with your csf config file and you need to fix it as per the instructions in this section (Tip: check '<code>TCP_IN</code>' has your special SSH port listed).</p>
<p>Now that password based authentication is deactivated and there's a firewall running on your VPS, it's time for the next steps. &nbsp;</p>
<p>In Part 3 of this guide I'll cover installing software to detect rootkits and ensuring that your VPS can talk to you to tell you that something may need checking out.</p>
<p><br></p>
<h3>Buy Me A Beer</h3>
<p><br></p>
<p>If you find this guide at all useful, feel free to buy me a beer. Or part of a beer. Or a keg. Or two:- &nbsp;</p>
<p><code>LTC: LcorMkmmMpbVn2vBy5dUCGNDytnmHSW5vy</code></p>
<p><code>DOGE: D8PUDKoF7ZkHN43ELdRZZGNUv6iZ9xLScL</code></p>
<p><code>BCH: &nbsp;bitcoincash:qq2u2tcmwefpwh5lz5x9pndz30jathm93539m42dkj / 12z7QcbwYXirNj8EhzXHsuDMz6BjY9An6e</code></p>
</html>
👍  , ,
properties (23)
authordocdagbjort
permlinkmasternode-vps-security-the-basics-part-2
categorycrypto
json_metadata{"tags":["crypto","masternode","security","vps","linux"],"image":["https://www.bollock.stream/images/12csf.png","https://www.bollock.stream/images/13csf.png","https://www.bollock.stream/images/14csf.png"],"links":["https://steemit.com/crypto/@docdagbjort/masternode-vps-security-the-basics-part-1","https://configserver.com/cp/csf.html"],"app":"steemit/0.1","format":"html"}
created2018-01-20 19:15:57
last_update2018-01-20 19:15:57
depth0
children0
last_payout2018-01-27 19:15:57
cashout_time1969-12-31 23:59:59
total_payout_value1.404 HBD
curator_payout_value0.458 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12,530
author_reputation525,271,309
root_title"MasterNode VPS Security - The Basics - Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,933,368
net_rshares163,845,682,340
author_curate_reward""
vote details (3)