SQL 注入漏洞的危害与预防

SQL 注入就是通过把 SQL 命令插入到 Web 表单中提交或者把 SQL 命令插入到页面请求的查询字符串中,最终达到欺骗服务器并执行恶意的 SQL 命令。

通常情况下,骇客攻击一个网站,优先使用的就是 SQL 注入漏洞,比如下面的登录代码就存在注入漏洞:

string commandText = "select * from T_User where UserCode='" + userCode + "' and UserPassword='" + userPassword + "'";

乍一看这段代码其实没毛病,但是如果把 userCode 设置成某些特殊值,就会发现问题:

string userCode = "' or 1='1";

另外,网站的搜索功能往往也会存在 SQL 注入漏洞,例如:

string commandText = "select * from table where title like '%" + searchKey + "%'";
// 
string searchKey = "%';delete from table;select * from table where title like '";

SQL 注入的危害相当大,严重时可以导致网站数据丢失、系统瘫痪,因此可以通过下面的方法来预防:

1、过滤危险字符,比如过滤单引号,delete、drop、exec 等特殊关键字。
2、通过参数形式传递字符串,.NET 中使用 SqlParameter 。

管理员

转载请注明出处!如果本博文或者本站对您(网站)的内容/素材构成侵权,请第一时间与本博主联系!

Press ESC to close