【问题标题】:This code for preventing MySQL injection is good?这个防止SQL注入的代码好用吗?
【发布时间】:2012-10-21 04:34:41
【问题描述】:

找到此代码用于防止使用 HTTPModules 进行一些基本的 MySql 注入

public class SampleSqlInjectionScreeningModuleCS : IHttpModule
{
    //Defines the set of characters that will be checked.
    //You can add to this list, or remove items from this list, as appropriate for your site
    public static string[] blackList = {"--",";--",";","/*","*/","@@","@",
                                       "char","nchar","varchar","nvarchar",
                                       "alter","begin","cast","create","cursor","declare","delete","drop","end","exec","execute",
                                       "fetch","insert","kill","open",
                                       "select", "sys","sysobjects","syscolumns",
                                       "table","update"};

    public void Dispose()
    {
        //no-op 
    }

    //Tells ASP.NET that there is code to run during BeginRequest
    public void Init(HttpApplication app)
    {
        app.BeginRequest += new EventHandler(app_BeginRequest);
    }

    //For each incoming request, check the query-string, form and cookie values for suspicious values.
    void app_BeginRequest(object sender, EventArgs e)
    {
        HttpRequest Request = (sender as HttpApplication).Context.Request;

        foreach (string key in Request.QueryString)
            CheckInput(Request.QueryString[key]);
        foreach (string key in Request.Form)
            CheckInput(Request.Form[key]);
        foreach (string key in Request.Cookies)
            CheckInput(Request.Cookies[key].Value);
    }

    //The utility method that performs the blacklist comparisons
    //You can change the error handling, and error redirect location to whatever makes sense for your site.
    private void CheckInput(string parameter)
    {
        for (int i = 0; i < blackList.Length; i++)
        {
            if ((parameter.IndexOf(blackList[i], StringComparison.OrdinalIgnoreCase) >= 0))
            {
                //
                //Handle the discovery of suspicious Sql characters here
                //
                HttpContext.Current.Response.Redirect("~/About.aspx");  //generic error page on your site
            }
        }
    }

}

这是一个好的代码还是你认为我需要在黑名单中添加更多的东西,或者忘记这个并尝试另一种方法来防止注入?

【问题讨论】:

  • 忘记这种方法,您可能会阻止有效内容。验证您的输入是否适合适当的类型,然后查看参数化查询。
  • 为什么不直接为 SQL 查询使用参数,不用担心?
  • 动态 SQL 是 99.999999999% 的邪恶。当您可以参数化输入并使用存储过程时,清理输入是不可靠的,并且会适得其反。
  • Bridge,整个系统已经围绕字符串中的命令构建,我们并没有真正考虑安全问题。所以我们没有时间重新全部重建,现在在其他项目中我们使用实体框架,更安全。

标签: c# mysql sql-injection


【解决方案1】:

不,不好。

它将阻止有效输入,并且绝不会保护从错误/无效数据构造查询的代码。

假设传入的数据很糟糕,只需正确构造查询,您的情况就会好得多。

【讨论】:

    【解决方案2】:

    不,黑名单无法阻止 SQL 注入。有关绕过黑名单的方法,请参阅 OWASP 页面。你应该只使用parameterized queries

    【讨论】:

      【解决方案3】:

      清理/过滤数据的黑名单方法从来没有是清理数据的最佳方法。 (虽然在某些情况下是合适的,这取决于权衡)

      这里有一个简单的解释:http://www.testingsecurity.com/whitelists_vs_blacklists

      黑名单正在根据否定列表测试所需的输入 输入的。基本上你会编制一份所有负面或 条件不好,然后验证接收到的输入不是 坏的或消极的条件。白名单正在测试所需的输入 针对可能的正确输入列表。要做到这一点,你会 编译所有良好输入值/条件的列表,然后验证 接收到的输入是这种正确条件之一。

      你觉得哪个更好?攻击者会使用任何手段 可以访问您的基于 Web 的应用程序。这包括 尝试各种负面或不良条件,各种编码 方法,并将恶意输入数据附加到有效数据中。你 认为你能想到所有可能的坏排列 发生?白名单是验证输入的最佳方式。你会知道 确切地说是需要什么,并且不接受任何错误的类型。 通常,创建白名单的最佳方法是使用 常用表达。使用正则表达式是一个很好的方法 抽象白名单,而不是手动列出所有可能的 正确的值。

      您最好使用标准的、久经考验的防御措施:参数化查询参数化存储过程

      【讨论】:

        【解决方案4】:

        parameterized queries 可以为您(以及更多)工作时,为什么还要执行字符串检查?

        在从代码发出的 SQL 语句中使用 Parameters.Add()Parameters.AddWithValue()

        【讨论】:

          猜你喜欢
          • 2012-07-30
          • 2015-11-08
          • 2010-12-20
          • 1970-01-01
          • 2020-04-16
          • 2011-10-18
          • 2022-08-05
          • 2014-01-28
          相关资源
          最近更新 更多