Steps to block Suspicious IP addresses to prevent attacks in Magento

Creating a website with Magento is just one part done. The other part is to protect it from malicious cyber attacks. Without proper security measures, your Magento store is always at risk of data manipulation, leaks, and various types of spams.

One way to protect your website is to blacklist suspicious IP addresses. If you believe a visitor is behaving suspiciously or trying to steal information, then you can blacklist or block the IP address he’s accessing from, In this article, we’ll explain how to do that to prevent such attacks.

5 Ways to Block IP addresses

There are not one, but five ways you can use to block a particular IP address from visiting your Magento store. So let’s explore the different methods available to you.

Use the ,htaccess file to restrict IP

The easiest and most basic way to block an IP address is to use the ,htaccess file. This is a hidden file from the general public that is used by admins to configure additional features if your website is hosted on Apache Web Server. You can do a whole lot of things with it including disallowing access to a particular IP address.

Here’s how to do it:
  • Log in to your admin panel
  • Locate your .htaccess file and open it
  • Add the following code to restrict an IP address:
Code:
<Directory>
order deny,allow
deny from 125.xx.99x.12x
</Directory>
The 125.xx.99x.12x is the IP address you want to block. If you want to block a specific country or a range, then you can replace it with 125.xx.99x.*/24. So your final code will look like:

Code:
<Directory>
order deny, allow
deny from 125.xx.99x.*/24
</Directory>
Use index.php

You can place restrictions at PHP level instead. This is considered much safer than editing .htaccess file. You just need to add the following code to your main index.php file:

Code:
<?php
$banned = array('129.xxx.1.1');
if(in_array($_SERVER['REMOTE_ADDR'], $banned))
{
exit();
}
Employ Magento Plugins

There are third-party plugins or extensions you can use to restrict IP addresses. The main benefit is that you don’t have to do any manual coding. Everything is taken care of by the plugin.

There are different modules to choose from. Some allow IP restrictions, while others offer complete IP management service where you can blacklist, whitelist, and literally do anything with all the IP addresses that come in to your website. Geo-IP Ultimate Lock is one such plugin. You can visit the Magento Marketplace to access all such plugins, but the key is to select the one that fit your requirements. Otherwise, you may end up overpaying.

Use a Firewall

There are software firewalls available that allow you to restrict incoming IP addresses. One such firewall is CSF. You can use the CSF Firewall in the cPanel server to block country-wide or single IP addresses.

Just open the CSF config file and add the country code as mentioned below:

Code:
CC_DENY = CN, CR
All of this is done at server level.

Use a Blacklist Feature

There is an in-built Blacklist option that ships with the Magento admin panel. You can use this to block suspicious IP addresses. You can block multiple IP addresses at once or even range of IP addresses.

When editing the files, there’s a risk of messing up with the codes. This can result in website getting down till the problem the fixed. Most of the time, it’s a typo error, a bracket error, or a space error. Therefore, it’s wise to take necessary backup before editing crucial files like .htaccess or index.php.

With precaution, you can easily blacklist IP addresses and later whitelist them if you find the need.
Author
bhawanisingh
Views
131
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from bhawanisingh

Top