asp.net(C#)防sql注入組件的實(shí)現(xiàn)代碼
更新時(shí)間:2009年12月27日 22:02:45 作者:
經(jīng)常要寫(xiě)一些.net的程序,對(duì)于數(shù)據(jù)庫(kù)的防注入要求要比較高。這時(shí)我從網(wǎng)上搜了一些代碼。查看了一下主要是通過(guò)HTTPModel來(lái)進(jìn)行對(duì)客戶端轉(zhuǎn)過(guò)來(lái)的數(shù)據(jù)進(jìn)行處理。
在服務(wù)器安全欄目里我寫(xiě)過(guò)一篇《破解通用Sql防注入方法》的文章中說(shuō)到,一些通用的防注入方法中沒(méi)有對(duì)cookie數(shù)據(jù)進(jìn)行過(guò)濾,會(huì)給黑客留下可乘之機(jī)。當(dāng)然我的這段代碼對(duì)提交過(guò)來(lái)的cookie數(shù)據(jù)也進(jìn)行了過(guò)濾。
代碼:
using System;
using System.Configuration;
using System.Web;
using System.Globalization;
namespace JNYW.StuM.SqlInject
{
public class SqlstrAny : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += (new
EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
ProcessRequest pr = new ProcessRequest();
pr.StartProcessRequest();
}
public void Dispose()
{
}
}
public class ProcessRequest
{
private static string SqlStr = System.Configuration.ConfigurationManager.AppSettings["SqlInject"].ToString();
private static string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["SQLInjectErrPage"].ToString();
///
/// 用來(lái)識(shí)別是否是流的方式傳輸
///
///
///
bool IsUploadRequest(HttpRequest request)
{
return StringStartsWithAnotherIgnoreCase(request.ContentType, "multipart/form-data");
}
///
/// 比較內(nèi)容類(lèi)型
///
///
///
///
private static bool StringStartsWithAnotherIgnoreCase(string s1, string s2)
{
return (string.Compare(s1, 0, s2, 0, s2.Length, true, CultureInfo.InvariantCulture) == 0);
}
//SQL注入式攻擊代碼分析
#region SQL注入式攻擊代碼分析
///
/// 處理用戶提交的請(qǐng)求
///
public void StartProcessRequest()
{
HttpRequest Request = System.Web.HttpContext.Current.Request;
HttpResponse Response = System.Web.HttpContext.Current.Response;
try
{
string getkeys = "";
if (IsUploadRequest(Request)) return; //如果是流傳遞就退出
//字符串參數(shù)
if (Request.QueryString != null)
{
for (int i = 0; i < Request.QueryString.Count; i++)
{
getkeys = Request.QueryString.Keys[i];
if (!ProcessSqlStr(Request.QueryString[getkeys]))
{
Response.Redirect(sqlErrorPage + "?errmsg=QueryString中含有非法字符串&sqlprocess=true");
Response.End();
}
}
}
//form參數(shù)
if (Request.Form != null)
{
for (int i = 0; i < Request.Form.Count; i++)
{
getkeys = Request.Form.Keys[i];
if (!ProcessSqlStr(Request.Form[getkeys]))
{
Response.Redirect(sqlErrorPage + "?errmsg=Form中含有非法字符串&sqlprocess=true");
Response.End();
}
}
}
//cookie參數(shù)
if (Request.Cookies != null)
{
for (int i = 0; i < Request.Cookies.Count; i++)
{
getkeys = Request.Cookies.Keys[i];
if (!ProcessSqlStr(Request.Cookies[getkeys].Value))
{
Response.Redirect(sqlErrorPage + "?errmsg=Cookie中含有非法字符串&sqlprocess=true");
Response.End();
}
}
}
}
catch
{
// 錯(cuò)誤處理: 處理用戶提交信息!
Response.Clear();
Response.Write("CustomErrorPage配置錯(cuò)誤");
Response.End();
}
}
///
/// 分析用戶請(qǐng)求是否正常
///
/// 傳入用戶提交數(shù)據(jù)
/// 返回是否含有SQL注入式攻擊代碼
private bool ProcessSqlStr(string Str)
{
bool ReturnValue = true;
try
{
if (Str != "")
{
string[] anySqlStr = SqlStr.Split('|');
foreach (string ss in anySqlStr)
{
if (Str.IndexOf(ss) >= 0)
{
ReturnValue = false;
break;
}
}
}
}
catch
{
ReturnValue = false;
}
return ReturnValue;
}
#endregion
}
}
在實(shí)際使用時(shí),我們要在Web.config文件中的配置節(jié)中加上下面的代碼
以下是示例代碼:
<!--防注入設(shè)置-->
<add value="and |exec |insert |select |delete |update |count | * |chr |mid |master |truncate |char |declare " key="SQLInject" />
<add value="ShowErr.aspx" key="SQLInjectErrPage" />
并且在Web.Config文件的<SYSTEM.WEB>中再加上下面的代碼。 以下是示例代碼:
<!--防注入設(shè)置-->
<HTTPMODULES>
<aDD name="SqlstrAny" type="JNYW.StuM.SqlInject.SqlstrAny,SqlstrAny" />
</HTTPMODULES>
代碼:
復(fù)制代碼 代碼如下:
using System;
using System.Configuration;
using System.Web;
using System.Globalization;
namespace JNYW.StuM.SqlInject
{
public class SqlstrAny : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += (new
EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
ProcessRequest pr = new ProcessRequest();
pr.StartProcessRequest();
}
public void Dispose()
{
}
}
public class ProcessRequest
{
private static string SqlStr = System.Configuration.ConfigurationManager.AppSettings["SqlInject"].ToString();
private static string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["SQLInjectErrPage"].ToString();
///
/// 用來(lái)識(shí)別是否是流的方式傳輸
///
///
///
bool IsUploadRequest(HttpRequest request)
{
return StringStartsWithAnotherIgnoreCase(request.ContentType, "multipart/form-data");
}
///
/// 比較內(nèi)容類(lèi)型
///
///
///
///
private static bool StringStartsWithAnotherIgnoreCase(string s1, string s2)
{
return (string.Compare(s1, 0, s2, 0, s2.Length, true, CultureInfo.InvariantCulture) == 0);
}
//SQL注入式攻擊代碼分析
#region SQL注入式攻擊代碼分析
///
/// 處理用戶提交的請(qǐng)求
///
public void StartProcessRequest()
{
HttpRequest Request = System.Web.HttpContext.Current.Request;
HttpResponse Response = System.Web.HttpContext.Current.Response;
try
{
string getkeys = "";
if (IsUploadRequest(Request)) return; //如果是流傳遞就退出
//字符串參數(shù)
if (Request.QueryString != null)
{
for (int i = 0; i < Request.QueryString.Count; i++)
{
getkeys = Request.QueryString.Keys[i];
if (!ProcessSqlStr(Request.QueryString[getkeys]))
{
Response.Redirect(sqlErrorPage + "?errmsg=QueryString中含有非法字符串&sqlprocess=true");
Response.End();
}
}
}
//form參數(shù)
if (Request.Form != null)
{
for (int i = 0; i < Request.Form.Count; i++)
{
getkeys = Request.Form.Keys[i];
if (!ProcessSqlStr(Request.Form[getkeys]))
{
Response.Redirect(sqlErrorPage + "?errmsg=Form中含有非法字符串&sqlprocess=true");
Response.End();
}
}
}
//cookie參數(shù)
if (Request.Cookies != null)
{
for (int i = 0; i < Request.Cookies.Count; i++)
{
getkeys = Request.Cookies.Keys[i];
if (!ProcessSqlStr(Request.Cookies[getkeys].Value))
{
Response.Redirect(sqlErrorPage + "?errmsg=Cookie中含有非法字符串&sqlprocess=true");
Response.End();
}
}
}
}
catch
{
// 錯(cuò)誤處理: 處理用戶提交信息!
Response.Clear();
Response.Write("CustomErrorPage配置錯(cuò)誤");
Response.End();
}
}
///
/// 分析用戶請(qǐng)求是否正常
///
/// 傳入用戶提交數(shù)據(jù)
/// 返回是否含有SQL注入式攻擊代碼
private bool ProcessSqlStr(string Str)
{
bool ReturnValue = true;
try
{
if (Str != "")
{
string[] anySqlStr = SqlStr.Split('|');
foreach (string ss in anySqlStr)
{
if (Str.IndexOf(ss) >= 0)
{
ReturnValue = false;
break;
}
}
}
}
catch
{
ReturnValue = false;
}
return ReturnValue;
}
#endregion
}
}
在實(shí)際使用時(shí),我們要在Web.config文件中的配置節(jié)中加上下面的代碼
以下是示例代碼:
復(fù)制代碼 代碼如下:
<!--防注入設(shè)置-->
<add value="and |exec |insert |select |delete |update |count | * |chr |mid |master |truncate |char |declare " key="SQLInject" />
<add value="ShowErr.aspx" key="SQLInjectErrPage" />
并且在Web.Config文件的<SYSTEM.WEB>中再加上下面的代碼。 以下是示例代碼:
復(fù)制代碼 代碼如下:
<!--防注入設(shè)置-->
<HTTPMODULES>
<aDD name="SqlstrAny" type="JNYW.StuM.SqlInject.SqlstrAny,SqlstrAny" />
</HTTPMODULES>
相關(guān)文章
導(dǎo)致Asp.Net站點(diǎn)重啟10個(gè)原因小結(jié)分析
Asp.Net站點(diǎn)有時(shí)候會(huì)莫名其妙的重啟,什么原因?qū)е碌膮s不得而知,經(jīng)過(guò)一番折騰后,我總結(jié)了導(dǎo)致Asp.Net站點(diǎn)重啟的10個(gè)原因,需要的朋友可以參考下。2011-08-08GridView分頁(yè)的實(shí)現(xiàn)以及自定義分頁(yè)樣式功能實(shí)例
本文為大家詳細(xì)介紹下GridView實(shí)現(xiàn)分頁(yè)并自定義的分頁(yè)樣式,具體示例代碼如下,有想學(xué)習(xí)的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07.net 像hao123的快捷郵箱登陸的實(shí)現(xiàn)代碼
.net實(shí)現(xiàn)的像hao123的快捷郵箱登陸的代碼,需要的朋友可以參考下。2010-03-03ASP.NET MVC學(xué)習(xí)教程之Razor語(yǔ)法
這篇文章主要給大家介紹了關(guān)于ASP.NET MVC學(xué)習(xí)教程之Razor語(yǔ)法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05.net6簡(jiǎn)單使用NPOI讀取Excel的項(xiàng)目實(shí)踐
本文主要介紹了.net6簡(jiǎn)單使用NPOI讀取Excel的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Asp.net MVC 中利用jquery datatables 實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示功能
這篇文章主要介紹了Asp.net MVC 中利用jquery datatables 實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06