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 舵手 閱讀(20335) 評論(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

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 黑水县| 景洪市| 余干县| 泾阳县| 马鞍山市| 高碑店市| 克什克腾旗| 昌吉市| 尚义县| 仙居县| 博兴县| 迁安市| 沙雅县| 从化市| 福州市| 喀什市| 新津县| 苏州市| 沭阳县| 读书| 婺源县| 桦南县| 万山特区| 安吉县| 仁布县| 靖安县| 米脂县| 芦溪县| 南开区| 宁乡县| 邢台市| 黄山市| 贵港市| 富源县| 康马县| 城口县| 名山县| 德庆县| 安宁市| 寻乌县| 利辛县|