Friday, June 26, 2015

How to Install Fail2Ban to Prevent SSH Brute Force

Hello, now i want to share how to secure our VPS, VPS has many weak spot to breach, especially in opened port. If you follow my tutorial here, it's not makes your VPS secure, just decrease the chance being breach by unauthorized person, i didn't say hacker, just unauthorized person.
What i want to share is how to prevent your VPS being brute forced through SSH, an old school method, but sometimes it's really effective for VPS with weak password, are you sure your password are not listed in brute force dictionaries? many good dictionaries out there, so beware about your password, you must read tutorial about create strong password (How to Create Strong Password), or you can search in google.com, many good tutorial about create password.

how to install fail2ban to prevent bruteforce ssh

My VPS is sponsored by EthernetServers.com, and i already ask them for permission to brute force it in order for review only and education purpose, but remember, without special permission by asking directly to EthernetServers team or person in charge there, it will cause permanently ban, so read again the Terms of Service.

Here's part of EthernetServers TOS (Terms of Service)

Server AbuseAny attempt to undermine or cause harm to a server or customer of Ethernet Servers is strictly prohibited. As our customer you are responsible for all your accounts. Should you violate the Terms of Services outlined within, your account will be cancelled without chance of refund.

Examples of unacceptable content or links: - Hacking Tools and/or programs
>> Including hacking script using python, bash, etc.
>> Including Metasploit, even if you are a pentester, so there are no exception for this matter.
>> Including Port Scanner too :P, so no hacking activity too, even if your reason is for security purpose.

Now let's Start, first i will scan for opened port.

Host is up (0.036s latency).
Not shown: 994 closed ports
PORT     STATE    SERVICE
22/tcp   open     ssh
80/tcp   open     http
161/tcp  filtered snmp
1723/tcp open     pptp
5901/tcp open     vnc-1
6001/tcp open     X11:1

Port 22 is open, so let's brute port 22, with username root
Let's assume we don't know the password for root, and using dictionary file for brute it.

*************************************
*SSH ----------- Ver. 0.2           *
*Coded by --------------------      *
*Ed---------------earch             *
*lar--------gmail.com               *
*************************************

HOST: 198.23.189.37 Username: root Password file: pass10.txt
====================================================================
Trying password...

Auth OK ---> Password Found: letmein

Times -- > Init: 0.06 End: 1.15

we got the password, which is letmein, let's try using another bruteforcer with the famous Ncrack

Starting Ncrack 0.4ALPHA ( http://ncrack.org ) at 2015-06-26 17:54 CEST

Discovered credentials on ssh://198.23.189.37:22 'root' 'letmein'
ssh://198.23.189.37:22 finished.

Discovered credentials for ssh on 198.23.189.37 22/tcp:
198.23.189.37 22/tcp ssh: 'root' 'letmein'

Ncrack done: 1 service scanned in 18.14 seconds.
Probes sent: 6 | timed-out: 0 | prematurely-closed: 0

Ncrack finished.

Done, that two bruteforcer can detect the password, not more that 1 minute, it is because i already know the right password and i put my password in dictionary, so it will be detected easily, and because i only have 10 word in my dictionary, so it only take a minute less, in the real probe, it takes hour maybe day to brute using dictionary, and it consume lot of resource.

Now, let's see how to prevent bruteforce using fail2ban.
First, you must install fail2ban.

wget https://raw.githubusercontent.com/zaq111/bash/master/angelimus-install-fail2ban.sh
sh angelimus-install-fail2ban.sh

Then wait, it less than 1 minute, there are not much step, i already create auto install for you.
After install, you will automatically see vi editor with file jail.local opened

# Fail2Ban jail base specification file
#
# HOW TO ACTIVATE JAILS:
#
# YOU SHOULD NOT MODIFY THIS FILE.
#
# It will probably be overwitten or improved in a distribution update.
#
# Provide customizations in a jail.local file or a jail.d/customisation.local.
# For example to change the default bantime for all jails and to enable the
# ssh-iptables jail the following (uncommented) would appear in the .local file.
# See man 5 jail.conf for details.
#
# [DEFAULT]
# bantime = 3600
#
# [ssh-iptables]
# enabled = true

It will looks like above, you can scroll it down, and edit it depend on your need, let's scroll down a bit, until you find similar like this (see below)

[DEFAULT]

# "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not
# ban a host which matches an address in this list. Several addresses can be
# defined using space separator.
ignoreip = 127.0.0.1/8

# External command that will take an tagged arguments to ignore, e.g. <ip>,
# and return true if the IP is to be ignored. False otherwise.
#
# ignorecommand = /path/to/command <ip>
ignorecommand =

# "bantime" is the number of seconds that a host is banned.
bantime  = 600

# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime  = 600

# "maxretry" is the number of failures before a host get banned.
maxretry = 3

ignoreip >> it means, the ip address in list will not caught by fail2ban if you have wrong input password more than "n" times.

bantime >> it means if someone try to login / brute, it will ban the IP if wrong password for "n" times

maxretry >> this is maximum retry (n) for wrong password. By default, it is 3 times wrong password, you will get banned by 600 (equal to 10 Minute)

if you already configure, type :wq to save and exit from vi editor, and type:

sudo service fail2ban start

It's done, fail2ban already installed and configured, now let's check again by scanning the port

Host is up (0.088s latency).
Not shown: 994 closed ports
PORT     STATE    SERVICE
22/tcp   open     ssh
80/tcp   open     http
161/tcp  filtered snmp
1723/tcp open     pptp
5901/tcp open     vnc-1
6001/tcp open     X11:1

then, try to brute force with the sampe method like before we use.

*************************************
*SSH ----------- Ver. 0.2           *
*Coded by --------------------      *
*Ed---------------earch             *
*lar--------gmail.com               *
*************************************

HOST: 198.23.189.37 Username: root Password file: pass10.txt
====================================================================
Trying password...

Exception in thread mypassw0rd
:
Traceback (most recent call last):
  File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "brutessh.py", line 44, in run
    t = paramiko.Transport(hostname)
  File "/home/transport.py", line 235, in __init__
    sock.connect((hostname, port))
  File "<string>", line 1, in connect
error: [Errno 111] Connection refused

Exception in thread r00tismine
:
Traceback (most recent call last):
  File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "brutessh.py", line 44, in run
    t = paramiko.Transport(hostname)
  File "/home/transport.py", line 235, in __init__
    sock.connect((hostname, port))
  File "<string>", line 1, in connect
error: [Errno 111] Connection refused

Exception in thread stayawayfromr00t
:
Traceback (most recent call last):
  File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "brutessh.py", line 44, in run
    t = paramiko.Transport(hostname)
  File "/home/transport.py", line 235, in __init__
    sock.connect((hostname, port))
  File "<string>", line 1, in connect
error: [Errno 111] Connection refused

Exception in thread letmein
:
Traceback (most recent call last):
  File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "brutessh.py", line 44, in run
    t = paramiko.Transport(hostname)
  File "/home/transport.py", line 235, in __init__
    sock.connect((hostname, port))
  File "<string>", line 1, in connect
error: [Errno 111] Connection refused

it's a long list, i just entered a few line that show Connection refused, the password is letmein, but it not accepted, and refused too, because it's retry more than 3 times, but how about if you password is in first line? or second? you must be unluck person :P, but at least your VPS can be a "little secure" newbie who like brute force method will not break in into your VPS :P.

now let's try using Ncrack

Starting Ncrack 0.4ALPHA ( http://ncrack.org ) at 2015-06-26 18:27 CEST

caught SIGINT signal, cleaning up

Ncrack just stand like that, like there are nothing to do, until i press Ctrl+C to break it.
Now try scan again to see our port status.

Not shown: 994 closed ports
PORT     STATE    SERVICE
22/tcp   filtered ssh
80/tcp   open     http
161/tcp  filtered snmp
1723/tcp open     pptp
5901/tcp open     vnc-1
6001/tcp open     X11:1

Our port is filtered, because we are scanning from IP in banned period.
It's done, above are very-very simple and easy technics, but do your VPS can survive from very simple easy technics? thanks for reading, see you in next security testing and how to prevent it. And Big Thanks to EthernetServers.com for providing free VPS.

0 comments:

Post a Comment