设为首页 收藏本站
查看: 665|回复: 0

[经验分享] 通过分析nginx log 后使用iptables来防ddos攻击

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-1-21 08:59:09 | 显示全部楼层 |阅读模式
[iyunv@test-host nginx]#
[iyunv@test-host nginx]#
[iyunv@test-host nginx]# pwd
/etc/nginx
[iyunv@test-host nginx]# more nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
user              nginx;
worker_processes  2;
#worker_cpu_affinity 2;
error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    use epoll;
}

http {
    include       /etc/nginx/mime.types;
    #default_type  application/octet-stream;
     default_type  text/plain;
    server_names_hash_bucket_size 64;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    log_format  main  '$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" '
                      '$status $upstream_addr $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" ';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
   

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;
}
[iyunv@test-host nginx]#
[iyunv@test-host nginx]#
[iyunv@test-host nginx]#  tail /var/log/nginx/access.log
- 183.222.154.40 - [20/Jan/2015:11:32:01 +0800] "POST /index.php?m=awmembers&c=ajax&a=ajax_check_account HTTP/1.1" 200 10.80.124.46:8081 2 "http://www.jedy.com/index.php?m=awmembers&c=ajax&a=ajax_check_account" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)"
- 116.207.86.146 - [20/Jan/2015:11:32:01 +0800] "GET /UpdateClientVersion.ini HTTP/1.1" 400 10.80.124.46:8081 39 "-" "UpdateClient_Updater"
- 113.139.38.21 - [20/Jan/2015:11:32:01 +0800] "POST /index.php?m=awmembers&c=ajax&a=ajax_check_account HTTP/1.0" 200 10.80.124.46:8081 2 "http://www.jedy.com/index.php?m=awmembers&c=ajax&a=ajax_check_account" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)"
- 190.75.114.131 - [20/Jan/2015:11:32:02 +0800] "POST /index.php?m=awmembers&c=ajax&a=ajax_check_account HTTP/1.0" 200 10.80.124.46:8081 2 "http://www.jedy.com/index.php?m=awmembers&c=ajax&a=ajax_check_account" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)"
- 116.207.86.146 - [20/Jan/2015:11:32:02 +0800] "GET /api.php?op=game&target=popad&clientid=AM1AWBFBJQQBCYAAGElTRN1VdFlBStACCYFXbdAUDYgVQJQVUNVXSdwCDAQVAAQSJYFUTBgVEYQVEkwU HTTP/1.1" 302 10.80.124.46:8082 0 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
- 186.94.184.28 - [20/Jan/2015:11:32:02 +0800] "POST /index.php?m=awmembers&c=ajax&a=ajax_check_account HTTP/1.0" 200 10.80.124.46:8081 2 "http://www.jedy.com/index.php?m=awmembers&c=ajax&a=ajax_check_account" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)"
- 190.38.80.164 - [20/Jan/2015:11:32:02 +0800] "POST /index.php?m=awmembers&c=ajax&a=ajax_check_account HTTP/1.0" 200 10.80.124.46:8081 2 "http://www.jedy.com/index.php?m=awmembers&c=ajax&a=ajax_check_account" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)"
- 27.192.40.74 - [20/Jan/2015:11:32:02 +0800] "POST /index.php?m=awmembers&c=ajax&a=ajax_check_account HTTP/1.0" 200 10.80.124.46:8081 2 "http://www.jedy.com/index.php?m=awmembers&c=ajax&a=ajax_check_account" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)"
- 200.84.196.41 - [20/Jan/2015:11:32:02 +0800] "POST /index.php?m=awmembers&c=ajax&a=ajax_check_account HTTP/1.0" 200 10.80.124.46:8081 2 "http://www.jedy.com/index.php?m=awmembers&c=ajax&a=ajax_check_account" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)"
181.225.58.165 200.62.19.43 - [20/Jan/2015:11:32:02 +0800] "POST /index.php?m=awmembers&c=ajax&a=ajax_check_account HTTP/1.1" 200 10.80.124.46:8081 2 "http://www.jedy.com/index.php?m=awmembers&c=ajax&a=ajax_check_account" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)"
[iyunv@test-host nginx]#
[iyunv@test-host nginx]#
[iyunv@test-host nginx]# more ~/ddos.sh               
#!/bin/sh
date1=`date +%d/%b/%Y:%H:%M -d "1 minute ago"`
access=`more /var/log/nginx/access.log | grep $date1 |awk '{print $1,$2}' | sort -nr`
add1=`echo "$access" | awk '{print $1}'|grep -v - | sort -nr |uniq -c|sort -nr|head -1`
add2=`echo "$access" | awk '{print $2}'|grep -v - | sort -nr |uniq -c|sort -nr|head -1`
echo "$add1"
echo "$add2"
if [ -n "$add1" ]
then
    NUM1=`echo "$add1" |awk '{print $1}'`
    IP1=`echo "$add1" |awk '{print $2}'`
        if [ $NUM1 -gt 1000 ]
        then
                echo "`date`  IP:$IP1 is over $NUM1, BAN IT!" >>/root/ddos.log
                /sbin/iptables -I DROP_WEB 1 -s $IP1 -j DROP
        fi
fi
NUM2=`echo "$add2" |awk '{print $1}'`
IP2=`echo "$add2" |awk '{print $2}'`
#result2=`echo "$NUM2 > 1000" | bc`
#if [ $result2 = 1 ]
if [ $NUM2 -gt 1000 ]
        then
        echo "`date`  IP:$IP2 is over $NUM2, BAN IT!" >>/root/ddos.log
        /sbin/iptables -I DROP_WEB 1 -s $IP2 -j DROP
fi
[iyunv@test-host nginx]#
[iyunv@test-host nginx]#
[iyunv@test-host nginx]#  crontab -l
*/1 * * * * /root/ddos.sh
#0 * * * * /root/nginx-log.sh
[iyunv@test-host nginx]#
[iyunv@test-host nginx]#
[iyunv@test-host ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination         
306M  283G DROP_WEB   all  --  *      *       0.0.0.0/0            0.0.0.0/0           
  269  173K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
33255   13M ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           limit: avg 1/sec burst 10
    0     0 ACCEPT     tcp  --  *      *       192.168.0.0/16       0.0.0.0/0           tcp dpt:22
#####################################################
######################略#############################
#####################################################
        
   76  3916 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80 #conn/32 > 100 reject-with icmp-port-unreachable
338M  306G ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
15124  814K LOG        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp flags:0x17/0x02 recent: CHECK seconds: 6 hit_count: 20 name: webpool side: source LOG flags 4 level 5 prefix `DDOS:'
191K   11M DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp flags:0x17/0x02 recent: CHECK seconds: 6 hit_count: 20 name: webpool side: source
  10M  547M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp flags:0x17/0x02 recent: SET name: webpool side: source
43799 3012K REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 62M packets, 39G bytes)
pkts bytes target     prot opt in     out     source               destination        
Chain DROP_WEB (1 references)
pkts bytes target     prot opt in     out     source               destination         
[iyunv@test-host ~]#


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-41183-1-1.html 上篇帖子: nginx apache区别 下篇帖子: debian 系统python+uwsgi+nginx实现web环境
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表