当前位置: 首页 > 图文教程 > 网络编程 > ASP > 在ASP中过滤用户输入 提高安全性

ASP
ASP动态生成的javascript表单验证代码
将身份证从15位升级为18位的函数
ASP自定义函数,仿VBA中域函数DLookup
6行代码实现无组件上传
用VS2003调试ASP的方法和体会
ASP中存储过程调用的两种方式及比较
升级到2003后访问数据库发生8007007f错误的解决
ACCESS转化成SQL2000需要注意的几个问题
让ASP程序运行于非Windows平台
一些不长见的ASP调用存储过程的技巧
网站图片扫描类
一条sql 语句搞定数据库分页
在ASP中取得服务器网卡的MAC地址、DNS地址等网络信息
ASP中轻松实现变量名-值变换
用Xml2OleDb将XML文件插入到数据库
购物车中数据的存放方式
ASP数据库编程SQL常用技巧
在ASP中利用ADO显示Excel文件内容的函数
ASP+SQL Server之图象数据处理
在 Access 中使用“存储过程”

在ASP中过滤用户输入 提高安全性


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 22 ::
收藏到网摘: n/a

 

安全对于所有应用程序来说都是十分重要的。一个简单应用程序里的某个失误都会造成对数据库或者其他企业资源未经授权的访问,因此安全尤其重要。一种常用的攻击方法是将命令嵌入到用户的回应里,而从用户输入里过滤掉这些非法的字符就能够防止这种攻击。

 
允许用户输入非法的字符会增加用户导致问题的机会。例如,很多应用程序都能够接受用户在SQL命令里增加的WHERE子句。恶意用户会通过向其输入的信息里增加额外命令的方法,来执行数据库服务器上的代码。例如,他们不是输入“Smith”,将其作为检索字符串,而是输入“Smith'; EXEC master..xp_cmdshell 'dir *.exe”。

下面这段代码是设计用来处理从服务器返回的多个Recordset的。用户的输入会包含一个额外的、未预料的的执行命令。当NextRecordset方法被调用的时候,潜藏的恶意代码就会被执行。

这一攻击可以通过过滤掉用户输入信息中的非法字符(在注释段里)来避免。这样做了之后,用户的输入仍然被允许处理,但是清除掉了所有的非法字符。

Dim rst As Recordset
Dim rst2 As Recordset
Dim strUserInput As String

strUserInput = "Smith';EXEC master..xp_cmdshell 'dir *.exe"

'Filter input for invalid characters
strUserInput = Replace(strUserInput, "<", vbNullString)
strUserInput = Replace(strUserInput, ">", vbNullString)
strUserInput = Replace(strUserInput, """", vbNullString)
strUserInput = Replace(strUserInput, "'", vbNullString)
strUserInput = Replace(strUserInput, "%", vbNullString)
strUserInput = Replace(strUserInput, ";", vbNullString)
strUserInput = Replace(strUserInput, "(", vbNullString)
strUserInput = Replace(strUserInput, ")", vbNullString)
strUserInput = Replace(strUserInput, "&", vbNullString)
strUserInput = Replace(strUserInput, "+", vbNullString)
strUserInput = Replace(strUserInput, "-", vbNullString)

Set rst = New Recordset
rst.ActiveConnection = "PROVIDER=SQLOLEDB;DATA SOURCE=SQLServer;" & _
                       "Initial Catalog=pubs;Integrated Security=SSPI"
rst.Open "Select * from authors where au_lname = '" & strUserInput & _
         "'", , adOpenStatic
'Do something with recordset 1

Set rst2 = rst.NextRecordset()
'Do something with recordset 2

在用户的输入中嵌入命令也是攻击ASP Web应用程序的一种常见手法,也叫做跨网站脚本攻击。过滤输入的内容并使用Server.HTMLEncode和Server.URLEncode这两个方法会有助于防止你ASP应用程序里这类问题的发生。