• 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 , centos geoip block ssh , fail2ban block countries , fail2ban country ban , fail2ban by country , fail2ban block web uris , fail2ban ban ip country , fail2ban ban by country , fail2ban && country , ssh fail2ban geoip
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

  • Configure Apache With Fail2Ban on Ubuntu 18.04

    Here is How To Configure Apache With Fail2Ban on Ubuntu 18.04 to block more types of malicious attempts towards server to create a practical firewall.

  • iptables Basics : Chapter 2, Fail2Ban

    This is Era of Automation. iptables Rules Can Be Automated With Interactive Package Fail2Ban Which iptables Basics Chapter 2 Will Explain.

  • iptables Basics : Chapter 3, Configuring Fail2Ban With WordPress

    iptables Basics Chapter 3 Describes Configuring Fail2Ban With WordPress to Give Ultimate Protection. Thus We Secured OSI Model’s All Layers.

  • Installing Fail2ban on Ubuntu 14.04 : Nginx, HP Cloud

    Here is Guide With Steps on Installing Fail2ban on Ubuntu 14.04 on Server Running Nginx on HP Cloud. It adds a layer of security to SSH.

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

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
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