我是靠谱客的博主 朴素抽屉,这篇文章主要介绍mysql语句的注入错误是什么?,现在分享给大家,希望可以做个参考。

(推荐教程:mysql视频教程)

sql注入式错误(SQL injection)

危害
攻击者利用它来读取、修改或者删除数据库内的数据,获得数据库中用户资料和密码等信息,更严重的就是获得管理员的权限。

例子

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//注入式错误 public static void test3(String name,String passward){ Connection connection = null; Statement st = null; ResultSet rs = null; try { // 加载JDBC 驱动 Class.forName("com.mysql.jdbc.Driver"); // 获得JDBC 连接 String url = "jdbc:mysql://localhost:3306/tulun"; connection = DriverManager.getConnection(url,"root","123456"); //创建一个查询语句 st = connection.createStatement(); //sql语句 String sql = "select * from student where name = '"+ name+"' and passward = '"+passward+"'"; rs = st.executeQuery(sql); if(rs.next()){ System.out.println("登录成功。"); }else{ System.out.println("登录失败。"); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { test3("wjm3' or '1 = 1","151515"); }
登录后复制

数据库信息
在这里插入图片描述
如上面的代码所示,用户名为wjm3’ or '1 = 1,密码为151515,从数据库中可以看出我们没有这样的用户,本来应该显示登录失败,但是结果却是登陆成功,因为or '1 = 1 已经不是用户名里面的内容了,它现在为SQL 语句里面的内容,不论如何,结果都为true,等于不用输密码都可以登录。这里就产生了安全问题。

解决方法

1. PrepareStatement

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//注入式错误 public static void test3(String name,String passward){ Connection connection = null; PreparedStatement st = null; ResultSet rs = null; try { // 加载JDBC 驱动 Class.forName("com.mysql.jdbc.Driver"); // 获得JDBC 连接 String url = "jdbc:mysql://localhost:3306/tulun"; connection = DriverManager.getConnection(url,"root","123456"); //创建一个查询语句 String sql1 = "select * from student where name = ? and passward = ?"; st = connection.prepareStatement(sql1); st.setString(1,name); st.setString(2,passward); //sql语句 //String sql = "select * from student where name = '"+ name+"' and passward = '"+passward+"'"; rs = st.executeQuery(); if(rs.next()){ System.out.println("登录成功。"); }else{ System.out.println("登录失败。"); } } catch (Exception e) { e.printStackTrace(); }finally{ try { connection.close(); st.close(); rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { test3("wjm3' or '1 = 1","151515"); }
登录后复制

上面这个代码不管name 参数是什么,它都只是name 参数,不会作为sql语句的一部分来执行,一般来说推荐这个方法,比较安全。
2.自己定义函数进行校验

  • 整理数据使之变得有效
  • 拒绝已知的非法输入
  • 只接受已知的合法输入

以上就是mysql语句的注入错误是什么?的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是朴素抽屉最近收集整理的关于mysql语句的注入错误是什么?的全部内容,更多相关mysql语句内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(122)

评论列表共有 0 条评论

立即
投稿
返回
顶部