免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: wddllyy
打印 上一主题 下一主题

几天以来的防御syn flood攻击心得 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2004-09-17 23:27 |只看该作者

几天以来的防御syn flood攻击心得

原帖由 "wddllyy" 发表:


呵呵  程序太小还不够友好和完善
我打算完善后再发布

如果你急需,给我个邮箱  我发给你可执行程序
ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (..........


//////////////////////////////////////////////////////////////////////////////
//  Limit_con.cpp
//  Copyright 2004 liu yang (x-yao@zju.edu.cn)
//  Distributed under the GPL
//
//  2004.9.17
//  Limit the connection number of each IP in linux system
//  If the number larger than MaxConnect ,the ip will be block by iptables
//  Can use it to defend simple DoS attack(eg: syn flood),
//
//  install:
//                 gcc  Limit_con.cpp -o Limit_con ; cp Limit_con /bin/Limit_con
//  
//  usage:  (exec Limit_con every 3 minutes)
//          crontab -e
//          */3 * * * * /bin/Limit_con   
//
////////////////////////////////////////////////////////////////////////////

#include <stdio.h>;
#include <vector>;
#include <string>;
#include <iostream>;
#include <fstream>;
#include <algorithm>;
using namespace std;
const string BanListFile = "/tmp/ban_list"; // the file record the block ip

const string DataCacheFile = "/tmp/data.cache";//the file use for cache

const int MaxConnect = 31 ;//the max connection number that you allow for each ip

int main(int argc, char *argv[ ])
{
   string a="/bin/netstat | grep -e \"ESTABLISHED\\|FIN_WAIT1\\|SYN_RECV\" | awk '{print $5}' >; "+DataCacheFile;
   vector < string >; IpList;
   vector < int >; IpConNum;
   string Ipatables_rule="/sbin/iptables -I INPUT -s ";
   string BanIp;
   string Ipatables_rule2=" -j REJECT";
   vector < string >; BlackList;
   string BlackIpString;
   vector < string >;::iterator pos;       
   BlackList.resize(0);

//read the file to get which ip  has been blocked
   fstream BlackIp(BanListFile.c_str() , ios::in);
   if(BlackIp.is_open())   
   {
        while(1)
                {
                  BlackIp  >;>; BlackIpString ;
                    if(!BlackIp.eof())
                        {
                            BlackList.push_back(BlackIpString) ;
                        //  cout<< BlackIpString<<"  "<<BlackList.size()<<"  "<<endl;
                        }
                   else break;
                        }

   }
//exec the shell command to get current connection status,
//and send the infomation to a cache file
   system(a.c_str());

//read the file to get current connection status
   fstream CurrentCon(DataCacheFile.c_str(),ios::in);

        if(!CurrentCon.eof())
        do
        {
        string SingleIp;
        CurrentCon >;>; SingleIp;
        int i=SingleIp.find(":";
        if(i==-1)break;
        SingleIp.resize(i);

               
                pos=find(IpList.begin() , IpList.end() , SingleIp);
                if(pos != IpList.end())//if the ip in the Iplist
                {
                        IpConNum[pos-IpList.begin()]++;//just add the ip's connection number
                }       
                else{//else the ip is first time list in the cache file
                        pos=find(BlackList.begin() , BlackList.end() , SingleIp);
       
                        //if the ip was not blocked,
                        //sometimes you blocked the ip
                        //but it's connection will remain a little time
                        //So we check this .
                        if(pos == BlackList.end())
                               {
                              IpList.push_back(SingleIp);//add the ip to Iplist
                              IpConNum.push_back(1);
                               }

                }




        }
        while(!CurrentCon.eof());
//      cout<<"--------------"<<endl;
//      for(int i=0; i<IpList.size() ; i++)
//        {
//            cout << IpList <<":"<<IpConNum<<endl;
//
//        }

        for(int i=0; i<IpList.size() ; i++)
        {   
                //if the ip's connection number is larger than MaxConnect
                if( IpConNum >; MaxConnect )
                {
                        BanIp=IpList;
                        
                        if(BanIp!=""
                                {
                                Ipatables_rule=Ipatables_rule+BanIp+Ipatables_rule2;
                            cout << "banip "<< Ipatables_rule <<endl;
                            system(Ipatables_rule.c_str());//use iptables to block the ip
                                  string baninfo = "echo ";
                            baninfo = baninfo+BanIp.c_str()+" >;>;"+BanListFile;
                                system(baninfo.c_str());//add the ip to the BanListFile
                            Ipatables_rule = "/sbin/iptables -I INPUT -s ";
                                }
                }

        }

}

论坛徽章:
0
12 [报告]
发表于 2004-09-18 21:15 |只看该作者

几天以来的防御syn flood攻击心得

GOOD!祖国的花朵啊!
在以前netfilter还没推出connlimit模块的时候,我也用C写过一个类似的限制并发连接数的程序。但是netfilter推出connlimit之后,我立刻改用了connlimit。
这类程序有相似的问题:如果扫描间隔太长,还是会DOS(3分钟有点长!如果对手盯上你还是没戏),如果扫描间隔太短,太浪费资源。我当时使用的方法是直接读取/proc/net/ip_conntrack,但是无论是使什么方法,只要你读取网络状态,就会引发内核对一些数据结构的加锁,就会增加数据报转发的时延,那么我们能做的就是把数据尽快地读回来。其次,无论我们怎么取网络状态,都不可避免地造成数据从核心内存向用户态内存拷贝,这是很慢的,所以最好还是让netfilter在内核内部解决。
而你用bash读取数据后通过匿名管道转交给grep和awk,grep和awk都是很慢的大程序,造成管道阻塞后会导致netstat阻塞(我没看netstat的源码,也许没有这个问题)进一步导致内核连接跟踪的延迟。

总得来说还是很不错的!比我那帮学生强多了!

论坛徽章:
0
13 [报告]
发表于 2004-09-18 22:31 |只看该作者

几天以来的防御syn flood攻击心得

uuuupppp

获益菲潜! 感谢楼主!

论坛徽章:
0
14 [报告]
发表于 2004-09-18 23:10 |只看该作者

几天以来的防御syn flood攻击心得

原帖由 "JohnBull" 发表:
GOOD!祖国的花朵啊!
在以前netfilter还没推出connlimit模块的时候,我也用C写过一个类似的限制并发连接数的程序。但是netfilter推出connlimit之后,我立刻改用了connlimit。
这类程序有相似的问题:如果扫描间隔太长,还是会DOS(3分钟有点长!如果对手盯上你还是没戏),如果扫描间隔太短,太浪费资源。我当时使用的方法是直接读取/proc/net/ip_conntrack,但是无论是使什么方法,只要你读取网络状态,就会引发内核对一些数据结构的加锁,就会增加数据报转发的时延,那么我们能做的就是把数据尽快地读回来。其次,无论我们怎么取网络状态,都不可避免地造成数据从核心内存向用户态内存拷贝,这是很慢的,所以最好还是让netfilter在内核内部解决。
而你用bash读取数据后通过匿名管道转交给grep和awk,grep和awk都是很慢的大程序,造成管道阻塞后会导致netstat阻塞(我没看netstat的源码,也许没有这个问题)进一步导致内核连接跟踪的延迟。

总得来说还是很不错的!比我那帮学生强多了!


读了版版的话,我只有一个词,汗颜~~
我只想着先解决问题,没想到底层实现上的问题,想起来的确是啊,能在高优先的内核态高效率的解决问题真的比我这个方法要好太多了.
ps : 版版是老师啊 ? 什么学校什么专业啊?

论坛徽章:
0
15 [报告]
发表于 2004-09-19 08:38 |只看该作者

几天以来的防御syn flood攻击心得

楼上的也很不错啊,不用谦虚。一个问题有多个解决方法,当然是在INPUT入网卡的时候解决,比已经建立起了连接后进行判断解决会优先而且占优势多了,netfilter在内核中的各个挂载点的那则文章写得很独特,甚至连自己挂hook都写了。

我也对JohnBull在哪个学校感兴趣。。。哈哈

论坛徽章:
0
16 [报告]
发表于 2004-09-19 13:21 |只看该作者

几天以来的防御syn flood攻击心得

[quote]原帖由 "skylove"]netfilter在内核中的各个挂载点的那则文章写得很独特,甚至连自己挂hook都写了[/quote 发表:


老大能说说是那篇文章么?小弟有空了去看看.看看能不能看懂.
呵呵 谢谢啦

论坛徽章:
0
17 [报告]
发表于 2004-09-28 21:55 |只看该作者

几天以来的防御syn flood攻击心得

[quote]原帖由 "skylove"]楼上的也很不错啊,不用谦虚。一个问题有多个解决方法,当然是在INPUT入网卡的时候解决,比已经建立起了连接后进行判断解决会优先而且占优势多了,netfilter在内核中的各个挂载点的那则文章写得很独特,甚至连自己挂..........[/quote 发表:



哈哈!

透露一点点,是北京一所非常有名气的培训学校!!!

我就是牛老师的学生!哈哈!因为牛老师讲的知识太精点了!!

所以我又重读一次!牛老师是我们学校最强最好的老师!!!


牛老师, 我一直在努力!!!    我需要时间!!

不久的将来, 你会知道的,您的学生,也有“高手”——大侠!!

论坛徽章:
0
18 [报告]
发表于 2007-04-24 12:47 |只看该作者
受益非浅!受益非浅!受益非浅!受益非浅!

论坛徽章:
5
IT运维版块每日发帖之星
日期:2015-08-06 06:20:00IT运维版块每日发帖之星
日期:2015-08-10 06:20:00IT运维版块每日发帖之星
日期:2015-08-23 06:20:00IT运维版块每日发帖之星
日期:2015-08-24 06:20:00IT运维版块每日发帖之星
日期:2015-11-12 06:20:00
19 [报告]
发表于 2007-04-24 14:07 |只看该作者
好是好,还要小心无辜群众遭殃。

论坛徽章:
0
20 [报告]
发表于 2007-04-24 17:50 |只看该作者
辛苦了...支持一下。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP