首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于HTTP或Server的SQL注入解决方案

基于HTTP或Server的SQL注入解决方案
EN

Stack Overflow用户
提问于 2008-12-09 20:41:59
回答 5查看 1.6K关注 0票数 1

我正在使用Server 2005和Windows 2000,我想知道在我支持代码的同时是否有任何“自动”的方法来阻止SQL注入攻击。

有些人建议,有办法:

  1. 引入了某种ISAPI或HTTP模块,该模块为面向注入的符号过滤请求、帖子和查询字符串,甚至在请求到达应用程序之前就失败了。这些解决方案中的大多数特定于IIS 6或更高版本。
  2. 保证每个命令对象一次只运行一个SQL命令。

对我的配置还有其他想法吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2008-12-09 21:34:20

当我在服务器上进行大量的注入攻击尝试时,我担心它们占用了不必要的资源。我写了(黑!)HttpModule在c#中,将过滤掉大多数xss和sql注入攻击。下面粘贴代码,以及使网站使用该代码所需的配置部分。它应该放在一个项目中并编译到WebSecurityFilter.dll中,然后web项目应该引用它(或者以其他方式放在bin目录中)。

这只适用于asp.net,所以希望您的站点是基于asp.net的(我在评论中问过,但没有得到答复)。

Web部分(将其添加到的部分:

代码语言:javascript
复制
  <add name="SecurityHttpModule" type="WebSecurityFilter.SecurityHttpModule, WebSecurityFilter" />

模块代码(SecurityHttpModule.cs):

代码语言:javascript
复制
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
    }
}

希望一切都格式化好了..。

今天晚上,我将尝试在一个邮编中发布一个完整项目的链接。

票数 1
EN

Stack Overflow用户

发布于 2008-12-09 20:49:34

在一般情况下,没有防止SQL注入的自动解决方案。SQL注入是应用程序错误,而不是数据库错误。

解决方案是对执行将应用程序数据插入查询的SQL的所有情况进行代码检查。

票数 1
EN

Stack Overflow用户

发布于 2008-12-09 20:56:00

这一拟议的解决办法:

保证每个命令对象一次只运行一个SQL命令。

实际上并不能阻止注射。例如,攻击者可以插入登录查询,以便在没有凭据的情况下登录。考虑:

代码语言:javascript
复制
"SELECT COUNT(*) FROM Users WHERE UserId = '{0}' AND PassHash = '{1}'"

这个模板可以注入一个UserId值为:

代码语言:javascript
复制
' OR 1=1 --

屈服:

代码语言:javascript
复制
"SELECT COUNT(*) FROM Users WHERE UserId = '' OR 1=1 --' AND PassHash = 'sdfklahsdlg'"

集中精力消除代码中的漏洞。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/354161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档