C# 这个防止MySQL注入的代码好吗?

C# 这个防止MySQL注入的代码好吗?,c#,mysql,sql-injection,C#,Mysql,Sql Injection,找到了使用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

找到了使用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注入。查看页面了解绕过黑名单的方法。您应该只使用黑名单方法对数据进行加密/筛选数据从来都不是对数据进行加密的最佳方法。尽管它在某些情况下是合适的,这取决于权衡

这里有一个简单的解释:

黑名单是针对一个负面列表测试所需的输入 输入的。基本上,您将编译一个所有负面或负面信息的列表 条件不良,然后验证接收到的输入不是 坏的或负面的情况。白名单正在测试所需的输入 对照可能正确输入的列表。要做到这一点,你必须 编制所有良好输入值/条件的列表,然后验证 接收到的输入是正确的条件之一

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


您最好使用标准的、经过验证的、真正的防御措施:参数化查询或参数化存储过程。

为什么要执行字符串检查,什么时候可以对您和其他人起作用


在您从代码发出的SQL语句上使用Parameters.Add或Parameters.AddWithValue。

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