通过HttpModule实现数据库防注入

昨天刚把数据库防注入的原型发了上去,发觉好像还漏了点东西,现在把它全部补上.
Sql注入是常常被一些菜鸟级黑客高手惯用的手法, 就是基于Sql的注入实现, 防注入程序其实就是处理Http请求,把Get和Post的请求数据中做过滤.

通过相应的关键字去识别是否有 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这个事件就可以了.

1 public class SqlstrAny:IHttpModule
2 {
3publicvoidInit(HttpApplicationapplication)
4{
5application.BeginRequest+=(new
6EventHandler(this.Application_BeginRequest));
7}

8privatevoidApplication_BeginRequest(Objectsource,EventArgse)
9{
10ProcessRequestpr=newProcessRequest();
11pr.StartProcessRequest();
12}

13publicvoidDispose()
14{
15}

16}

17 public class ProcessRequest
18 {
19(数据库防注入的核心请参阅:http://s.sams.cnblogs.com/articles/377624.html)
20}

记得在前面载入相应名字空间哦.
using System;
using System.Web;

忘了还得加个自定名字空间 namespace Theme.Script

以上就是通Application_BeginRequest实现的
ProcessRequest pr = new ProcessRequest();
pr.StartProcessRequest();
(数据库防注入的核心 请参阅: http://s.sams.cnblogs.com/articles/377624.html)

完整的类如下:

//-----------------SqlstrAny.cs------------------------

1 using System;
2 using System.Web;
3 namespace Theme.Script
4 {
5publicclassSqlstrAny:IHttpModule
6{
7publicvoidInit(HttpApplicationapplication)
8{
9application.BeginRequest+=(new
10EventHandler(this.Application_BeginRequest));
11}

12privatevoidApplication_BeginRequest(Objectsource,EventArgse)
13{
14//HttpApplicationApplication=(HttpApplication)source;
15//HttpResponseResponse=Application.Context.Response;
16//Response.Write("<h1>BeginningofRequest</h1><hr>");
17ProcessRequestpr=newProcessRequest();
18pr.StartProcessRequest();
19}

20publicvoidDispose()
21{
22}

23}

24
25publicclassProcessRequest
26{
27SQL注入式攻击代码分析#regionSQL注入式攻击代码分析
28/**////<summary>
29///处理用户提交的请求
30///</summary>

31publicvoidStartProcessRequest()
32{
33try
34{
35stringgetkeys="";
36stringsqlErrorPage=System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString();
37if(System.Web.HttpContext.Current.Request.QueryString!=null)
38{
39
40for(inti=0;i<System.Web.HttpContext.Current.Request.QueryString.Count;i++)
41{
42getkeys=System.Web.HttpContext.Current.Request.QueryString.Keys[i];
43if(!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
44{
45System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
46System.Web.HttpContext.Current.Response.End();
47}

48}

49}

50if(System.Web.HttpContext.Current.Request.Form!=null)
51{
52for(inti=0;i<System.Web.HttpContext.Current.Request.Form.Count;i++)
53{
54getkeys=System.Web.HttpContext.Current.Request.Form.Keys[i];
55if(!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
56{
57System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
58System.Web.HttpContext.Current.Response.End();
59}

60}

61}

62}

63catch
64{
65//错误处理:处理用户提交信息!
66}

67}

68/**////<summary>
69///分析用户请求是否正常
70///</summary>
71///<paramname="Str">传入用户提交数据</param>
72///<returns>返回是否含有SQL注入式攻击代码</returns>

73privateboolProcessSqlStr(stringStr)
74{
75boolReturnValue=true;
76try
77{
78if(Str!="")
79{
80stringSqlStr="and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";
81string[]anySqlStr=SqlStr.Split('|');
82foreach(stringssinanySqlStr)
83{
84if(Str.IndexOf(ss)>=0)
85{
86ReturnValue=false;
87}

88}

89}

90}

91catch
92{
93ReturnValue=false;
94}

95returnReturnValue;
96}

97#endregion

98}

99
100}

101
102


编写完处理后,我们把它生成类库请在Bin的目录下

csc.exe / t:librarySqlstrAny.cs / r:C:/windows/Microsoft.NET/Framework/v1. 1.4322 /Microsoft.VisualBasic.dll

编译完后你会发现已经生成了一个 SqlstrAny.Dll 文件,这个就是我们需要的.

最后在 Web.Config 中注册它就可以用了.

< system.web >
< httpModules >
< addname = " SqlstrAny " type = " Theme.Script.SqlstrAny,SqlstrAny " />
</ httpModules >
</ system.web >

最后别忘了在 Web.Config 中加入错误处理的导向页面哦.

< addkey = " CustomErrorPage " value = " ../Error.html " />

至此,所有步骤就完成了, 打你的项目运行它, 在URL后加上 Select / and ...试试

http://localhost/Theme.Script/Process/CreatePay.aspx?action=select s&t=true

OK,完成!

源码下载: http://www.cnblogs.com/Files/S.Sams/SqlstrAny.rar



-- S.Sams Lifexperience!

你可能感兴趣的:(Module)