【问题标题】:Safety from SQL injectionSQL注入的安全性
【发布时间】:2014-04-19 00:30:24
【问题描述】:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["techconn"].ToString());

            SqlCommand com = new SqlCommand("select * from hs where ac between'" + TextBox1.Text + "'and '" + TextBox2.Text + "' and em='" + DropDownList1.SelectedItem.Text.ToString() + "'", con);

            DataTable dt = new DataTable();

            con.Open();

            SqlDataAdapter sqlDa = new SqlDataAdapter(com);

            sqlDa.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
            {
                GridView1.Visible = false;
            }

            con.Close();

这段代码对 SQL 注入安全吗?

如果不是,请更正此代码,使其免受 SQL 注入。

我使用的是 SQL Server 2008。

【问题讨论】:

  • 不,不安全。您需要对 SQL 参数 进行一些研究。
  • @AndrewMorton 请纠正此代码
  • 这是一个教科书式的易受注入攻击的代码示例。
  • 这不是一个“为我写代码”的网站。你必须付出一些努力。 How to Ask.

标签: sql sql-server c#-4.0 sql-injection


【解决方案1】:

简而言之,答案是否定的。您需要始终在查询中使用参数。

SqlCommand com = new SqlCommand("select * from hs where ac between @ac1 and @ac2 and em=@em", con);

然后将参数添加到 SqlCommand 对象 (com)。

【讨论】:

    【解决方案2】:

    是的,您的代码很容易出现问题,而不仅仅是 sql 注入攻击。请尝试以下操作:

        public DataTable GetData(string textbox1, string textbox2, string dropdown)
        {
            DataTable result = null;
            string connString = null;
    
            if (ConfigurationManager.ConnectionStrings["techconn"] != null)
                connString = ConfigurationManager.ConnectionStrings["techconn"].ConnectionString;
    
            if (!string.IsNullOrEmpty(connString))
            using (SqlConnection con = new SqlConnection(connString))
            {
                con.Open();
    
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = "select * from hs where (ac between @a and @b) and em = @c";
    
                    cmd.Parameters.AddWithValue("@a", textbox1);
                    cmd.Parameters.AddWithValue("@b", textbox2);
                    cmd.Parameters.AddWithValue("@c", dropdown);
    
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        result = new DataTable();
                        da.Fill(result);
    
                    }
                }
            }
    
            return result;
    
        }
    

    将其粘贴到您的代码中并供

    使用
    DataTable dt = GetData(TextBox1.Text, TextBox2.Text, DropDownList1.SelectedItem.Text.ToString());
    
                if (dt != null && dt.Rows.Count > 0)
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
                else
                {
                    GridView1.Visible = false;
                }
    

    也正确测试一下。

    【讨论】:

      【解决方案3】:

      如果您使用直接文本框引用到您的 sql 查询,那么它永远不会是 SQL 注入安全的,任何最终用户都可以将 Injecting 值传递给您的文本框并且它将被注入。

      永远不要在你的 SQL 中直接使用 UI 元素,你可以试试下面的 CODE 行

      SqlConnection conn = new SqlConnection(_connectionString);  
      conn.Open();  
      string s = "select * from hs where ac between @TextBoxOnevaluevariable and
      @TextBoxTwovaluevariable and em=@DropdownSelectedTextVariable";
      
      SqlCommand cmd = new SqlCommand(s);  
      cmd.Parameters.Add("@TextBoxOnevaluevariable", Texbox1.Text);  
      cmd.Parameters.Add("@TextBoxTwovaluevariable", Texbox2.Text);  
      cmd.Parameters.Add("@DropdownSelectedTextVariable",DropDownList1.SelectedItem.Text.ToString());  
      SqlDataReader reader = cmd.ExecuteReader();
      

      【讨论】:

        猜你喜欢
        • 2017-12-21
        • 2011-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-28
        • 2023-03-16
        • 2010-10-12
        • 1970-01-01
        相关资源
        最近更新 更多