Servelt学习:SQL注入漏洞及其避免方法
来源:互联网 发布:如何缩小贫富差距 知乎 编辑:程序博客网 时间:2024/06/05 08:58
SQL注入漏洞是指执行查询语句时在正常的查询语句之后加入 or 1='1'
例如将语句select * FROM [Students].[dbo].[User] where name='admin' and passwd='123'改写为
select * FROM [Students].[dbo].[User] where name='admin' and passwd='123' or 1='1'时便可以查询出数据表中全部内容(甚至name与passwd值可以随意乱填)
正常情况时得到正确的查询结果:
用户名或密码错误则无法获得查询结果
而在正常的查询语句之后加入or 1='1'却可以得到整个表的查询结果
黑客有可能利用SQL漏洞对网站进行攻击。其可以在用户名称栏中随意输入字符,密码栏中输入XXX' or 1='1便可以进入网站。
例如:
(数据库中无该用户)
(由于SQL漏洞,页面成功跳转)
解决方法:
不要用户名和密码同时进行验证,而是通过用户名去查询数据库,返回一个密码。然后用该密码与用户输入密码进行比较,如果相同则用户合法,否则为非法用户。
将查询语句:
ps=ct.prepareStatement("select top 1 * from [Students].[dbo].[User] where name='"+u+"'and passwd='"+p+"'");
rs=ps.executeQuery(); if(rs.next()) { //跳转到welcome res.sendRedirect("Wel?user="+u+"&pass="+p); //HttpSession hs=req.getSession(true); //hs.setAttribute("uname",u); }else { res.sendRedirect("login");//写需要跳转的servlet的那个url }
改为:
ps=ct.prepareStatement("select top 1 passwd from [Students].[dbo].[User] where name='"+u+"'"); rs=ps.executeQuery(); if(rs.next()) { String dbPasswd=rs.getString(1); System.out.println(dbPasswd); if(dbPasswd.equals(p)) { System.out.println(dbPasswd); res.sendRedirect("Wel?user="+u+"&pass="+p); HttpSession hs=req.getSession(true); hs.setAttribute("uname",u); } }else//说明用户名不存在 { res.sendRedirect("login");//写需要跳转的servlet的那个url }
- Servelt学习:SQL注入漏洞及其避免方法
- 如何避免SQL注入漏洞
- 避免SQL注入漏洞攻击
- sql注入漏洞检测方法
- 避免sql注入的方法
- 【黑马程序员】参数化查询,避免sql注入漏洞攻击(学习笔记)
- SQL防注入漏洞的方法
- 防止SQL注入漏洞简单方法
- SQL注入漏洞及防止方法
- 避免SQL注入三大方法
- 通过DVWA学习SQL注入漏洞
- SQL注入漏洞入侵的过程及其防范措施
- Sql 注入漏洞,注意
- SQL注入漏洞
- sql注入漏洞原理
- SQL 注入漏洞新动向
- SQL注入漏洞接触
- SQL注入漏洞
- 传感器介绍
- informix错误Could not do a physical-order read to fetch next row解决
- 提升linux下tcp服务器限制
- asp.net导出Excel/Csv格式数据最优方案(C#)
- c中static的作用
- Servelt学习:SQL注入漏洞及其避免方法
- android启动过程配置文件的解析与语法
- 指针计算问题
- poj 1180 Batch Scheduling
- mysql 常见错误说明
- SourceInsight 实用教程和技巧(链接)
- 内核中的 likely() 与 unlikely()
- C++逗号运算符重载
- 比较Vector,List 和ArrayList间的区别