我正在使用 SQL Server 2005 和 Windows Server 2000,想知道在我支持我的代码时是否有任何“自动”方法来阻止 SQL 注入攻击。
有人建议有以下方法:
- 放入某种 ISAPI 或 HTTP 模块,用于过滤请求帖子和查询字符串以查找面向注入的符号,并在请求到达应用程序之前使请求失败。这些解决方案中的大多数特定于 IIS 6 或更高版本。我跑5。
- 保证每个命令对象一次只运行一个 SQL 命令。
我的配置还有其他想法吗?
我正在使用 SQL Server 2005 和 Windows Server 2000,想知道在我支持我的代码时是否有任何“自动”方法来阻止 SQL 注入攻击。
有人建议有以下方法:
我的配置还有其他想法吗?
一般来说,没有自动的解决方案来防止 SQL 注入。SQL 注入是应用程序故障,而不是数据库故障。
解决方案是让您对执行将应用程序数据插入查询的 SQL 的所有情况进行代码审查。
这个建议的解决方案:
保证每个命令对象一次只运行一个 SQL 命令。
实际上并不能防止注射。例如,攻击者可以注入登录查询以在没有凭据的情况下登录。考虑:
"SELECT COUNT(*) FROM Users WHERE UserId = '{0}' AND PassHash = '{1}'"
可以使用以下 UserId 注入此模板:
' OR 1=1 --
产量:
"SELECT COUNT(*) FROM Users WHERE UserId = '' OR 1=1 --' AND PassHash = 'sdfklahsdlg'"
集中精力消除代码中的漏洞。
确保所有数据库调用都使用存储过程或参数化查询。
当我在我的服务器上进行大量注入攻击尝试时,我担心它们会占用不必要的资源。我用 c# 编写(被黑!)了一个 HttpModule,它可以过滤掉大多数 xss 和 sql 注入攻击。代码粘贴在下面,以及使网站使用它所需的配置部分。它应该放在一个项目中并编译为 WebSecurityFilter.dll,然后应该由 Web 项目引用(或以其他方式放在 bin 目录中)。
这仅适用于 asp.net,因此希望您的网站是基于 asp.net 的(我确实在评论中询问过,但没有得到答复)。
Web 配置部分(将其添加到 <system.web> 的 <httpModules> 部分:
<add name="SecurityHttpModule" type="WebSecurityFilter.SecurityHttpModule, WebSecurityFilter" />
模块代码(SecurityHttpModule.cs):
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
namespace WebSecurityFilter
{
class SecurityHttpModule : IHttpModule
{
class RegexWithDesc : Regex
{
string _errorText;
public string ErrorText
{
get { return _errorText; }
}
public RegexWithDesc(string regex, RegexOptions options, string errorText)
:base(regex, options)
{
_errorText = errorText;
}
}
/// <summary>
/// error text displayed when security violation is detected
/// </summary>
private string _errorhtml =
@"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.1//EN"" ""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"">" +
@"<html xmlns=""http://www.w3.org/1999/xhtml"" >" +
@"<body style=""background:black;"">" +
@"<table style=""width:100%"" >" +
@"<tr><td align=""center"">" +
@"<div style=""border:3px solid red;text-align:center;width:95%;color:red;padding:10px;text-decoration:blink;"">" +
@"SECURITY VIOLATION" +
@"<br/>" +
//@"<br/>" +
//@"go away" +
//@"<br/>" +
@"<br/>" +
@"{0}" +
@"<br/>" +
@"</div>" +
@"</td></tr>" +
@"</table>" +
@"</body>" +
@"</html>";
// regex for default checks
// http://www.securityfocus.com/infocus/1768
static RegexOptions _defaultRegexOptions = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace;
RegexWithDesc[] _regexCollection = new RegexWithDesc[]
{
new RegexWithDesc(@"((¼|<)[^\n]+(>|¾)*)|javascript|unescape", _defaultRegexOptions, "XSS 1"), //3.3
// new RegexWithDesc(@"(\')|(\-\-)", _defaultRegexOptions, "SQL 1"), //2.1
new RegexWithDesc(@"(=)[^\n]*(\'|(\-\-)|(;))", _defaultRegexOptions, "SQL 2"), //2.2
//new RegexWithDesc(@"\w*(\')(or)", _defaultRegexOptions, "SQL 3"), //2.3
new RegexWithDesc(@"(\')\s*(or|union|insert|delete|drop|update|create|(declare\s+@\w+))", _defaultRegexOptions, "SQL 4"), //2.4
new RegexWithDesc(@"exec(((\s|\+)+(s|x)p\w+)|(\s@))", _defaultRegexOptions, "SQL 5") //2.5
};
#region IHttpModule Members
public void Dispose()
{
// nothing to do
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
try
{
List<string> toCheck = new List<string>();
foreach (string key in HttpContext.Current.ApplicationInstance.Request.QueryString.AllKeys)
{
toCheck.Add(HttpContext.Current.ApplicationInstance.Request[key]);
}
foreach (string key in HttpContext.Current.ApplicationInstance.Request.Form.AllKeys)
{
toCheck.Add(HttpContext.Current.ApplicationInstance.Request.Form[key]);
}
foreach (RegexWithDesc regex in _regexCollection)
{
foreach (string param in toCheck)
{
string dp = HttpUtility.UrlDecode(param);
if (regex.IsMatch(dp))
{
HttpContext.Current.ApplicationInstance.Response.Write(string.Format(_errorhtml, regex.ErrorText));
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
}
}
}
}
catch (System.Threading.ThreadAbortException x)
{
throw;
}
catch (Exception ex)
{
HttpContext.Current.ApplicationInstance.Response.Write(string.Format(_errorhtml, "Attack Vector Detected"));
HttpContext.Current.ApplicationInstance.Response.Write(string.Format(_errorhtml, ex.GetType().ToString()));
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
}
}
#endregion
}
}
希望一切都格式化好...
今天晚上,我将尝试以 zip 的形式发布指向完整项目的链接。
始终清理用户输入