java--SQL注入攻击

SQL注入攻击概述

该攻击也就是黑客或居心不良的人知晓了你底层使用拼接的方式连接SQL语句,也就有可能研究出绕过你检查的SQL语句,以下实例说明:

登录操作,检查用户名、密码是否正确

用Statement操作SQL语句

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class StatementDemo {
    private StatementDemo() {

    }
    public static void method(String use, String pass){
        try(Connection connection=SqlConnection.con()){
            Statement stmt = connection.createStatement();
            //Statement方式操作SQL语句只能使用拼接的方式,不能使用占位方式
            String sql="select uname,password from user where uname='" + use + "'and password='" + pass + "'";
            //输出拼接的字符串
            System.out.println(sql);
            //执行SQL语句
            ResultSet rs = stmt.executeQuery(sql);
            //如果输入的密码和用户名满足,指针就会移到该用户名和密码这一行,输出true,否则false
            if(rs.next()){

                System.out.println(true);
            }else{
                System.out.println(false);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}

用PreparedStatement操作SQL语句

import java.sql.*;

public class PreparedStatementDemo {
    private PreparedStatementDemo(){

    }
    public static void method(String uname, String password){
        try(Connection connection=SqlConnection.con()){
            PreparedStatement psmt = connection.prepareStatement("select uname,password from user where uname=? and password=?");
            //这里的1、2,表示列的位置(从1开始),也可以用列名替代
            psmt.setString(1,uname);
            psmt.setString(2,password);
            //执行SQL语句
            ResultSet rs = psmt.executeQuery();
            //如果输入的密码和用户名满足,指针就会移到该用户名和密码这一行,输出true,否则false
            if(rs.next()){
                System.out.println(true);
            }else{
                System.out.println(false);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}

main方法

public class StatementDemoTest {
    public static void main(String[] args) {
        StatementDemo.method("张三","' or '1'='1");
        PreparedStatementDemo.method("张三","' or '1'='1");
    }
}

结果

Thu Dec 27 20:30:48 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
select uname,password from user where uname='张三'and password='' or '1'='1'
true
Thu Dec 27 20:30:49 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
false

你会发现还是上面那个是true,明明密码不对为什么会是true呢?这是利用了Statement的漏洞,从结果上可以看出拼接好的SQL语句,你会看到password后面连接空字符串,后面有个1=1中间连的是or,or表示的是其中任一个是真就为真,1=1为真, password=''为假,前面的的张三也是真用and相连,and表示都为真时为真,前面的真和后面的这个真在连一起就通过了判断,而PreparedStatement就不会出现这个漏洞,因为他的语句使用“?”符号占位,也就是把输入的' or '1'='1字符串为密码,这样就不会通过判断,因此要想避开这个攻击有以下两个方法:

1. 对参数内存做检查,内部不能有sql关键字例如:or

2. PreparedStatement 

总结

因此,以后用到数据库连接时,操作数据库语句不要用拼接的方式,这会让人有机可乘,用 PreparedStatement 方式操作SQL语句是不错的选择,建议以后操作SQL语句都用PreparedStatement 方式。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值