SQL 注入在 winform 中有效吗?

发布于 2024-10-29 14:47:36 字数 535 浏览 2 评论 0原文

我正在用 C# 制作一个 Windows 软件。我已阅读有关 sql-injection 的内容,但我没有发现它适用于我的应用程序。

SQL 注入在 winforms 中有效吗?
如果是的话如何预防。

编辑: 我正在使用文本框来读取用户名和密码。通过使用 textboxex 我发现文本框中的文本位于双引号之间("")。所以我没有发现它有效。

当我在文本框中使用引号 " OR ' 时,文本将被读取为 \" OR \'

示例:

            ...................
USER NAME:  | a" OR "1"=="1   |
            ```````````````````
// it is read as textBox1.Text = "a\" OR \"1\"==\"1";

I am making an windows software in c#. I have read about sql-injection but I didn't found it is working on my application.

Do SQL Injection works in winforms?
If yes how to prevent them.

EDIT:
I am using a textboxes for reading user-name and password. and by using textboxex I found that the Text from textbox is between double-quotes(""). So I didn't found it to be worked.

And when, I use Quotes " OR ' in Textbox, the text is read as \" OR \'

Example:

            ...................
USER NAME:  | a" OR "1"=="1   |
            ```````````````````
// it is read as textBox1.Text = "a\" OR \"1\"==\"1";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不吻晚风 2024-11-05 14:47:36

SQL注入是一个普遍问题,不依赖于任何技术。如果您使用 .NET 并希望防止 SQL 注入,请始终使用 SqlParameter 而不是字符串连接。

SQL injection is general issue not depending on any technology. If you using .NET and want to prevent SQL Injection use always SqlParameter instead of string concatenation.

夜深人未静 2024-11-05 14:47:36

是的。防止它的最简单方法是使用 SqlParameter s 用于发送到数据库的任何用户输入。或者不使用 SqlDataAdapter 并使用实体框架。

Yes. Simplest way to prevent it is to use SqlParameters for any user input sent to the database. Or don't use the SqlDataAdapter and use the Entity Framework instead.

来日方长 2024-11-05 14:47:36

SQL 注入是通过在动态构建的 SQL 语句(称为动态 SQL)中直接使用用户输入而引起的,这使用户能够破坏 SQL 或“注入”自己的 SQL 代码。

使用存储过程或带有参数的 SQL 可以解决这个问题。

所以是的,如果 SQL 是这样编码的,那么这种情况可能会发生在 winforms 中。

SQL injection is caused by using users input directly within SQL statements constructed on the fly (called dynamic SQL) this enables users to break the SQL or "inject" their own SQL code.

Using Stored Procedures or SQL with parameters gets around this.

So yes this can occur within winforms if the SQL is coded that way.

谜泪 2024-11-05 14:47:36

Winforms 中可以进行 SQL 注入。您可以遵循以下策略

  1. 为用户提供尽可能少的权限

  2. 使用dbStoredProcedureOnlyAccessAmar数据库角色,如下所示

    <前><代码>使用[YOURDb]

    创建角色 [dbStoredProcedureOnlyAccessAmar]

  3. 创建后

     授予执行角色 [dbStoredProcedureOnlyAccessAmar]
    
  4. 基于错误的SQL注入预防:在存储过程中完成(LOGIN、SEARCH等,欧洲和亚洲:SQL Server 2014)

     如果不存在(从 dbo.MyTable 中选择 1,其中 MyPrimaryKey = @MyNewValue)
     -- 这会检查是否会发生主键冲突,并且仅当 @MyNewValue 尚不存在时才执行代码。
     开始
         -- 这里的代码通常会在没有任何错误检查的情况下出错
     结尾
     别的
     开始
          -- 此处的代码用于说明如果发现错误情况该怎么办
     结尾
    
     -- 最终结果是,由于您事先检查过,因此没有遇到错误,因此不会向最终用户显示
    
     -- 这变得很棘手,因为您必须预测错误情况。未检查结果的任何错误条件
    
     -- 给客户端的错误消息。
    
  5. 之后在后面的代码中添加checkForSQLInjection方法=>此方法检查输入字符串是否存在 SQL 注入。这里我必须列出字符串数组中的所有 SQL 注入输入。添加此方法返回 true 和 false。

     public static Boolean checkForSQLInjection(string userInput)
     {
         bool isSQLInjection = false;
         字符串[] sqlCheckList = 
                  { “--”, “;--”, “;”, “/*”, “*/”,
                    “@@”、“@”、“char”、“nchar”、“varchar”、
                    “nvarchar”,“改变”,“开始”,“强制转换”,
                    “创建”、“光标”、“声明”、“删除”、
                    “删除”,“结束”,“执行”,“执行”,“获取”,
                    “插入”、“杀死”、“选择”、“sys”、“sysobjects”、
                    “系统列”、“表”、“更新”
                  };
    
         string CheckString = userInput.Replace("'", "''");
    
         for (int i = 0; i <= sqlCheckList.Length - 1; i++)
         {
             if ((CheckString.IndexOf(sqlCheckList[i], StringComparison.OrdinalIgnoreCase) >= 0))
             {
                 isSQLInjection = true;
             }
         }
    
         返回 isSQLInjection;
     }
    

然后双击按钮并编写以下代码:=>这里我必须编写将数据插入数据库的代码,并检查输入数据是否存在 SQL 注入。

protected void btnSave_Click(object sender, EventArgs e)
{
    try
    {
        using (SqlCommand cmd = new SqlCommand("insert into testSqlinjection(Name) values(@name) ", con))
        {

            cmd.CommandType = CommandType.Text;

            if (checkForSQLInjection(txtName.Text.Trim())) 
            { 
                lblMesg.Text = "Sql Injection Attack"; 
                return;
            }

            checkForSQLInjection(txtName.Text.Trim());
            cmd.Parameters.AddWithValue("@name", txtName.Text.Trim());
            con.Close();
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            lblMesg.Text = "Data Saved succsessfuly";
        }
    }
    catch (Exception ex)
    {
        lblMesg.Text = ex.Message;
    }
}

It is possible to SQL injection in Winforms. You may follow below as strategy

  1. Provide user least possible privilege

  2. Use dbStoredProcedureOnlyAccessAmar database role as shown below

     USE [YOURDb]
     GO
    
     CREATE ROLE [dbStoredProcedureOnlyAccessAmar]
     GO
    
  3. After creation

     GRANT EXECUTE ROLE [dbStoredProcedureOnlyAccessAmar]
    
  4. Error-based SQL injection prevention: to be done in a stored procedure (LOGIN, SEARCH Etc., Europe & Asia: SQL Server 2014)

     IF NOT EXISTS (SELECT 1 FROM dbo.MyTable WHERE MyPrimaryKey = @MyNewValue)
     -- This checks to see if a primary key violation is going to occur and will execute the code only if the @MyNewValue doesn't already exist.
     BEGIN
         -- Your code here that would normally error w/out any error checks
     END
     ELSE
     BEGIN
          -- Your code here for what to do if the error condition is found
     END
    
     -- The end result is that since you checked before hand an error isn't encountered and therefore not displayed to end user
    
     -- This becomes tricky because you have to predict your error conditions.  Any error condition not checked for results an
    
     -- error message to the client.
    
  5. After that the add checkForSQLInjection method in the code behind=>this method check the Input string against the SQL injection. Here I have to list all SQL injection input in array of string. Adding this method returns true and false.

     public static Boolean checkForSQLInjection(string userInput)
     {
         bool isSQLInjection = false;
         string[] sqlCheckList = 
                  { "--", ";--", ";", "/*", "*/",
                    "@@", "@", "char", "nchar", "varchar",
                    "nvarchar", "alter", "begin", "cast",
                    "create", "cursor", "declare", "delete",
                    "drop", "end", "exec", "execute", "fetch",
                    "insert", "kill", "select", "sys", "sysobjects",
                    "syscolumns", "table", "update"
                  };
    
         string CheckString = userInput.Replace("'", "''");
    
         for (int i = 0; i <= sqlCheckList.Length - 1; i++)
         {
             if ((CheckString.IndexOf(sqlCheckList[i], StringComparison.OrdinalIgnoreCase) >= 0))
             {
                 isSQLInjection = true;
             }
         }
    
         return isSQLInjection;
     }
    

Then double click on the Button and write this code:=>here I have to write the code for inserting the data in a database and also check the input data against the SQL injection.

protected void btnSave_Click(object sender, EventArgs e)
{
    try
    {
        using (SqlCommand cmd = new SqlCommand("insert into testSqlinjection(Name) values(@name) ", con))
        {

            cmd.CommandType = CommandType.Text;

            if (checkForSQLInjection(txtName.Text.Trim())) 
            { 
                lblMesg.Text = "Sql Injection Attack"; 
                return;
            }

            checkForSQLInjection(txtName.Text.Trim());
            cmd.Parameters.AddWithValue("@name", txtName.Text.Trim());
            con.Close();
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            lblMesg.Text = "Data Saved succsessfuly";
        }
    }
    catch (Exception ex)
    {
        lblMesg.Text = ex.Message;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文