C# SQL注入在winforms中工作吗?
我正在用c#制作一个windows软件。我已经阅读了有关sql注入的内容,但我没有发现它在我的应用程序中起作用 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 |
如果是,如何预防 编辑: 我使用文本框来读取用户名和密码。通过使用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注入。您可以遵循以下策略
这不是关于双引号,而是关于如何将参数传递到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;
}
}