Accueil > Réseau, Sécurité > Preventing a DDoS from China, a Great Firewall of China gone rogue?

Preventing a DDoS from China, a Great Firewall of China gone rogue?

19/05/2024 Categories: Réseau, Sécurité Tags: , , ,
Print Friendly, PDF & Email


On the 25th of January one of my sites was struggling to stay up, my “Dos Deflate” emails were popping into my inbox at a great frequency.

A quick run of the following command:

netstat -tn 2>/dev/null | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -nr | awk ‘{print $2}’

Gave me a list of addresses connected to the site. The number of addresses was way way above the number normally connected to the site.  A site which although modestly doing 100,000s of page views per day will normally have only 300-500 port 80 connections at any one instance.  This time however we had a 2 or 3 thousand.

A quick copy and paste into the following tool (max 2000 ips per lookup) which is very usefully for bulk ip address to location lookups, yielded the following # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China # CN China
 (And on and on)

We clearly had an issue with China, but a quick log in to Google Analytics indicated almost no real-time traffic visiting the site.

Due to the sheer volume of connections for the next 45 mins, I was regularly running this command pasting into excel, then bulk iptabling the the ip address as well as the 65,000 neighbouring addresses.

sudo iptables -A INPUT -s -m comment –comment “China IP” -j DROP
sudo iptables -A INPUT -s -m comment –comment “China IP” -j DROP

etc etc.

Lire aussi:  How To Set Up an Iptables Firewall to Protect Traffic Between your Servers

It didn’t take to long until around 100 million ip addresses had been blocked by the rules. This activity brought the site back into a more functioning state, with page load times drastically improved.   Clearly a perpetual activity such as this was not the solution. So I got the code editor up and knocked together in a hurried fashion the following python file, which blocks non whitelisted visitors from china, can block “all” of china, block project honey pot threats and also block ip addresses with over 200 connections.

Feel free to use/modify as you wish, it is still a bit rough and ready but works well(remember to get a project honeypotAPI key):

I set that to run as a cron job every 15 mins.

*/15 * * * * root /usr/bin/python /root/ >/dev/null 2>&1

This removed the manual element of the process and allowed me to sleep at night.

The reality of the siltation was that although our site was back and running with almost no performance issues, the China IP blocklist wasn’t all encompassing and we were still getting ip address connections from China hitting the site. So yesterday (about two weeks after the inital issues) I enabled Apache logging (we have it turned off for performance reasons) to see what was still getting through.

Even with huge ranges of ip addresses blocked we were will getting traffic from China. The example shows three page requests per second: - - [05/Feb/2015:16:45:43 +0000] "GET /announce.php?info_hash=%92%5F%1C%F3%E2%0Fj%2D%BD%C7ux%E0%B8t%F0%28%60%E5%CA&peer_id=%2DSD0100%2D%1E%D5%60u%40%AF%8A7%B2vB%0E&ip= HTTP/1.0" 301 1186 "-" "Bittorrent" - - [05/Feb/2015:16:45:43 +0000] "GET /announce.php?info_hash=%AC%F7%9E%EF%DC%AE%94%25%0D%9C%E9%0A%D6%D5R%27%F8Tj%DE&peer_id=%2DSD0100%2DA%2B%29%5D%2B%C3%2E%2C%D1%D5I%CE&ip= HTTP/1.0" 301 1222 "-" "Bittorrent" - - [05/Feb/2015:16:45:43 +0000] "GET /announce?info_hash=%92%5F%1C%F3%E2%0Fj%2D%BD%C7ux%E0%B8t%F0%28%60%E5%CA&peer_id=%2DSD0100%2D%1E%D5%60u%40%AF%8A7%B2vB%0E&ip= HTTP/1.0" 301 1178 "-" "Bittorrent"

So it looks to be that for some reason China is incorrect sending Bittorrent traffic to the site, effectively weaponising the Great Firewall of China or that the site’s IP address has been added to 1000s of torrent trackers to initiate a denial of service attack.

Lire aussi:  Prevent DDoS with iptables

Solution #1 – Block “Bittorrent” user agent requests in .htaccess

As things currently stood any request for a page on our site that didn’t exist would send the visitor to the homage via a 301 redirect.  This was putting load on our server.  With almost all the bad traffic having the “Bittorrent” user agent lets send the 301 redirect elsewhere using “.htaccess”. See the code below:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_USER_AGENT} ^Bittorrent [NC]
  RewriteRule ^ [R=301,L]

This is better than the default behaviour or redirecting to our homepage or a 404 as it shifts the page serving load off the site.  In this case we are sending Bittorrent traffic to the bittorrent site, as it seems more appropriate.   Ideally we should not look to block a denial of service at layer 7 of the OSI model, so lets see what we can do with iptables.

Solution #2 – Block “GET /announce.php” tracking requests in ip tables

Almost all bad traffic were seeing was a request for the trackers “announce.php” file.  By using the string searching capabilities of packets in iptables, we were able to block the request at Layer 3/4 of the OSI model

sudo iptables -A INPUT -p tcp -m tcp –dport 80 -m string –string “GET /announce.php” –algo bm –to 65535 -j DROP

In an ideal world we would not want to be searching all incoming port 80 packets for a specific string, but it is probably the most effective method to address what has been a consistent and ongoing issue.

In summary, if you have a similar issue you are welcome to use any one or all of those three methods to address the issue.

Lire aussi:  Make the configuration of iptables persistent (Debian)

The good news is that the site is performing as normal. The one downside is that after nearly two weeks of massive volumes of traffic, ultimately it may be that completely moving ip address might be the only way to completely stop the traffic hitting the server.

Les commentaires sont fermés.