<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vitaliy Kolesov</title>
    <description>The latest articles on DEV Community by Vitaliy Kolesov (@vkolesov).</description>
    <link>https://dev.to/vkolesov</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F144432%2F3d3a3266-ecf2-4bb0-b647-b605cf23422b.gif</url>
      <title>DEV Community: Vitaliy Kolesov</title>
      <link>https://dev.to/vkolesov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vkolesov"/>
    <language>en</language>
    <item>
      <title>How to Protect Your Server From Hackers</title>
      <dc:creator>Vitaliy Kolesov</dc:creator>
      <pubDate>Wed, 27 Mar 2019 13:07:41 +0000</pubDate>
      <link>https://dev.to/vkolesov/how-to-protect-your-server-from-hackers-4j6l</link>
      <guid>https://dev.to/vkolesov/how-to-protect-your-server-from-hackers-4j6l</guid>
      <description>&lt;p&gt;This post was originally posted in &lt;a href="https://vkolesov.com/protected-your-server.html"&gt;my personal blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It is not a hard deal to make your server secure, but when a lot of routines comes, It is possible to forget to do this. In my case, ssh server was hacked in two weeks after I bought it. One morning my mail had a couple of the abuses from third-side people said "something" on my server tried to hack their servers. So, I should solve the problem quickly. 
&lt;/p&gt;


&lt;h2&gt;How to find the vulnerability&lt;/h2&gt;
In my case it was simple. I executed next command
&lt;pre&gt;&lt;code class="bash"&gt;cat /var/log/auth.log |  grep Accepted&lt;/code&gt;&lt;/pre&gt; 
and it returns me a list of  successful authorization to my server. From the all returned lines I found one IP that is not my own. So, In my case, the SSH was a source of vulnerability.





&lt;h2&gt;How to protect server&lt;/h2&gt;

&lt;p&gt;Briefly about what I needed to do immediately after buying the server.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;update &amp;amp;&amp;amp; upgrade the all packages on the server;&lt;/li&gt;
&lt;li&gt;Install &lt;i&gt;&lt;strong&gt;ufw&lt;/strong&gt;&lt;/i&gt; -  plain firewall;&lt;/li&gt;
&lt;li&gt;close all server's ports besides SSH, HTTP(s) ports;&lt;/li&gt;
&lt;li&gt;Install and config &lt;i&gt;&lt;strong&gt;fail2ban&lt;/strong&gt;&lt;/i&gt; utility. It helps  to analyze the /var/log/auth.log and ban some IPs if they make some wrong activity;&lt;/li&gt;
&lt;li&gt;change sshd config to accept the authorization only by private key.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;What to do?&lt;/h2&gt;

&lt;p&gt;If you were hacked, your server is infected, and you need to know how to research and clean it. The best way - recreating the VPS. That was my case. I had the server at hetzner. From their dashboard, it is possible to recreate (drop and create new) VPS with the same IP in one click. So, I did. After that on my local PC, was generated SSH keys with an ssh-keygen utility (is a part of standard OpenSSH package). The command bellow same for Linux and MacOS.&lt;/p&gt;

&lt;pre&gt;&lt;code class="bash"&gt;ssh-keygen&lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;It creates the pairs of keys in the ~/.ssh directory. After that running&lt;/p&gt;

&lt;pre&gt;&lt;code class="bash"&gt; ssh-copy-id you_user@your_server_id&lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;will upload your "just created" public key to the server. Next step, log in to the server and edit the  config file for sshd:&lt;/p&gt;

&lt;pre&gt;&lt;code class="bash"&gt;nano /etc/ssh/sshd_config&lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;In the config  make changes for PasswordAuthentication variable&lt;/p&gt;

&lt;pre&gt;&lt;code class="ini"&gt;PasswordAuthentication no&lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;This instruction close the possibility to connect with the password (only connection with private key accepted)&lt;br&gt;
&lt;/p&gt;


&lt;h2&gt;Installing and tuning ufw and fail2ban&lt;/h2&gt;

&lt;p&gt;I used ubuntu on server, so installation is &lt;/p&gt;

&lt;pre&gt;&lt;code class="bash"&gt;apt install ufw fail2ban&lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;next step open only ssh, https port on server so:&lt;/p&gt;

&lt;pre&gt;&lt;code class="bash"&gt;ufw allow ssh
ufw allow 80
ufw allow 443
&lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;and enable the ufw:&lt;/p&gt;

&lt;pre&gt;&lt;code class="bash"&gt;ufw enable&lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;Next step is configuring the fail2ban utility&lt;/p&gt;

&lt;pre&gt;&lt;code class="bash"&gt;# make a copy of default config (this copy will overload default params according to manual)
cp  /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
nano /etc/fail2ban/jail.local
&lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;in there find &lt;i&gt;&lt;strong&gt;"banaction = "&lt;/strong&gt;&lt;/i&gt;  and set &lt;i&gt;&lt;strong&gt;ufw&lt;/strong&gt;&lt;/i&gt;  as a value. After that reload fail2ban&lt;/p&gt;

&lt;pre&gt;&lt;code class="bash"&gt;fail2ban-client reload&lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;According to this simple config, any three wrong attempts from particular IP get to access to ssh port will ban this IP for 10 minutes. Personally, I changed the ban time for 7 days.&lt;br&gt;
How to check the status:&lt;/p&gt;

&lt;pre&gt;&lt;code class="bash"&gt;fail2ban-client status sshd&lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;will return in my case&lt;/p&gt;

&lt;pre&gt;&lt;code class="bash"&gt;
Status for the jail: sshd
|- Filter
|  |- Currently failed: 1
|  |- Total failed: 6
|  `- File list:   /var/log/auth.log
`- Actions
   |- Currently banned: 1
   |- Total banned: 2
   `- Banned IP list:   187.109.168.150
&lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;You can see, that one IP already blocked by the firewall. Same things possible to see with ufw report:&lt;/p&gt;

&lt;pre&gt;&lt;code class="bash"&gt;ufw status
Status: active

To                         Action      From
--                         ------      ----
Anywhere                   REJECT      187.109.168.150           
80/tcp                     ALLOW       Anywhere                  
22                         ALLOW       Anywhere                  
443                        ALLOW       Anywhere           &lt;/code&gt;&lt;/pre&gt; 

&lt;p&gt;The fail2ban can be configured to send reports to your email if some IP has been banned. &lt;br&gt;
&lt;/p&gt;


</description>
      <category>linux</category>
      <category>tutorial</category>
      <category>security</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
