频道栏目
首页 > 资讯 > 系统安全 > 正文

通过HttpModule实现数据库防注入

07-02-24        来源:[db:作者]  
收藏   我要投稿

通过相应的关键字去识别是否有 Sql注入攻击代码

string SqlStr = "and |exec |insert |select |delete |update |count | * |chr |mid |master |truncate |char |declare ";

在下面的代码中你要看以上面的定义, 其实就是定义要识别的关键字.

而我们处理请求一般都是通过 Request.QueryString / Request.Form 这两种

我们可以专门写一个类去处理这些请求, 但如果在每一个处理环节都载入这个类去做处理, 那太麻烦了.

如果写一个ISAPI当然也能完成这个功能的实现, 但在.NET 中 HttpModule帮我们实现了类似于ISAPI Filter的功能, 所以改为通过 HttpModule 去处理这些事情是最好不过的啦.

我们现在要用到的只是里面的BeginRequest这个事件, 所以只需要注册BeginRequest这个事件就可以了.

 

REM 过滤字符串
Dim strFilter As String = "and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare|&"
REM 分割后的过滤字符串数组
Dim strf() As String
Dim strTemp1, strTemp2 As String
strf = strFilter.Split("|")

If Request.RequestType = "GET" Then
For Each strTemp1 In Request.QueryString
For Each strTemp2 In strf
If InStr(LCase(strTemp1), LCase(strTemp2), CompareMethod.Text) Then
Response.Write("想干啥?别注我!有漏洞通知QQ:26242000")
Response.End()
End If
Next
Next
ElseIf Request.RequestType = "POST" Then
For Each strTemp1 In Request.Form
For Each strTemp2 In strf
If InStr(LCase(strTemp1), LCase(strTemp2), CompareMethod.Text) Then
Response.Write("想干啥?别注我!有漏洞通知QQ:26242000")
Response.End()
End If
Next
Next
End If


再来看看我在百度上找的sql防攻击代码

 

// @copyright S.Sams Lifexperience http://blog.8see.net/
using System;

namespace Theme.Services.Public
{
/// <summary>
/// SqlstrAny 的摘要说明。
/// </summary>
public class ProcessRequest
{
public ProcessRequest()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

#region SQL注入式攻击代码分析
/// <summary>
/// 处理用户提交的请求
/// </summary>
public void StartProcessRequest()
{
try
{
string getkeys = "";
string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString();
if (System.Web.HttpContext.Current.Request.QueryString != null)
{

for(int i=0;i<System.Web.HttpContext.Current.Request.QueryString.Count;i++)
{
getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];
if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
{
System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
System.Web.HttpContext.Current.Response.End();
}
}
}
if (System.Web.HttpContext.Current.Request.Form != null)
{
for(int i=0;i<System.Web.HttpContext.Current.Request.Form.Count;i++)
{
getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];
if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
{
System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
System.Web.HttpContext.Current.Response.End();
}
}
}
}
catch
{
// 错误处理: 处理用户提交信息!
}
}
/// <summary>
/// 分析用户请求是否正常
/// </summary>
/// <param name="Str">传入用户提交数据</param>
/// <returns>返回是否含有SQL注入式攻击代码</returns>
private bool ProcessSqlStr(string Str)
{
bool ReturnValue = true;
try
{
if (Str != "")
{
string SqlStr = "and |exec |insert |select |delete |update |count | * |chr |mid |master |truncate |char |declare ";
string[] anySqlStr = SqlStr.Split(|);
foreach (string ss in anySqlStr)
{
if (Str.IndexOf(ss)>=0)
{
ReturnValue = false;
}
}
}
}
catch
{
ReturnValue = false;
}
return ReturnValue;
}
#endregion

}
}

// System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString(); 这个为用户自定义错误页面提示地址,
//在Web.Config文件时里面添加一个 CustomErrorPage 即可
//<!-- 防止SQL数据库注入攻击的出错页面自定义地址 -->
// <add key="CustomErrorPage" value="../Error.html" />

相关TAG标签
上一篇:2010年Linux价值
下一篇:详细解析Inetinfo占CPU 100%的解决方案
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站