温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

单例模式中反射漏洞和反序列化漏洞的示例分析

发布时间:2021-09-03 13:53:50 来源:亿速云 阅读:117 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关单例模式中反射漏洞和反序列化漏洞的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

除了枚举式单例模式外,其余4种在单例模式提到的单例模式的实现方式都存在反射漏洞和反序列化漏洞。

package singleton;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;

/**
 * 用反射和反序列化的方法破解单例模式
 * @author weiyx15
 *
 */
public class SingletonCrack {
	public static void main(String[] args) throws Exception
	{
		// 正常创建单例对象
		SingletonLazy s1 = SingletonLazy.getInstance();
		SingletonLazy s2 = SingletonLazy.getInstance();
		System.out.println(s1);
		System.out.println(s2);
		
		// 用反射破解单例
		Class<SingletonLazy> cls = (Class<SingletonLazy>) Class.forName("singleton.SingletonLazy");		// 获取SingletonLazy类
		Constructor<SingletonLazy> cons = cls.getDeclaredConstructor(null);		// 获取SingletonLazy的构造方法
		cons.setAccessible(true);			// 跳过方法的可见性检查
		SingletonLazy s3 = cons.newInstance();	// 调用构造方法生成新对象
		SingletonLazy s4 = cons.newInstance();	// 调用构造方法生成新对象
		System.out.println(s3);
		System.out.println(s4);
		
		// 用反序列化破解单例
		FileOutputStream fos = new FileOutputStream("object.out");	// 文件输出流
		ObjectOutputStream oos = new ObjectOutputStream(fos);		// 对象输出流
		oos.writeObject(s1);										// 向文件序列化对象
		oos.close();												// 关闭对象输出流
		fos.close();												// 关闭文件输出流
		
		FileInputStream fis = new FileInputStream("object.out");	// 文件输入流
		ObjectInputStream ois = new ObjectInputStream(fis);			// 对象输入流
		SingletonLazy s5 = (SingletonLazy) ois.readObject();		// 从文件反序列化对象
		ois.close();												// 关闭对象输入流
		fis.close();												// 关闭文件输入流
		System.out.println(s5);
	}
}

运行结果

singleton.SingletonLazy@15db9742 // s1
singleton.SingletonLazy@15db9742// s2
singleton.SingletonLazy@6d06d69c// s3
singleton.SingletonLazy@7852e922// s4
singleton.SingletonLazy@3b07d329 // s5

 从运行结果可以看到,通过反射可以得到私有构造方法,从而实例化两个不同的对象实例 codesingleton.SingletonLazy@6d06d69c}和{@code singleton.SingletonLazy@7852e922}. 通过反序列化,也可以得到新对象{@code singleton.SingletonLazy@3b07d329}.

以懒汉式单例模式的实现为例,解决反射漏洞和反序列化漏洞的方法如下:

package singleton;

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * 排除了反射漏洞和反序列化漏洞的懒汉式单例模式
 * @author weiyx15
 *
 */
public class SingletonLazySafe implements Serializable{
	private static SingletonLazySafe instance;
	
	private SingletonLazySafe() {
		// 防止反射漏洞通过再次调用私有构造方法实例化新的instance
		if (instance != null)
		{
			throw new RuntimeException();	// 抛出运行时异常
		}
	}
	
	public static synchronized SingletonLazySafe getInstance() {
		if (instance == null)	// 如果未实例化,则先实例化
		{
			instance = new SingletonLazySafe();	// 调用getInstance方法后再实例化对象
		}
		return instance;
	}
	
	/**
	 * 从I/O流读取对象时会调用readResolve接口
	 * 在readResolve接口中直接返回instance对象
	 * 避免反序列化时重新实例化对象
	 * @return 单例对象
	 * @throws ObjectStreamException
	 */
	private Object readResolve() throws ObjectStreamException {
		return instance;
	}
}

感谢各位的阅读!关于“单例模式中反射漏洞和反序列化漏洞的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI