在伺服器安全欄目裡我寫過一篇《破解通用Sql防注入方法》的文章中說到,一些通用的防注入方法中沒有對cookie資料進行過濾,會給駭客留下可乘之機。當然我的這段代碼對提交過來的cookie資料也進行了過濾。
代碼:
複製代碼 代碼如下: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();
///
/// 用來識別是否是流的方式傳輸
///
///
///
bool IsUploadRequest(HttpRequest request)
{
return StringStartsWithAnotherIgnoreCase(request.ContentType, "multipart/form-data");
}
///
/// 比較內容類型
///
///
///
///
private static bool StringStartsWithAnotherIgnoreCase(string s1, string s2)
{
return (string.Compare(s1, 0, s2, 0, s2.Length, true, CultureInfo.InvariantCulture) == 0);
}
//SQL注入式攻擊程式碼分析
#region SQL注入式攻擊程式碼分析
///
/// 處理使用者提交的請求
///
public void StartProcessRequest()
{
HttpRequest Request = System.Web.HttpContext.Current.Request;
HttpResponse Response = System.Web.HttpContext.Current.Response;
try
{
string getkeys = "";
if (IsUploadRequest(Request)) return; //如果是流傳遞就退出
//字串參數
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參數
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參數
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
{
// 錯誤處理: 處理使用者提交資訊!
Response.Clear();
Response.Write("CustomErrorPage配置錯誤");
Response.End();
}
}
///
/// 分析使用者請求是否正常
///
/// 傳入使用者提交資料
/// 返回是否含有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
}
}
在實際使用時,我們要在Web.config檔案中的配置節中加上下面的代碼
以下是範例程式碼: 複製代碼 代碼如下:<!--防注入設定-->
<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>中再加上下面的代碼。 以下是範例程式碼: 複製代碼 代碼如下:<!--防注入設定-->
<HTTPMODULES>
<aDD name="SqlstrAny" type="JNYW.StuM.SqlInject.SqlstrAny,SqlstrAny" />
</HTTPMODULES>