Use shell and iptables to automatically handle CC attacks

Source: Internet
Author: User

The pressure on Web and database servers has increased to several hundred times. Only after reading the logs can we know the general situation. Someone is refreshing the database and starting to manually process these IP addresses. After processing a batch and a batch, it is not enough, finally, I came up with a task plan for automatic processing. After testing, the results were very good. You can try it, and of course you can also handle DDOS attacks.

Some logs:

Reference
Www. *****. com: 80 118.251.244.183--[26/May/2010: 20: 22: 15 + 0800] "POST/syxcms/vote. php? Act = submit HTTP/1.1 "200 56" html "> http: // www. *****. com/news/201005/news-6213.shtml "" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )"
Www. *****. com: 80 118.251.244.183--[26/May/2010: 20: 22: 15 + 0800] "POST/syxcms/vote. php? Act = submit HTTP/1.1 "200 56" http: // www. *****. com/news/201005/news-6213.shtml "" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )"
Www. *****. com: 80 118.251.244.183--[26/May/2010: 20: 22: 15 + 0800] "POST/syxcms/vote. php? Act = submit HTTP/1.1 "200 56" http: // www. *****. com/news/201005/news-6213.shtml "" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )"
Www. *****. com: 80 118.251.244.183--[26/May/2010: 20: 22: 15 + 0800] "POST/syxcms/vote. php? Act = submit HTTP/1.1 "200 56" http: // www. *****. com/news/201005/news-6213.shtml "" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )"
Www. *****. com: 80 118.251.244.183--[26/May/2010: 20: 22: 15 + 0800] "POST/syxcms/vote. php? Act = submit HTTP/1.1 "200 56" http: // www. *****. com/news/201005/news-6213.shtml "" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )"
Www. *****. com: 80 118.251.244.183--[26/May/2010: 20: 22: 15 + 0800] "POST/syxcms/vote. php? Act = submit HTTP/1.1 "200 56" http: // www. *****. com/news/201005/news-6213.shtml "" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )"
Www. *****. com: 80 118.251.244.183--[26/May/2010: 20: 22: 16 + 0800] "POST/syxcms/vote. php? Act = submit HTTP/1.1 "200 56" http: // www. *****. com/news/201005/news-6213.shtml "" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )"
Www. *****. com: 80 118.251.244.183--[26/May/2010: 20: 22: 16 + 0800] "POST/syxcms/vote. php? Act = submit HTTP/1.1 "200 72" http: // www. *****. com/news/201005/news-6213.shtml "" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )"
Www. *****. com: 80 118.251.244.183--[26/May/2010: 20: 22: 16 + 0800] "POST/syxcms/vote. php? Act = submit HTTP/1.1 "200 56" http: // www. *****. com/news/201005/news-6213.shtml "" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )"
The general principle is to sample and analyze the client access IP addresses in the latest log, and shield the IP addresses that exceed the normal access count, as shown in the following statistical analysis result:

Sort the client access IP addresses of the latest 1000 logs and count the number of visits. For example, the first IP address 219.128.20.68 contains 1000 logs, and the access is definitely not normal.

Root @ ubuntu134: # tail access. log-n 1000 | grep vote. php | awk {print $2} | sort | uniq-c | sort-nr
295 219.128.20.68
175 113.250.97.209
164 218.87.140.39
153 59.61.215.42
98 222.240.182.234
83 220.181.110.65
73 120.38.1.255
62 221.3.99.106
21 220.249.83.74
12 218.22.10.114
1 123.52.158.16
1 114.81.115.201
 

Then it will be automatically processed. If there are more than 50 IP addresses for 1000 logs, it will be blocked.

*/2 */usr/local/nginx/var/log/drop. sh

#! /Bin/sh
Cd/usr/local/nginx/var/log
Tail access. log-n 1000 | grep vote. php | awk {print $2} | sort | uniq-c | sort-nr | awk {if ($2! = Null & $1> 50) {print $2 }}> drop_ip.txt
For I in 'cat drop_ip.txt'
Do
/Sbin/iptables-I input-s $ I-j DROP;
Done
 

The shell runs once every few minutes to automatically block abnormal IP addresses. I believe everyone can understand it. The following code is used to block connections.

#! /Bin/sh
/Bin/netstat-ant | grep 80 | awk {print $5} | awk-F: {print $1} | sort | uniq-c | sort-rn | grep-v-E 192.168 | 127.0 | awk {if ($2! = Null & $1> 50) {print $2 }}> drop_ip.txt
For I in 'cat drop_ip.txt'
Do
/Sbin/iptables-I input-s $ I-j DROP;
Done
 

To put it down, grep-v-E 192.168 | 127.0 is to exclude the Intranet IP address, so that you do not block yourself. Of course, you can add your own IP address.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.