Question

How do I stop spam attack on my website?

Within few hours of launching my website (http://www.winstondigital.com) on a droplet, my Nginx access log is filled with access requests like this:

**61.160.213.56 - - [22/Feb/2016:11:22:13 -0500] “GET http://zc.qq.com/cgi-bin/chs/numreg/init? HTTP/1.0” 404 1564 “-” “-” **

First question is: how the domain “zc.qq.com” is mapping to my public IP address? When I ping “zc.qq.com”, it gives an IP address that is registered in China. It appears that many others on the internet are getting spammed by these kind of attack.

Second question is: How can I stop these requests at the firewall, before it hits my application server (Rails). It seems harmless as all these requests are returned with a “404 Not found” error code. Still it is filling up my log files and creating unnecessary traffic.

Anyone else is seeing this problem?

Thanks for your help in advance.

Subscribe
Share

I did a search on the search box on this page and found this tutorial about protecting nginx. See if that may be of use to you.

did you find a solution, i am having the same problem


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This is where iptables and a little cron job can be useful.

If you’re regularly being hit by the same IP addresses, to block them out make a rule using iptables. With spam, it’s best to simply drop the traffic so try :

iptables -I INPUT -s xx.xx.xx.xx -j DROP

This will block IP address xx.xx.xx.xx accessing your droplet. You can also use CIDR format to block a range of IP Addresses if necessary.

This is not, however, persistent across a reboot so I’d recommend making a little script and run it at boot time as a cron job.

Assuming you’re logged in as root (if not then su as required)

  1. Create a file called load_iptables.sh in /root/ :
nano ~/load_iptables.sh 
  1. On each line place a rule using iptables. Don’t forget to add the path to iptables!
/sbin/iptables -I INPUT -s xx.xx.xx.xx -j DROP
/sbin/iptables -I INPUT -s yy.yy.yy.yy -j DROP
  1. Save the file and make it executable :
chmod 700 ~/load_iptables.sh
  1. Test your script by running it :
/root/load_iptables.sh
  1. If all is good, set it as a cron job on reboot :
crontab -e

Then add the following to the crontab that opens :

@reboot    /root/load_iptables.sh

This will then load and run your script as root each time you restart your server. To add new addresses to block, just add a new line to your load_iptables.sh script.

This is a basic way of dealing with spam. If you are comfortable with linux it’s definitely worth looking at something like fail2ban which is a much nicer automated way of snipping spam attacks in the bud :)

I hope that helps!

Regards, Mike