无法阻止SQL注入错误

时间:2015-05-29 12:08:51

标签: c# sql-server ado.net sql-injection

我终于明白了。它不仅仅是我用来执行ExecuteScalar方法的代码,而且它主要是执行类的代码上游流。这就是调用代码的一切。也就是说,有人可以看看执行我的SQL类的代码是否有错误。我仍然无法通过扫描。首先,我将向您展示调用我的代码的代码的两个示例,然后是调用代码,最后是我在上一篇文章中制定和显示的执行代码。

使用三个参数调用代码:

public bool isTamAsp(int aspKey, int fy, string accountCode)
{
    MyParam myParam;

    string sqlQuery = "select isTamMacom = count(macom_key) FROM hier_fy " +
        "WHERE hier_key = @aspKey AND fy = @fy  AND @accountCode NOT IN (3,4,7,8) AND macom_key IN (select hier_key from lkup_e581_MacomThatRequireTAM) AND is_visible = 1 AND is_active = 1";

    QueryContainer Instance = new QueryContainer(sqlQuery);

    myParam = new MyParam();

    myParam.SqlParam = new SqlParameter("@aspKey", Instance.AddParameterType(_DbTypes.Int));

    myParam.SqlParam.Value = aspKey;

    Instance.parameterList.Add(myParam);

    myParam = new MyParam();

    myParam.SqlParam = new SqlParameter("@fy", Instance.AddParameterType(_DbTypes.Int));

    myParam.SqlParam.Value = fy;

    Instance.parameterList.Add(myParam);

    myParam = new MyParam();

    myParam.SqlParam = new SqlParameter("@accountCode", Instance.AddParameterType(_DbTypes._string));

    myParam.SqlParam.Value = accountCode;

    Instance.parameterList.Add(myParam);

    if (Convert.ToInt32(ExecuteScaler(Instance)) < 1)
        return false;

    return true;
}

调用没有参数的代码:

public long GetMarinesUploadNextUploadKey()
{
    string query = "SELECT MAX(upload_key) FROM temp_auth_usmc_upload";

    QueryContainer Instance = new QueryContainer(query);

    string result = Convert.ToString(ExecuteScaler(Instance));
    if (string.IsNullOrEmpty(result))
        return 1;
    else
        return Convert.ToInt64(result) + 1;
} 

使用三个参数调用我之前的代码:

public bool isTamAsp(int aspKey, int fy, string accountCode)
{
    return e581provider.isTamAsp(aspKey, fy, accountCode);
}

调用SQL执行我的代码的方法:

DbCommand command = _provider.CreateCommand();

command.Connection = _connection;
{
    command.CommandText = Instance.Query;
    command.CommandType = CommandType.Text;

    if (Instance.parameterList.Count > 0)
    {
        foreach (var p in Instance.parameterList)
        {
            command.Parameters.Add(p.SqlParam);
        }
    }

    if (_useTransaction) { command.Transaction = _transaction; }

    try
    {
        returnValue = command.ExecuteScalar();
    }

我的类包含SQL字符串和cmd参数列表

public enum _DbTypes
{
    Int = 1, _string = 2, _long = 3, _bool = 4, _DateTime = 5,
    _decimal = 6, _float = 7, _short = 8, _bite = 9
} 

public class MyParam
{
    public SqlParameter SqlParam { get; set; }
}
/// <summary>
/// Summary description for QueryContainer SGH
/// </summary>
public class QueryContainer
{

    string _query;

    public List<MyParam> parameterList = new List<MyParam>();

    public QueryContainer(string query) { _query = query; }

    public SqlDbType AddParameterType(_DbTypes id)
    {
        switch (id)
        {
            case _DbTypes.Int:
                return (SqlDbType)Enum.Parse(typeof(SqlDbType), "int", true);
            case _DbTypes._string:
                return (SqlDbType)Enum.Parse(typeof(SqlDbType), "NVarChar", true);
            case _DbTypes._long:
                return (SqlDbType)Enum.Parse(typeof(SqlDbType), "SqlDbType.BigInt", true);
            case _DbTypes._bool:
                return (SqlDbType)Enum.Parse(typeof(SqlDbType), "SqlDbType.Bit", true);
        }

        return SqlDbType.VarChar;

    }

    public string Query
    {
        get
        {
            return _query;
        }

        set { _query = value; }
    }
}

1 个答案:

答案 0 :(得分:0)

我没有在该代码中看到漏洞,但我知道扫描可能要求的内容。问题可能在于此代码使开发人员很容易忽略您班级中的parameterList集合。如果我是您组织中尚未发现Sql注入的新开发人员,我很想忽略所有复杂的查询参数,只需在设置Query属性之前使用字符串连接。

不是将它包装在一个类中,而是我更习惯看到的是一个具有如下签名的方法:

IEnumerable<T> GetData<T>(string query, IEnumerable<Sqlparameter> parameters)

...或者可能使用数组或列表而不是IEnumerable的方法签名的一些排列。这迫使下游开发人员处理该方法的parameters参数。他们不能忽视它,因此减少了使用快速,懒惰的字符串连接调用将一些用户提供的数据替换为查询的诱惑。