Linux shell script for simple CC attacks

Source: Internet
Author: User

The first script is to find ip addresses that are frequently accessed in logs and shield them with iptables.
#! /Bin/bash
Cur = 'date + % H % M % s'
Becur = 'date-d "1 minute ago" + % H % M % s'
Badip = 'Tail-n 10000/home/www. centos. bz/log/access. log | egrep-v "\. (gif | jpg | jpeg | png | css | js) "| awk-v a =" $ becur "-v B =" $ cur "-F ['':] '{t = $5 $6 $7; if (t> = a & t <= B) print $1} '| sort | uniq-c | awk' {if ($1> = 20) print $2 }''
If [! -Z "$ badip"]; then
For ip in $ badip;
Do
If test-z "'/sbin/iptables-nL | grep $ ip'"; then
/Sbin/iptables-I INPUT-s $ ip-j DROP
Fi
Done
Fi www.2cto.com
Save the code as ban. sh and add cronjob to execute the code once every minute.
This script uses iptables to block more than 20 IP addresses accessing pages per minute. These pages have excluded static files such as images, css, and js.
The second script is to block cc attack features in logs.
#! /Bin/bash
Keyword = "cc-atack"
Badip = 'Tail-n 5000/home/www. centos. bz/log/access. log | grep "$ keyword" | awk '{print $1}' | sort | uniq-c | sort-nr | awk '{print $2 }''
If [! -Z "$ badip"]; then
For ip in $ badip;
Do
If test-z "'/sbin/iptables-nL | grep $ ip'"; then
/Sbin/iptables-I INPUT-s $ ip-j DROP
Fi
Done
Fi
Keyword is the cc feature in the log, which can be replaced with valid.

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.