閒言碎語先不談,上代碼,javascript
我這個是在Web項目,首先先建立SqlChecker類,用於校驗前端傳到後臺全部請求中的過濾前端
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Web { public class SqlChecker { //當前請求對象 private HttpRequest request; //當前響應對象 private HttpResponse response; //安全Url,當出現Sql注入時,將導向到的安全頁面,若是沒賦值,則停留在當前頁面 private string safeUrl = String.Empty; //Sql注入時,可能出現的sql關鍵字,可根據本身的實際狀況進行初始化,每一個關鍵字由'|'分隔開來 //private const string StrKeyWord = @"select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec master|netlocalgroup administrators|:|net user|""|or|and"; private const string StrKeyWord = @"select|insert|delete|from|drop table|update|truncate|exec master|netlocalgroup administrators|:|net user|or|and"; //Sql注入時,可能出現的特殊符號,,可根據本身的實際狀況進行初始化,每一個符號由'|'分隔開來 private const string StrRegex = @"-|;|,|/|(|)|[|]|}|{|%|@|*|!|'"; //private const string StrRegex = @"=|!|'"; public SqlChecker() { // // TODO: 在此處添加構造函數邏輯 // } /// <summary> /// 由此構造函數建立的對象,在驗證Sql注入以後將停留在原來頁面上 /// </summary> /// <param name="_request">當前請求的 Request 對象</param> /// <param name="_response">當前請求的 Response 對象</param> public SqlChecker(HttpRequest _request, HttpResponse _response) { this.request = _request; this.response = _response; } /// <summary> /// 由此構造函數建立的對象,在驗證Sql注入以後將請求將導向由 _safeUrl 指定的安全url頁面上 /// </summary> /// <param name="_request">當前請求的 Request 對象</param> /// <param name="_response">當前請求的 Response 對象</param> /// <param name="_safeUrl">驗證Sql注入以後將導向的安全 url</param> public SqlChecker(HttpRequest _request, HttpResponse _response, string _safeUrl) { this.request = _request; this.response = _response; this.safeUrl = _safeUrl; } /// <summary> /// 只讀屬性 SQL關鍵字 /// </summary> public string KeyWord { get { return StrKeyWord; } } /// <summary> /// 只讀屬性過濾特殊字符 /// </summary> public string RegexString { get { return StrRegex; } } /// <summary> /// 當出現Sql注入時須要提示的錯誤信息(主要是運行一些客戶端的腳本) /// </summary> public string Msg { get { string msg = "<script type='text/javascript'> " + " alert('請勿輸入非法字符!'); "; if (this.safeUrl == String.Empty) msg += " window.location.href = '" + request.RawUrl + "'"; else msg += " window.location.href = '" + safeUrl + "'"; msg += "</script>"; return msg; } } /// <summary> /// 檢查URL參數中是否帶有SQL注入的可能關鍵字。 /// </summary> /// <returns>存在SQL注入關鍵字時返回 true,不然返回 false</returns> public bool CheckRequestQuery() { bool result = false; if (request.QueryString.Count != 0) { //若URL中參數存在,則逐個檢驗參數。 foreach (string queryName in this.request.QueryString) { //過慮一些特殊的請求狀態值,主要是一些有關頁面視圖狀態的參數 if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION") continue; //開始檢查請求參數值是否合法 if (CheckKeyWord(request.QueryString[queryName])) { //只要存在一個可能出現Sql注入的參數,則直接退出 result = true; break; } } } return result; } /// <summary> /// 檢查提交表單中是否存在SQL注入的可能關鍵字 /// </summary> /// <returns>存在SQL注入關鍵字時返回 true,不然返回 false</returns> public bool CheckRequestForm() { bool result = false; if (request.Form.Count > 0) { //若獲取提交的表單項個數不爲0,則逐個比較參數 foreach (string queryName in this.request.Form) { //過慮一些特殊的請求狀態值,主要是一些有關頁面視圖狀態的參數 if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION") continue; //開始檢查提交的表單參數值是否合法 if (CheckKeyWord(request.Form[queryName])) { //只要存在一個可能出現Sql注入的參數,則直接退出 result = true; break; } } } return result; } /// <summary> /// 檢查_sword是否包涵SQL關鍵字 /// </summary> /// <param name="_sWord">須要檢查的字符串</param> /// <returns>存在SQL注入關鍵字時返回 true,不然返回 false</returns> public bool CheckKeyWord(string _sWord) { bool result = false; //模式1 : 對應Sql注入的可能關鍵字 string[] patten1 = StrKeyWord.Split('|'); //模式2 : 對應Sql注入的可能特殊符號 string[] patten2 = StrRegex.Split('|'); //開始檢查 模式1:Sql注入的可能關鍵字 的注入狀況 foreach (string sqlKey in patten1) { if (_sWord.IndexOf(" " + sqlKey) >= 0 || _sWord.IndexOf(sqlKey + " ") >= 0) { //只要存在一個可能出現Sql注入的參數,則直接退出 result = true; break; } } //開始檢查 模式1:Sql注入的可能特殊符號 的注入狀況 foreach (string sqlKey in patten2) { if (_sWord.IndexOf(sqlKey) >= 0) { //只要存在一個可能出現Sql注入的參數,則直接退出 result = true; break; } } return result; } /// <summary> /// 執行Sql注入驗證 /// </summary> public void Check() { if (CheckRequestQuery() || CheckRequestForm()) { response.Write(Msg); response.End(); } } } }
在Global.asax.cs中找到Application_BeginRequest,若是找不到Global.asax 也可Ctrl+F進行搜索,java
範圍能夠選擇【整個解決方案】點擊→進行查找。sql
具體代碼以下:shell
protected void Application_BeginRequest(object sender, EventArgs e) { SqlChecker SqlChecker = new SqlChecker(this.Request, this.Response); //或 SqlChecker SqlChecker = new SqlChecker(this.Request,this.Response,safeUrl); SqlChecker.Check(); }
SqlChecker SqlChecker = new SqlChecker(this.Request, this.Response); //或 SqlChecker SqlChecker = new SqlChecker(this.Request,this.Response,safeUrl); SqlChecker.Check();
以上就是C#.net防止SQL注入的代碼,若是有些關鍵字和特殊符號不想加在過濾中能夠自定義SqlChecker類的StrKeyWord變量和StrRegex變量c#