in the meantime, host was able to block all traffic from vietnam, brazil, ukraine and russia.
nearly 2 weeks later and have had zero issues with bots or similar, t
I took your info here and did the same thing. First off, I'm already on Cloudflare, but it was still letting lots of traffic through. The issue is my forum is on a subdomain, and I can't block those countries' traffic to a subdomain. (Maybe you can with CF page rules, etc, but I didn't want to mess with my production site on the primary domain)
So I solved this with Apache tools. The steps below assume you already have Cloudflare running as a proxy.
Step 1: Modify the Apache server config to save the country header field CF-IPCountry to a separate log file by adding these lines to your apache virtual host config:
Code:
LogFormat "%{CF-Connecting-IP}i %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\" CF-Country:%{CF-IPCountry}i" cloudflare_real_ip
CustomLog /home/xen/logs/access.log cloudflare_real_ip
This does two things -- it adds the originating IP address to your logs instead of the CF server IP address. Second, it adds a "CF-Country" field at the end of each log line.
Step 2: Restart Apache to enable these changes.
Step 3: Wait an hour or two. The run this bash command to summarize the most countries hitting your server:
Code:
grep -o 'CF-Country:[A-Z][A-Z]' access.log | cut -d: -f2 | sort | uniq -c | sort -nr | head -20
Output will look like this:
1409 US
457 BD
370 IN
268 IQ
221 GB
140 AR
127 PK
122 SA
120 ZA
107 TR
97 EC
96 UZ
91 KE
84 NP
82 CA
75 JO
72 VE
72 ET
68 FR
67 MX
Step 5: Figure out which countries you want to allow or block. I blocked: VN|BR|UA|RU|IN|BD|IQ|PK|SR|ZA|VE|AR|SA|UZ|KE|MA|CO|TR
Step 6: Modify .htaccess thusly with the countries you want to block, like below:
Code:
RewriteEngine On
# Set environment variable for blocked requests
RewriteCond %{HTTP:CF-IPCountry} ^(VN|BR|UA|RU|IN|BD|IQ|PK|SR|ZA|VE|AR|SA|UZ|KE|MA|CO|TR)$ [NC]
RewriteRule ^(.*)$ - [F,L,E=blocked:%{HTTP:CF-IPCountry}]
# Set environment variable for allowed requests
RewriteCond %{HTTP:CF-IPCountry} !^(VN|BR|UA|RU|IN|BD|IQ|PK|SR|ZA|VE|AR|SA|UZ|KE|MA|CO|TR)$ [NC]
RewriteRule ^(.*)$ - [E=allowed:1,L]
Right there that's enough to block all traffic from those countries!
Step 7: If you want to track allowed versus blocked requests, go back to your Apache config for the virtual host, and change/add these lines to log the blocked accesses:
Code:
# Use the custom format to log all traffic (blocked or not)
CustomLog /home/xen/logs/access.log cloudflare_real_ip
# Log blocked attempts to separate file
CustomLog /home/xen/logs/blocked.log cloudflare_real_ip env=blocked
# Log allowed requests with country info
CustomLog /home/xen/logs/allowed.log cloudflare_real_ip env=allowed
I personally just skipped the allowed.log and changed the main access.log to only save allowed requests like this:
Code:
# Log only allowed requests
CustomLog /home/xen/logs/access.log cloudflare_real_ip env=allowed
That should do it. I haven't had any forum crashes since.