How To Block an IP Address in Magento 2 to Prevent Cyber Attacks

How To Block an IP Address in Magento 2 to Prevent Cyber Attacks

The Magento 2 Block IP Address feature safeguards your ecommerce platform against potential cyber threats. It prevents malicious activities such as data manipulation, leaks, and various forms of spam. It also blocks IP addresses associated with unauthorized attempts to access your store's admin page.


This tutorial will guide you through effectively implementing and blocking IP addresses in Magento. It will also help you reinforce the protective layers of your online store.

Key Takeaways

  • Discover the importance of blocking IP addresses in Magento for enhanced store security.

  • Explore effective methods, including .htaccess configuration and Magento extensions, to restrict access to the admin page.

  • Learn to safeguard your Magento store from suspicious activities and potential fraud by blocking malicious IP addresses.

  • Understand the benefits of limiting login attempts to prevent unauthorized access and potential breaches.

  • Explore the dual approach of using both blacklist and whitelist settings for fine-tuned access control on your Magento 2 store.

  • Learn about the continuous protection blocking IP addresses provide to improve website performance and a safer user experience.

What is IP address blocking- Whitelist and Blacklist?

In Magento 2, blocking IP addresses is essential for online shop security. It protects against threats and unauthorized access, offering enhanced control to store owners. It stops spam bots and preserves your Magento site’s speed and efficiency. You can whitelist or blacklist specific IPs to enhance security and reduce fraudulent orders.

The blacklist is a compilation of IPs that automatically get blocked if they attempt to access your store's admin page. It reduces the chances of unauthorized access by hackers or unfamiliar entities.


The whitelist comprises approved IPs with permission to access the admin page. Only IPs listed in the whitelist can log in. It clearly distinguishes between valid and invalid access attempts. Store owners can also restrict the number of login attempts to prevent brute-force attacks. Blocking IP addresses builds customer confidence, ensuring transaction safety and trust among Magento users.

Ways to Block IP Addresses in Magento 2

1. Use the .htaccess file to restrict IP access

  1. Log in to your Magento admin panel.

  2. Locate and open the .htaccess file. It is used for configuring additional features on Apache web server-hosted websites.

  3. Insert the following code in the .htaccess file to block an IP address:


<Directory>
order deny,allow
deny from 125.xx.99x.12x
</Directory>

Note: Replace "125.xx.99x.12x" with the IP address you want to block.

  1. (Optional) If you want to block a range or specific country, modify the code like this:
<Directory>
order deny,allow
deny from 125.xx.99x.*/24
</Directory>

It will block the IP range 125.xx.99x.*/24.

  1. Save changes. Your .htaccess file is now configured to restrict access to the specified IP address or range.

2. Prevent and Block IPs in Magento Using the index.php File

  1. In the index.php file, create an array of IP addresses using this code:

<?php $deny = array("123.123.123", "233.233.233", "344.344.344");
if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) 
{
   exit();
} ?>

Note: Replace the example IP addresses in the array with the ones you want to block.

3. Install Magento Extension or Employ Magento Plug-ins

Magento Open Source does not provide a built-in feature to block IPs or countries. To restrict access to your website, install Magento security extensions.

  1. Visit the Magento Marketplace and browse the available modules restricting IPs.

  2. After installing any IP Addresses & Country Blocker plugin, expand the FME Extensions tab In your Magento Admin panel and select the Block IPs option.

  3. Click Add New to restrict IPs.

Step-by-step guide for installing IP blocking extensions in Magento 2

  1. Enter an IP or multiple IPs in the Block IP(s) field and click Save.

4. Add an ACL for Fastly in the Admin Panel

Access Control List (ACL) rules allow administrators to limit the user permissions. Fastly services for Adobe Commerce on cloud infrastructure using the Fastly CDN module for Magento 2 installed in the following environments:

  1. Pro Staging

  2. Production

  3. Starter Production (master branch)

To enable Blocking:

  1. Go to Magento admin > Stores > Configuration > Advanced > System > Full Page Cache > Fastly Configuration.

  2. You will see a screen like this under the Blocking tab.

Configuration process of ACL for Fastly in Magento 2 admin panel

  1. Create a new ACL with a list of IP addresses or subnets you want to block.

  2. Add it to the ACL list and block it by saving your changes.

5. Use a Firewall Server Configuration

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

Open the CSF config file and add the country code: CC_DENY = CN, CR

Note: This is performed at the server level.

Best practices for blocking IP addresses in Magento 2

  1. Regularly update blocked list: Maintain an up-to-date list of blocked IP addresses to ensure robust security. Regular updates protect your store from emerging threats and potential harm.

  2. Employ blacklist and whitelist settings: Leverage both blacklist and whitelist settings to fine-tune access control. Use the blacklist to block unwanted addresses and the whitelist to allow access to trusted ones. This dual approach enhances security and ensures authorized access.

  3. Utilize modules: Consider using specialized modules for IP address management. These extensions can simplify the process and provide additional features for IP blocking.

  4. Choose the most efficient method: Decide whether to block IP addresses at the Fastly level or by modifying the .htaccess file. Both methods offer control over access to your Magento 2 store.

Benefits of Blocking IP Addresses in Magento 2

  1. Improved Security: Blocking IP addresses in Magento 2 significantly enhances your Magento store's security by preventing unauthorized access and deterring cyberattacks. This control ensures that only legitimate visitors can access your admin panel, providing a safe environment for you and your customers.

  2. Prevent Fraud: Fraudulent activities can damage your store's reputation and financial health. Restricting access to malicious individuals reduces the likelihood of fraudulent transactions.

  3. Hackers Deterrence: Hackers often attempt to breach your store's system via the internet, frequently using single IP addresses. Blocking these malicious IP addresses is an effective way to prevent any form of cyber attack. Magento 2 simplifies this process with blacklisting and whitelisting features.

  4. Easy Implementation: Magento 2 offers user-friendly tools for IP address blocking, eliminating the need for specialized coding skills. This accessibility ensures that anyone can utilize these features to enhance their store's security without technical barriers.

  5. Enhanced Performance: Blocking unwanted IP addresses not only strengthens security but also contributes to improved website performance. By filtering out undesirable traffic, your website operates more efficiently. It leads to faster loading times and a smoother user experience.

  6. Continuous Protection: Implementing IP address blocking means your online store is protected around the clock. It remains shielded from potential threats and hacking attempts, reducing the risk of damage to your business over time.

FAQs

1. How can I limit access to the admin page of my Magento store to specific IP addresses?

To restrict access to the admin page, you can utilize the Magento 2 feature of blocking IP addresses. It helps safeguard your store from suspicious activities and potential threats. By configuring the .htaccess file or employing Magento extensions, you can effectively control access and enhance the security of your online shop.


2. What steps should I take if there is an unusual surge in web traffic to my Magento store?

If you notice a sudden increase in web traffic, it's essential to investigate the source. Utilize tools like Fastly ACL or web hosting firewalls to identify and block suspicious IPs. This approach helps prevent potential failures, ensuring the smooth operation of your Magento store.


3. How does blocking IP addresses in Magento 2 contribute to preventing fraud in my online shop?

Blocking your Magento store's IP addresses is a powerful protection against fraudulent activities. Restricting access to malicious individuals reduces the risk of fraudulent transactions. This measure enhances the overall security and reputation of your online business.


4. Are there any recommended best practices for updating the list of blocked IP addresses?

Regularly updating the list of blocked IP addresses is a best practice for maintaining robust security. It ensures your Magento store remains protected against emerging threats. Shop owners should proactively manage and modify the blacklist to stay ahead of potential security risks.


5. How can I employ both blacklist and whitelist settings for access control on my Magento 2 store?

The blacklist blocks unwanted addresses, while the whitelist allows access to trusted IPs. This dual approach enhances security. It ensures that only authorized users, including admins and shop owners, can access your Magento store's admin panel.


6. Is blocking IP addresses only limited to Magento 2, or can I implement it at the web hosting level?

Blocking IP addresses is not limited to Magento 2. It can be implemented at the web hosting level as well. Using firewalls like ConfigServer Security and Firewall (CSF) at the server level allows you to restrict incoming IP addresses or block entire countries. It provides additional protection for your Magento store beyond the platform-specific features.

Summary

Magento 2 Block IP Address safeguards your online store against cyber threats. It prevents unauthorized access and adds protective layers to your ecommerce platform. This tutorial covered how to block suspicious IP addresses. It covered various methods to restrict access, set login limits, and employ both blacklisting and whitelisting for enhanced control.


Choose reliable Magento hosting services to complement your IP address-blocking measures and provide a seamless experience for your customers.

Maria A.
Maria A.
Technical Writer

Maria has over five years of expertise in content marketing, specialising in Magento insights and industry trends. She excels in creating engaging content that resonates within the Magento community.


Get the fastest Magento Hosting! Get Started