• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » Limit SSH Access By Country To Save From SSH Brute Force

By Abhishek Ghosh November 20, 2016 2:51 pm Updated on November 20, 2016

Limit SSH Access By Country To Save From SSH Brute Force

Advertisement

SSH Brute Force is one of the old way which nicely works, proof is latest attacks on Dyn DNS. The target is to bring DDoS. Frankly, the solutions like anti-DDoS for IaaS, anti-DDoS for PaaS are very costly and without hardware it is difficult to terminate attack. Many a times, the new sysadmin can not understand the odd errors on SSH like pam_unix(sushi:auth): authentication failure, which are indicative of flooding, sometimes Nginx starts to throw PHP-FPM errors when attack is combined with XML-RPC attack. Here is How To Limit SSH Access By Country To Save From SSH Brute Force & Filter With Fail2Ban.

 

Things To Do Before Reading This Guide on How To Limit SSH Access By Country

 

The way we are going to describe is one extra layer of security. We suggest the unused readers to read iptables basics chapter 1, iptables basics chapter 2, iptables basics chapter 3. The guides are combined with Fail2Ban. If you are WordPress user, then add Best WordPress Brute Force Plugin too. Basic thing is that, we have to control from all possible layers of OSI model. The danger of following this guide by a newbie is completely locked in situation, specially if the server has no way to access via internal network. Most Cloud Servers has virtual console, which allows to reset the settings and SSH. We tested our method (although we have no discovered it) numerous times, still be careful before hitting exit from the server you used the method for the first time.

We suggest to have different servers in different countries, like ArubaCloud’s 1GB 1 Euro server, VPSDime’s 6GB $7 server to use their hosting countries as way to test SSH. Apply the rules and test many ways to check and re-check before hitting exit from the server you are going to use it.

Advertisement

---

We will block at step 1 on this diagram :

limit-ssh-access-by-country-to-save-from-ssh-brute-force

 

Things To Do Before Reading This Guide on How To Limit SSH Access By Country

 

We are describing for Ubuntu 16.04, method will be same for CentOS. First you need to install these two packages :

Vim
1
apt-get install geoip-bin geoip-database

If you followed manual method before like that we described for using Nginx Geo-IP module for access log, then running the above commands may tell you that you are using manual method for Geo IP. Such output will not make any difference in the next steps.

cd to /usr/local/bin/ and create a file named sshfilter.sh :

Vim
1
2
cd /usr/local/bin/
nano sshfilter.sh

Copy-paste this script :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
 
# country codes to ACCEPT
ALLOW_COUNTRIES="US UK FR"
 
if [ $# -ne 1 ]; then
  echo "Usage:  `basename $0` <ip>" 1>&2
  exit 0 # return true in case of config issue
fi
 
COUNTRY=`/usr/bin/geoiplookup $1 | awk -F ": " '{ print $2 }' | awk -F "," '{ print $1 }' | head -n 1`
 
[[ $COUNTRY = "IP Address not found" || $ALLOW_COUNTRIES =~ $COUNTRY ]] && RESPONSE="ALLOW" || RESPONSE="DENY"
 
if [ $RESPONSE = "ALLOW" ]
then
  exit 0
else
  logger "$RESPONSE sshd connection from $1 ($COUNTRY)"
  exit 1
fi

I have kept it on GitHub as gist so that you can wget the raw file from server. Where from the script came we do not know! This is used for this purpose. Notice the ALLOW_COUNTRIES="US UK FR" line. By default this script will allow US, UK and France. You should edit it. We recommend to keep minimum 2 countries. Other countries will be not allowed. Unrecognised countries will be allowed.

chmod the file properly to make it executable :

Vim
1
chmod +x /usr/local/bin/ipfilter.sh

Now open /etc/hosts.deny file :

Vim
1
nano /etc/hosts.deny

Add these lines at the end of that file and save it :

Vim
1
2
sshd: ALL
vsftpd: ALL

If you use vsftpd then you’ll add the second line. For only SSH, first line is enough. So we have blocked all to SSH. Now open /etc/hosts.allow and these lines at the end of that file and save it :

Vim
1
2
sshd: ALL: aclexec /usr/local/bin/sshfilter.sh %a
vsftpd: ALL: aclexec /usr/local/bin/sshfilter.sh %a

Now restart the SSH service :

Vim
1
service ssh restart

Now test SSH from various servers from various countries. It is funny to edit the file /usr/local/bin/sshfilter.sh to disallow US and try from US server. This can happen if you wrongly block yourself! There is nothing more to write as guide.

Now first check various log files :

Vim
1
2
3
4
5
cat /var/log/messages
cat /var/log/fail2ban.log
cat /var/log/auth.log
tailf /var/log/syslog
# exit with [control] + [z]

Empty all of them :

Vim
1
2
3
4
echo " " > /var/log/messages
echo " " > /var/log/fail2ban.log
echo " " > /var/log/auth.log
echo " " > /var/log/syslog

Normally running websites always have some sort of attacks running. Run cat after few minutes, you’ll see /var/log/auth.log will show entries like :

Vim
1
2
aclexec returned 1
Nov 20 18:54:00 thecustomizewindows sshd[15644]: refused connect from 125.88.158.219 (125.88.158.219)

What exactly happening, Fail2Ban is getting a guard with this setup – RAM usage is decreasing when such attack is regular matter. Major disturbing countries are Korea, China, India, Russia etc. When they will try via those allowed countries, Fail2Ban will catch them. So the total security becoming :

Vim
1
iptables - SSH Deny - Fail2Ban

This method itself not very powerful, but in combination it does great work. Basic disadvantages of this method is locking up own self, allowing unidentified countries, allowing 1-2 countries where from you’ll SSH. Port knocking is good way but many of us dislike it.

Tagged With how to restrict ssh access by country

This Article Has Been Shared 949 Times!

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Limit SSH Access By Country To Save From SSH Brute Force

  • How To Generate Let’s Encrypt ECC SSL (ECDSA) Certificate

    Few Costly Paid SSL Have Support For ECC. Let’s Encrypt Supports For Free. Here is How To Generate Let’s Encrypt ECC SSL (ECDSA) Certificate.

  • SELinux and Security in the Context of Cloud Servers

    SELinux Was Developed By United States National Security Agency (NSA). SELinux and Security in the Context of Cloud Servers Can Be Questionable.

  • Linode Cloud Server Review (Taking HP Cloud as Standard)

    We Tested Linode Cloud Server Review Taking HP Cloud Standard. Free RedHat OpenShift is better for testing, OVH is better for cost reduction.

  • Enable HTTP Public Key Pinning (HPKP) Nginx With report-uri

    HPKP is a security feature is to prevent fraud TLS certificates. Here Are the Steps to Enable HTTP Public Key Pinning (HPKP) on Nginx.

  • Ubuntu 14.04 Nginx HTTP/2 Directive, NPN & ALPN

    Here is a Handy Guide For the New Users Around Ubuntu 14.04 Nginx HTTP/2 Directive, NPN & ALPN. Sadly, HTTP/2 is Not Exactly Easy Like SPDY.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • What is Voice User Interface (VUI) January 31, 2023
  • Proxy Server: Design Pattern in Programming January 30, 2023
  • Cyberpunk Aesthetics: What’s in it Special January 27, 2023
  • How to Do Electrical Layout Plan for Adding Smart Switches January 26, 2023
  • What is a Data Mesh? January 25, 2023

About This Article

Cite this article as: Abhishek Ghosh, "Limit SSH Access By Country To Save From SSH Brute Force," in The Customize Windows, November 20, 2016, January 31, 2023, https://thecustomizewindows.com/2016/11/limit-ssh-access-by-country-to-save-from-ssh-brute-force/.

Source:The Customize Windows, JiMA.in

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT