拼接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注入的更多相关文章

  1. 使用参数化查询防止SQL注入漏洞

    参数化查询防止SQL注入漏洞 看别人的登录注册sql语句有没漏洞即可 Where  name=‘admin’ or ‘1=1’ and password=’123’; 可以Or ‘1=1’就是漏洞 h ...

  2. 023. Asp.net参数化查询预防Sql注入攻击

    /// <summary> /// 参数化查询预防SQL注入式攻击 /// </summary> public int checkLogin(string loginName, ...

  3. 使用参数化查询防止SQL注入漏洞(转)

    SQL注入的原理 以往在Web应用程序访问数据库时一般是采取拼接字符串的形式,比如登录的时候就是根据用户名和密码去查询: string sql * FROM [User] WHERE UserName ...

  4. python mysql参数化查询防sql注入

    一.写法 cursor.execute('insert into user (name,password) value (?,?)',(name,password)) 或者 cursor.execut ...

  5. mybatis模糊查询防止SQL注入

    SQL注入,大家都不陌生,是一种常见的攻击方式.攻击者在界面的表单信息或URL上输入一些奇怪的SQL片段(例如“or ‘1’=’1’”这样的语句),有可能入侵参数检验不足的应用程序.所以,在我们的应用 ...

  6. 安全 -- mysql参数化查询,防止Mysql注入

    参数化查询(Parameterized Query 或 Parameterized Statement)是指在设计与数据库链接并访问数据时,在需要填入数值或数据的地方,使用参数(Parameter) ...

  7. MySQL参数化有效防止SQL注入

    sql语句的参数化,可以有效防止sql注入 注意:此处不同于python的字符串格式化,全部使用%s占位 from pymysql import * def main(): find_name = i ...

  8. ADO.NET笔记——带参数的查询防止SQL注入攻击

    相关知识: 把单引号替换成两个单引号,虽然能起到一定的防止SQL注入攻击的作用,但是更为有效的办法是把要拼接的内容做成“参数” SQLCommand支持带参数的查询,也就是说,可以在查询语句中指定参数 ...

  9. 带参数的查询防止SQL注入攻击

    把单引号替换成两个单引号,虽然能起到一定的防止SQL注入攻击的作用,但是更为有效的办法是把要拼接的内容做成“参数” SQLCommand支持带参数的查询,也就是说,可以在查询语句中指定参数: 参数的设 ...

随机推荐

  1. 123456------com.threeapp.erTongHuiHua01-----儿童绘画游戏01

    com.threeapp.erTongHuiHua01-----儿童绘画游戏01

  2. (六)利用JackSon工具将JSON文件和对象互转

    1. 需要下载JackSon工具,并导入到: 2. 编写html页面: <!DOCTYPE html> <html> <head> <meta charset ...

  3. jQuery BlockUI Plugin Demo 5(Simple Modal Dialog Example)

    Simple Modal Dialog Example This page demonstrates how to display a simple modal dialog. The button ...

  4. 已经安装了VRay但3dmax的材质编辑器里没有VRay材质的解决过程

    已经安装了VRay但3dmax的材质编辑器里没有VRay材质怎么办? 众所周知,vray是一款很好用的渲染器,但是安装过程和使用当中总会出现各种问题.昨天我就遇到了,捣鼓半天终于解决,分享给大家自己的 ...

  5. python 实现快速排序(面试经常问到)

    # -*- coding: UTF-8 -*- # 递归实现, 把过程打印出来便于理解 def quick_sort1(lis, start, end): if start >= end: re ...

  6. c.a.o.c.p.inbound.mysql.rds.RdsBinlogEventParserProxy - prepare to find start position just show master status

    2018-12-27 08:39:49.808 [destination = example , address = /127.0.0.1:3308 , EventParser] WARN c.a.o ...

  7. Java Script 学习日志 Div

    2019年7月7日 学习网站: http://www.w3school.com.cn/js/js_examples.asp 1.输出 1. 1首先就是输出看 <script> docume ...

  8. sql复合索引使用和注意事项

    1.定义: 单一索引: 单一索引是指索引列为一列的情况,即新建索引的语句只实施在一列上; 复合索引: 复合索引也叫组合索引: 用户可以在多个列上建立索引,这种索引叫做复合索引(组合索引). 复合索引在 ...

  9. vue图片点击放大功能

    因项目需求(ui框架element-ui),需要实现图片的点击放大,还要能旋转以及上下切换.当时第一反应,element-ui好像没有这样的组件,就想过自己写,但是那个旋转翻页上下切换感觉有点麻烦,不 ...

  10. python学习-34 内置函数的补充

    其他内置函数 1.ord()    与chr()相反 2.pow() print(pow(3,3)) # 相当于3**3 print(pow(3,3,2)) # 相当于3*3%2 运行结果: 27 1 ...