防止注入 RunCustomQuery(string SqlQuery, List<IDbDataParameter> SqlParameters)

Protect RunCustomQuery(string SqlQuery, List<IDbDataParameter> SqlParameters) from injection

提问人:Jean-David Lanz 提问时间:10/11/2022 最后编辑:Mohammad AghazadehJean-David Lanz 更新时间:10/12/2022 访问量:50

问:

我们使用类 , 连接到各种数据库并运行查询。DBLinker

它依赖于其核心方法,以返回查询结果或执行或查询(在第一种情况下可能返回新行的 ID)。RunCustomQuery(string p_strSqlQuery, List<IDbDataParameter> p_lisParameters)SelectInsertUpdateDelete

值得一提的是,查询可以在各种数据库系统上运行,具体取决于我不会详细介绍的属性。DBLinker

最近,我们所有的项目都经过了Kiuwan的分析。上述分析通过注入带来了非常高的漏洞。RunCustomQuery()

以下是相关代码,为简洁起见,进行了删减:

public virtual object RunCustomQuery(string p_strSqlQuery, out string p_strSqlActualQuery, List<IDbDataParameter> p_lisParameters = null)
{
    p_strSqlActualQuery = p_strSqlQuery;
    // [...]

    IDbCommand z_dcdCommand = null;
    IDbDataAdapter z_daaAdaptateur = null;
    try
    {
      // [...] create z_dcdCommand according to the target database system
      z_dcdCommand.CommandType = CommandType.Text;
      // [...]
      if (p_lisParameters != null)
      {
         foreach (IDbDataParameter z_dpaParameter in p_lisParameters) // Here Kiuwan says there's a data path sink
         {
            // [...]
            p_strSqlActualQuery =p_strSqlActualQuery.Replace(z_dpaParameter.ParameterName.ToString(),DBHelper.FormatForSql(z_dpaParameter, SGBD));            
            switch (SGBD)
            {
               case TypeSGBD.SqlServer:{  // RAS break; }
               case TypeSGBD.Oracle:
               case TypeSGBD.Sybase:
               {
                    // Caution: parameters must be declared in the same order in the query and in the list.
                    p_strSqlQuery = p_strSqlQuery.Replace(z_dpaParameter.ParameterName.ToString(), "?");
                    z_dpaParameter.ParameterName = "?";
                    break;
                }
             }
           z_dpaParameter.Direction = ParameterDirection.Input;
           z_dcdCommand.Parameters.Add(z_dpaParameter);
          }
        }

           z_dcdCommand.CommandText = p_strSqlQuery; // Here Kiuwan says there's a propagation path sink
           // [...]
           // [...] Actually run the command, with ExecuteReader(), a IDbDataAdapter or ExecuteNonQuery() according to the query type
       }
       catch (Exception z_excException)
       {
          // [...] Management (transaction rollback, among others)
          throw;
       }
       finally
       {
          // [...] Tie up loose ends
          z_dcdCommand?.Dispose();
       }
 }

显然,我们希望这些注入风险消失,但我不知道这里能做些什么。仅仅是没有什么可以保证“干净”的查询吗?在这种情况下,我看不出如何挽救该方法。p_strSqlQuery

另外,我不明白为什么 Kiuwan 会挑出这一行。foreach (p_lisParameters)

我们该怎么办?

C# SQL 注入 代码分析

评论


答:

0赞 Egret 10/12/2022 #1

从设计上讲,这是一个危险的水槽。实际上,这是为了涵盖不同数据库的查询功能。该工具应将其视为与底层 JDBC 函数相同的处理方式。

从这个代码片段中很难看出,但看起来它可能没有对每个数据库使用参数化查询机制。如果是这样的话,它应该这样做。这将确保参数被正确“清理”。

SQL 本身将是一个危险的接收器(SQL 注入端点)。这意味着您需要确保此参数仅使用受信任的来源。确保数据库语句中的任何 SQL 语句都是可信的。

我不熟悉 Kiuwan,但大多数代码分析工具都有一种机制,可以将函数中的参数标记为危险源。这将允许您确定是否可以将任何不受信任(受污染)的输入追溯到此参数。