Detect network-based DDOS attacks and block them in bulk using IPtables in Plesk

DDOS attacks are not always from an army of malicious network of zombie PC’s accross the world. Sometimes, legitimate services such as search engine bots, or other bots such as Semrush or DataForSeo consume too much resources on your hosting server and use > 100% of your CPUs. All the sites and services on your server get slow, and you get spammed by tons of alerts on your email and mobile.

This post was writting with Plesk 18 (Obsidian) and Debian 9 in mind.

Detecting attacks from Plesk’s 360° Monitoring

You can get 1 free server monitor at Plesk 360, if it sends you the kind of alerts shown below then you are probably under attack because something is consuming too many resources on your box.

Inspect the server activity

SSH into the machine and launch the htop command.
The example below is pretty cool but if everything is red and your load average is > to your number of CPUs or VPCUs, you have to mitigate the situation.

Sort the processes by CPU% (press F6, then arrow-down key navigate to PERCENT_CPU and press Enter).

If the top process is Apache / Nginx / PHP and belongs to a user who owns a website that you are hosting, note it down and let’s inspect this!

Acknowledge suspicious activity

If the user owns multiple websites in your Plesk installation, you can use some advice at Plesk KB: How to diagnose a DoS/DDoS attack and find websites under attack on a Plesk server, in particular the second section that describes the counting of the requests per website by parsing the logs for a string that matches a date or hour:

Replace ’02/Oct/2022′ by a relevant string

root@***:/var/www/vhosts/*****/logs# cd /var/www/vhosts/system
root@***:/var/www/vhosts/system# for i in *;do echo -n "$i "; grep '02/Oct/2022' $i/logs/access_ssl_log | awk '{print $1}' | wc -l;done > ~/requests
root@***:/var/www/vhosts/system# cat ~/requests
site1****** 0
site2****** 6
site3****** 21
site4****** 9858
site5****** 52
site6****** 512
site7****** 109
site8****** 4521
site9****** 3449
site10***** 5
site11***** 571
site12***** 309
site13***** 16 1254

Here we can see that site4 is consuming ~half of the server resources.

Take a look at the access logs directory of the website that belongs to the user whose processes are killing your CPU. The logs are located in /var/www/vhosts/domain-name/logs, use the ls -lah command to find large log files. In the example below, 3.7 GB of logs since 6:38 this morning is horrifyingly huge!

Use the tail -f access_ssl_log command to get an idea of what is filling this log so fast:

We can see that Semrush bot and DataForSeo bots are sending many queries.

Bulk blocking

There are many was to block requests, especially when it comes to bots, for example:

  • Custom rules in your .htaccess (or Nginx rules)
  • Custom rules in your robots.txt file
  • Relying on the plesk-apache-badbot fail2ban built-in rules
  • Using an extension such as (D)DoS Deflate Interface

Custom rules imply changing your customer’s website, which you certainly don’t want to do.
The last two ideas didn’t auto-mitigate the attack.
Therefore we have to handle the situation manually.

Single-IP blocking

If all the requests come from the same IP, simply create a basic firewall rule from Plesk admin:

Don’t forget to click « Apply » at the second step after clicking « OK »!

Blocking a company or ISP, aka range-blocking

It may not be a wise idea to block a whole company or ISP, but we’re trying to mitigate an attack here! Of course you don’t want to block all users from all your country’s ISPs at once, but there’s a good chance that you can block large company networks (including web and cloud hosters) without compromising end-users access to the websites you host.

From the logs below, let’s have a look at this DataForSEO’s IP:

Find whose IP it is

I like to use for this because they gather the data from all RIRs at once (RIPE for Europe, ARIN for USA, etc), it’s more convenient.

From the results we can tell that the said IP belogs to Contabo:

It is an hosting provider, the one used by DataForSEO’s bot.

Is it safe to block all traffic to a hosting provider?
Heck yes!
We want to preserve resources for real human users, not bots.

Beware: by doing so, you’ll be blocking Contabo and all their users, including legitimate users who have setup a VPN and access to your website behind it.

Find all the IPs that this company owns

As you know, a hosting company has many IPs, so we want to find them all – not only the one used by the bots. By definition, bots are deployed under multiple instances, which all have different and changing IPs.

What we are looking for is the « Autonomous System Number » (ASN), in this case: AS51167

Here we can see that the source is RIPE, which is the european RIR.

The easiest way to find all the IPs that belong to an ASN is by using Just type in the ASN and all its IP ranges will pop:

Bulk-block IPs in IPtables through Plesk

It would be cumbersome to copy/paste all IPs one by one in the firewall Custom Rule Plesk form.
Here’s my solution to do it en masse.

First, copy-paste all the IP ranges inside a blank Excel spreadsheet:

Then, paste the column A contents into Visual Studio Code to edit multiple lines at once.
It also works with SublimText (shortcuts may vary).

Type ctrl + a to select all,
Type ctrl + shift + i to enter multi-line edit mode,
Type 🡼 to go to the begining of each line and remove extra whitespaces,
Type End to go to the end of each line and add a coma,
Type Del to remove all linebreaks and concatenate all lines into a single one,
Type spacebar to add an extra spacer,
Type End to go to the end of the single line,
Type backspace twice to remove the last space and coma,

You should end up with this:

Now that you have a single-line, coma-separated list of IP ranges, you need to insert it into to IPtables.
However the Plesk firewall Custom Rules form doesn’t accept this syntax:

So you have to SSH into your server again and do it with these commands (obviously you must replace my Contebo’s list with yours):

root@***:~# /usr/local/psa/bin/modules/firewall/settings -s -name 'ddos-contabo' -direction input -action deny -ports '' -remote-addresses ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
Firewall rule was successfully created; rule id: '403'.

root@***:~# /usr/local/psa/bin/modules/firewall/settings -a
Firewall rules were successfully activated, use --confirm command within 60 second(s) to confirm activation, otherwise all changes will be rolled back.

root@***:~# /usr/local/psa/bin/modules/firewall/settings -c
Applying and confirmation of firewall changes should be done via different ssh sessions

Note that the « Confirm » command (last one) must be done from another SSH session.
That is to avoid locking yourself out of your server!

Now you should enjoy seeing your server activity decrease in the htop monitoring screen!

Relevant services you may need to block during your attack mitigation period

ServicesIP ranges
Semrush (2)
Semrush (3)
China Unicom
Triolan (MJ12bot)

Quelques bots célèbres :

Crédit :

Bon courage.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.