参数化查询防止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支持带参数的查询,也就是说,可以在查询语句中指定参数: 参数的设 ...
随机推荐
- 关于采样率&位深&码率&无损的一些心得
转载自:https://blog.csdn.net/Marenow/article/details/85253283 记笔记,记下来自己的一些关于音频基础知识的总结. 采样率外界的声音都是模拟信号,在 ...
- delphi中TTreeView的使用方法
[学习万一老师博客摘要] TTreeView 与两个重要的类相关:TTreeNodes.TTreeNode . TTreeNodes即是TTreeView 的Items属性,TTreeNodes是TT ...
- 【c# 学习笔记】使用virtual和override关键字实现方法重写
只有基类成员声明为virtual或abstract时,才能被派生类重写:而如果子类想改变虚方法的实现行为,则必须使用override关键字. public class Animal { private ...
- laravel进程管理supervisor的简单说明
原文地址:https://www.cnblogs.com/zhoujinyi/p/6073705.html 背景: 项目中遇到有些脚本需要通过后台进程运行,保证不被异常中断,之前都是通过nohup.& ...
- Redis ZSet 有序集合
有序集合类型与集合类型的区别就是他是有序的.有序集合是在集合的基础上为每一个元素关联一个分数,这就让有序集合不仅支持插入,删除,判断元素是否存在等操作外,还支持获取分数最高/最低的前N个元素.有序集合 ...
- 给定一个字符串str,将str中连续两个字符为a的字符替换为b(一个或连续超过多个字符a则不替换)
需求:给定一个字符串str,将str中连续两个字符为a的字符替换为b(一个或连续超过多个字符a则不替换) 如: a 不替换 b 不替换 ab 不替换 ba 不替换 aba 不替换 aab 替换为 ...
- FineReport工具
PostgreSQL链接问题: 正常安装FineReport工具和PostgreSQL数据库之后,在FineReport工具当中链接数据库显示连接失败的原因是因为FineReport安装包里面没有Po ...
- printf特殊用法
printf("%*.*lf\n", a, b, c); //表示a宽距,保留b位小数 用这种方法可以通过输入控制a和b
- 学习笔记:oracle学习一:oracle11g体系结构之体系结构概述和逻辑存储结构
目录 1.oracle 11g体系结构概述 1.1 三个重要概念 1.2 oracle数据库存储结构 2 逻辑存储结构 2.1 数据块(Data Blocks) 2.2 数据区(Extent) 2.3 ...
- Lamda