在Global.asax文件里實(shí)現(xiàn)通用防SQL注入漏洞程序(適應(yīng)于post/get請求)
代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
/// <summary>
///SQLInjectionHelper 的摘要說明
/// </summary>
public class SQLInjectionHelper
{
/// <summary>
/// 獲取Post的數(shù)據(jù)
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static bool ValidUrlData(string request)
{
bool result = false;
if (request == "POST")
{
for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
{
result = ValidData(HttpContext.Current.Request.Form[i].ToString());
if (result)
{
break;
}
}
}
else
{
for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
{
result = ValidData(HttpContext.Current.Request.QueryString[i].ToString());
if (result)
{
break;
}
}
}
return result;
}
/// <summary>
/// 驗(yàn)證是否存在注入代碼
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
private static bool ValidData(string inputData)
{
//驗(yàn)證inputData是否包含惡意集合
if (Regex.IsMatch(inputData, GetRegexString()))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 獲取正則表達(dá)式
/// </summary>
/// <returns></returns>
private static string GetRegexString()
{
//構(gòu)造SQL的注入關(guān)鍵字符
string[] strChar = { "and", "exec", "insert", "select", "update", "delete", "count", "from", "drop", "asc", "or", "char", "%", ";", ":", "\'", "\"", "-", "chr", "master", "mid", "truncate", "declare", "char", "SiteName", "/add", "xp_cmdshell", "net user", "net localgroup administrators", "exec master.dbo.xp_cmdshell" };
string str_Regex = ".*(";
for (int i = 0; i < strChar.Length - 1; i++)
{
str_Regex += strChar[i] + "|";
}
str_Regex += strChar[strChar.Length - 1] + ").*";
return str_Regex;
}
}
有此類后即可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件來實(shí)現(xiàn)表單或者URL提交數(shù)據(jù)的獲取,獲取后傳給SQLInjectionHelper類ValidUrlData方法來完成檢查
代碼如下
protected void Application_BeginRequest(object sender, EventArgs e)
{
bool result = false;
result = SQLInjectionHelper.ValidUrlData(Request.RequestType.ToUpper());
if (result)
{
Response.Write("您提交的數(shù)據(jù)有惡意字符");
Response.End();
}
}
下面以一個小程序測試:
創(chuàng)建一個頁面,如下
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnPost" runat="server" Text="獲取Post數(shù)據(jù)"
onclick="btnPost_Click" />
</div>
<asp:Button ID="btnGet" runat="server" Text="獲取Get數(shù)據(jù)" onclick="btnGet_Click" />
</form>
</body>
</html>
分別添加單擊事件,如下
protected void btnPost_Click(object sender, EventArgs e)
{
}
protected void btnGet_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx?a=1&b=2&c=3");
}
在文本框中輸入非法字符串,無論post請求還是get請求,都會被防SQL注入程序所截獲

圖1 測試防SQL注入程序的頁面

圖2 錯誤信息
相關(guān)文章
深入淺析WinForm 進(jìn)程、線程及區(qū)別介紹
進(jìn)程是一個具有獨(dú)立功能的程序關(guān)于某個數(shù)據(jù)集合的一次運(yùn)行活動。線程,有時(shí)被稱為輕量級進(jìn)程(Lightweight Process,LWP),是程序執(zhí)行流的最小單元。這篇文章主要介紹了WinForm 進(jìn)程、線程的相關(guān)資料,需要的朋友可以參考下2016-09-09
在ASP.NET使用JavaScript顯示信息提示窗口實(shí)現(xiàn)原理及代碼
在ASP.NET使用JavaScript顯示信息窗口,感興趣的朋友可以了解一下,本文將介紹詳細(xì)的操作步驟,希望對你的JavaScript知識鞏固有所幫助2013-01-01
Asp.Net中的字符串和HTML十進(jìn)制編碼轉(zhuǎn)換實(shí)現(xiàn)代碼
這篇文章主要介紹了Asp.Net中的字符串和HTML十進(jìn)制編碼轉(zhuǎn)換實(shí)現(xiàn)代碼,本文一并列出了javascript語言的實(shí)現(xiàn)方法,用以實(shí)現(xiàn)字符串和HTML十進(jìn)制編碼之間互相轉(zhuǎn)換功能,需要的朋友可以參考下2014-08-08
ASP.NET下使用WScript.Shell執(zhí)行命令
ASP.NET下有自己的執(zhí)行CMD命令的方式,這里用WScript.Shell似有畫蛇添足之嫌,但是我們也不能排除真的有機(jī)器禁用了.NET的相關(guān)類,未雨綢繆嘛。當(dāng)然也不僅僅局限于WScript.Shell,只要是ASP中能用的組件,統(tǒng)統(tǒng)都可以用于ASP.NET中,而且還更方便!2008-05-05
ASP.NET Core 配置和使用環(huán)境變量的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core 配置和使用環(huán)境變量的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
.Net Winform 實(shí)現(xiàn)CSS3.0 潑墨畫效果(示例代碼)
這篇文章主要介紹了.Net Winform 實(shí)現(xiàn)CSS3.0 潑墨畫效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-12-12
Asp.net中Response.Charset與Response.ContentEncoding區(qū)別示例分析
這篇文章主要介紹了Asp.net中Response.Charset與Response.ContentEncoding區(qū)別示例分析,對于深入理解Asp.net程序設(shè)計(jì)有一定的幫助,需要的朋友可以參考下2014-08-08

