2023年6月21日发(作者:)
写参数化查询语句防⽌SQL注⼊1.什么是SQL注⼊,为什么要防⽌SQL注⼊SQL注⼊是通过⽤户提交的数据,拼装成了恶意的数据库执⾏语句,从⽽对服务器端造成安全问题的⼀个漏洞。举个例⼦,⽐如在登录页⾯,正常的情况下,⽐如我账号输⼊
zhangsan 密码输⼊
147258,然后点击登录,服务器端执⾏的SQL语句可能是。select ID,UserName,Account from Users where Account = 'zhangsan' and Password = '147258'那⽐如我⽤户名输⼊了'or 1 =1--SQL语句就可能变成这样了。select ID,UserName,Account from Users where Account = '' or 1=1 -- and Password = ''这⾥我们能看到 实际执⾏的SQL语句为。select ID,UserName,Account from Users where Account = '' or 1=1条件为真,--在SQL中 是注释符号。所以连密码都不需要直接就可以登录成功了。在看⼀个⽐较严重的例⼦。⽐如我输⼊了。';drop table Users;-- 或者我知道你的数据库名字
';use master go drop database MyBlog;--那么实际执⾏的SQL语句为。select ID,UserName,Account from Users where Account ='';drop table Users;--and Password = ''select ID,UserName,Account from Users where Account ='';use master go drop database MyBlog;--and Password = ''如果写代码的时候没有注意这些,⿊客很可能通过这些漏洞进⾏攻击,造成巨⼤的损失。2.如何防治SQL注⼊防⽌SQL注⼊⼀般有三种⽅法。1. 过滤原则:对⽤户输⼊的数据进⾏判断,⽤⿊名单,或者⽩名单的⽅式验证,或者替换危险字符。2.写数据存储过程:这个还是有⼀定的风险有可能会有漏⽹之鱼,还有就是查询太多的话,存储过程也会写的特别的多,但是写存储过程会提⾼查询的效率,有⼀定好处。3.使⽤参数化查询语句进⾏查询,这也是本⽂重点介绍的⼀种⽅式。请看⽰例。 string Account =["Account"];
string sql = "select id,Name,Account from User where Account = @Account"; SqlParameter[] values = new SqlParameter[] { //参数化查询, 防⽌sql注⼊ new SqlParameter("@Account",Account), }; DataTable datatable = aTable(sql, values);这⾥把⽤户传到后台的账号Account 赋值给
@Account这样⼀来,数据库服务器不会将参数的内容视为SQL指令的⼀部份来处理,⽽是在数据库完成 SQL 指令的编译后,才套⽤参数运⾏,因此就算参数中含有恶意的指令,由于已经编译完成,就不会被数据库所运⾏。另外GetDataTable是⼀个封装好的⽅法。代码如下。 public static DataTable GetDataTable(string sql, params SqlParameter[] values) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(sql, connection); ge(values); SqlDataAdapter da = new SqlDataAdapter(cmd); (ds); return [0]; } }把需要的参数放到SqlParameter[]数组⾥,⽅便其他层的代码调⽤。Study hard and make progress every day.
2023年6月21日发(作者:)
写参数化查询语句防⽌SQL注⼊1.什么是SQL注⼊,为什么要防⽌SQL注⼊SQL注⼊是通过⽤户提交的数据,拼装成了恶意的数据库执⾏语句,从⽽对服务器端造成安全问题的⼀个漏洞。举个例⼦,⽐如在登录页⾯,正常的情况下,⽐如我账号输⼊
zhangsan 密码输⼊
147258,然后点击登录,服务器端执⾏的SQL语句可能是。select ID,UserName,Account from Users where Account = 'zhangsan' and Password = '147258'那⽐如我⽤户名输⼊了'or 1 =1--SQL语句就可能变成这样了。select ID,UserName,Account from Users where Account = '' or 1=1 -- and Password = ''这⾥我们能看到 实际执⾏的SQL语句为。select ID,UserName,Account from Users where Account = '' or 1=1条件为真,--在SQL中 是注释符号。所以连密码都不需要直接就可以登录成功了。在看⼀个⽐较严重的例⼦。⽐如我输⼊了。';drop table Users;-- 或者我知道你的数据库名字
';use master go drop database MyBlog;--那么实际执⾏的SQL语句为。select ID,UserName,Account from Users where Account ='';drop table Users;--and Password = ''select ID,UserName,Account from Users where Account ='';use master go drop database MyBlog;--and Password = ''如果写代码的时候没有注意这些,⿊客很可能通过这些漏洞进⾏攻击,造成巨⼤的损失。2.如何防治SQL注⼊防⽌SQL注⼊⼀般有三种⽅法。1. 过滤原则:对⽤户输⼊的数据进⾏判断,⽤⿊名单,或者⽩名单的⽅式验证,或者替换危险字符。2.写数据存储过程:这个还是有⼀定的风险有可能会有漏⽹之鱼,还有就是查询太多的话,存储过程也会写的特别的多,但是写存储过程会提⾼查询的效率,有⼀定好处。3.使⽤参数化查询语句进⾏查询,这也是本⽂重点介绍的⼀种⽅式。请看⽰例。 string Account =["Account"];
string sql = "select id,Name,Account from User where Account = @Account"; SqlParameter[] values = new SqlParameter[] { //参数化查询, 防⽌sql注⼊ new SqlParameter("@Account",Account), }; DataTable datatable = aTable(sql, values);这⾥把⽤户传到后台的账号Account 赋值给
@Account这样⼀来,数据库服务器不会将参数的内容视为SQL指令的⼀部份来处理,⽽是在数据库完成 SQL 指令的编译后,才套⽤参数运⾏,因此就算参数中含有恶意的指令,由于已经编译完成,就不会被数据库所运⾏。另外GetDataTable是⼀个封装好的⽅法。代码如下。 public static DataTable GetDataTable(string sql, params SqlParameter[] values) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(sql, connection); ge(values); SqlDataAdapter da = new SqlDataAdapter(cmd); (ds); return [0]; } }把需要的参数放到SqlParameter[]数组⾥,⽅便其他层的代码调⽤。Study hard and make progress every day.
发布评论