在研究Microsoft Site Server的示例项目vcturbo的XT_SHOPPER_LOOKUP.ASP程序时,发现如下语句:
sql = "select shopper_id from vcturbo_shopper where email = '" & Replace(shopper_email, "'", "''") & "' and password = '" & Replace(shopper_password, "'", "''" & "'") & "'"
该sql语句就是提交给服务器进行客户用户名和口令验证的sql语句,用户名就是用户的E-Mail,在
变量shopper_email中,口令在变量shopper-password中.
我发现,总有如下调用:Replace(shopper_email, "'", "''"),Replace(shopper_password, "'", "''" & "'")。这个作用是将输入的用户名和口令中的“'”符号改为“''”,为什么呢?如果不改,去掉Replace(shopper_email, "'", "''"),直接用shopper_email替换会如何呢?
因为,在以前的程序中,虎虎总是使用如下语句判断用户的合法性的:
"select count(*) as num from UserTable where User='" + UserInput + "' and password='" + PwdInput + "'" 判断返回的记录集中的num字段是否大于0,如果大于0,则表示存在该用户,否则显示登录失败信息。
如果去掉Replace(shopper_email, "'", "''"),直接用shopper_email替换,如果用户输入“user'”,口令输入“pwd”,那么sql语句就变成了: select shopper_id from vcturbo_shopper where email = 'user'' and password='pwd'
显然,提交给SQL Server后,会返回错误,但如果用户名输入"user'--",口令输入“pwd”呢?,
那么sql语句就变成了: select shopper_id from vcturbo_shopper where email = 'user'--' and password='pwd'
哈哈,SQL Server成功执行,返回找到了用户user,因为字符“--”对于SQL Server来说,表示
该语句结束了,后面的内容为注释,所以,不用输入口令,也能登录了。
类似的问题还存在,如用户名输入一个存在的用户名,口令输入:“1' OR 1='1 ”。。。
解决的方法很简单,使用Microsoft Site Server的那种Replace(shopper_password, "'", "''" & "'")就可以,当然,如果考虑更加安全,可以过滤掉跟多的字符,如:',",--,-,%,#,@,&,*,),(, :,?,/,\,!,+,|,这些字符都具有特殊的意义。
原标题:只需用户名------ASP登录程序易犯的错误
关键词:ASP