在Global.asax文件里实现通用防SQL注入漏洞程序
来源:互联网 发布:mac imovie不见了 编辑:程序博客网 时间:2024/04/30 16:29
首先,创建一个SQLInjectionHelper类完成恶意代码的检查
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
/// <summary>
///SQLInjectionHelper 的摘要说明
/// </summary>
public
class
SQLInjectionHelper
{
/// <summary>
/// 获取Post的数据
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public
static
bool ValidUrlData(string request)
{
bool result = false;
if
(request ==
"POST"
)
{
for
(int i = 0; i < HttpContext.Current.Request.Form.
Count
; i++)
{
result = ValidData(HttpContext.Current.Request.Form[i].ToString());
if
(result)
{
break
;
}
}
}
else
{
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>
/// <returns></returns>
private
static
bool ValidData(string inputData)
{
//验证inputData是否包含恶意集合
if
(Regex.IsMatch(inputData, GetRegexString()))
{
return
true;
}
else
{
return
false;
}
}
/// <summary>
/// 获取正则表达式
/// </summary>
/// <returns></returns>
private
static
string GetRegexString()
{
//构造SQL的注入关键字符
string[] strChar = {
"and"
,
"exec"
,
"insert"
,
"select"
,
"update"
,
"delete"
,
"count"
,
"from"
,
"drop"
,
"asc"
,
"or"
,
"char"
,
"%"
,
";"
,
":"
,
"\'"
,
"\""
,
"-"
,
"chr"
,
"master"
,
"mid"
,
"truncate"
,
"declare"
,
"char"
,
"SiteName"
,
"/add"
,
"xp_cmdshell"
,
"net user"
,
"net localgroup administrators"
,
"exec master.dbo.xp_cmdshell"
};
string str_Regex =
".*("
;
for
(int i = 0; i < strChar.Length - 1; i++)
{
str_Regex += strChar[i] +
"|"
;
}
str_Regex += strChar[strChar.Length - 1] +
").*"
;
return
str_Regex;
}
}
有此类后即可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件来实现表单或者URL提交数据的获取,获取后传给SQLInjectionHelper类ValidUrlData方法来完成检查
代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
protected
void Application_BeginRequest(object sender, EventArgs e)
{
bool result = false;
result = SQLInjectionHelper.ValidUrlData(Request.RequestType.ToUpper());
if
(result)
{
Response.Write(
"您提交的数据有恶意字符"
);
Response.
End
();
}
}
下面以一个小程序测试:
创建一个页面,如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeFile=
"Default.aspx.cs"
Inherits=
"_Default"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head runat=
"server"
>
<title></title>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<div>
<asp:TextBox ID=
"TextBox1"
runat=
"server"
></asp:TextBox>
<br />
<asp:Button ID=
"btnPost"
runat=
"server"
Text=
"获取Post数据"
onclick=
"btnPost_Click"
/>
</div>
<asp:Button ID=
"btnGet"
runat=
"server"
Text=
"获取Get数据"
onclick=
"btnGet_Click"
/>
</form>
</body>
</html>
分别添加单击事件,如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
protected
void btnPost_Click(object sender, EventArgs e)
{
}
protected
void btnGet_Click(object sender, EventArgs e)
{
Response.Redirect(
"Default.aspx?a=1&b=2&c=3"
);
}
在文本框中输入非法字符串,无论post请求还是get请求,都会被防SQL注入程序所截获
图1 测试防SQL注入程序的页面
转载于:http://www.cnblogs.com/shouce/p/5309538.html
0 0
- 在Global.asax文件里实现通用防SQL注入漏洞程序
- 一种通用防SQL注入漏洞程序(Global.asax方式)
- 在Gobal.asax文件中实现通用防sql注入漏洞程序
- .net通用防SQL注入漏洞程序(Global.asax方式)详细用法
- .net通用防SQL注入漏洞程序(Global.asax方式)详细用法
- Global.asax sql防注入
- ASP.NET 修改Global.asax文件,防SQL注入式攻击
- Global.asax.cs防注入问题
- Global.asax.cs防注入问题
- ASP 防 SQL 注入攻击通用程序
- 通用ASP的sql防注入程序
- SQL通用防注入程序 完美版
- 编写通用的sql防注入程序
- jsp的防sql注入通用程序
- sql 防注入漏洞
- 通过添加Global.asax防止SQL注入
- SQL 注入防漏洞攻击
- ASP.net--全局程序文件:Global.asax
- Androidd Intent总结
- Android 最火快速开发框架androidannotation简介
- 信用卡
- 2016.4.8每日小结
- php中的字符串操作
- 在Global.asax文件里实现通用防SQL注入漏洞程序
- 在命令行中运行eclipse中创建的java项目
- 分布式服务框架 Zookeeper -- 管理分布式环境中的数据
- git跟踪文件的修改信息及日志信息
- struts校验框架的一个异常
- Java NIO源码剖析及使用实例(一):Buffer
- 网页下拉加载更多
- Hibernate经典总结
- Java程序员应当知道的10个面向对象设计原则