README.html 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.12: http://docutils.sourceforge.net/" />
  7. <title>defusedxml -- defusing XML bombs and other exploits</title>
  8. <style type="text/css">
  9. /*
  10. Stylesheet for Docutils.
  11. Based on `blue_box.css` by Ian Bicking
  12. and `voidspace.css` by Ian Bicking, Michael Foord
  13. and `html4css1.css`
  14. */
  15. .borderless, table.borderless td, table.borderless th
  16. {
  17. border: 0;
  18. }
  19. table.borderless td, table.borderless th
  20. {
  21. padding: 0 0.5em 0 0 ! important;
  22. }
  23. .first
  24. {
  25. margin-top: 0 ! important;
  26. }
  27. .last, .with-subtitle
  28. {
  29. margin-bottom: 0 ! important;
  30. }
  31. .hidden
  32. {
  33. display: none;
  34. }
  35. a.toc-backref
  36. {
  37. color: black;
  38. text-decoration: none;
  39. }
  40. blockquote.epigraph
  41. {
  42. margin: 2em 5em;
  43. }
  44. dl.docutils dd
  45. {
  46. margin-bottom: 0.5em;
  47. }
  48. object[type="image/svg+xml"], object[type="application/x-shockwave-flash"]
  49. {
  50. overflow: hidden;
  51. }
  52. div.abstract
  53. {
  54. margin: 2em 5em;
  55. }
  56. div.abstract p.topic-title
  57. {
  58. font-weight: bold;
  59. text-align: center;
  60. }
  61. div.admonition, div.attention, div.caution, div.danger, div.error,
  62. div.hint, div.important, div.note, div.tip, div.warning
  63. {
  64. border: medium outset;
  65. margin: 2em;
  66. padding: 1em;
  67. }
  68. div.admonition p.admonition-title, div.hint p.admonition-title,
  69. div.important p.admonition-title, div.note p.admonition-title,
  70. div.tip p.admonition-title
  71. {
  72. font-family: sans-serif;
  73. font-weight: bold;
  74. }
  75. div.attention p.admonition-title, div.caution p.admonition-title,
  76. div.danger p.admonition-title, div.error p.admonition-title,
  77. div.warning p.admonition-title
  78. {
  79. color: red;
  80. font-family: sans-serif;
  81. font-weight: bold;
  82. }
  83. div.dedication
  84. {
  85. font-style: italic;
  86. margin: 2em 5em;
  87. text-align: center;
  88. }
  89. div.dedication p.topic-title
  90. {
  91. font-style: normal;
  92. font-weight: bold;
  93. }
  94. div.figure
  95. {
  96. margin-left: 2em;
  97. margin-right: 2em;
  98. }
  99. div.footer, div.header
  100. {
  101. clear: both;
  102. font-size: smaller;
  103. }
  104. div.line-block
  105. {
  106. display: block;
  107. margin-bottom: 1em;
  108. margin-top: 1em;
  109. }
  110. div.line-block div.line-block
  111. {
  112. margin-bottom: 0;
  113. margin-left: 1.5em;
  114. margin-top: 0;
  115. }
  116. div.sidebar
  117. {
  118. background-color: #ffffee;
  119. border: medium outset;
  120. clear: right;
  121. float: right;
  122. margin: 0 0 0.5em 1em;
  123. padding: 1em;
  124. width: 40%;
  125. }
  126. div.sidebar p.rubric
  127. {
  128. font-family: sans-serif;
  129. font-size: medium;
  130. }
  131. div.system-messages
  132. {
  133. margin: 5em;
  134. }
  135. div.system-messages h1
  136. {
  137. color: red;
  138. }
  139. div.system-message
  140. {
  141. border: medium outset;
  142. padding: 1em;
  143. }
  144. div.system-message p.system-message-title
  145. {
  146. color: red;
  147. font-weight: bold;
  148. }
  149. div.topic
  150. {
  151. margin: 2em;
  152. }
  153. h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
  154. h4.section-subtitle, h5.section-subtitle, h6.section-subtitle
  155. {
  156. margin-top: 0.4em;
  157. }
  158. h1.title
  159. {
  160. text-align: center;
  161. }
  162. h2.subtitle
  163. {
  164. text-align: center;
  165. }
  166. hr.docutils
  167. {
  168. width: 75%;
  169. }
  170. img.align-left, .figure.align-left, object.align-left
  171. {
  172. clear: left;
  173. float: left;
  174. margin-right: 1em;
  175. }
  176. img.align-right, .figure.align-right, object.align-right
  177. {
  178. clear: right;
  179. float: right;
  180. margin-left: 1em;
  181. }
  182. img.align-center, .figure.align-center, object.align-center
  183. {
  184. display: block;
  185. margin-left: auto;
  186. margin-right: auto;
  187. }
  188. .align-left
  189. {
  190. text-align: left;
  191. }
  192. .align-center
  193. {
  194. clear: both;
  195. text-align: center;
  196. }
  197. .align-right
  198. {
  199. text-align: right;
  200. }
  201. div.align-right
  202. {
  203. text-align: inherit;
  204. }
  205. ol.simple, ul.simple
  206. {
  207. margin-bottom: 1em;
  208. }
  209. ol.arabic
  210. {
  211. list-style: decimal;
  212. }
  213. ol.loweralpha
  214. {
  215. list-style: lower-alpha;
  216. }
  217. ol.upperalpha
  218. {
  219. list-style: upper-alpha;
  220. }
  221. ol.lowerroman
  222. {
  223. list-style: lower-roman;
  224. }
  225. ol.upperroman
  226. {
  227. list-style: upper-roman;
  228. }
  229. p.attribution
  230. {
  231. margin-left: 50%;
  232. text-align: right;
  233. }
  234. p.caption
  235. {
  236. font-style: italic;
  237. }
  238. p.credits
  239. {
  240. font-size: smaller;
  241. font-style: italic;
  242. }
  243. p.label
  244. {
  245. white-space: nowrap;
  246. }
  247. p.rubric
  248. {
  249. color: maroon;
  250. font-size: larger;
  251. font-weight: bold;
  252. text-align: center;
  253. }
  254. p.sidebar-title
  255. {
  256. font-family: sans-serif;
  257. font-size: larger;
  258. font-weight: bold;
  259. }
  260. p.sidebar-subtitle
  261. {
  262. font-family: sans-serif;
  263. font-weight: bold;
  264. }
  265. p.topic-title
  266. {
  267. font-weight: bold;
  268. }
  269. pre.address
  270. {
  271. font: inherit;
  272. margin-bottom: 0;
  273. margin-top: 0;
  274. }
  275. pre.literal-block, pre.doctest-block, pre.math
  276. {
  277. margin-left: 2em;
  278. margin-right: 2em;
  279. background-color: #eeeeee;
  280. }
  281. span.classifier
  282. {
  283. font-family: sans-serif;
  284. font-style: oblique;
  285. }
  286. span.classifier-delimiter
  287. {
  288. font-family: sans-serif;
  289. font-weight: bold;
  290. }
  291. span.interpreted
  292. {
  293. font-family: sans-serif;
  294. }
  295. span.option
  296. {
  297. white-space: nowrap;
  298. }
  299. span.pre
  300. {
  301. white-space: pre;
  302. }
  303. span.problematic
  304. {
  305. color: red;
  306. }
  307. span.section-subtitle
  308. {
  309. font-size: 80%;
  310. }
  311. table.citation
  312. {
  313. border-left: solid 1px gray;
  314. margin-left: 1px;
  315. }
  316. table.docinfo
  317. {
  318. margin: 2em 4em;
  319. }
  320. table.docutils
  321. {
  322. margin-bottom: 0.5em;
  323. margin-top: 0.5em;
  324. }
  325. table.footnote
  326. {
  327. border-left: solid 1px black;
  328. margin-left: 1px;
  329. }
  330. table.docutils td, table.docutils th,
  331. table.docinfo td, table.docinfo th
  332. {
  333. padding-left: 0.5em;
  334. padding-right: 0.5em;
  335. vertical-align: top;
  336. }
  337. table.docutils th.field-name, table.docinfo th.docinfo-name
  338. {
  339. font-weight: bold;
  340. padding-left: 0;
  341. text-align: left;
  342. white-space: nowrap;
  343. }
  344. h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
  345. h4 tt.docutils, h5 tt.docutils, h6 tt.docutils
  346. {
  347. font-size: 100%;
  348. }
  349. ul.auto-toc
  350. {
  351. list-style-type: none;
  352. }
  353. body
  354. {
  355. font-family: Arial, sans-serif;
  356. margin-left: auto;
  357. margin-right: auto;
  358. width: 800px;
  359. }
  360. em, i
  361. {
  362. font-family: Times New Roman, Times, serif;
  363. }
  364. a.target
  365. {
  366. color: blue;
  367. }
  368. a.target
  369. {
  370. color: blue;
  371. }
  372. a.toc-backref
  373. {
  374. color: black;
  375. text-decoration: none;
  376. }
  377. a.toc-backref:hover
  378. {
  379. background-color: inherit;
  380. }
  381. a:hover
  382. {
  383. background-color: #cccccc;
  384. }
  385. div.attention, div.caution, div.danger, div.error, div.hint,
  386. div.important, div.note, div.tip, div.warning
  387. {
  388. padding: 3px;
  389. width: 80%;
  390. }
  391. div.admonition p.admonition-title, div.hint p.admonition-title,
  392. div.important p.admonition-title, div.note p.admonition-title,
  393. div.tip p.admonition-title
  394. {
  395. display: block;
  396. margin: 0;
  397. text-align: center;
  398. }
  399. div.attention p.admonition-title, div.caution p.admonition-title,
  400. div.danger p.admonition-title, div.error p.admonition-title,
  401. div.warning p.admonition-title
  402. {
  403. display: block;
  404. font-family: sans-serif;
  405. margin: 0;
  406. text-align: center;
  407. }
  408. h1.title
  409. {
  410. text-align: center;
  411. }
  412. table.footnote
  413. {
  414. padding-left: 0.5ex;
  415. }
  416. table.citation
  417. {
  418. padding-left: 0.5ex;
  419. }
  420. pre.literal-block, pre.doctest-block
  421. {
  422. padding: 5px;
  423. }
  424. h1 tt, h2 tt, h3 tt, h4 tt, h5 tt, h6 tt
  425. {
  426. font-size: 100%;
  427. }
  428. code, tt
  429. {
  430. color: #000066;
  431. }
  432. p
  433. {
  434. text-align: justify;
  435. }
  436. dt
  437. {
  438. font-weight: bold;
  439. }
  440. tt.literal
  441. {
  442. background-color: #eeeeee;
  443. }
  444. h1
  445. {
  446. border-bottom: solid 1px black;
  447. padding-top: 20px;
  448. }
  449. caption
  450. {
  451. margin-bottom: 0.4em;
  452. font-weight: bold;
  453. font-size: 120%;
  454. }
  455. </style>
  456. </head>
  457. <body>
  458. <div class="document" id="defusedxml-defusing-xml-bombs-and-other-exploits">
  459. <h1 class="title">defusedxml -- defusing XML bombs and other exploits</h1>
  460. <blockquote>
  461. &quot;It's just XML, what could probably go wrong?&quot;</blockquote>
  462. <p>Christian Heimes &lt;<a class="reference external" href="mailto:christian&#64;python.org">christian&#64;python.org</a>&gt;</p>
  463. <div class="section" id="synopsis">
  464. <h1><a class="toc-backref" href="#id2">Synopsis</a></h1>
  465. <p>The results of an attack on a vulnerable XML library can be fairly dramatic.
  466. With just a few hundred <strong>Bytes</strong> of XML data an attacker can occupy several
  467. <strong>Gigabytes</strong> of memory within <strong>seconds</strong>. An attacker can also keep
  468. CPUs busy for a long time with a small to medium size request. Under some
  469. circumstances it is even possible to access local files on your
  470. server, to circumvent a firewall, or to abuse services to rebound attacks to
  471. third parties.</p>
  472. <p>The attacks use and abuse less common features of XML and its parsers. The
  473. majority of developers are unacquainted with features such as processing
  474. instructions and entity expansions that XML inherited from SGML. At best
  475. they know about <tt class="docutils literal">&lt;!DOCTYPE&gt;</tt> from experience with HTML but they are not
  476. aware that a document type definition (DTD) can generate an HTTP request
  477. or load a file from the file system.</p>
  478. <p>None of the issues is new. They have been known for a long time. Billion
  479. laughs was first reported in 2003. Nevertheless some XML libraries and
  480. applications are still vulnerable and even heavy users of XML are
  481. surprised by these features. It's hard to say whom to blame for the
  482. situation. It's too short sighted to shift all blame on XML parsers and
  483. XML libraries for using insecure default settings. After all they
  484. properly implement XML specifications. Application developers must not rely
  485. that a library is always configured for security and potential harmful data
  486. by default.</p>
  487. <div class="contents topic" id="table-of-contents">
  488. <p class="topic-title first">Table of Contents</p>
  489. <ul class="simple">
  490. <li><a class="reference internal" href="#synopsis" id="id2">Synopsis</a></li>
  491. <li><a class="reference internal" href="#attack-vectors" id="id3">Attack vectors</a><ul>
  492. <li><a class="reference internal" href="#billion-laughs-exponential-entity-expansion" id="id4">billion laughs / exponential entity expansion</a></li>
  493. <li><a class="reference internal" href="#quadratic-blowup-entity-expansion" id="id5">quadratic blowup entity expansion</a></li>
  494. <li><a class="reference internal" href="#external-entity-expansion-remote" id="id6">external entity expansion (remote)</a></li>
  495. <li><a class="reference internal" href="#external-entity-expansion-local-file" id="id7">external entity expansion (local file)</a></li>
  496. <li><a class="reference internal" href="#dtd-retrieval" id="id8">DTD retrieval</a></li>
  497. </ul>
  498. </li>
  499. <li><a class="reference internal" href="#python-xml-libraries" id="id9">Python XML Libraries</a><ul>
  500. <li><a class="reference internal" href="#settings-in-standard-library" id="id10">Settings in standard library</a></li>
  501. </ul>
  502. </li>
  503. <li><a class="reference internal" href="#defusedxml" id="id11">defusedxml</a><ul>
  504. <li><a class="reference internal" href="#defusedxml-package" id="id12">defusedxml (package)</a></li>
  505. <li><a class="reference internal" href="#defusedxml-celementtree" id="id13">defusedxml.cElementTree</a></li>
  506. <li><a class="reference internal" href="#defusedxml-elementtree" id="id14">defusedxml.ElementTree</a></li>
  507. <li><a class="reference internal" href="#defusedxml-expatreader" id="id15">defusedxml.expatreader</a></li>
  508. <li><a class="reference internal" href="#defusedxml-sax" id="id16">defusedxml.sax</a></li>
  509. <li><a class="reference internal" href="#defusedxml-expatbuilder" id="id17">defusedxml.expatbuilder</a></li>
  510. <li><a class="reference internal" href="#defusedxml-minidom" id="id18">defusedxml.minidom</a></li>
  511. <li><a class="reference internal" href="#defusedxml-pulldom" id="id19">defusedxml.pulldom</a></li>
  512. <li><a class="reference internal" href="#defusedxml-xmlrpc" id="id20">defusedxml.xmlrpc</a></li>
  513. <li><a class="reference internal" href="#defusedxml-lxml" id="id21">defusedxml.lxml</a></li>
  514. </ul>
  515. </li>
  516. <li><a class="reference internal" href="#defusedexpat" id="id22">defusedexpat</a><ul>
  517. <li><a class="reference internal" href="#modifications-in-expat" id="id23">Modifications in expat</a></li>
  518. </ul>
  519. </li>
  520. <li><a class="reference internal" href="#how-to-avoid-xml-vulnerabilities" id="id24">How to avoid XML vulnerabilities</a><ul>
  521. <li><a class="reference internal" href="#best-practices" id="id25">Best practices</a></li>
  522. </ul>
  523. </li>
  524. <li><a class="reference internal" href="#other-things-to-consider" id="id26">Other things to consider</a><ul>
  525. <li><a class="reference internal" href="#attribute-blowup-hash-collision-attack" id="id27">attribute blowup / hash collision attack</a></li>
  526. <li><a class="reference internal" href="#decompression-bomb" id="id28">decompression bomb</a></li>
  527. <li><a class="reference internal" href="#processing-instruction" id="id29">Processing Instruction</a></li>
  528. <li><a class="reference internal" href="#other-dtd-features" id="id30">Other DTD features</a></li>
  529. <li><a class="reference internal" href="#xpath" id="id31">XPath</a></li>
  530. <li><a class="reference internal" href="#xpath-injection-attacks" id="id32">XPath injection attacks</a></li>
  531. <li><a class="reference internal" href="#xinclude" id="id33">XInclude</a></li>
  532. <li><a class="reference internal" href="#xmlschema-location" id="id34">XMLSchema location</a></li>
  533. <li><a class="reference internal" href="#xsl-transformation" id="id35">XSL Transformation</a></li>
  534. </ul>
  535. </li>
  536. <li><a class="reference internal" href="#related-cves" id="id36">Related CVEs</a></li>
  537. <li><a class="reference internal" href="#other-languages-frameworks" id="id37">Other languages / frameworks</a><ul>
  538. <li><a class="reference internal" href="#perl" id="id38">Perl</a></li>
  539. <li><a class="reference internal" href="#ruby" id="id39">Ruby</a></li>
  540. <li><a class="reference internal" href="#php" id="id40">PHP</a></li>
  541. <li><a class="reference internal" href="#c-net-mono" id="id41">C# / .NET / Mono</a></li>
  542. <li><a class="reference internal" href="#java" id="id42">Java</a></li>
  543. </ul>
  544. </li>
  545. <li><a class="reference internal" href="#todo" id="id43">TODO</a></li>
  546. <li><a class="reference internal" href="#license" id="id44">License</a></li>
  547. <li><a class="reference internal" href="#acknowledgements" id="id45">Acknowledgements</a></li>
  548. <li><a class="reference internal" href="#references" id="id46">References</a></li>
  549. <li><a class="reference internal" href="#changelog" id="id47">Changelog</a><ul>
  550. <li><a class="reference internal" href="#defusedxml-0-5-0-rc1" id="id48">defusedxml 0.5.0.rc1</a></li>
  551. <li><a class="reference internal" href="#defusedxml-0-4-1" id="id49">defusedxml 0.4.1</a></li>
  552. <li><a class="reference internal" href="#defusedxml-0-4" id="id50">defusedxml 0.4</a></li>
  553. <li><a class="reference internal" href="#defusedxml-0-3" id="id51">defusedxml 0.3</a></li>
  554. <li><a class="reference internal" href="#defusedxml-0-2" id="id52">defusedxml 0.2</a></li>
  555. <li><a class="reference internal" href="#defusedxml-0-1" id="id53">defusedxml 0.1</a></li>
  556. </ul>
  557. </li>
  558. </ul>
  559. </div>
  560. </div>
  561. <div class="section" id="attack-vectors">
  562. <h1><a class="toc-backref" href="#id3">Attack vectors</a></h1>
  563. <div class="section" id="billion-laughs-exponential-entity-expansion">
  564. <h2><a class="toc-backref" href="#id4">billion laughs / exponential entity expansion</a></h2>
  565. <p>The <a class="reference external" href="http://en.wikipedia.org/wiki/Billion_laughs">Billion Laughs</a> attack -- also known as exponential entity expansion --
  566. uses multiple levels of nested entities. The original example uses 9 levels
  567. of 10 expansions in each level to expand the string <tt class="docutils literal">lol</tt> to a string of
  568. 3 * 10 <sup>9</sup> bytes, hence the name &quot;billion laughs&quot;. The resulting string
  569. occupies 3 GB (2.79 GiB) of memory; intermediate strings require additional
  570. memory. Because most parsers don't cache the intermediate step for every
  571. expansion it is repeated over and over again. It increases the CPU load even
  572. more.</p>
  573. <p>An XML document of just a few hundred bytes can disrupt all services on a
  574. machine within seconds.</p>
  575. <p>Example XML:</p>
  576. <pre class="literal-block">
  577. &lt;!DOCTYPE xmlbomb [
  578. &lt;!ENTITY a &quot;1234567890&quot; &gt;
  579. &lt;!ENTITY b &quot;&amp;a;&amp;a;&amp;a;&amp;a;&amp;a;&amp;a;&amp;a;&amp;a;&quot;&gt;
  580. &lt;!ENTITY c &quot;&amp;b;&amp;b;&amp;b;&amp;b;&amp;b;&amp;b;&amp;b;&amp;b;&quot;&gt;
  581. &lt;!ENTITY d &quot;&amp;c;&amp;c;&amp;c;&amp;c;&amp;c;&amp;c;&amp;c;&amp;c;&quot;&gt;
  582. ]&gt;
  583. &lt;bomb&gt;&amp;d;&lt;/bomb&gt;
  584. </pre>
  585. </div>
  586. <div class="section" id="quadratic-blowup-entity-expansion">
  587. <h2><a class="toc-backref" href="#id5">quadratic blowup entity expansion</a></h2>
  588. <p>A quadratic blowup attack is similar to a <a class="reference external" href="http://en.wikipedia.org/wiki/Billion_laughs">Billion Laughs</a> attack; it abuses
  589. entity expansion, too. Instead of nested entities it repeats one large entity
  590. with a couple of thousand chars over and over again. The attack isn't as
  591. efficient as the exponential case but it avoids triggering countermeasures of
  592. parsers against heavily nested entities. Some parsers limit the depth and
  593. breadth of a single entity but not the total amount of expanded text
  594. throughout an entire XML document.</p>
  595. <p>A medium-sized XML document with a couple of hundred kilobytes can require a
  596. couple of hundred MB to several GB of memory. When the attack is combined
  597. with some level of nested expansion an attacker is able to achieve a higher
  598. ratio of success.</p>
  599. <pre class="literal-block">
  600. &lt;!DOCTYPE bomb [
  601. &lt;!ENTITY a &quot;xxxxxxx... a couple of ten thousand chars&quot;&gt;
  602. ]&gt;
  603. &lt;bomb&gt;&amp;a;&amp;a;&amp;a;... repeat&lt;/bomb&gt;
  604. </pre>
  605. </div>
  606. <div class="section" id="external-entity-expansion-remote">
  607. <h2><a class="toc-backref" href="#id6">external entity expansion (remote)</a></h2>
  608. <p>Entity declarations can contain more than just text for replacement. They can
  609. also point to external resources by public identifiers or system identifiers.
  610. System identifiers are standard URIs. When the URI is a URL (e.g. a
  611. <tt class="docutils literal"><span class="pre">http://</span></tt> locator) some parsers download the resource from the remote
  612. location and embed them into the XML document verbatim.</p>
  613. <p>Simple example of a parsed external entity:</p>
  614. <pre class="literal-block">
  615. &lt;!DOCTYPE external [
  616. &lt;!ENTITY ee SYSTEM &quot;http://www.python.org/some.xml&quot;&gt;
  617. ]&gt;
  618. &lt;root&gt;&amp;ee;&lt;/root&gt;
  619. </pre>
  620. <p>The case of parsed external entities works only for valid XML content. The
  621. XML standard also supports unparsed external entities with a
  622. <tt class="docutils literal">NData declaration</tt>.</p>
  623. <p>External entity expansion opens the door to plenty of exploits. An attacker
  624. can abuse a vulnerable XML library and application to rebound and forward
  625. network requests with the IP address of the server. It highly depends
  626. on the parser and the application what kind of exploit is possible. For
  627. example:</p>
  628. <ul class="simple">
  629. <li>An attacker can circumvent firewalls and gain access to restricted
  630. resources as all the requests are made from an internal and trustworthy
  631. IP address, not from the outside.</li>
  632. <li>An attacker can abuse a service to attack, spy on or DoS your servers but
  633. also third party services. The attack is disguised with the IP address of
  634. the server and the attacker is able to utilize the high bandwidth of a big
  635. machine.</li>
  636. <li>An attacker can exhaust additional resources on the machine, e.g. with
  637. requests to a service that doesn't respond or responds with very large
  638. files.</li>
  639. <li>An attacker may gain knowledge, when, how often and from which IP address
  640. a XML document is accessed.</li>
  641. <li>An attacker could send mail from inside your network if the URL handler
  642. supports <tt class="docutils literal"><span class="pre">smtp://</span></tt> URIs.</li>
  643. </ul>
  644. </div>
  645. <div class="section" id="external-entity-expansion-local-file">
  646. <h2><a class="toc-backref" href="#id7">external entity expansion (local file)</a></h2>
  647. <p>External entities with references to local files are a sub-case of external
  648. entity expansion. It's listed as an extra attack because it deserves extra
  649. attention. Some XML libraries such as lxml disable network access by default
  650. but still allow entity expansion with local file access by default. Local
  651. files are either referenced with a <tt class="docutils literal"><span class="pre">file://</span></tt> URL or by a file path (either
  652. relative or absolute).</p>
  653. <p>An attacker may be able to access and download all files that can be read by
  654. the application process. This may include critical configuration files, too.</p>
  655. <pre class="literal-block">
  656. &lt;!DOCTYPE external [
  657. &lt;!ENTITY ee SYSTEM &quot;file:///PATH/TO/simple.xml&quot;&gt;
  658. ]&gt;
  659. &lt;root&gt;&amp;ee;&lt;/root&gt;
  660. </pre>
  661. </div>
  662. <div class="section" id="dtd-retrieval">
  663. <h2><a class="toc-backref" href="#id8">DTD retrieval</a></h2>
  664. <p>This case is similar to external entity expansion, too. Some XML libraries
  665. like Python's xml.dom.pulldom retrieve document type definitions from remote
  666. or local locations. Several attack scenarios from the external entity case
  667. apply to this issue as well.</p>
  668. <pre class="literal-block">
  669. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  670. &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
  671. &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
  672. &lt;html&gt;
  673. &lt;head/&gt;
  674. &lt;body&gt;text&lt;/body&gt;
  675. &lt;/html&gt;
  676. </pre>
  677. </div>
  678. </div>
  679. <div class="section" id="python-xml-libraries">
  680. <h1><a class="toc-backref" href="#id9">Python XML Libraries</a></h1>
  681. <table border="1" class="docutils">
  682. <caption>vulnerabilities and features</caption>
  683. <colgroup>
  684. <col width="31%" />
  685. <col width="9%" />
  686. <col width="10%" />
  687. <col width="10%" />
  688. <col width="9%" />
  689. <col width="10%" />
  690. <col width="10%" />
  691. <col width="10%" />
  692. </colgroup>
  693. <thead valign="bottom">
  694. <tr><th class="head">kind</th>
  695. <th class="head">sax</th>
  696. <th class="head">etree</th>
  697. <th class="head">minidom</th>
  698. <th class="head">pulldom</th>
  699. <th class="head">xmlrpc</th>
  700. <th class="head">lxml</th>
  701. <th class="head">genshi</th>
  702. </tr>
  703. </thead>
  704. <tbody valign="top">
  705. <tr><td>billion laughs</td>
  706. <td><strong>True</strong></td>
  707. <td><strong>True</strong></td>
  708. <td><strong>True</strong></td>
  709. <td><strong>True</strong></td>
  710. <td><strong>True</strong></td>
  711. <td>False (1)</td>
  712. <td>False (5)</td>
  713. </tr>
  714. <tr><td>quadratic blowup</td>
  715. <td><strong>True</strong></td>
  716. <td><strong>True</strong></td>
  717. <td><strong>True</strong></td>
  718. <td><strong>True</strong></td>
  719. <td><strong>True</strong></td>
  720. <td><strong>True</strong></td>
  721. <td>False (5)</td>
  722. </tr>
  723. <tr><td>external entity expansion (remote)</td>
  724. <td><strong>True</strong></td>
  725. <td>False (3)</td>
  726. <td>False (4)</td>
  727. <td><strong>True</strong></td>
  728. <td>false</td>
  729. <td>False (1)</td>
  730. <td>False (5)</td>
  731. </tr>
  732. <tr><td>external entity expansion (local file)</td>
  733. <td><strong>True</strong></td>
  734. <td>False (3)</td>
  735. <td>False (4)</td>
  736. <td><strong>True</strong></td>
  737. <td>false</td>
  738. <td><strong>True</strong></td>
  739. <td>False (5)</td>
  740. </tr>
  741. <tr><td>DTD retrieval</td>
  742. <td><strong>True</strong></td>
  743. <td>False</td>
  744. <td>False</td>
  745. <td><strong>True</strong></td>
  746. <td>false</td>
  747. <td>False (1)</td>
  748. <td>False</td>
  749. </tr>
  750. <tr><td>gzip bomb</td>
  751. <td>False</td>
  752. <td>False</td>
  753. <td>False</td>
  754. <td>False</td>
  755. <td><strong>True</strong></td>
  756. <td><strong>partly</strong> (2)</td>
  757. <td>False</td>
  758. </tr>
  759. <tr><td>xpath support (7)</td>
  760. <td>False</td>
  761. <td>False</td>
  762. <td>False</td>
  763. <td>False</td>
  764. <td>False</td>
  765. <td><strong>True</strong></td>
  766. <td>False</td>
  767. </tr>
  768. <tr><td>xsl(t) support (7)</td>
  769. <td>False</td>
  770. <td>False</td>
  771. <td>False</td>
  772. <td>False</td>
  773. <td>False</td>
  774. <td><strong>True</strong></td>
  775. <td>False</td>
  776. </tr>
  777. <tr><td>xinclude support (7)</td>
  778. <td>False</td>
  779. <td><strong>True</strong> (6)</td>
  780. <td>False</td>
  781. <td>False</td>
  782. <td>False</td>
  783. <td><strong>True</strong> (6)</td>
  784. <td><strong>True</strong></td>
  785. </tr>
  786. <tr><td>C library</td>
  787. <td>expat</td>
  788. <td>expat</td>
  789. <td>expat</td>
  790. <td>expat</td>
  791. <td>expat</td>
  792. <td>libxml2</td>
  793. <td>expat</td>
  794. </tr>
  795. </tbody>
  796. </table>
  797. <ol class="arabic simple">
  798. <li>Lxml is protected against billion laughs attacks and doesn't do network
  799. lookups by default.</li>
  800. <li>libxml2 and lxml are not directly vulnerable to gzip decompression bombs
  801. but they don't protect you against them either.</li>
  802. <li>xml.etree doesn't expand entities and raises a ParserError when an entity
  803. occurs.</li>
  804. <li>minidom doesn't expand entities and simply returns the unexpanded entity
  805. verbatim.</li>
  806. <li>genshi.input of genshi 0.6 doesn't support entity expansion and raises a
  807. ParserError when an entity occurs.</li>
  808. <li>Library has (limited) XInclude support but requires an additional step to
  809. process inclusion.</li>
  810. <li>These are features but they may introduce exploitable holes, see
  811. <a class="reference internal" href="#other-things-to-consider">Other things to consider</a></li>
  812. </ol>
  813. <div class="section" id="settings-in-standard-library">
  814. <h2><a class="toc-backref" href="#id10">Settings in standard library</a></h2>
  815. <div class="section" id="xml-sax-handler-features">
  816. <h3>xml.sax.handler Features</h3>
  817. <dl class="docutils">
  818. <dt>feature_external_ges (<a class="reference external" href="http://xml.org/sax/features/external-general-entities">http://xml.org/sax/features/external-general-entities</a>)</dt>
  819. <dd>disables external entity expansion</dd>
  820. <dt>feature_external_pes (<a class="reference external" href="http://xml.org/sax/features/external-parameter-entities">http://xml.org/sax/features/external-parameter-entities</a>)</dt>
  821. <dd>the option is ignored and doesn't modify any functionality</dd>
  822. </dl>
  823. </div>
  824. <div class="section" id="dom-xml-dom-xmlbuilder-options">
  825. <h3>DOM xml.dom.xmlbuilder.Options</h3>
  826. <dl class="docutils">
  827. <dt>external_parameter_entities</dt>
  828. <dd>ignored</dd>
  829. <dt>external_general_entities</dt>
  830. <dd>ignored</dd>
  831. <dt>external_dtd_subset</dt>
  832. <dd>ignored</dd>
  833. <dt>entities</dt>
  834. <dd>unsure</dd>
  835. </dl>
  836. </div>
  837. </div>
  838. </div>
  839. <div class="section" id="defusedxml">
  840. <h1><a class="toc-backref" href="#id11">defusedxml</a></h1>
  841. <p>The <a class="reference external" href="https://bitbucket.org/tiran/defusedxml">defusedxml package</a> (<a class="reference external" href="https://pypi.python.org/pypi/defusedxml">defusedxml on PyPI</a>)
  842. contains several Python-only workarounds and fixes
  843. for denial of service and other vulnerabilities in Python's XML libraries.
  844. In order to benefit from the protection you just have to import and use the
  845. listed functions / classes from the right defusedxml module instead of the
  846. original module. Merely <a class="reference internal" href="#defusedxml-xmlrpc">defusedxml.xmlrpc</a> is implemented as monkey patch.</p>
  847. <p>Instead of:</p>
  848. <pre class="literal-block">
  849. &gt;&gt;&gt; from xml.etree.ElementTree import parse
  850. &gt;&gt;&gt; et = parse(xmlfile)
  851. </pre>
  852. <p>alter code to:</p>
  853. <pre class="literal-block">
  854. &gt;&gt;&gt; from defusedxml.ElementTree import parse
  855. &gt;&gt;&gt; et = parse(xmlfile)
  856. </pre>
  857. <p>Additionally the package has an <strong>untested</strong> function to monkey patch
  858. all stdlib modules with <tt class="docutils literal">defusedxml.defuse_stdlib()</tt>.</p>
  859. <p>All functions and parser classes accept three additional keyword arguments.
  860. They return either the same objects as the original functions or compatible
  861. subclasses.</p>
  862. <dl class="docutils">
  863. <dt>forbid_dtd (default: False)</dt>
  864. <dd>disallow XML with a <tt class="docutils literal">&lt;!DOCTYPE&gt;</tt> processing instruction and raise a
  865. <em>DTDForbidden</em> exception when a DTD processing instruction is found.</dd>
  866. <dt>forbid_entities (default: True)</dt>
  867. <dd>disallow XML with <tt class="docutils literal">&lt;!ENTITY&gt;</tt> declarations inside the DTD and raise an
  868. <em>EntitiesForbidden</em> exception when an entity is declared.</dd>
  869. <dt>forbid_external (default: True)</dt>
  870. <dd>disallow any access to remote or local resources in external entities
  871. or DTD and raising an <em>ExternalReferenceForbidden</em> exception when a DTD
  872. or entity references an external resource.</dd>
  873. </dl>
  874. <div class="section" id="defusedxml-package">
  875. <h2><a class="toc-backref" href="#id12">defusedxml (package)</a></h2>
  876. <p>DefusedXmlException, DTDForbidden, EntitiesForbidden,
  877. ExternalReferenceForbidden, NotSupportedError</p>
  878. <p>defuse_stdlib() (<em>experimental</em>)</p>
  879. </div>
  880. <div class="section" id="defusedxml-celementtree">
  881. <h2><a class="toc-backref" href="#id13">defusedxml.cElementTree</a></h2>
  882. <p>parse(), iterparse(), fromstring(), XMLParser</p>
  883. </div>
  884. <div class="section" id="defusedxml-elementtree">
  885. <h2><a class="toc-backref" href="#id14">defusedxml.ElementTree</a></h2>
  886. <p>parse(), iterparse(), fromstring(), XMLParser</p>
  887. </div>
  888. <div class="section" id="defusedxml-expatreader">
  889. <h2><a class="toc-backref" href="#id15">defusedxml.expatreader</a></h2>
  890. <p>create_parser(), DefusedExpatParser</p>
  891. </div>
  892. <div class="section" id="defusedxml-sax">
  893. <h2><a class="toc-backref" href="#id16">defusedxml.sax</a></h2>
  894. <p>parse(), parseString(), create_parser()</p>
  895. </div>
  896. <div class="section" id="defusedxml-expatbuilder">
  897. <h2><a class="toc-backref" href="#id17">defusedxml.expatbuilder</a></h2>
  898. <p>parse(), parseString(), DefusedExpatBuilder, DefusedExpatBuilderNS</p>
  899. </div>
  900. <div class="section" id="defusedxml-minidom">
  901. <h2><a class="toc-backref" href="#id18">defusedxml.minidom</a></h2>
  902. <p>parse(), parseString()</p>
  903. </div>
  904. <div class="section" id="defusedxml-pulldom">
  905. <h2><a class="toc-backref" href="#id19">defusedxml.pulldom</a></h2>
  906. <p>parse(), parseString()</p>
  907. </div>
  908. <div class="section" id="defusedxml-xmlrpc">
  909. <h2><a class="toc-backref" href="#id20">defusedxml.xmlrpc</a></h2>
  910. <p>The fix is implemented as monkey patch for the stdlib's xmlrpc package (3.x)
  911. or xmlrpclib module (2.x). The function <cite>monkey_patch()</cite> enables the fixes,
  912. <cite>unmonkey_patch()</cite> removes the patch and puts the code in its former state.</p>
  913. <p>The monkey patch protects against XML related attacks as well as
  914. decompression bombs and excessively large requests or responses. The default
  915. setting is 30 MB for requests, responses and gzip decompression. You can
  916. modify the default by changing the module variable <cite>MAX_DATA</cite>. A value of
  917. <cite>-1</cite> disables the limit.</p>
  918. </div>
  919. <div class="section" id="defusedxml-lxml">
  920. <h2><a class="toc-backref" href="#id21">defusedxml.lxml</a></h2>
  921. <p>The module acts as an <em>example</em> how you could protect code that uses
  922. lxml.etree. It implements a custom Element class that filters out
  923. Entity instances, a custom parser factory and a thread local storage for
  924. parser instances. It also has a check_docinfo() function which inspects
  925. a tree for internal or external DTDs and entity declarations. In order to
  926. check for entities lxml &gt; 3.0 is required.</p>
  927. <p>parse(), fromstring()
  928. RestrictedElement, GlobalParserTLS, getDefaultParser(), check_docinfo()</p>
  929. </div>
  930. </div>
  931. <div class="section" id="defusedexpat">
  932. <h1><a class="toc-backref" href="#id22">defusedexpat</a></h1>
  933. <p>The <a class="reference external" href="https://bitbucket.org/tiran/defusedexpat">defusedexpat package</a> (<a class="reference external" href="https://pypi.python.org/pypi/defusedexpat">defusedexpat on PyPI</a>)
  934. comes with binary extensions and a
  935. <a class="reference external" href="https://bitbucket.org/tiran/expat">modified expat</a> libary instead of the standard <a class="reference external" href="http://expat.sourceforge.net/">expat parser</a>. It's
  936. basically a stand-alone version of the patches for Python's standard
  937. library C extensions.</p>
  938. <div class="section" id="modifications-in-expat">
  939. <h2><a class="toc-backref" href="#id23">Modifications in expat</a></h2>
  940. <p>new definitions:</p>
  941. <pre class="literal-block">
  942. XML_BOMB_PROTECTION
  943. XML_DEFAULT_MAX_ENTITY_INDIRECTIONS
  944. XML_DEFAULT_MAX_ENTITY_EXPANSIONS
  945. XML_DEFAULT_RESET_DTD
  946. </pre>
  947. <p>new XML_FeatureEnum members:</p>
  948. <pre class="literal-block">
  949. XML_FEATURE_MAX_ENTITY_INDIRECTIONS
  950. XML_FEATURE_MAX_ENTITY_EXPANSIONS
  951. XML_FEATURE_IGNORE_DTD
  952. </pre>
  953. <p>new XML_Error members:</p>
  954. <pre class="literal-block">
  955. XML_ERROR_ENTITY_INDIRECTIONS
  956. XML_ERROR_ENTITY_EXPANSION
  957. </pre>
  958. <p>new API functions:</p>
  959. <pre class="literal-block">
  960. int XML_GetFeature(XML_Parser parser,
  961. enum XML_FeatureEnum feature,
  962. long *value);
  963. int XML_SetFeature(XML_Parser parser,
  964. enum XML_FeatureEnum feature,
  965. long value);
  966. int XML_GetFeatureDefault(enum XML_FeatureEnum feature,
  967. long *value);
  968. int XML_SetFeatureDefault(enum XML_FeatureEnum feature,
  969. long value);
  970. </pre>
  971. <dl class="docutils">
  972. <dt>XML_FEATURE_MAX_ENTITY_INDIRECTIONS</dt>
  973. <dd><p class="first">Limit the amount of indirections that are allowed to occur during the
  974. expansion of a nested entity. A counter starts when an entity reference
  975. is encountered. It resets after the entity is fully expanded. The limit
  976. protects the parser against exponential entity expansion attacks (aka
  977. billion laughs attack). When the limit is exceeded the parser stops and
  978. fails with <cite>XML_ERROR_ENTITY_INDIRECTIONS</cite>.
  979. A value of 0 disables the protection.</p>
  980. <dl class="last docutils">
  981. <dt>Supported range</dt>
  982. <dd>0 .. UINT_MAX</dd>
  983. <dt>Default</dt>
  984. <dd>40</dd>
  985. </dl>
  986. </dd>
  987. <dt>XML_FEATURE_MAX_ENTITY_EXPANSIONS</dt>
  988. <dd><p class="first">Limit the total length of all entity expansions throughout the entire
  989. document. The lengths of all entities are accumulated in a parser variable.
  990. The setting protects against quadratic blowup attacks (lots of expansions
  991. of a large entity declaration). When the sum of all entities exceeds
  992. the limit, the parser stops and fails with <cite>XML_ERROR_ENTITY_EXPANSION</cite>.
  993. A value of 0 disables the protection.</p>
  994. <dl class="last docutils">
  995. <dt>Supported range</dt>
  996. <dd>0 .. UINT_MAX</dd>
  997. <dt>Default</dt>
  998. <dd>8 MiB</dd>
  999. </dl>
  1000. </dd>
  1001. <dt>XML_FEATURE_RESET_DTD</dt>
  1002. <dd><p class="first">Reset all DTD information after the &lt;!DOCTYPE&gt; block has been parsed. When
  1003. the flag is set (default: false) all DTD information after the
  1004. endDoctypeDeclHandler has been called. The flag can be set inside the
  1005. endDoctypeDeclHandler. Without DTD information any entity reference in
  1006. the document body leads to <cite>XML_ERROR_UNDEFINED_ENTITY</cite>.</p>
  1007. <dl class="last docutils">
  1008. <dt>Supported range</dt>
  1009. <dd>0, 1</dd>
  1010. <dt>Default</dt>
  1011. <dd>0</dd>
  1012. </dl>
  1013. </dd>
  1014. </dl>
  1015. </div>
  1016. </div>
  1017. <div class="section" id="how-to-avoid-xml-vulnerabilities">
  1018. <h1><a class="toc-backref" href="#id24">How to avoid XML vulnerabilities</a></h1>
  1019. <div class="section" id="best-practices">
  1020. <h2><a class="toc-backref" href="#id25">Best practices</a></h2>
  1021. <ul class="simple">
  1022. <li>Don't allow DTDs</li>
  1023. <li>Don't expand entities</li>
  1024. <li>Don't resolve externals</li>
  1025. <li>Limit parse depth</li>
  1026. <li>Limit total input size</li>
  1027. <li>Limit parse time</li>
  1028. <li>Favor a SAX or iterparse-like parser for potential large data</li>
  1029. <li>Validate and properly quote arguments to XSL transformations and
  1030. XPath queries</li>
  1031. <li>Don't use XPath expression from untrusted sources</li>
  1032. <li>Don't apply XSL transformations that come untrusted sources</li>
  1033. </ul>
  1034. <p>(based on Brad Hill's <a class="reference external" href="https://www.isecpartners.com/media/12976/iSEC-HILL-Attacking-XML-Security-bh07.pdf">Attacking XML Security</a>)</p>
  1035. </div>
  1036. </div>
  1037. <div class="section" id="other-things-to-consider">
  1038. <h1><a class="toc-backref" href="#id26">Other things to consider</a></h1>
  1039. <p>XML, XML parsers and processing libraries have more features and possible
  1040. issue that could lead to DoS vulnerabilities or security exploits in
  1041. applications. I have compiled an incomplete list of theoretical issues that
  1042. need further research and more attention. The list is deliberately pessimistic
  1043. and a bit paranoid, too. It contains things that might go wrong under daffy
  1044. circumstances.</p>
  1045. <div class="section" id="attribute-blowup-hash-collision-attack">
  1046. <h2><a class="toc-backref" href="#id27">attribute blowup / hash collision attack</a></h2>
  1047. <p>XML parsers may use an algorithm with quadratic runtime O(n <sup>2</sup>) to
  1048. handle attributes and namespaces. If it uses hash tables (dictionaries) to
  1049. store attributes and namespaces the implementation may be vulnerable to
  1050. hash collision attacks, thus reducing the performance to O(n <sup>2</sup>) again.
  1051. In either case an attacker is able to forge a denial of service attack with
  1052. an XML document that contains thousands upon thousands of attributes in
  1053. a single node.</p>
  1054. <p>I haven't researched yet if expat, pyexpat or libxml2 are vulnerable.</p>
  1055. </div>
  1056. <div class="section" id="decompression-bomb">
  1057. <h2><a class="toc-backref" href="#id28">decompression bomb</a></h2>
  1058. <p>The issue of decompression bombs (aka <a class="reference external" href="http://en.wikipedia.org/wiki/Zip_bomb">ZIP bomb</a>) apply to all XML libraries
  1059. that can parse compressed XML stream like gzipped HTTP streams or LZMA-ed
  1060. files. For an attacker it can reduce the amount of transmitted data by three
  1061. magnitudes or more. Gzip is able to compress 1 GiB zeros to roughly 1 MB,
  1062. lzma is even better:</p>
  1063. <pre class="literal-block">
  1064. $ dd if=/dev/zero bs=1M count=1024 | gzip &gt; zeros.gz
  1065. $ dd if=/dev/zero bs=1M count=1024 | lzma -z &gt; zeros.xy
  1066. $ ls -sh zeros.*
  1067. 1020K zeros.gz
  1068. 148K zeros.xy
  1069. </pre>
  1070. <p>None of Python's standard XML libraries decompress streams except for
  1071. <tt class="docutils literal">xmlrpclib</tt>. The module is vulnerable &lt;<a class="reference external" href="http://bugs.python.org/issue16043">http://bugs.python.org/issue16043</a>&gt;
  1072. to decompression bombs.</p>
  1073. <p>lxml can load and process compressed data through libxml2 transparently.
  1074. libxml2 can handle even very large blobs of compressed data efficiently
  1075. without using too much memory. But it doesn't protect applications from
  1076. decompression bombs. A carefully written SAX or iterparse-like approach can
  1077. be safe.</p>
  1078. </div>
  1079. <div class="section" id="processing-instruction">
  1080. <h2><a class="toc-backref" href="#id29">Processing Instruction</a></h2>
  1081. <p><a class="reference external" href="https://en.wikipedia.org/wiki/Processing_Instruction">PI</a>'s like:</p>
  1082. <pre class="literal-block">
  1083. &lt;?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;style.xsl&quot;?&gt;
  1084. </pre>
  1085. <p>may impose more threats for XML processing. It depends if and how a
  1086. processor handles processing instructions. The issue of URL retrieval with
  1087. network or local file access apply to processing instructions, too.</p>
  1088. </div>
  1089. <div class="section" id="other-dtd-features">
  1090. <h2><a class="toc-backref" href="#id30">Other DTD features</a></h2>
  1091. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Document_Type_Definition">DTD</a> has more features like <tt class="docutils literal">&lt;!NOTATION&gt;</tt>. I haven't researched how
  1092. these features may be a security threat.</p>
  1093. </div>
  1094. <div class="section" id="xpath">
  1095. <h2><a class="toc-backref" href="#id31">XPath</a></h2>
  1096. <p>XPath statements may introduce DoS vulnerabilities. Code should never execute
  1097. queries from untrusted sources. An attacker may also be able to create a XML
  1098. document that makes certain XPath queries costly or resource hungry.</p>
  1099. </div>
  1100. <div class="section" id="xpath-injection-attacks">
  1101. <h2><a class="toc-backref" href="#id32">XPath injection attacks</a></h2>
  1102. <p>XPath injeciton attacks pretty much work like SQL injection attacks.
  1103. Arguments to XPath queries must be quoted and validated properly, especially
  1104. when they are taken from the user. The page <a class="reference external" href="http://www.ibm.com/developerworks/xml/library/x-xpathinjection/index.html">Avoid the dangers of XPath injection</a>
  1105. list some ramifications of XPath injections.</p>
  1106. <p>Python's standard library doesn't have XPath support. Lxml supports
  1107. parameterized XPath queries which does proper quoting. You just have to use
  1108. its xpath() method correctly:</p>
  1109. <pre class="literal-block">
  1110. # DON'T
  1111. &gt;&gt;&gt; tree.xpath(&quot;/tag[&#64;id='%s']&quot; % value)
  1112. # instead do
  1113. &gt;&gt;&gt; tree.xpath(&quot;/tag[&#64;id=$tagid]&quot;, tagid=name)
  1114. </pre>
  1115. </div>
  1116. <div class="section" id="xinclude">
  1117. <h2><a class="toc-backref" href="#id33">XInclude</a></h2>
  1118. <p><a class="reference external" href="http://www.w3.org/TR/xinclude/#include_element">XML Inclusion</a> is another way to load and include external files:</p>
  1119. <pre class="literal-block">
  1120. &lt;root xmlns:xi=&quot;http://www.w3.org/2001/XInclude&quot;&gt;
  1121. &lt;xi:include href=&quot;filename.txt&quot; parse=&quot;text&quot; /&gt;
  1122. &lt;/root&gt;
  1123. </pre>
  1124. <p>This feature should be disabled when XML files from an untrusted source are
  1125. processed. Some Python XML libraries and libxml2 support XInclude but don't
  1126. have an option to sandbox inclusion and limit it to allowed directories.</p>
  1127. </div>
  1128. <div class="section" id="xmlschema-location">
  1129. <h2><a class="toc-backref" href="#id34">XMLSchema location</a></h2>
  1130. <p>A validating XML parser may download schema files from the information in a
  1131. <tt class="docutils literal">xsi:schemaLocation</tt> attribute.</p>
  1132. <pre class="literal-block">
  1133. &lt;ead xmlns=&quot;urn:isbn:1-931666-22-9&quot;
  1134. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  1135. xsi:schemaLocation=&quot;urn:isbn:1-931666-22-9 http://www.loc.gov/ead/ead.xsd&quot;&gt;
  1136. &lt;/ead&gt;
  1137. </pre>
  1138. </div>
  1139. <div class="section" id="xsl-transformation">
  1140. <h2><a class="toc-backref" href="#id35">XSL Transformation</a></h2>
  1141. <p>You should keep in mind that XSLT is a Turing complete language. Never
  1142. process XSLT code from unknown or untrusted source! XSLT processors may
  1143. allow you to interact with external resources in ways you can't even imagine.
  1144. Some processors even support extensions that allow read/write access to file
  1145. system, access to JRE objects or scripting with Jython.</p>
  1146. <p>Example from <a class="reference external" href="https://www.isecpartners.com/media/12976/iSEC-HILL-Attacking-XML-Security-bh07.pdf">Attacking XML Security</a> for Xalan-J:</p>
  1147. <pre class="literal-block">
  1148. &lt;xsl:stylesheet version=&quot;1.0&quot;
  1149. xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
  1150. xmlns:rt=&quot;http://xml.apache.org/xalan/java/java.lang.Runtime&quot;
  1151. xmlns:ob=&quot;http://xml.apache.org/xalan/java/java.lang.Object&quot;
  1152. exclude-result-prefixes= &quot;rt ob&quot;&gt;
  1153. &lt;xsl:template match=&quot;/&quot;&gt;
  1154. &lt;xsl:variable name=&quot;runtimeObject&quot; select=&quot;rt:getRuntime()&quot;/&gt;
  1155. &lt;xsl:variable name=&quot;command&quot;
  1156. select=&quot;rt:exec($runtimeObject, &amp;apos;c:\Windows\system32\cmd.exe&amp;apos;)&quot;/&gt;
  1157. &lt;xsl:variable name=&quot;commandAsString&quot; select=&quot;ob:toString($command)&quot;/&gt;
  1158. &lt;xsl:value-of select=&quot;$commandAsString&quot;/&gt;
  1159. &lt;/xsl:template&gt;
  1160. &lt;/xsl:stylesheet&gt;
  1161. </pre>
  1162. </div>
  1163. </div>
  1164. <div class="section" id="related-cves">
  1165. <h1><a class="toc-backref" href="#id36">Related CVEs</a></h1>
  1166. <dl class="docutils">
  1167. <dt>CVE-2013-1664</dt>
  1168. <dd>Unrestricted entity expansion induces DoS vulnerabilities in Python XML
  1169. libraries (XML bomb)</dd>
  1170. <dt>CVE-2013-1665</dt>
  1171. <dd>External entity expansion in Python XML libraries inflicts potential
  1172. security flaws and DoS vulnerabilities</dd>
  1173. </dl>
  1174. </div>
  1175. <div class="section" id="other-languages-frameworks">
  1176. <h1><a class="toc-backref" href="#id37">Other languages / frameworks</a></h1>
  1177. <p>Several other programming languages and frameworks are vulnerable as well. A
  1178. couple of them are affected by the fact that libxml2 up to 2.9.0 has no
  1179. protection against quadratic blowup attacks. Most of them have potential
  1180. dangerous default settings for entity expansion and external entities, too.</p>
  1181. <div class="section" id="perl">
  1182. <h2><a class="toc-backref" href="#id38">Perl</a></h2>
  1183. <p>Perl's XML::Simple is vulnerable to quadratic entity expansion and external
  1184. entity expansion (both local and remote).</p>
  1185. </div>
  1186. <div class="section" id="ruby">
  1187. <h2><a class="toc-backref" href="#id39">Ruby</a></h2>
  1188. <p>Ruby's REXML document parser is vulnerable to entity expansion attacks
  1189. (both quadratic and exponential) but it doesn't do external entity
  1190. expansion by default. In order to counteract entity expansion you have to
  1191. disable the feature:</p>
  1192. <pre class="literal-block">
  1193. REXML::Document.entity_expansion_limit = 0
  1194. </pre>
  1195. <p>libxml-ruby and hpricot don't expand entities in their default configuration.</p>
  1196. </div>
  1197. <div class="section" id="php">
  1198. <h2><a class="toc-backref" href="#id40">PHP</a></h2>
  1199. <p>PHP's SimpleXML API is vulnerable to quadratic entity expansion and loads
  1200. entites from local and remote resources. The option <tt class="docutils literal">LIBXML_NONET</tt> disables
  1201. network access but still allows local file access. <tt class="docutils literal">LIBXML_NOENT</tt> seems to
  1202. have no effect on entity expansion in PHP 5.4.6.</p>
  1203. </div>
  1204. <div class="section" id="c-net-mono">
  1205. <h2><a class="toc-backref" href="#id41">C# / .NET / Mono</a></h2>
  1206. <p>Information in <a class="reference external" href="http://msdn.microsoft.com/en-us/magazine/ee335713.aspx">XML DoS and Defenses (MSDN)</a> suggest that .NET is
  1207. vulnerable with its default settings. The article contains code snippets
  1208. how to create a secure XML reader:</p>
  1209. <pre class="literal-block">
  1210. XmlReaderSettings settings = new XmlReaderSettings();
  1211. settings.ProhibitDtd = false;
  1212. settings.MaxCharactersFromEntities = 1024;
  1213. settings.XmlResolver = null;
  1214. XmlReader reader = XmlReader.Create(stream, settings);
  1215. </pre>
  1216. </div>
  1217. <div class="section" id="java">
  1218. <h2><a class="toc-backref" href="#id42">Java</a></h2>
  1219. <p>Untested. The documentation of Xerces and its <a class="reference external" href="http://xerces.apache.org/xerces2-j/javadocs/xerces2/org/apache/xerces/util/SecurityManager.html">Xerces SecurityMananger</a>
  1220. sounds like Xerces is also vulnerable to billion laugh attacks with its
  1221. default settings. It also does entity resolving when an
  1222. <tt class="docutils literal">org.xml.sax.EntityResolver</tt> is configured. I'm not yet sure about the
  1223. default setting here.</p>
  1224. <p>Java specialists suggest to have a custom builder factory:</p>
  1225. <pre class="literal-block">
  1226. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  1227. builderFactory.setXIncludeAware(False);
  1228. builderFactory.setExpandEntityReferences(False);
  1229. builderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, True);
  1230. # either
  1231. builderFactory.setFeature(&quot;http://apache.org/xml/features/disallow-doctype-decl&quot;, True);
  1232. # or if you need DTDs
  1233. builderFactory.setFeature(&quot;http://xml.org/sax/features/external-general-entities&quot;, False);
  1234. builderFactory.setFeature(&quot;http://xml.org/sax/features/external-parameter-entities&quot;, False);
  1235. builderFactory.setFeature(&quot;http://apache.org/xml/features/nonvalidating/load-external-dtd&quot;, False);
  1236. builderFactory.setFeature(&quot;http://apache.org/xml/features/nonvalidating/load-dtd-grammar&quot;, False);
  1237. </pre>
  1238. </div>
  1239. </div>
  1240. <div class="section" id="todo">
  1241. <h1><a class="toc-backref" href="#id43">TODO</a></h1>
  1242. <ul class="simple">
  1243. <li>DOM: Use xml.dom.xmlbuilder options for entity handling</li>
  1244. <li>SAX: take feature_external_ges and feature_external_pes (?) into account</li>
  1245. <li>test experimental monkey patching of stdlib modules</li>
  1246. <li>improve documentation</li>
  1247. </ul>
  1248. </div>
  1249. <div class="section" id="license">
  1250. <h1><a class="toc-backref" href="#id44">License</a></h1>
  1251. <p>Copyright (c) 2013-2017 by Christian Heimes &lt;<a class="reference external" href="mailto:christian&#64;python.org">christian&#64;python.org</a>&gt;</p>
  1252. <p>Licensed to PSF under a Contributor Agreement.</p>
  1253. <p>See <a class="reference external" href="http://www.python.org/psf/license">http://www.python.org/psf/license</a> for licensing details.</p>
  1254. </div>
  1255. <div class="section" id="acknowledgements">
  1256. <h1><a class="toc-backref" href="#id45">Acknowledgements</a></h1>
  1257. <dl class="docutils">
  1258. <dt>Brett Cannon (Python Core developer)</dt>
  1259. <dd>review and code cleanup</dd>
  1260. <dt>Antoine Pitrou (Python Core developer)</dt>
  1261. <dd>code review</dd>
  1262. <dt>Aaron Patterson, Ben Murphy and Michael Koziarski (Ruby community)</dt>
  1263. <dd>Many thanks to Aaron, Ben and Michael from the Ruby community for their
  1264. report and assistance.</dd>
  1265. <dt>Thierry Carrez (OpenStack)</dt>
  1266. <dd>Many thanks to Thierry for his report to the Python Security Response
  1267. Team on behalf of the OpenStack security team.</dd>
  1268. <dt>Carl Meyer (Django)</dt>
  1269. <dd>Many thanks to Carl for his report to PSRT on behalf of the Django security
  1270. team.</dd>
  1271. <dt>Daniel Veillard (libxml2)</dt>
  1272. <dd>Many thanks to Daniel for his insight and assistance with libxml2.</dd>
  1273. <dt>semantics GmbH (<a class="reference external" href="http://www.semantics.de/">http://www.semantics.de/</a>)</dt>
  1274. <dd>Many thanks to my employer semantics for letting me work on the issue
  1275. during working hours as part of semantics's open source initiative.</dd>
  1276. </dl>
  1277. </div>
  1278. <div class="section" id="references">
  1279. <h1><a class="toc-backref" href="#id46">References</a></h1>
  1280. <ul class="simple">
  1281. <li><a class="reference external" href="http://msdn.microsoft.com/en-us/magazine/ee335713.aspx">XML DoS and Defenses (MSDN)</a></li>
  1282. <li><a class="reference external" href="http://en.wikipedia.org/wiki/Billion_laughs">Billion Laughs</a> on Wikipedia</li>
  1283. <li><a class="reference external" href="http://en.wikipedia.org/wiki/Zip_bomb">ZIP bomb</a> on Wikipedia</li>
  1284. <li><a class="reference external" href="http://www.ibm.com/developerworks/xml/library/x-tipcfsx/index.html">Configure SAX parsers for secure processing</a></li>
  1285. <li><a class="reference external" href="https://www.owasp.org/index.php/Testing_for_XML_Injection_(OWASP-DV-008)">Testing for XML Injection</a></li>
  1286. </ul>
  1287. </div>
  1288. <div class="section" id="changelog">
  1289. <h1><a class="toc-backref" href="#id47">Changelog</a></h1>
  1290. <div class="section" id="defusedxml-0-5-0-rc1">
  1291. <h2><a class="toc-backref" href="#id48">defusedxml 0.5.0.rc1</a></h2>
  1292. <p><em>Release date: 28-Jan-2017</em></p>
  1293. <ul class="simple">
  1294. <li>Add compatibility with Python 3.6</li>
  1295. <li>Drop support for Python 2.6, 3.1, 3.2, 3.3</li>
  1296. <li>Fix lxml tests (XMLSyntaxError: Detected an entity reference loop)</li>
  1297. </ul>
  1298. </div>
  1299. <div class="section" id="defusedxml-0-4-1">
  1300. <h2><a class="toc-backref" href="#id49">defusedxml 0.4.1</a></h2>
  1301. <p><em>Release date: 28-Mar-2013</em></p>
  1302. <ul class="simple">
  1303. <li>Add more demo exploits, e.g. python_external.py and Xalan XSLT demos.</li>
  1304. <li>Improved documentation.</li>
  1305. </ul>
  1306. </div>
  1307. <div class="section" id="defusedxml-0-4">
  1308. <h2><a class="toc-backref" href="#id50">defusedxml 0.4</a></h2>
  1309. <p><em>Release date: 25-Feb-2013</em></p>
  1310. <ul class="simple">
  1311. <li>As per <a class="reference external" href="http://seclists.org/oss-sec/2013/q1/340">http://seclists.org/oss-sec/2013/q1/340</a> please REJECT
  1312. CVE-2013-0278, CVE-2013-0279 and CVE-2013-0280 and use CVE-2013-1664,
  1313. CVE-2013-1665 for OpenStack/etc.</li>
  1314. <li>Add missing parser_list argument to sax.make_parser(). The argument is
  1315. ignored, though. (thanks to Florian Apolloner)</li>
  1316. <li>Add demo exploit for external entity attack on Python's SAX parser, XML-RPC
  1317. and WebDAV.</li>
  1318. </ul>
  1319. </div>
  1320. <div class="section" id="defusedxml-0-3">
  1321. <h2><a class="toc-backref" href="#id51">defusedxml 0.3</a></h2>
  1322. <p><em>Release date: 19-Feb-2013</em></p>
  1323. <ul class="simple">
  1324. <li>Improve documentation</li>
  1325. </ul>
  1326. </div>
  1327. <div class="section" id="defusedxml-0-2">
  1328. <h2><a class="toc-backref" href="#id52">defusedxml 0.2</a></h2>
  1329. <p><em>Release date: 15-Feb-2013</em></p>
  1330. <ul class="simple">
  1331. <li>Rename ExternalEntitiesForbidden to ExternalReferenceForbidden</li>
  1332. <li>Rename defusedxml.lxml.check_dtd() to check_docinfo()</li>
  1333. <li>Unify argument names in callbacks</li>
  1334. <li>Add arguments and formatted representation to exceptions</li>
  1335. <li>Add forbid_external argument to all functions and classs</li>
  1336. <li>More tests</li>
  1337. <li>LOTS of documentation</li>
  1338. <li>Add example code for other languages (Ruby, Perl, PHP) and parsers (Genshi)</li>
  1339. <li>Add protection against XML and gzip attacks to xmlrpclib</li>
  1340. </ul>
  1341. </div>
  1342. <div class="section" id="defusedxml-0-1">
  1343. <h2><a class="toc-backref" href="#id53">defusedxml 0.1</a></h2>
  1344. <p><em>Release date: 08-Feb-2013</em></p>
  1345. <ul class="simple">
  1346. <li>Initial and internal release for PSRT review</li>
  1347. </ul>
  1348. </div>
  1349. </div>
  1350. </div>
  1351. </body>
  1352. </html>