T-SQL篇如何防止SQL注入的解决方法
1.什么是SQL注入
所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令。在某些表单中,用户输入的内容直接用来构造(或者影响)动态SQL命令,或作为存储过程的输入参数,这类表单特别容易受到SQL注入式攻击。
2.怎么进行SQL注入
关于怎么进行SQL注入,网上已经有很多文章详细介绍过了,可以参考博友滴答的雨的博文 《SQL注入攻防入门详解》,亲测有效。当执行完文中的5、6、7三步的时候,你会发现服务器上的安全保护措施都已是浮云,服务器也因此变成了名副其实的“肉机”。下面附上一张我在本机执行完文中描述的脚本后的效果截图(Win8 x64 操作系统):
<!--防SQL注入时的特征字符集-->
<add key="SqlKeyWord" value="select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec|master|net local group administrators|net user|or|and"/>
<add key="SqlRegex" value=";|(|)|[|]|{|}|%|@|*|'|!"/>
使用方法很简单,在站点的Global文件中,添加 Application_BeginRequest 事件即可:
protected void Application_BeginRequest(object sender, EventArgs e)
{
//防SQL注入代码
SqlInject myCheck = new SqlInject(this.Request);
myCheck.CheckSqlInject();
}
ASP.NET SQL 注入免费解决方案
任何一种使用数据库web程序(当然,也包括桌面程序)都有被SQL注入的风险。防止被SQL注入,最基本的方法是在代码级别就要阻止这种可能,这个网上讲的很多,我就不多说了。不过如果你拿到的是一个已经完工的产品,这个时候该如何解决呢?我介绍几种对于ASP和ASP.NET有效的防止SQL注入的方案,而且是免费的。
UrlScan 3.1
UrlScan 3.1是一个安全方面的工具,微软官方的东西。它会检查所有IIS处理的HTTP请求。UrlScan 可以在有安全问题的HTTP请求到达应用程序之前就阻止这个请求。UrlScan 3.1 是UrlScan 2.5的一个升级版本,支持Windows Vista 和Windows Server 2008系统之上的IIS 5.1, IIS 6.0 和 IIS 7.0。
链接地址:http://www.iis.net/expand/UrlScan 这里还有很多非常有用的IIS扩展,可以看看。
IIS 6 SQL Injection Sanitation ISAPI Wildcard
这个ISAPI dll 也是通过检查HTTP请求避免SQL注入。只兼容windows 2003上的 IIS 6.0。对于Windows XP 上的 IIS 5 不支持。
所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令。在某些表单中,用户输入的内容直接用来构造(或者影响)动态SQL命令,或作为存储过程的输入参数,这类表单特别容易受到SQL注入式攻击。
2.怎么进行SQL注入
关于怎么进行SQL注入,网上已经有很多文章详细介绍过了,可以参考博友滴答的雨的博文 《SQL注入攻防入门详解》,亲测有效。当执行完文中的5、6、7三步的时候,你会发现服务器上的安全保护措施都已是浮云,服务器也因此变成了名副其实的“肉机”。下面附上一张我在本机执行完文中描述的脚本后的效果截图(Win8 x64 操作系统):
1: /// <summary>
2: ///SqlInject 的摘要说明
3: /// </summary>
4: public class SqlInject : System.Web.UI.Page
5: {
6: //检测到注入后的处理方式: 0:仅警告;1:警告+记录;2:警告+自定义错误页面;3:警告+记录+自定义错误页面
7: private const int _type = 0;
8: private const string errRedirectPage = "/err.aspx";
9:
10: //如果记录注入信息,那么请设置:errMDBpath:数据库路径
11: private const string errMDBpath = "/SqlInject.mdb";
12:
13:
14: //过滤特征字符
15: //过滤特征字符
16: private static string StrKeyWord = ConfigurationManager.AppSettings["SqlKeyWord"]; //@"select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec|master|net local group administrators|net user|or|and";
17: private static string StrRegex = ConfigurationManager.AppSettings["SqlRegex"]; //@";|/|(|)|[|]|{|}|%|@|*|'|!"; // 原始过滤条件:【-|;|,|/|(|)|[|]|{|}|%|@|*|'|!】
18:
19: private HttpRequest request;
20: public SqlInject(System.Web.HttpRequest _request)
21: {
22: this.request = _request;
23: }
24: ///<summary>
25: ///检测SQL注入及记录、显示出错信息
26: ///</summary>
27: public void CheckSqlInject()
28: {
29: bool isInject = false;
30: if (CheckRequestQuery() || CheckRequestForm())
31: {
32: isInject = true;
33: }
34: else
35: {
36: return;
37: }
38:
39: switch (_type)
40: {
41: case 0:
42: ShowErr();
43: break;
44: case 1:
45: ShowErr();
46: SaveToMdb();
47: break;
48: case 2:
49: ShowErr();
50: string temp;
51: System.Web.HttpContext.Current.Response.Write("<script>setTimeout(\"" + "location.href='" + errRedirectPage + "'" + "\",5000)</script>");
52: break;
53: case 3:
54: ShowErr();
55: SaveToMdb();
56: System.Web.HttpContext.Current.Response.Write("<script>setTimeout(\"" + "location.href='" + errRedirectPage + "'" + "\",5000)</script>");
57: break;
58: default:
59: break;
60: }
61: System.Web.HttpContext.Current.Response.End();
62:
63: }
64: private void SaveToMdb()
65: {
66: OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Server.MapPath(errMDBpath));
67: conn.Open();
68: OleDbCommand cmd = conn.CreateCommand();
69:
70: cmd.CommandText = "insert into [Record] (sIP,sDate,sPath) values ('" +
71: request.ServerVariables["REMOTE_ADDR"].ToString() + "','" +
72: DateTime.Now + "','" + request.ServerVariables["URL"].ToLower() + RelaceSingleQuotes(request.QueryString.ToString()) + "')";
73: int code = cmd.ExecuteNonQuery();
74: if (code == 1)
75: System.Web.HttpContext.Current.Response.Write("<br>****以上信息已记录至日志数据库****");
76: else
77: System.Web.HttpContext.Current.Response.Write("<br>日志数据库出错");
78: conn.Close();
79:
80: }
81: private string RelaceSingleQuotes(string _url)
82: {
83: string URL = _url.Replace("'", "单引号");
84: return URL;
85: }
86: private void ShowErr()
87: {
88: //string msg = @"<font color=red>请不要尝试未授权之入侵检测!</font>" + @"<br><br>";
89: //msg += @"操作IP:" + request.ServerVariables["REMOTE_ADDR"] + @"<br>";
90: //msg += @"操作时间:" + DateTime.Now + @"<br>";
91: //msg += @"页面:" + request.ServerVariables["URL"].ToLower() + request.QueryString.ToString() + @"<br>";
92: //msg += @"<a href='#' onclick='javascript:window.close()'>关闭</a>";
93: //System.Web.HttpContext.Current.Response.Clear();
94: //System.Web.HttpContext.Current.Response.Write(msg);
95: System.Web.HttpContext.Current.Response.Write("<script>alert('请不要尝试未授权之入侵检测!');javascript:history.go(-1);</script>");
96: }
97: ///<summary>
98: /// 特征字符
99: ///</summary>
100: public static string KeyWord
101: {
102: get
103: {
104: return StrKeyWord;
105: }
106: }
107: ///<summary>
108: /// 特征符号
109: ///</summary>
110: public static string RegexString
111: {
112: get
113: {
114: return StrRegex;
115: }
116: }
117:
118: ///<summary>
119: ///检查字符串中是否包含Sql注入关键字
120: /// <param name="_key">被检查的字符串</param>
121: /// <returns>如果包含注入true;否则返回false</returns>
122: ///</summary>
123: private static bool CheckKeyWord(string _key)
124: {
125: string[] pattenString = StrKeyWord.Split('|');
126: string[] pattenRegex = StrRegex.Split('|');
127: foreach (string sqlParam in pattenString)
128: {
129: if (_key.Contains(sqlParam + " ") || _key.Contains(" " + sqlParam))
130: {
131: return true;
132: }
133: }
134: foreach (string sqlParam in pattenRegex)
135: {
136: if (_key.Contains(sqlParam))
137: {
138: return true;
139: }
140: }
141: return false;
142:
143: }
144: ///<summary>
145: ///检查URL中是否包含Sql注入
146: /// <param name="_request">当前HttpRequest对象</param>
147: /// <returns>如果包含注入true;否则返回false</returns>
148: ///</summary>
149: public bool CheckRequestQuery()
150: {
151: if (request.QueryString.Count > 0)
152: {
153: foreach (string sqlParam in this.request.QueryString)
154: {
155: if (sqlParam == "__VIEWSTATE") continue;
156: if (sqlParam == "__EVENTVALIDATION") continue;
157: if (CheckKeyWord(request.QueryString[sqlParam].ToLower()))
158: {
159: return true;
160: }
161: }
162: }
163: return false;
164: }
165: ///<summary>
166: ///检查提交的表单中是否包含Sql注入
167: /// <param name="_request">当前HttpRequest对象</param>
168: /// <returns>如果包含注入true;否则返回false</returns>
169: ///</summary>
170: public bool CheckRequestForm()
171: {
172: if (request.Form.Count > 0)
173: {
174: foreach (string sqlParam in this.request.Form)
175: {
176: if (sqlParam == "__VIEWSTATE") continue;
177: if (sqlParam == "__EVENTVALIDATION") continue;
178: if (CheckKeyWord(request.Form[sqlParam]))
179: {
180: return true;
181: }
182: }
183: }
184: return false;
185: }
186: }
过滤类是在某前辈的作品基础上改的,很抱歉我已经找不到最原始的出处了。需要在Web.Config中添加防SQL注入的特征字符集:
代码如下:
<!--防SQL注入时的特征字符集-->
<add key="SqlKeyWord" value="select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec|master|net local group administrators|net user|or|and"/>
<add key="SqlRegex" value=";|(|)|[|]|{|}|%|@|*|'|!"/>
使用方法很简单,在站点的Global文件中,添加 Application_BeginRequest 事件即可:
代码如下:
protected void Application_BeginRequest(object sender, EventArgs e)
{
//防SQL注入代码
SqlInject myCheck = new SqlInject(this.Request);
myCheck.CheckSqlInject();
}
ASP.NET SQL 注入免费解决方案
任何一种使用数据库web程序(当然,也包括桌面程序)都有被SQL注入的风险。防止被SQL注入,最基本的方法是在代码级别就要阻止这种可能,这个网上讲的很多,我就不多说了。不过如果你拿到的是一个已经完工的产品,这个时候该如何解决呢?我介绍几种对于ASP和ASP.NET有效的防止SQL注入的方案,而且是免费的。
UrlScan 3.1
UrlScan 3.1是一个安全方面的工具,微软官方的东西。它会检查所有IIS处理的HTTP请求。UrlScan 可以在有安全问题的HTTP请求到达应用程序之前就阻止这个请求。UrlScan 3.1 是UrlScan 2.5的一个升级版本,支持Windows Vista 和Windows Server 2008系统之上的IIS 5.1, IIS 6.0 和 IIS 7.0。
链接地址:http://www.iis.net/expand/UrlScan 这里还有很多非常有用的IIS扩展,可以看看。
IIS 6 SQL Injection Sanitation ISAPI Wildcard
这个ISAPI dll 也是通过检查HTTP请求避免SQL注入。只兼容windows 2003上的 IIS 6.0。对于Windows XP 上的 IIS 5 不支持。