asp.net下檢測SQL注入式攻擊代碼

來源:互聯網
上載者:User
兩個類:
(頁面資料校正類)PageValidate.cs基本通用。
代碼如下:


使用系統;
使用System.Text;
使用的System.Web;
使用System.Web.UI.WebControls;
使用System.Text.RegularExpressions;
命名空間常用
{
///
///頁面資料校正類
///
public類PageValidate
{
私人靜態RegexRegNumber =新的Regex(“^ [0-9] + $”);
私人靜態RegexRegNumberSign =新的Regex(“^ [+ - ] [0-9] + $?”);
私人靜態RegexRegDecimal =新的Regex(“[]?^ [0-9] + [0-9] + $”);
私人靜態RegexRegDecimalSign =新的Regex(“^ [+ - ] [0-9] + [0-9] + $?[]?”); //等價於^ [+ - ] \ D + \ D + $?[]
私人靜態RegexRegEmail =新的Regex(“^ [\\ W - ] + @ \\ W - ] + \\(COM |網路|組織| EDU |密|電視| BIZ |資訊)$“); //W¯¯英文字母或數位字串,和[A-ZA-Z0-9]文法一樣
私人靜態RegexRegCHZN =新的Regex( “[\ u4e00- \ u9fa5]”);
公用PageValidate()
{
}

#地區數字字串檢查
///
///檢查申請查詢字串的索引值,是否是數字,最大長度限制
///
///請求
///請求的索引值
// /最大長度
///返回請求查詢字串
的公用靜態字串FetchInputDigit(REQ的HttpRequest,串inputKey,MAXLEN詮釋)
{
字串= retVal的的String.Empty;
如果(inputKey = NULL && inputKey =的String.Empty!)
{
retVal的= req.QueryString [inputKey]
如果(空== retVal的)
retVal的= req.Form [inputKey]
如果(空= retVal的!)
{
retVal的= SQLTEXT(retVal的,MAXLEN);
如果(ISNUMBER(retVal的)!)
retVal的=的String.Empty;
}
}
如果(retVal的== NULL)
retVal的=的String.Empty;
返回retVal的;
}
///
///是否數字字串
///
///輸入字串
///
公用靜態布爾ISNUMBER(字串inputData)
{
匹配M = RegNumber.Match(inputData);
返回m.Success;
}
///
///是否數字字串可帶加號或減號
///
///輸入字串
///
公用靜態布爾IsNumberSign(字串inputData)
{
匹配M = RegNumberSign.Match(inputData);
返回m.Success;
}
///
///是否是浮點數
///
///輸入字串
///
公用靜態布爾IsDecimal(字串inputData)
{
匹配M = RegDecimal.Match(inputData);
返回m.Success;
}
///
///是否是浮點數可帶加號或減號
///
///輸入字串
///
公用靜態布爾IsDecimalSign(字串inputData)
{
匹配M = RegDecimalSign.Match(inputData);
返回m.Success;
}
#endregion
#地區中文檢測
///
///檢測是否有中文字元
///
///
///
公用靜態布爾IsHasCHZN(字串inputData)
{
匹配M = RegCHZN.Match(inputData);
返回m.Success;
}
#endregion
#地區郵件地址
///
///是否是浮點數可帶加號或減號
///
///輸入字串
///
公用靜態布爾ISEMAIL(字串inputData)
{
匹配M = RegEmail.Match(輸入資料);
返回m.Success;
}
#endregion
#地區其他
///
///檢查字串最大長度,返回指定長度的串
///
///輸入字串
///最大長度
///
公用靜態字串SQLTEXT(字串的SQLInput,INT最大長度)
{
如果(的SQLInput = NULL &&的SQLInput =的String.Empty)!
{
的SQLInput = sqlInput.Trim();
如果(sqlInput.Length>最大長度)//按最大長度截取字串
的SQLInput = sqlInput.Substring(0,最大長度);
}
返回的SQLInput;
}
///
///字串編碼
///
///
///
公用靜態字串的HTMLEncode(字串inputData)
{
返回HttpUtility.HtmlEncode(inputData);
}
///
///設定標籤顯示編碼的字串
///
///
///
公用靜態無效SetLabel(標籤LBL,串txtInput)
{
lbl.Text =的HTMLEncode(txtInput);
}
公用靜態無效SetLabel(LBL標籤,對象inputObj)
{
SetLabel(LBL,inputObj.ToString());
}
//字串清理
公用靜態字串的inputText(字串inputString,INT最大長度)
{
StringBuilder的retVal的=新的StringBuilder();
//檢查是否為空白
,如果((inputString = NULL)&&(inputString =的String.Empty)!)
{
inputString = inputString.Trim();
//檢查長度
如果(inputString.Length>最大長度)
inputString = inputString.Substring(0,最大長度);
//替換危險字元
的for(int i = 0;我<inputString.Length;我++)
{
開關(inputString [I])
{
案'“:
retVal.Append(”“”);
打破;
案'<':
retVal.Append(“<”);
打破;
案例'>':
retVal.Append(“>”);
打破;
預設:
retVal.Append(inputString [I]);
打破;
}
}
retVal.Replace(“'”,“”); //替換單引號
}
返回retVal.ToString();
}
///
///轉換成HTML代碼
///
///串
///字串
公用靜態字串編碼(字串str)
{
海峽= str.Replace(“&”,“&”);
海峽= str.Replace(“'”,“'”);
海峽= str.Replace(“\”“,”“”);
海峽= str.Replace(“”,“”);
海峽= str.Replace(“<”,“<”);
海峽= str.Replace(“>”,“>”);
海峽= str.Replace(“\ n”,“
”);
返回海峽;
}
///
///解析HTML成普通文本
///
///字串
///字串
公用靜態字串解碼(字串str)
{
海峽= str.Replace(“
”,“\ n”);
海峽= str.Replace(“>”,“>”);
海峽= str.Replace(“<”,“<”);
海峽= str.Replace(“”,“”);
海峽= str.Replace(“”“,”\“”);
返回海峽;
}
#endregion
}
}


通用檔案(Global.asax中),儲存為的Global.asax檔案名稱放到網站根木馬下即可。(其他功能自行補上)

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.