原理很简单:使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件,实现表单或URL提交数据的获取,然后通过SQLInjectionHelper类完成恶意代码的检查。
本代码只是考虑到通用性和部署简易性,因为项目已经开发完毕,并已经上线,为避免大量修改,才写的这个通用程序,大家可以在这里面加入不需要检查的表单值,这样不检查某些字段也是可以实现的。
直接贴代码:
Global.ascx.cs
Code
//在接收到一个应用程序请求时触发。对于一个请求来说,它是第一个被触发的事件,请求一般是用户输入的一个页面请求(URL)。
void Application_BeginRequest(object sender, EventArgs e)
{
//Response.Write("通用注入检查");
bool result = false;
if (Request.RequestType.ToUpper() == "POST")
{
result = SQLInjectionHelper.ValidUrlPostData();//Post数据检查
}
else
{
result = SQLInjectionHelper.ValidUrlGetData();//Get数据检查
}
if (result)
{
Response.Write("您提交的数据有恶意字符!");
Response.Write("<script>alert('您提交的数据有恶意字符!');<//script>");
Response.End();
}
}
SQLInjectionHelper.cs注入检测辅助类
/// <summary>
/// 获取Post的数据
/// </summary>
public static bool ValidUrlPostData()
{
bool result = false;
for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
{
result = ValidData(HttpContext.Current.Request.Form[i].ToString());
if (result)
{
break;
}//如果检测存在漏洞
}
return result;
}
/// <summary>
/// 获取QueryString中的数据
/// </summary>
public static bool ValidUrlGetData()
{
bool result = false;
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>
/// 验证是否存在注入代码
/// </summary>
/// <param name="inputData"></param>
public static bool ValidData(string inputData)
{
//里面定义恶意字符集合
//验证inputData是否包含恶意集合
if (Regex.IsMatch(inputData, GetRegexString()))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 获取正则表达式
/// </summary>
/// <param name="queryConditions"></param>
/// <returns></returns>
private static string GetRegexString()
{
//构造SQL的注入关键字符
string[] strBadChar = {"and"
,"exec"
,"insert"
,"select"
,"delete"
,"update"
,"count"
,"from"
,"drop"
,"asc"
,"char"
,"or"
//,"*"
,"%"
,";"
,":"
,"\'"
,"\""
,"-"
,"chr"
,"mid"
,"master"
,"truncate"
,"char"
,"declare"
,"SiteName"
,"net user"
,"xp_cmdshell"
,"/add"
,"exec master.dbo.xp_cmdshell"
,"net localgroup administrators"};
//构造正则表达式
string str_Regex = ".*(";
for (int i = 0; i < strBadChar.Length - 1; i++)
{
str_Regex += strBadChar[i] + "|";
}
str_Regex += strBadChar[strBadChar.Length - 1] + ").*";
return str_Regex;
}
http://www.cnblogs.com/hongxuecn/archive/2009/07/17/1525631.html
//在接收到一个应用程序请求时触发。对于一个请求来说,它是第一个被触发的事件,请求一般是用户输入的一个页面请求(URL)。
void Application_BeginRequest(object sender, EventArgs e)
{
//Response.Write("通用注入检查");
bool result = false;
if (Request.RequestType.ToUpper() == "POST")
{
result = SQLInjectionHelper.ValidUrlPostData();//Post数据检查
}
else
{
result = SQLInjectionHelper.ValidUrlGetData();//Get数据检查
}
if (result)
{
Response.Write("您提交的数据有恶意字符!");
Response.Write("<script>alert('您提交的数据有恶意字符!');<//script>");
Response.End();
}
}
SQLInjectionHelper.cs注入检测辅助类
/// <summary>
/// 获取Post的数据
/// </summary>
public static bool ValidUrlPostData()
{
bool result = false;
for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
{
result = ValidData(HttpContext.Current.Request.Form[i].ToString());
if (result)
{
break;
}//如果检测存在漏洞
}
return result;
}
/// <summary>
/// 获取QueryString中的数据
/// </summary>
public static bool ValidUrlGetData()
{
bool result = false;
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>
/// 验证是否存在注入代码
/// </summary>
/// <param name="inputData"></param>
public static bool ValidData(string inputData)
{
//里面定义恶意字符集合
//验证inputData是否包含恶意集合
if (Regex.IsMatch(inputData, GetRegexString()))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 获取正则表达式
/// </summary>
/// <param name="queryConditions"></param>
/// <returns></returns>
private static string GetRegexString()
{
//构造SQL的注入关键字符
string[] strBadChar = {"and"
,"exec"
,"insert"
,"select"
,"delete"
,"update"
,"count"
,"from"
,"drop"
,"asc"
,"char"
,"or"
//,"*"
,"%"
,";"
,":"
,"\'"
,"\""
,"-"
,"chr"
,"mid"
,"master"
,"truncate"
,"char"
,"declare"
,"SiteName"
,"net user"
,"xp_cmdshell"
,"/add"
,"exec master.dbo.xp_cmdshell"
,"net localgroup administrators"};
//构造正则表达式
string str_Regex = ".*(";
for (int i = 0; i < strBadChar.Length - 1; i++)
{
str_Regex += strBadChar[i] + "|";
}
str_Regex += strBadChar[strBadChar.Length - 1] + ").*";
return str_Regex;
}