Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL注入在winforms中工作吗?_C#_.net_Winforms_Sql Injection - Fatal编程技术网

C# SQL注入在winforms中工作吗?

C# SQL注入在winforms中工作吗?,c#,.net,winforms,sql-injection,C#,.net,Winforms,Sql Injection,我正在用c#制作一个windows软件。我已经阅读了有关sql注入的内容,但我没有发现它在我的应用程序中起作用 SQL注入在winforms中工作吗? 如果是,如何预防 编辑: 我使用文本框来读取用户名和密码。通过使用textboxx,我发现textbox中的文本位于双引号(“)之间。所以我觉得它没用 当我在文本框中使用引号“或”时,文本被解读为\“或\” 示例: ................... USER NAME: | a" OR "1"=="1 |

我正在用c#制作一个windows软件。我已经阅读了有关sql注入的内容,但我没有发现它在我的应用程序中起作用

SQL注入在winforms中工作吗?
如果是,如何预防

编辑: 我使用文本框来读取用户名和密码。通过使用textboxx,我发现textbox中的文本位于双引号(
)之间。所以我觉得它没用

当我在文本框中使用引号
时,文本被解读为
\“
\”

示例:

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

对。防止这种情况的最简单方法是对发送到数据库的任何用户输入使用s。或者不要使用
SqlDataAdapter
,而是使用实体框架。

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

SQL注入是由用户直接在动态构造的SQL语句(称为动态SQL)中输入导致的,这使用户能够中断SQL或“注入”自己的SQL代码

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


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

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

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

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

    使用[YOURDb] 去

    创建角色[dbStoredProcedureOnlyAccessAmar] 去

  • 创造之后 大执行角色[dbStoredProcedureOnlyAccessAmar]

  • 4.基于错误的SQL注入预防:在存储过程中执行 (登录、搜索等,欧洲和亚洲:MSSQL 2014)


  • 之后,代码隐藏=>this方法中的addcheckforsqlinjection方法根据SQL注入检查输入字符串。这里我必须列出字符串数组中的所有SQL注入输入。添加此方法将返回true和false


  • 这不是关于双引号,而是关于如何将参数传递到SQL查询。有一幅漫画显示了它“…你真的给你的儿子取名为罗伯特吗?”;DROP TABLE Students;-?”——@Alexei:我看过这幅漫画,我可能在webforms中工作,但在windows窗体中,被理解为
    \
    。因此,它在winforms中不起作用。它与web vs win完全无关。Winforms和其他所有技术如果写得不好也同样容易受到影响。但通常网络系统(尤其是公共系统)会受到更多的恶意对待,因此威胁更为直接。它肯定也会影响winforms等。不。。。看看字符串中的单个字符-双引号是双引号,您展示的是带双引号的字符串如何在调试器中可见。但是textBox1.Text[1]将是',而不是'\'。
    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 messge to the client.
    
    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;
    }
    
    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;
        }
    }