服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 核心技术 > 查看文档

java web中的入侵检测及简单实现

  在java web应用程中,特别是网站开发中,我们有时候需要为应用程序增加一个入侵检测程序来防止恶意刷新的功能,防止非法用户不断的往web应用中重复发送数据。当然,入侵检测可以用很多方法实现,包括软件、硬件防火墙,入侵检测的策略也很多。在这里我们主要介绍的是java web应用程序中通过软件的方式实现简单的入侵检测及防御。

一、简介

  在java web应用程中,特别是网站开发中,我们有时候需要为应用程序增加一个入侵检测程序来防止恶意刷新的功能,防止非法用户不断的往web应用中重复发送数据。当然,入侵检测可以用很多方法实现,包括软件、硬件防火墙,入侵检测的策略也很多。在这里我们主要介绍的是java web应用程序中通过软件的方式实现简单的入侵检测及防御。

  该方法的实现原理很简单,就是用户访问web系统时记录每个用户的信息,然后进行对照,并根据设定的策略(比如:1秒钟刷新页面10次)判断用户是否属于恶意刷新。

  我们的入侵检测程序应该放到所有java web程序的执行前,也即若发现用户是恶意刷新就不再继续执行java web中的其它部分内容,否则就会失去了意义。这就需要以插件的方式把入侵检测的程序置入java web应用中,使得每次用户访问java web,都先要到这个入侵检测程序中报一次到,符合规则才能放行。

  java web应用大致分为两种,一种纯jsp(+java bean)方式,一种是基于框架(如struts、easyjweb等)的。第一种方式的java web可以通过java servlet中的filter接口实现,也即实现一个filter接口,在其dofilter方法中插入入侵检测程序,然后再web.xml中作简单的配置即可。在基于框架的web应用中,由于所有应用都有一个入口,因此可以把入侵检测的程序直接插入框架入口引擎中,使框架本身支持入侵检测功能。当然,也可以通过实现filter接口来实现。

  在easyjweb框架中,已经置入了简单入侵检测的程序,因此,这里我们以easyjweb框架为例,介绍具体的实现方法及源码,完整的代码可以在easyjweb源码中找到。

  在基于easyjweb的java web应用中(如http://www.easyjf.com/bbs/),默认情况下你只要连续刷新页面次数过多,即会弹出如下的错误:

  easyjweb框架友情提示!:-): 
  您对页面的刷新太快,请等待60秒后再刷新页面! 
  详细请查询http://www.easyjf.com

二、用户访问信息记录userconnect.java类  

  这个类是一个简单的java bean,主要代表用户的信息,包括用户名、ip、第一次访问时间、最后登录时间、登录次数、用户状态等。全部代码如下:

package com.easyjf.web;
import java.util.date;
/**
*
* <p>title:用户验证信息</p>
* <p>description:记录用户登录信息,判断用户登录情况</p>
* <p>copyright: copyright (c) 2006</p>
* <p>company: www.easyjf.com</p>
* @author 蔡世友
* @version 1.0
*/
public class userconnect {
private string username;
private string ip;
private date firstfailuretime;
private date lastlogintime;
private int failuretimes;//用户登录失败次数
private int status=0;//用户状态0表示正常,-1表示锁定
public int getfailuretimes() {
 return failuretimes;
}
public void setfailuretimes(int failuretimes) {
 this.failuretimes = failuretimes;
}
public date getfirstfailuretime() {
 return firstfailuretime;
}
public void setfirstfailuretime(date firstfailuretime) {
 this.firstfailuretime = firstfailuretime;
}
public string getip() {
 return ip;
}
public void setip(string ip) {
 this.ip = ip;
}
public date getlastlogintime() {
 return lastlogintime;
}
public void setlastlogintime(date lastlogintime) {
 this.lastlogintime = lastlogintime;
}
public string getusername() {
 return username;
}
public void setusername(string username) {
 this.username = username;
}
public int getstatus() {
 return status;
}
public void setstatus(int status) {
 this.status = status;
}
}

三、监控线程userconnectmanage.java类

  这是入侵检测的核心部分,主要实现具体的入侵检测、记录、判断用户信息、在线用户的刷新等功能,并提供其它应用程序使用本组件的调用接口。

package com.easyjf.web;
import java.util.date;
import java.util.hashmap;
import java.util.hashset;
import java.util.iterator;
import java.util.map;
import java.util.set;
import org.apache.log4j.logger;
/**
*
* <p>title:用户入侵检测信息</p>
* <p>description:用于判断用户刷新情况检查,默认为10秒钟之内连续连接10次为超时</p>
* <p>copyright: copyright (c) 2006</p>
* <p>company: www.easyjf.com</p>
* @author 蔡世友
* @version 1.0
*/
public class userconnectmanage {
private static final logger logger = (logger) logger.getlogger(userconnectmanage.class.getname());
private static int maxfailuretimes=10;//最大登录失败次数
private static long maxfailureinterval=10000;//毫秒,达到最大登录次数且在这个时间范围内
private static long waitinterval=60000;//失败后接受连接的等待时间,默认1分钟
private static int maxonlineuser=200;//同时在线的最大数
private final static map users=new hashmap();//使用ip+username为key存放用户登录信息userloginauth
private static thread checkthread=null;
private static class checktimeout implements runnable{ 
private thread parentthread;
public  checktimeout(thread parentthread) 
 {
  this.parentthread=parentthread; 
  synchronized(this){
  if(checkthread==null){    
   checkthread= new thread(this);
   //system.out.println("创建一个新线程!");
   checkthread.start();   
     }
  }
 } 
 public void run() {  
  while(true)
  {
   if(parentthread.isalive()){
   try{
   thread.sleep(2000);
   int i=0;
   if(users.size()>maxonlineuser)//当达到最大用户数时清除
   {
    synchronized(users){//执行删除操作
    iterator it=users.keyset().iterator();
    set set=new hashset();
    date now=new date();
    while(it.hasnext())
    {
     object key=it.next();
     userconnect user=(userconnect)users.get(key);
     if(now.gettime()-user.getfirstfailuretime().gettime()>maxfailureinterval)//删除超时的用户
     {      
      set.add(key);
      logger.info("删除了一个超时的连接"+i);
      i++;
     }
    }
    if(i<5)//如果删除少于5个,则强行删除1/2在线记录,牺牲性能的情况下保证内存
    {
     int num=maxonlineuser/2;
     it=users.keyset().iterator();
     while(it.hasnext() && i<num)
     {      
      set.add(it.next());
      logger.info("删除了一个多余的连接"+i);
      i++;
     }
    }
    users.keyset().removeall(set);
    }
   }
   
   }
   catch(exception e)
   {
    e.printstacktrace();
   }
   
  }
   else
   {   
   break;
   }
  }
  logger.info("监视程序运行结束!"); 
 }
}
//通过checkloginvalidate判断是否合法的登录连接,如果合法则继续,非法则执行
public static boolean checkloginvalidate(string ip,string username)//只检查认证失败次数
{
 boolean ret=true;
 date now=new date(); 
 string key=ip+":"+username;
 userconnect auth=(userconnect)users.get(key);
 if(auth==null)//把用户当前的访问信息加入到users容器中
 {
  auth=new userconnect();
  auth.setip(ip);
  auth.setusername(username);
  auth.setfailuretimes(0);
  auth.setfirstfailuretime(now);
  users.put(key,auth);  
  if(checkthread==null)new checktimeout(thread.currentthread());
 } 
 else
 {
  if(auth.getfailuretimes()>maxfailuretimes)
  {
           //如果在限定的时间间隔内,则返回拒绝用户连接的信息
   if((now.gettime()-auth.getfirstfailuretime().gettime())<maxfailureinterval)
   {
    ret=false;
    auth.setstatus(-1);
   }
   else  if(auth.getstatus()==-1 && (now.gettime()-auth.getfirstfailuretime().gettime()<(maxfailureinterval+waitinterval)))//重置计数器
   {
    ret=false;
   }
   else    
   {    
    auth.setfailuretimes(0);
    auth.setfirstfailuretime(now);
    auth.setstatus(0);
   }
   
  }
  //登录次数加1
  auth.setfailuretimes(auth.getfailuretimes()+1);
 }
 //system.out.println(key+":"+auth.getfailuretimes()+":"+ret+":"+(now.gettime()-auth.getfirstfailuretime().gettime()));
 return ret;
}
public static void reset(string ip,string username)//重置用户信息

 date now=new date(); 
 string key=ip+":"+username;
 userconnect auth=(userconnect)users.get(key);
 if(auth==null)//把用户当前的访问信息加入到users容器中
 {
  auth=new userconnect();
  auth.setip(ip);
  auth.setusername(username);
  auth.setfailuretimes(0);
  auth.setfirstfailuretime(now);
  users.put(key,auth);
 } 
 else
 {
  auth.setfailuretimes(0);
  auth.setfirstfailuretime(now);
 }
}
public static void remove(string ip,string username)//删除用户在容器中的记录
{
 string key=ip+":"+username;
 users.remove(key);
}
public static void clear()//清空容器中内容
{
 if(!users.isempty())users.clear();
}
public static long getmaxfailureinterval() {
 return maxfailureinterval;
}
public static void setmaxfailureinterval(long maxfailureinterval) {
 userconnectmanage.maxfailureinterval = maxfailureinterval;
}
public static int getmaxfailuretimes() {
 return maxfailuretimes;
}
public static void setmaxfailuretimes(int maxfailuretimes) {
 userconnectmanage.maxfailuretimes = maxfailuretimes;
}
public static int getmaxonlineuser() {
 return maxonlineuser;
}
public static void setmaxonlineuser(int maxonlineuser) {
 userconnectmanage.maxonlineuser = maxonlineuser;
}
public static long getwaitinterval() {
 return waitinterval;
}
public static void setwaitinterval(long waitinterval) {
 userconnectmanage.waitinterval = waitinterval;
}


四、调用接口

  在需要进入侵检测判断的地方,直接使用userconnectmanage类中的checkloginvalidate方法即可。如easyjweb的核心servlet 

com.easyjf.web.actionservlet中调用userconnectmanage的代码:
   if(!userconnectmanage.checkloginvalidate(request.getremoteaddr(),"guest"))
       {            
           info(request,response,new exception("您对页面的刷新太快,请等待"+userconnectmanage.getwaitinterval()/1000+"秒后再刷新页面!"));
           return;
       }
   

五、总结

  当然,这里提供的方法只是一个简单的实现示例,由于上面的用户信息是直接保存在内存中,若并发用户很大的时候的代码的占用,可以考虑引入数据库来记录用户的访问信息,当然相应的执行效率肯定用降低。上面介绍的实现中,入侵检测判断的策略也只有用户访问次数及时间间隔两个元素,您还可以根据你的实现情况增加其它的检测元素。

扫描关注微信公众号