java jdbc_sql注入攻击数据 案例。

JDBC URL: jdbc:mysql://mysql.sqlpub.com:3306/huangjin
Username: XXXXXX
Password: fc12f7a5215e8e0a
请输入要查找的id:1 or 1=1
1, ml5 [email protected] gmail 22
2, bb44 [email protected] facebook 1
3, je22 [email protected] github 49
4, ae5 [email protected] facebook 64
5, ls5 [email protected] twitter 10
6, ay0 [email protected] facebook 1
7, je1 [email protected] github 99
8, mk57 [email protected] github 15
9, tt34 [email protected] github 90
10, cc100 [email protected] github 10

# JDBC配置

# config.properties
db.url=jdbc:mysql://mysql.sqlpub.com:3306/huangjin
db.username=XXXXXX
db.password=fc12f7a5215e8e0a

package com.abc.project3;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

public class D {
    public static void main(String[] args) throws IOException, SQLException {

        String dbUrl = null;
        String dbUsername = null;
        String dbPassword = null;

        Properties properties = new Properties();
        FileInputStream fis = null;

        String fName = "config.properties";
        File file = new File(fName);
        if (!file.isFile()) {
            System.err.println(fName + " 文件不存在");
            System.exit(0);
        }


        // 读取配置文件
        fis = new FileInputStream(fName);
        properties.load(fis);

        // 获取 JDBC 连接信息
        dbUrl = properties.getProperty("db.url");
        dbUsername = properties.getProperty("db.username");
        dbPassword = properties.getProperty("db.password");

        if(fis!=null)
            fis.close();

        // 打印 JDBC 连接信息
        System.out.println("JDBC URL: " + dbUrl);
        System.out.println("Username: " + dbUsername);
        System.out.println("Password: " + dbPassword);

        Connection con = DriverManager.getConnection(dbUrl,dbUsername,dbPassword);

        //4.获取执行者对象
        Statement stat = con.createStatement();
        String sql ="SELECT * FROM user WHERE id = ";
        ResultSet resultSet = null;
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要查找的id:");
        String str =sc.nextLine();  // 注入攻击  1 or 1=1
        sc.close();
        resultSet = stat.executeQuery(sql + str);

        while(resultSet.next()){
            String  res =resultSet.getInt("ID")+", ";
                    res += resultSet.getString("username")+" ";
                    res += resultSet.getString("email")+" ";
                    res += resultSet.getString("authType")+" ";
            res += resultSet.getInt("reputation");
            System.out.println(res);
        }

        if (resultSet != null)
            resultSet.close();
        stat.close();
        con.close();



    }
}

你可能感兴趣的:(java,开发语言)