tags:

views:

220

answers:

3
<!DOCTYPE root [
 <!ENTITY ha "Ha !">
 <!ENTITY ha2 "&ha; &ha;">
 <!ENTITY ha3 "&ha2; &ha2;">
 <!ENTITY ha4 "&ha3; &ha3;">
 <!ENTITY ha5 "&ha4; &ha4;">
 ...
 <!ENTITY ha128 "&ha127; &ha127;">
 ]>
 <root>&ha128;</root>

supposedly this is called a billion laughs DoS attack.

does anyone know how it works?

+1  A: 

It writes "Ha !" 2128 times.

zneak
how does it do that?
I__
@zneak: how does it do that?
I__
@I__: The entity `` expands to two entities ``, which in turn expand to four ``, which in turn expand to eight ``, ... until it expands to thousands of millions of billions of ``, which resolves to `Ha !`.
zneak
can you show me a tutorial that explains how that works?
I__
so how do people protect themselves against that? you mean if you put that into an html file and have someone open it, it will crash their computer or what?
I__
@zneak: ahahhaahhahaha
I__
Nitpick: It would write "Ha !" 2^127 times, the string would have a theoretical total length of 2^129 characters.
0xA3
@I__: I dunno. I don't want to try. Opening something like that will most certainly hang the program, and probably the whole computer, before the kernel decides there's really really no more memory available, and kills it.
zneak
are you chicken?
I__
+7  A: 

One of the XML bombs - http://msdn.microsoft.com/en-us/magazine/ee335713.aspx

An attacker can now take advantage of these three properties of XML (substitution entities, nested entities, and inline DTDs) to craft a malicious XML bomb. The attacker writes an XML document with nested entities just like the previous example, but instead of nesting just one level deep, he nests his entities many levels deep...

There is also code to protect from these "bombs" (in .NET world):

XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.MaxCharactersFromEntities = 1024;
XmlReader reader = XmlReader.Create(stream, settings);
Andrei Taptunov
+5  A: 

<!ENTITY ha "Ha !"> defines an entity, &ha; that expands to "Ha !". The next line defines another entity, &ha2; that expands to "&ha; &ha;" and eventually, "Ha ! Ha !".

&ha3; turns into Ha ! Ha ! Ha ! Ha !, and so on, doubling the number each time. If you follow the pattern, &haN; is "Ha !", 2N-1 times, so &ha128, expands to 2127 "Ha !"s, which is too big for any computer to handle.

Matthew Crumley
Oops, I guess I divided by two instead of subtracting one.
Matthew Crumley