服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - 单例模式的反射漏洞和反序列化漏洞代码实例

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

2021-07-30 11:05da_kao_la Java教程

这篇文章主要介绍了单例模式的反射漏洞和反序列化漏洞,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

除了枚举式单例模式外,其余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;
    }
}

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

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

延伸 · 阅读

精彩推荐