首页 > 编程语言 >java基础漏洞学习----整数溢出漏洞+硬编码漏洞+不安全的随机数生成器

java基础漏洞学习----整数溢出漏洞+硬编码漏洞+不安全的随机数生成器

时间:2023-10-29 16:55:17浏览次数:30  
标签:java String 生成器 ---- 漏洞 static import properties

java基础漏洞学习----整数溢出漏洞+硬编码漏洞+不安全的随机数生成器

整数溢出漏洞

public class NumberLearning {
    public static void main(String[] args){
        System.out.println(Integer.MAX_VALUE+1);
        System.out.println(Integer.MIN_VALUE-1);
    }
}

硬编码漏洞

在进行数据库连接等操作的时候,经常会在db.properties写入明文的数据库用户名和密码
在编写程序的时候尽量对密码进行硬编码,而是采用对密码进行模糊化或先经过hash处理再转储,或在外部资源文件中进行处理
这里采用对username和password进行加密(测试)
db.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/security
username=cm9vdA==
password=cm9vdA==

主代码

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Base64;
import java.util.Properties;

public class DBConfig {
    private static final String PROPERTIES_FILE = "F:/db.properties";
    private static final String DRIVER_KEY = "driver";
    private static final String URL_KEY = "url";
    private static final String USERNAME_KEY = "username";
    private static final String PASSWORD_KEY = "password";

    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(PROPERTIES_FILE));

            String driver = properties.getProperty(DRIVER_KEY);
            String url = properties.getProperty(URL_KEY);
            String encryptedUsername = properties.getProperty(USERNAME_KEY);
            String encryptedPassword = properties.getProperty(PASSWORD_KEY);

            // 解密用户名和密码
            String username = decrypt(encryptedUsername);
            String password = decrypt(encryptedPassword);

            // 使用配置信息进行数据库连接
            Connection connection = null;
            try {
                Class.forName(driver);
                connection = DriverManager.getConnection(url, username, password);

                // 执行查询
                String query = "SELECT * FROM users WHERE id = ?";
                try (PreparedStatement statement = connection.prepareStatement(query)) {
                    statement.setInt(1, 2);
                    try (ResultSet resultSet = statement.executeQuery()) {
                        while (resultSet.next()) {
                            int id = resultSet.getInt("id");
                            String name = resultSet.getString("username");
                            // 处理查询结果
                            System.out.println("ID: " + id + ", Username: " + name);
                        }
                    }
                }
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String decrypt(String encryptedString) {
        // 进行解密操作,这里假设使用Base64进行加密
        byte[] decodedBytes = Base64.getDecoder().decode(encryptedString);
        return new String(decodedBytes);
    }
}

不安全的随机数生成器

三种随机数生成器
1.Random
本质上是系统当前时间,且如果种子相同的话,生成的随机数就相同
2.Math
3.SecureRandom
种子是不可预知的,产生非确定性输出

import java.util.Random;
import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;

public class RandomLearning {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        System.out.println("Random随机数");
        Random random1 = new Random(1);
        for(int i=0;i<5;i++){
            System.out.print(random1.nextInt(10)+" ");
        }
        System.out.println();
        Random random2 = new Random(1);
        for(int i=0;i<5;i++){
            System.out.print(random2.nextInt(10)+" ");
        }
        System.out.println();
        System.out.println("Math随机数");
        int randomNumber = (int) (Math.random() * 100);
        for(int i=0;i<5;i++){
            System.out.print(randomNumber+" ");
        }
        System.out.println();
        System.out.println("SecureRandom随机数");
        SecureRandom secureRandom1 = SecureRandom.getInstance("SHA1PRNG");
        for(int i=0;i<5;i++){
            System.out.print(secureRandom1.nextInt(10)+" ");
        }
        System.out.println();
        SecureRandom secureRandom2 = SecureRandom.getInstance("SHA1PRNG");
        for(int i=0;i<5;i++){
            System.out.print(secureRandom2.nextInt(10)+" ");
        }
    }
}

标签:java,String,生成器,----,漏洞,static,import,properties
From: https://www.cnblogs.com/thebeastofwar/p/17767492.html

相关文章

  • 类变量,类方法,私有变量,私有方法,继承,多态
    Defrun(self):Print(‘{}在跑’.format(self.name))dog.run()Self说明当前的方法是一个实例方法(类的实例化的方法),将方法绑定到当前的实例上面去。 类变量的调用 类方法的调用 cls是表示类的类型,看成默认的即可。   私有变量    私有方法  ......
  • 临时保存
    %Exampleofuseofoxmathproblemslatexclassforproblemsheets%\documentclass{oxmathproblems}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%UniversityofOxford,MathematicalInstituteLaTeXProblemSheetclass%Createdby......
  • Water Pollution in China
     Waterpollutionisamajorenvironmentalproblemworldwide,especiallyindevelopingcountries.By1989,436ofChina's532riverswerepolluted.In1994,theWorldHealthOrganizationreportedthatChina'scitiescontainmorepollutedwaterth......
  • 商用 LTS Qt 6.2.10 发布
    导读我们近日为商业许可证持有者发布了Qt6.2.10LTS。作为一个补丁版本,Qt6.2.10不添加任何新功能,但提供了错误修复和其他改进。你可以使用维护工具将Qt6.2.10添加到现有的在线安装中,也可以使用Qt在线安装程序进行简洁安装。离线安装程序可在Qt账户下载区下载......
  • 常见问题解决 --- 安卓12关闭phantom processes killer杀后台功能
    1.adb连接成功后,执行adbdevices2.执行adbshell3.执行device_configset_sync_disabled_for_testspersistentdevice_configputactivity_managermax_phantom_processes2147483647settingsputglobalsettings_enable_monitor_phantom_procsfalse......
  • Java面试题小练(一)
    java面向对象的三大特征封装,继承,多态封装说明一个类行为和属性与其他类的关系,低耦合,高内聚;继承是父类和子类的关系,多态说的是类与类的关系封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据。外界对他的内部细节是隐藏的,暴露在外界的只是它......
  • 提高组常见模板总结
    注$^1$:本文中带有~~小粉兔~~字样的文字均指不怎么重要的东西。~~主要是不会~~注$^2$:马蜂良好,可以超,没问题的##~~【小粉兔】~~区间数据结构###1.ST表适用范围:有函数$f$使得$x=f(x,x)$,如$\max,\gcd$等。代码实现:```cppvoidinit(){ for(inti=0;i<n;i++)s......
  • C语言运行库及glibc介绍
    C语言运行库任何一个C程序,它的背后都有一套庞大的代码来进行支撑,以使得该程序能够正常运行。这套代码至少包括入口函数,及其所依赖的函数所构成的函数集合。当然,它还理应包括各种标准库函数的实现。这样的一个代码集合称之为运行时库(RuntimeLibrary)。而C语言的运行库,即被称为C......
  • Pod生命周期说明
    一、Pod生命周期Pod对象从创建开始至终止退出之间的时间称为其生命周期,这段时间里的某个时间点,Pod会处于某个特定的运行阶段或相位(phase),以概括描述其在生命周期中所处的位置。Kubernetes为Pod资源严格定义了5种相位,并将特定Pod对象的当前相位存储在其内部的子对象PodStatus的phas......
  • 计算机基础dos命令
    打开cmd得方式开始+系统+命令提示符win+r输入cmd在任意文件夹下,按住shift+鼠标右键,在此处打开命令行资源管理器地址栏前面加上cmd路径管理员方式运行:windows系统,命令提示符,选择以管理员方式运行 常用的dos命令1#切换盘符E:2#查看有哪些文件dir3#切换目录......