English
 电子信箱
 加入收藏

  威盾防火墙 >> 支持与下载 >> 技术文章 >> 超强JSP防SQL注入攻击

 

超强JSP防SQL注入攻击

威盾防火墙 2015-01-06

 

 第一种采用预编译语句集,它内置了处理SQL注入的能力,只要使用它的setString方法传值即可:

  String sql= "select * from users where username=? and password=?;

  PreparedStatement preState = conn.prepareStatement(sql);

  preState.setString(1, userName);

  preState.setString(2, password);

  ResultSet rs = preState.executeQuery();

  ...

  第二种是采用正则表达式将包含有 单引号('),分号(;) 和 注释符号(--)的语句给替换掉来防止SQL注入

  例1

  public static String TransactSQLInjection(String str)

  {

  return str.replaceAll(".*([';]+|(--)+).*", " ");

  }

  userName=TransactSQLInjection(userName);

  password=TransactSQLInjection(password);

  String sql="select * from users where username='"+userName+"' and password='"+password+"' "

  Statement sta = conn.createStatement();

  ResultSet rs = sta.executeQuery(sql);

  ...

  或者例2

  要引入的包:

  import java.util.regex.*;

  正则表达式:

  private String CHECKSQL = “^(.+)sands(.+)|(.+)sor(.+)s$”;

  判断是否匹配:

  Pattern.matches(CHECKSQL,targerStr);

  下面是具体的正则表达式:

  检测SQL meta-characters的正则表达式 :

  /(%27)|(’)|(--)|(%23)|(#)/ix

  修正检测SQL meta-characters的正则表达式:/((%3D)|(=))[^n]*((%27)|(’)|(--)|(%3B)|(:))/i

  典型的 SQL 注入攻击的正则表达式:/w*((%27)|(’))((%6F)|o|(%4F))((%72)|r|(%52))/ix

  检测SQL注入,UNION查询关键字的正则表达式 :/((%27)|(’))union/ix(%27)|(’)

  检测MS SQL Server SQL注入攻击的正则表达式:

  /exec(s|+)+(s|x)pw+/ix

  等等…..

  第三种是字符串过滤

  例1

  sql_inj.java为一个改进的防注入bean,编译后将class文件放在tomcat的classes下的sql_inj目录中。

  sql_inj.java代码:

  ====================================================================

  package sql_inj;

  import java.net.*;

  import java.io.*;

  import java.sql.*;

  import java.text.*;

  import java.lang.String;

  public class sql_inj{

  public static boolean sql_inj(String str)

  {

  String inj_str ="'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,";//这里的东西还可以自己添加

  String[] inj_stra=inj_str.split("|");

  for (int i=0 ; i < inj_stra.length ; i++ )

  {

  if (str.indexOf(inj_stra[i])>=0)

  {

  return true;

  }

  }

  return false;

  }

  }

  ====================================================================

  JSP页面判断代码:

  


相关内容: 最新内容:
SQL注入中的WAF绕过技术[2015-01-06]
十大关系数据库SQL注入工具一览[2015-01-06]
数据库安全审计应用实践——追查数据库SQL注入30小时[2015-01-06]
好用的asp防SQL注入代码[2015-01-06]
c#.net全站防止SQL注入类的代码[2015-01-06]
asp.net 防止SQL注入攻击[2015-01-06]
防止Web应用威胁任重道远[2015-01-06]
SQL注入中的WAF绕过技术[2015-01-06]
保护应用程序安全成企业迫在眉睫问题[2015-01-06]
十大关系数据库SQL注入工具一览[2015-01-06]
数据库安全审计应用实践——追查数据库SQL注入30小时[2015-01-06]
好用的asp防SQL注入代码[2015-01-06]