| 通过HttpModule实现数据库防注入 |
| 责任编辑:水土不服 更新日期:2007-2-24 |
|
|
通过相应的关键字去识别是否有 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" />
|
|
| 上一篇文章: 推荐:保护系统 用SVS为Windows穿上“层层”防弹衣 |
| 下一篇文章: 关于运行ASP服务器的安全权限配置清单 |
|
|
|
|