• 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 » Fail2ban GeoIP Action Script to Block SSH by Country

By Abhishek Ghosh November 21, 2016 8:32 pm Updated on November 21, 2016

Fail2ban GeoIP Action Script to Block SSH by Country

Advertisement

In our previously published guide, we used a bash script to limit SSH access by countries to save ourselves from SSH Brute Force attack. But, Instead of Using Bash Script to Restrict SSH by Countries Based on GeoIP, We Can Create Fail2ban GeoIP Action Script to Block SSH by Country. We have not said this one as first method because the usage way for these two methods are quite different from OSI model. The bash script is great way to reduce load but fail2ban has the advantages of log, ban from iptables. We can not give warranty about effectiveness as it is not tested by anyone else.

 

To Do Before Following the Steps To Configure Fail2ban GeoIP Action Script to Block SSH by Country

 

First and most important is to have fail2ban installed and configured properly, like we have described in iptables basics chapter 2 supplying configurations. Here is that guide’s custom jail.conf config on GitHub as Gist.

It is probably better to run fail2ban for few days to month to have an experience of usage. It is practical to read our previous guide where we used a bash script to limit SSH access by countries. That guide has mentioned practical needs like servers from other countries to test yourself. We did more works with fail2ban with WordPress on iptables basics chapter 3. From that guide we worked on filter.d directory, which is located (in case of Ubuntu server, CentOS server) :

Advertisement

---

Vim
1
/etc/fail2ban/filter.d

SSH to your server and cd to that location. Do a ls. You’ll see lot of action scripts. Here is fail2ban official website for documentation :

Vim
1
http://www.fail2ban.org

Fail2ban GeoIP Action Script to Block SSH by Country

 

If You Followed Our Previous Guide To Configure SSH With Bash Script to Block SSH by Country

 

In case you followed our previous guide, then you need to do some extra steps.

First open /etc/hosts.deny and comment out or remove the line :

Vim
1
sshd: ALL

Save the file. Now open /etc/hosts.allow and comment out or remove the line :

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

Save the file. Restart SSH services :

Vim
1
service ssh restart

Login from different servers to check. These steps will make sure that you have reversed the old configuration to default.

 

Steps To Configure Fail2ban GeoIP Action Script to Block SSH by Country

 

Technically you already have GeoIP installed :

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

We actually use the file sshd.conf file located at /etc/fail2ban/filter.d. There is another directory named /etc/fail2ban/action.d. You can visually inspect the files at GitHub repo :

Vim
1
https://github.com/fail2ban/fail2ban/tree/master/config

In this location on server :

Vim
1
/etc/fail2ban/action.d

there is a file named iptables-multiport.conf which is this one in GitHub repo :

Vim
1
https://github.com/fail2ban/fail2ban/blob/master/config/action.d/iptables-multiport.conf

# Option: action ban starts at Line 35. We can create a new modified file with custom action ban and action unban. Which will look like this file on GitHub. We can wget that file on /etc/fail2ban/action.d location on our server :

Vim
1
2
cd /etc/fail2ban/action.d
wget https://raw.githubusercontent.com/AbhishekGhosh/fail2ban-geo/master/action.d/geo-iptables-multiport.conf

By default we enabled only India (IN) and US. You should change it.

We will add only one action named banaction = geo-iptables-multiport on default section of /etc/fail2ban/jail.local :

Vim
1
2
3
4
5
6
7
8
9
10
...
[default]
 
enabled = true
port    = ssh
filter  = sshd
banaction = geo-iptables-multiport
logpath  = /var/log/auth.log
maxretry = 3
..

In our fail2ban guides we configured /etc/fail2ban/jail.conf and copied it to /etc/fail2ban/jail.local`. Here we will edit only the local one :

Vim
1
2
3
4
5
# edit
nano /etc/fail2ban/jail.local
# paste/type the config & save
# restart fail2bain
sudo service fail2ban restart

We can check the status :

Vim
1
2
3
4
# check fail2ban service
sudo service fail2ban status
# check whether we did correct or not
sudo fail2ban-client status

Tagged With fail2ban geoip , fail2ban block countries , centos geoip block ssh , fail2ban && country , fail2ban ban by country , ssh fail2ban geoip , fail2ban block web uris , fail2ban by country , fail2ban country ban , fail2ban country block

This Article Has Been Shared 907 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 Fail2ban GeoIP Action Script to Block SSH by Country

  • 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.

  • How to Add DMARC, DKIM, SFP to Avoid Email Spoofing

    Commonly, Transactional Email Services Like Mandrill is used With Google Apps. Here is How to Add DMARC, DKIM, SFP to Avoid Email Spoofing.

  • 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.

  • Fix Nginx OCSP ERROR : OCSP Response Expired

    Here is How to Fix Nginx OCSP ERROR – OCSP Response Expired After Enabling OCSP Stapling on Nginx. Error is Due to Certain Criterion of RFC.

  • Nginx Content Security Policy Example Syntax For Normal Websites

    Here is Nginx Content Security Policy Example Syntax For Normal Websites Which Will Not Throw Any Error. You Can Use With CSP Report Only Too.

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 an Automatic Ethanol Fireplace February 8, 2023
  • Disadvantages of Cloud-Native Computing February 7, 2023
  • Projector Screen Basics February 6, 2023
  • What is Configuration Management February 5, 2023
  • What is ChatGPT? February 3, 2023

About This Article

Cite this article as: Abhishek Ghosh, "Fail2ban GeoIP Action Script to Block SSH by Country," in The Customize Windows, November 21, 2016, February 8, 2023, https://thecustomizewindows.com/2016/11/fail2ban-geoip-action-script-block-ssh-country/.

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