首頁  >  文章  >  後端開發  >  asp.net下偵測SQL注入式攻擊程式碼

asp.net下偵測SQL注入式攻擊程式碼

巴扎黑
巴扎黑原創
2016-12-20 17:09:581258瀏覽

兩個類別:
(頁面資料校驗類別)PageValidate.cs基本上通用。
程式碼如下:


使用系統; 
使用System.Text; 
使用的System.Web; 
使用System.Web.UI.WebControls; 
使用System.Text.RegularExpressions;頁面資料校驗類別
/// 
public類別PageValidate 

私有靜態正規表示RegNumber =新的正規表示式(“^ [0-9] + $”); 
私有私人正規表示式RegNumberSign =新的正規表示式(“^ [+ - ] [0-9] + $?”); 
私有靜態正規表示式RegDecimal =新的正規表示式(“[]?^ [0-9] + [0- 9] + $”); 
私有靜態正規表示式RegDecimalSign =新的正規表示式(“^ [+ - ] [0-9] + [0-9] + $?[]?”); //等價於^ [+ - ] D + D + $? [] 
私有靜態正規表示式RegEmail =新的正規表示式(“^ [\ W - ] + @ \ W - ] + \(COM |網|組織| EDU |密|電視| BIZ |資訊)$“ ); //W¯¯英文字母或數字的字串,和[A-ZA-Z0-9]語法一樣
私有靜態正規表示式RegCHZN =新的正規表示式( “[ u4e00- u9fa5]”);
公用PageValidate()



#地區數字字串檢查
/// 
///檢查申請查詢字串的鍵值,是否為數字,最大長度限制
/// 
//請求
///請求的鍵值
// /最大長度
///返回請求查詢字串
的公共靜態字串FetchInputDigit(REQ的HttpRequest,字串inputKey,MAXLEN詮釋)

= retVal的String.Empty; 
如果(inputKey = NULL && inputKey =的String.Empty!)

retVal的= req.QueryString [inputKey] 
如果(空== retVal的)
retValinputa =
如果(空= retVal的!)

retVal的= SQLTEXT(retVal的,MAXLEN); 
如果(ISNUMBER(retVal的)!)
retVal的=的String.Empty;的== NULL)
retVal的=的String.Empty; 
回傳retVal的; 

/// 
///是否數字字串
/// 
///輸入字串
/是否數字字串
/// 
///輸入字串
/是否/靜態布林ISNUMBER(字串inputData)

符合M = RegNumber.Match(inputData); 
回傳m.Success; 

/// 
///是否數字字串可帶正負號/負號/
///輸入字串
/// 
公共靜態布林IsNumberSign(字串inputData)

符合M = RegNumberSign.Match(inputData); 
/ /
}是否為浮點數
/// 
///輸入字串
/// 
公用靜態布林IsDecimal(字串inputData)

符合M = RegDecimal.Match(inputData); 

/// 
///是否為浮點數可帶正負號
/// 
///輸入字串
/// 
公用靜態布林IsDecimalSign(字串inputData)

公用靜態布林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 = sqlInput.Substring(0,最大長度); 
} ; 

/// 
///字串編碼
/// 
/// 
/// 
公用靜態字串的HTMLEncode(字串inputDataility)

公用靜態字串的HTMLEncode(字串inputDataility)

); } 
/// 
///設定標籤顯示編碼的字串
/// 
/// 
/// 
公共靜態無效SetLabel(標籤LBL,串txtInput)
{ txtInput); 

公用靜態無效SetLabel(LBL標籤,物件inputObj)

SetLabel(LBL,inputObj.ToString()); 

/////////////////////已清理字串公用靜態字串的inputText(字串inputString,INT最大長度)

StringBuilder的retVal的=新的StringBuilder(); 
//檢查是否為空
,如果((inputString = NULL)&&(inputString =的String.Empty)!字元
的for(int i = 0;我
開關(inputString [I])

案'“:
retVal.Append(”“”); 
打破;
案'retVal.Append(「打破; 
案例'>':
retVal.Append(「>」); 
打破; 
預設:
retVal.Append(inputString [I打破) ); 
打破; 


retVal.Replace(“'”,“”); //替換單引號

返回retVal.ToString(); 
}

返回retVal.ToString(); 
}代碼
/// 
///字串
///字串
公共靜態字串編碼(字串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 

}

網站根木馬下即可。 (其他功能自行補上)


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn