单例模式的反射漏洞和反序列化漏洞代码实例

2025-05-29 0 33

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

?

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

42

43

44

45

46
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}.

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

?

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
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;

}

}

以上所述是小编给大家介绍的单例模式反射漏洞反序列化漏洞详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:https://blog.csdn.net/da_kao_la/article/details/89111289

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 单例模式的反射漏洞和反序列化漏洞代码实例 https://www.kuaiidc.com/109510.html

相关文章

发表评论
暂无评论