加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

XXE漏洞修复

(2018-09-28 12:45:15)
标签:

xxe漏洞

dom4j

dom

分类: 网络安全

 Java Code 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;

///
 // XXE漏洞测试
 // 
 // 漏洞修复参考微信官方:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=23_5
 // 漏洞攻击参考:https://www.cnblogs.com/tongwen/p/5194483.html
 // 
 // @author iPan
 // @version 2018-09-28
 //
public class XXETest {

    public static String getXml(String fileName) {
        byte[] buf new byte[512];
        String result null;
        ByteArrayOutputStream out null;
        InputStream fin null;
        try {
            fin XXETest.class.getResourceAsStream(fileName);
            out new ByteArrayOutputStream();
            int len fin.read(buf);
            while (len 0{
                out.write(buf, 0len);
                len fin.read(buf);
            }
            result out.toString("utf-8");
        catch (Exception e) {
            e.printStackTrace();
        finally {
            if (out != null) {
                try {
                    out.close();
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fin != null) {
                try {
                    fin.close();
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        //dom4jTest();
        domTest();
    }
    
    public static void dom4jTest() throws Exception {
        String xml getXml("hello.xml");
        Document doc null;
        // 有问题的代码
//      doc DocumentHelper.parseText(xml);
        
        // 修改后的代码
        SAXReader reader new SAXReader();
//      reader.setIncludeExternalDTDDeclarations(false); // 测试结果:不起作用;
//      reader.setIncludeInternalDTDDeclarations(false); // 测试结果:不起作用;
        reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl"true); // 主要是这句发挥作用!!!
        reader.setFeature("http://xml.org/sax/features/external-general-entities"false); // 测试结果:可不写;
        reader.setFeature("http://xml.org/sax/features/external-parameter-entities"false); // 测试结果:可不写;
        reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd"false); // 测试结果:可不写;
        String encoding "utf-8"// getEncoding(xml);
        InputSource source new InputSource(new StringReader(xml));
        source.setEncoding(encoding);
        doc reader.read(source);

        // if the XML parser doesn't provide way to retrieve the encoding,
        // specify it manually
        if (doc.getXMLEncoding() == null) {
            doc.setXMLEncoding("utf-8");
        }
        System.out.println(doc.getRootElement().element("name").getText());
    }
    
    public static void domTest() throws Exception {
        DocumentBuilderFactory dbf DocumentBuilderFactory.newInstance();
        // 有问题的代码
//      DocumentBuilder builder dbf.newDocumentBuilder();
//      org.w3c.dom.Document doc builder.parse(Test3.class.getResourceAsStream("hello.xml"));
        
        // 修改后的代码
        String FEATURE null;
        // This is the PRIMARY defense. If DTDs (doctypes) are disallowed, almost all XML entity attacks are prevented
        // Xerces only http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl
        FEATURE "http://apache.org/xml/features/disallow-doctype-decl";
        dbf.setFeature(FEATURE, true); // 主要是这句发挥作用!!!
        
        // If you can't completely disable DTDs, then at least do the following:
        // Xerces http://xerces.apache.org/xerces-j/features.html#external-general-entities
        // Xerces http://xerces.apache.org/xerces2-j/features.html#external-general-entities
        // JDK7+ http://xml.org/sax/features/external-general-entities 
        FEATURE "http://xml.org/sax/features/external-general-entities";
        dbf.setFeature(FEATURE, false); // 测试结果:可不写;
        
        // Xerces http://xerces.apache.org/xerces-j/features.html#external-parameter-entities
        // Xerces http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities
        // JDK7+ http://xml.org/sax/features/external-parameter-entities 
        FEATURE "http://xml.org/sax/features/external-parameter-entities";
        dbf.setFeature(FEATURE, false); // 测试结果:可不写;
        
        // Disable external DTDs as well
        FEATURE "http://apache.org/xml/features/nonvalidating/load-external-dtd";
        dbf.setFeature(FEATURE, false); // 测试结果:可不写;
        
        // and these as well, per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks"
        dbf.setXIncludeAware(false); // 测试结果:可不写;
        dbf.setExpandEntityReferences(false); // 测试结果:可不写;
        DocumentBuilder builder dbf.newDocumentBuilder();
        org.w3c.dom.Document doc builder.parse(XXETest.class.getResourceAsStream("hello.xml"));
        System.out.println(doc.getElementsByTagName_r("name").item(0).getTextContent());
    }
    
}
XXE漏洞修复

XXE漏洞修复


XXE漏洞修复

XXE漏洞修复

小结
      XXE漏洞可以直接读取服务器的本地文件,这样可以轻松盗取服务器敏感信息;主要是XML定义可以直接从网络和本地文件系统加载DTD,实体定义也可以引用网络DTD以及本地文件系统;参数化实体还可以间接获取本地的实体信息组装成URL提交到黑客服务器!
      解决:禁用DTD加载以及实体定义!

iPan 2018-09-28

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有