【JDBC】SQL注入攻击演示与解决方法
来源:互联网 发布:东方财富 知乎 编辑:程序博客网 时间:2024/05/16 14:48
<pre name="code" class="java">import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/*SQL注入攻击演示*/public class Demo1 {public static void main(String[] args) throws ClassNotFoundException, SQLException {new Demo1().select();}public void select() throws ClassNotFoundException, SQLException{Class.forName("com.mysql.jdbc.Driver");String url="jdbc:mysql://localhost:3306/jdbc";String user="root";String password="root";Connection connection=DriverManager.getConnection(url, user, password);/*利用字符串拼接时单引号成对出现的规律,与MySQL关系型运算符and的优先级高于or,与MySQL查询语句的格式综合形成的漏洞,在输入的密码上做手脚->*/String id="1001";//输入账户IDString pw="2' or '1'='1";//输入密码。巧妙地在查询语句之后增加了一段String sqlCode="select * from t_login where userId="+id+" and password='"+pw+"'";//sqlCode结果:select * from t_login where userId='1001' and password='2' or '1'='1';// -> (true/false and false) or true -> true//等同于:select * from t_login where true;--输出所有语句//-------------------------------------Statement st=connection.createStatement();//可以改进的代码...ResultSet rs= st.executeQuery(sqlCode);//-------------------------------------//简单输出if(rs.first()){do{System.out.print(rs.getInt(1) +"\t");System.out.print(rs.getString(2) +"\t");System.out.println();}while(rs.next());}else{System.out.println("登录信息不正确,登陆失败!");}/*当用户ID和密码正确输入时的结果:返回表中该用户的账户ID与密码1001123456当受到注入攻击输出结果:输出了该表中所有用户的账户密码信息。1001123456100223333310038888881004666666*/rs.close();st.close();connection.close();}}
防止注入攻击的改进方法:使用PreparedStatement
将上述被//------标记的两行代码替换为如下代码:
PreparedStatement st=connection.prepareStatement("select * from t_login where userid=? and password=?");st.setString(1, id);st.setString(2, pw);ResultSet rs=st.executeQuery();//注意:换方法了,没有传参当受到注入攻击输出结果:登录信息不正确,登陆失败!
0 0
- 【JDBC】SQL注入攻击演示与解决方法
- SQL注入攻击演示代码
- JDBC中SQL注入与注入攻击原理
- PHP与SQL注入攻击
- PHP与SQL注入攻击
- PHP与SQL注入攻击
- PHP与SQL注入攻击
- PHP与SQL注入攻击
- PHP与SQL注入攻击
- PHP与SQL注入攻击
- SQL注入攻击与防御
- PHP与SQL注入攻击
- PHP与SQL注入攻击
- SQL注入攻击与防御
- PHP与SQL注入攻击
- SQL注入攻击与防御
- PHP与SQL注入攻击
- 网站攻击与Sql注入
- 快速幂的模板 (数论)
- Dubbo服务的部署
- Android开发环境搭建
- 测试主管如何扭临危受命,做正确的事
- listActivity和ExpandableListActivity的用法
- 【JDBC】SQL注入攻击演示与解决方法
- Instruments User Guide编程指南2-Common Tasks
- C语言 判断两个数组里是否有相同元素
- 关于第一天学习JAVA的心得,菜鸟心得,勿喷
- ThreadLocal介绍
- Python 文件操作
- UICollectionViewCell 添加textField Cell消失不见问题
- Memcache设置时遇到的常见问题
- [解读小程序]手机归属地查询Demo(二)