参数化查询防止Sql注入
拼接sql语句会造成sql注入,注入演示
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
FillData(dataGridView1);
} private void FillData(DataGridView dataGrid)
{
string connStr = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
string sql = "select * from Employees where EmployeeID=\'" + textBox1.Text + "\'";
using (SqlCommand sqlCommand = new SqlCommand(sql, conn))
{
using (SqlDataAdapter sqlData = new SqlDataAdapter(sqlCommand))
{
DataTable dataTable = new DataTable();
sqlData.Fill(dataTable);
dataGrid.DataSource = dataTable;
}
}
}
}
}
}
正常生成的Sql语句应该为
select * from Employees where EmployeeID=''

输入sql实际生成的Sql语句为
select * from Employees where EmployeeID='' or 1=1 --'
所有的数据都查询出来了

防止注入漏洞应该用SqlParameter做参数化查询
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
FillData(dataGridView1);
} private void FillData(DataGridView dataGrid)
{
string connStr = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
string sql = "select * from Employees where EmployeeID=@EmployeeID";
using (SqlCommand sqlCommand = new SqlCommand(sql, conn))
{
SqlParameter[] sqlParameter = { new SqlParameter("@EmployeeID", textBox1.Text) };
sqlCommand.Parameters.AddRange(sqlParameter);
using (SqlDataAdapter sqlData = new SqlDataAdapter(sqlCommand))
{
DataTable dataTable = new DataTable();
sqlData.Fill(dataTable);
dataGrid.DataSource = dataTable;
}
}
}
}
}
}
再输入sql注入会报错

如果用在登录或者未经授权的查询时很有用
重新整理代码
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration; namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
string connStr = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string sql = "select * from Employees where EmployeeID=@EmployeeID";
SqlParameter[] sqlParameter = { new SqlParameter("@EmployeeID", textBox1.Text) };
FillGridView(sql, dataGridView1, sqlParameter);
} private void FillGridView(string sql, DataGridView dataGrid, SqlParameter[] sqlParameter = null)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand sqlCommand = new SqlCommand(sql, conn))
{
if (sqlParameter != null)
{
sqlCommand.Parameters.AddRange(sqlParameter);
}
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
dataGrid.DataSource = dataTable;
}
}
}
}
}
}
参数化查询防止Sql注入的更多相关文章
- 使用参数化查询防止SQL注入漏洞
参数化查询防止SQL注入漏洞 看别人的登录注册sql语句有没漏洞即可 Where name=‘admin’ or ‘1=1’ and password=’123’; 可以Or ‘1=1’就是漏洞 h ...
- 023. Asp.net参数化查询预防Sql注入攻击
/// <summary> /// 参数化查询预防SQL注入式攻击 /// </summary> public int checkLogin(string loginName, ...
- 使用参数化查询防止SQL注入漏洞(转)
SQL注入的原理 以往在Web应用程序访问数据库时一般是采取拼接字符串的形式,比如登录的时候就是根据用户名和密码去查询: string sql * FROM [User] WHERE UserName ...
- python mysql参数化查询防sql注入
一.写法 cursor.execute('insert into user (name,password) value (?,?)',(name,password)) 或者 cursor.execut ...
- mybatis模糊查询防止SQL注入
SQL注入,大家都不陌生,是一种常见的攻击方式.攻击者在界面的表单信息或URL上输入一些奇怪的SQL片段(例如“or ‘1’=’1’”这样的语句),有可能入侵参数检验不足的应用程序.所以,在我们的应用 ...
- 安全 -- mysql参数化查询,防止Mysql注入
参数化查询(Parameterized Query 或 Parameterized Statement)是指在设计与数据库链接并访问数据时,在需要填入数值或数据的地方,使用参数(Parameter) ...
- MySQL参数化有效防止SQL注入
sql语句的参数化,可以有效防止sql注入 注意:此处不同于python的字符串格式化,全部使用%s占位 from pymysql import * def main(): find_name = i ...
- ADO.NET笔记——带参数的查询防止SQL注入攻击
相关知识: 把单引号替换成两个单引号,虽然能起到一定的防止SQL注入攻击的作用,但是更为有效的办法是把要拼接的内容做成“参数” SQLCommand支持带参数的查询,也就是说,可以在查询语句中指定参数 ...
- 带参数的查询防止SQL注入攻击
把单引号替换成两个单引号,虽然能起到一定的防止SQL注入攻击的作用,但是更为有效的办法是把要拼接的内容做成“参数” SQLCommand支持带参数的查询,也就是说,可以在查询语句中指定参数: 参数的设 ...
随机推荐
- LeetCode_66. Plus One
66. Plus One Easy Given a non-empty array of digits representing a non-negative integer, plus one to ...
- Page.ClientScript、ClientScript、ScriptManager、ClientScriptManager等的详细解说
在 .aspx.cs页面中,输入这四个东西:Page.ClientScript.ClientScript.ScriptManager.ClientScriptManager,均会出提示,表示它们均可用 ...
- dozer工具类
jar:commons-beanutils-1.9.3.jar.commons-lang-2.6.jar.dozer-5.3.2.jar.jcl-over-slf4j-1.7.25.jar.slf4j ...
- 【miscellaneous】gstreamer构建的简单方法
在博文"Gstreamer在Ubuntu上的安装和MP3的播放"中,写了在ubuntu上从头到尾构建gstreamer的详细过程,那是我在一次小项目培训中和队友一起努力了将近一周的 ...
- 【谷歌浏览器】修改和添加Cookie
一.使用谷歌浏览器 1.1.修改ookie 方法一:直接用开发者工具修改: 操作如图: 参考: 检查和删除 Cookie · Chrome 开发者工具中文文档 http://www.css88.c ...
- JDK+Jmeter 环境搭建
1.下载JDK安装包,默认安装next即可 2. 3. 4. 5. 6.变量名:JAVA_HOME 变量的值为你安装JDK的目录 我这里是放在C盘 7. 8.添加新的变量值: %JAVA_HOME%\ ...
- 超级块,i节点,数据块,目录块,间接块
一.物理磁盘到文件系统 文件系统用来存储文件内容,文件属性,和目录,这些类型的数据如何存储在磁盘块上的呢?Unix/linux使用了一个简单的方法. 他将磁盘分为3个部分: 超级块,文件系统中第一个块 ...
- NoSQL数据库一Redis基本使用
基本操作 参考教程:https://www.yiibai.com/redis/Redis 是 Key-Value 内存数据库,操作是通过各种指令进行的,比如 SET 指令可以设置键值对,而 GET 指 ...
- JMeter断言介绍
(1)作用:用于检查测试中得到的响应数据等是否符合预期,用以保证性能测试过程中的数据交互与预期一致 (2)目的:在request的返回层面增加一层判断机制:因为request成功了,并不代表结果一定正 ...
- JSON文件转为Excel
前言 今天在帮老师做年终党统的时候,发现管理平台上没有将正在发展的同志的信息导出功能,只能一个一个点击进去查看,操作起来步骤很多很麻烦,所以我就想到了"扒"一下这个网页,扒下来发现 ...