如何识别SQL注入的位置?

huangapple go评论68阅读模式
英文:

How to identify where SQL injection is?

问题

我被挑战任务来减少别人代码中的SQL注入风险,然而,我用来扫描代码的工具并没有解释注入发生在哪里,它只指出了方法。以以下代码为例:

public static CommandStatus<int> CreateTable(SqlConnection connection, String tableName, SQLColumn[] columns)
{
    string colSQL = columns.Select(f => f.ToString()).Aggregate((f1, f2) => f1 + ",\n" + f2);

    if(columns.FirstOrDefault(e => e.IsPK()) != null){
        string constraint = GenerateTableConstraint(tableName, columns.FirstOrDefault(e => e.IsPK()).ColumnName());
        colSQL += constraint;
    }

    string sql = "CREATE TABLE " + ParseTableName(tableName) + " (" + colSQL + ") ON [PRIMARY]";

    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = sql;
    cmd.Connection = connection;
    cmd.CommandTimeout = GetTimeout();

    try
    {
        int result = cmd.ExecuteNonQuery();

        return new CommandStatus<int>(result);
    }
    catch (Exception e)
    {
        return new CommandStatus<int>("Failed to create tableName " + tableName + " [SQL: " + sql + "]:" + e.Message, e);
    }
}

colSQL 的输出是:

"[RecordID] [NVARCHAR](255) NULL,\n[Address1] [NVARCHAR](255) NULL,\n[Address2] [NVARCHAR](255) NULL,\n[Address3] [NVARCHAR](255) NULL,\n[Address4] [NVARCHAR](255) NULL,\n[Address5] [NVARCHAR](255) NULL,\n[MongoId] [NVARCHAR](100) NOT NULL"

SQLColumn[] columns 只是以下数组的一部分:

Record
nvarchar(255)
null

我对注入的理解是,用 "[]" 包围字符串会使其安全。如果这不是情况,我该如何使这个方法免受SQL注入攻击?

如您所说,用 "[]" 包围列名是一种常见的方式来防止SQL注入。但是,这个方法仍然存在一些潜在的SQL注入风险,主要是因为它在生成SQL语句时使用了字符串拼接,而没有充分验证和转义输入数据。为了使这个方法更安全,您可以采取以下步骤:

  1. 使用参数化查询:不要直接将输入数据插入SQL语句中,而是使用参数化查询来传递参数。这将确保输入数据被正确地转义和验证,从而防止SQL注入攻击。

  2. 验证输入数据:在使用输入数据之前,确保对其进行验证,以防止恶意输入。在这种情况下,您可以验证列名和表名,以确保它们只包含有效的字符。

  3. 避免使用动态SQL:尽量避免使用动态生成的SQL语句。如果必须使用动态SQL,请确保正确地验证和转义所有输入数据,以防止注入。

  4. 使用存储过程:如果可能的话,考虑使用存储过程而不是动态生成的SQL语句。存储过程可以提供更好的安全性,因为它们通常不受SQL注入的影响。

  5. 使用ORM框架:考虑使用对象关系映射(ORM)框架,如Entity Framework或Dapper,来处理数据库操作。这些框架通常会自动处理参数化查询和其他安全性问题。

通过采取上述步骤,您可以显著提高代码的安全性,从而减少SQL注入的风险。

英文:

I have been challenged with the task of reducing SQL injection in somebody elses code however the tool that im using to scan my code doesnt give an explanation on where the injections are, as it just points out the method. Take the following code as an example

public static CommandStatus&lt;int&gt; CreateTable(SqlConnection connection, String tableName, SQLColumn[] columns)
{
    string colSQL = columns.Select(f =&gt; f.ToString()).Aggregate((f1, f2) =&gt; f1 + &quot;,\n&quot; + f2);


    if(columns.FirstOrDefault(e =&gt; e.IsPK()) != null){
        string constraint = GenerateTableConstraint(tableName, columns.FirstOrDefault(e =&gt; e.IsPK()).ColumnName());
        colSQL += constraint;
    }
   
    string sql = &quot;CREATE TABLE &quot; + ParseTableName(tableName) + &quot; (&quot; + colSQL + &quot;) ON [PRIMARY]&quot;;

    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = sql;
    cmd.Connection = connection;
    cmd.CommandTimeout = GetTimeout();

    try
    {
        int result = cmd.ExecuteNonQuery();

        return new CommandStatus&lt;int&gt;(result);
    }
    catch (Exception e)
    {
        return new CommandStatus&lt;int&gt;(&quot;Failed to create tableName &quot; + tableName + &quot; [SQL: &quot; + sql + &quot;]:&quot; + e.Message, e);
    }
}

The ouput from colSQL is

&quot;[RecordID] [NVARCHAR](255) NULL,\n[Address1] [NVARCHAR](255) NULL,\n[Address2] [NVARCHAR](255) NULL,\n[Address3] [NVARCHAR](255) NULL,\n[Address4] [NVARCHAR](255) NULL,\n[Address5] [NVARCHAR](255) NULL,\n[MongoId] [NVARCHAR](100) NOT NULL&quot;

SQLColumns[] columns is just an array of the following

Record
nvarchar(255)
null

My understanding of the inject is that surrounding a string with "[]" makes it safe. IF this is not the case how would I go about making this method SQL inject free.

答案1

得分: 0

如果您使用Visual Studio,那么您可以在解决方案上运行源代码分析,当它检测到问题点时,它会引发CA3001警告。

CA3001:检查代码是否存在SQL注入漏洞。

https://learn.microsoft.com/en-us/visualstudio/code-quality/ca3001

https://learn.microsoft.com/en-us/visualstudio/code-quality/code-analysis-for-managed-code-overview

英文:

If you're using Visual Studio, then you can run source code analysis on the solution and it will raise a CA3001 warning when it detects troublespots.

CA3001: Review code for SQL injection vulnerabilities

https://learn.microsoft.com/en-us/visualstudio/code-quality/ca3001

https://learn.microsoft.com/en-us/visualstudio/code-quality/code-analysis-for-managed-code-overview

huangapple
  • 本文由 发表于 2020年1月4日 01:00:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定