GalaxyPilot —— D.S


        生命不熄,战斗不止
数据加载中……

通用防SQL注入函数java版

public class StringUtil {

    public StringUtil() {
    }

    public static String replace(String str, String substr, String restr) {
        String[] tmp = split(str, substr);
        String returnstr = null;
        if (tmp.length != 0) {
            returnstr = tmp[0];
            for (int i = 0; i < tmp.length - 1; i++)
                returnstr = dealNull(returnstr) + restr + tmp[i + 1];
        }
        return dealNull(returnstr);
    }

    public static String[] split(String source, String div) {
        int arynum = 0, intIdx = 0, intIdex = 0, div_length = div.length();
        if (source.compareTo("") != 0) {
            if (source.indexOf(div) != -1) {
                intIdx = source.indexOf(div);
                for (int intCount = 1;; intCount++) {
                    if (source.indexOf(div, intIdx + div_length) != -1) {
                        intIdx = source.indexOf(div, intIdx + div_length);
                        arynum = intCount;
                    } else {
                        arynum += 2;
                        break;
                    }
                }
            } else
                arynum = 1;
        } else
            arynum = 0;

        intIdx = 0;
        intIdex = 0;
        String[] returnStr = new String[arynum];

        if (source.compareTo("") != 0) {

            if (source.indexOf(div) != -1) {

                intIdx = (int) source.indexOf(div);
                returnStr[0] = (String) source.substring(0, intIdx);

                for (int intCount = 1;; intCount++) {
                    if (source.indexOf(div, intIdx + div_length) != -1) {
                        intIdex = (int) source
                                .indexOf(div, intIdx + div_length);

                        returnStr[intCount] = (String) source.substring(intIdx
                                + div_length, intIdex);

                        intIdx = (int) source.indexOf(div, intIdx + div_length);
                    } else {
                        returnStr[intCount] = (String) source.substring(intIdx
                                + div_length, source.length());
                        break;
                    }
                }
            } else {
                returnStr[0] = (String) source.substring(0, source.length());
                return returnStr;
            }
        } else {
            return returnStr;
        }
        return returnStr;
    }

    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[] = split(inj_str, "|");
        for (int i = 0; i < inj_stra.length; i++) {
            if (str.indexOf(inj_stra[i]) >= 0) {
                return true;
            }
        }
        return false;
    }

    private static String dealNull(String str) {
        String returnstr = null;
        if (str == null)
            returnstr = "";
        else
            returnstr = str;
        return returnstr;
    }

    public static void main(String[] args) {
        System.out.println(sql_inj("admin;"));
    }
}

 jsp中调用该函数检查是否包函非法字符
 <%
 if(request.getParameter("userID") != null)
    userID = request.getParameter("userID").trim();

  if (StringUtil.sql_inj(userID) || StringUtil.sql_inj(pwd)){
   %>
   <Script Language=javascript>alert('参数中包含非法字符!');history.back(-1);</Script>" ;
   <%
  }else{
  ……
 }%>
 StringUtil 是我的通用防注入函数的包名,该函数参考了ASP通用防SQL注入函数,做了一些修改。

posted on 2006-03-30 16:58 舵手 阅读(20320) 评论(12)  编辑  收藏

评论

# re: 通用防SQL注入函数java版  回复  更多评论   

这个是不是有待商榷,比如or,这样处理,任何包含字符串or的都会被毖掉
2006-09-12 13:14 | lagrustler

# re: 通用防SQL注入函数java版  回复  更多评论   

这个函数只是用来过滤传送给SQL字的参数,并不是所有串
2006-09-29 14:26 | 舵手 QQ:8117892

# re: 通用防SQL注入函数java版  回复  更多评论   

JAVA版的是不是还要区分大小写呀?
2006-10-28 15:49 | 风投

# re: 通用防SQL注入函数java版  回复  更多评论   

多谢提醒,JSP版需要区分大小写
2006-10-30 17:25 | 舵手 QQ:8117892

# re: 通用防SQL注入函数java版  回复  更多评论   

大大大哥split方法在哪儿呢/
2007-11-14 12:44 | 小注

# re: 通用防SQL注入函数java版  回复  更多评论   

java.lang.String类就有split这个方法,静态的,可以直接调用。
2007-11-15 08:47 | 舵手 QQ:8117892

# re: 通用防SQL注入函数java版[未登录]  回复  更多评论   

这也叫通用?
2008-01-10 14:37 | XXX

# re: 通用防SQL注入函数java版  回复  更多评论   

那么请楼上的给一个更通用的。
2008-01-10 14:43 | 舵手 QQ:8117892

# re: 通用防SQL注入函数java版[未登录]  回复  更多评论   

哪里有静态split!!!!!! 不要误导
2008-08-29 10:03 | 过客

# re: 通用防SQL注入函数java版  回复  更多评论   

String确实没有静态的split方法,更正一下!
向被我误导的兄弟道歉!

发现当时只贴了部分代码,今天贴全一些。
2008-08-29 13:21 | 舵手 QQ:8117892

# re: 通用防SQL注入函数java版  回复  更多评论   

好像不能防 001001010 这样的吧!~
2008-10-17 14:50 | FB

# re: 通用防SQL注入函数java版  回复  更多评论   

PreparedStatement preState = conn.prepareStatement(sql);
还是用这个种方法吧!~
2008-10-17 14:52 | FB

只有注册用户登录后才能发表评论。


网站导航: