防止SQL注入式攻击的笔记

SQL注入式攻击是指利用设计上的漏洞攻击系统。如果动态生成SQL语句时没有对用户输入的数据

进行过滤,便会使SQL注入式攻击得逞。

例如用下面的SQL语句判断用户名和密码:

txtsql="select * from user_info where userid='"&txtuserid &"' and_

password='" & txtpassword.text & "'"

则通过SQL注入式攻击,在“密码”文本框中输入1'or '1'='1,非法用户便可在

没有密码的情况下轻松登录系统,因为SQL语句已经变为:

txtsql="select * from user_info where userid='"&txtuserid &"' and_

password='" &_1'or '1'='1 & "'"

要防范SQL注入式攻击,应该注意一下几点;

1,检查输入的SQL语句的内容,如果包含敏感字符,则删除敏感字符,敏感字符

包含【',>,<=,!,_,+,*,/,(),|和空格】

2,不要在用户输入过程中构造where子句,应该利用参数来使用存储过程。

下面举例来防范SQL注入式攻击:

Function inputString(mystr) as String

mystr=Trim(mystr)

mystr=Replace(mystr,"'","'")

mystr=Replace(mystr,";--","")

mystr=Replace(mystr,"=","")

mystr=Replace(mystr,"or","")

mystr=Replace(mystr,"and","")

End Function

txtsql="select * from user_info where userid='" & txtuserid & "' and_

password='" & inputString(txtpassword.text) & "'"

欢迎关注我的公众号(同步更新文章)DoNet技术分享平台

阅读原文

原文地址:https://www.cnblogs.com/hgmyz/p/12352642.html