Sql注入bug修改



SQL注入 將有可能泄露的數據的那個參數 改 str.replaceAll(".*([';]+|(--)+).*", " ");



在過濾器中加入這段代碼

 //獲得所有請求參數名  
       Enumeration params = req.getParameterNames();  
       String sql = "";  
       while (params.hasMoreElements()) {  
           //得到參數名  
           String name = params.nextElement().toString();  
           //System.out.println("name===========================" + name + "--");  
           //得到參數對應值  
           String[] value = req.getParameterValues(name);  
           for (int i = 0; i < value.length; i++) {  
               sql = sql + value[i];  
           }  
       }  
       //System.out.println("============================SQL"+sql);  
       //有sql關鍵字,跳轉到error.html  
       if (sqlValidate(sql)) {  
           throw new IOException("您發送請求中的參數中含有非法字符");  
           //String ip = req.getRemoteAddr();  
       } else {  
           chain.doFilter(request,response);  
       }  





//效驗  
    protected static boolean sqlValidate(String str) {  
        str = str.toLowerCase();//統一轉爲小寫  
        String badStr = "'|and|exec|execute|insert|select|delete|update|count|drop|*|%|chr|mid|master|truncate|" +  
                "char|declare|sitename|net user|xp_cmdshell|;|or|-|+|,|like'|and|exec|execute|insert|create|drop|" +  
                "table|from|grant|use|group_concat|column_name|" +  
                "information_schema.columns|table_schema|union|where|order|by|count|*|" +  
                "-|--|+|,|like|//|/|%|#";//過濾掉的sql關鍵字,可以手動添加  
        String[] badStrs = badStr.split("\\|");  
        for (int i = 0; i < badStrs.length; i++) {  
            if (str.indexOf(badStrs[i]) >= 0) {  
                return true;  
            }  
        }  
        return false;  
    }  






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章