在ASP程序中执行SQL语句的安全性问题
在ASP程序中,如果我们的程序设计不当,就有可能面临数据库被别人控制的危险
以下是一个简单的用户更改密码的代码 --------------------- username=request("user_name") pwd=request("pwd") username=replace(username,"'","''") pwd=replace(pwd,"'","''") sql="update tbl_test set pwd='" & pwd & "' where uid='" & username & "'" set rs=conn.execute (sql) -------------- 现在,假如我注册一个用户,用户名为 aa'; exec sp_addlogin 'haha 当该用户更改密码时(假设改为pp),会出现什么后果呢?? sql变为 update tbl_test set pwd='pp' where uid='aa' ; exec sp_addlogin 'haha' 结果是用户密码没有被修改,因为没有 aa这个用户, 但在你的数据库中创建了一个登陆,新登陆名为 haha 将用户名稍加修改,实际上可以运行任何sql语句,任何sql系统过程 而这一切都在你不知情的情况下发生的,实际上,上面的只是一个 示范,稍微修改一下用户名,我们可以做添加一个DBA账号,删除所 有纪录,读取用户密码等越权操作。 解决的办法: 在你使用参数前,对参数进行严格检验,尤其是用户输入的参数 不但要对其数据类型,长度进行检查,还要对其内容进行检查。 我们再看一段代码。(用户登陆) username=request("user_name") pwd=request("pwd") username=replace(username,"'","''") pwd=replace(pwd,"'","''") sql="select uid,pwd from account where uid='" & username & "' and pwd='" & pwd "'" rs.open sql,conn,1,1 if not rs.eof then response.write rs(0) & "欢迎您,您已登陆成功" else response.write "登陆失败,错误的用户名或密码" end if ............ 以上程序的漏洞是显而易见的 我们可以以 用户名: admin 密码: a' or '1'='1 轻易以admin的账号登陆系统 因为我们的sql 变为了 select uid,pwd from account where uid='admin' and pwd='a' or '1'='1' 显然 uid='admin' and pwd='a' or '1'='1'是恒为成立的所以 rs.eof 为false 正确的写法应为 sql="select uid,pwd from account where uid='" & username & "' and pwd='" & pwd "'" rs.open sql,conn,1,1 if rs(0)=username and rs(1)=pwd then response.write rs(0) & "欢迎您,您已登陆成功" else response.write "登陆失败,错误的用户名或密码" end if ----全文完-------- 上一篇:ASP错误的几种常规处理方式 下一篇:ASP实用技巧28则 更多相关文章
|
推荐文章
精彩文章
|