Technical explanation of The MySpace Worm
Also called the “Samy worm” or “JS.Spacehero worm”
Click here to read the entertaining story of the development, release, and ensued hilarity of The MySpace Worm
Full source code of worm at bottom.
Please note that this code and explanation was only released AFTER MySpace resolved this.
None of this would work on MySpace at the time it was released and it will not work now. Otherwise, there would have been mayhem.
Now, let’s talk more about the problems encountered, workarounds, and how it worked in general.
- Myspace blocks a lot of tags. In fact, they only seem to allow
<div>s…maybe a few others (
I think). They wouldn’t allow
- We couldn’t use quotes within the div because we had already used up single quotes and double quotes already. This made coding JS very difficult. In order to get around it, we used an expression to store the JS and then executed it by name.
<div id="mycode" expr="alert('hah!')" style="background:url('java\nscript:eval(document.all.mycode.expr)')">
- In order to post the code to the user’s profile who is viewing it, we need to actually get the source of the page. Ah, we can use
document.body.innerHTMLin order to get the page source which includes, in only one spot, the ID of the user viewing the page. Myspace gets me again and strips out the word “innerHTML” anywhere. To avoid this, we use an eval() to evaluate two strings and put them together to form “innerHTML”.
alert(eval('document.body.inne' + 'rHTML'));
- Time to actually access other pages. We would use iframes, but usually (even when hidden), iframes aren’t as useful and are more obvious to the user that “something else” is going on. So, we use AJAX (XML-HTTP) in order for the actual client to make HTTP GETs and POSTs to pages. However, myspace strips out the word “onreadystatechange” which is necessary for XML-HTTP requests. Again, we can use an eval to evade this. Another plus to XML-HTTP is that the necessary cookies required to perform actions on myspace are passed along without any hassle.
eval('xmlhttp.onread' 'ystatechange = callback');
- Time to perform a GET on the user’s profile so that we can get their current list of heroes. We don’t want to remove any heroes, we just want to append myself to their pre-existing list of heroes. If we GET their profile, we can grab their heroes and store it for later. With all the above figured out, this is simple with an XML-HTTP request except that we have to get the friend ID of the actual user viewing a profile. Like we said above, we can do this by grabbing the source of the page we’re on. However, now we need to perform a search in the page for a specific word to find it. So we perform this search, however if we do this, we may end up finding our actual code since it contains the same exact word we’re looking for…because saying “if this page contains ‘foo’, do this”, that will always return true because it can always find foo within the actual code that does the searching. Another eval() with a combination of strings avoids this problem.
var index = html.indexOf('frien' 'dID');
- At this point, we have the list of heroes. First, let’s add me as a friend by performing an XML-HTTP POST on the addFriends page. Oh no, this doesn’t work! Why not? We’re on profile.myspace.com, however the POSTing needs to be done on www.myspace.com. No big deal, however XML-HTTP won’t allow GETs/POSTs to sites with a different domain name. To get around this, let’s actually go to the same URL but on www.myspace.com. You can still view profiles from www.myspace.com, so reloading the page on the domain we want to be on allows us to do the POST.
Finally we can do a POST! However, when we send the post it never actually adds a friend. Why not? Myspace generates a random hash on a pre-POST page (for example, the “Are you sure you want to add this user as a friend” page). If this hash is not passed along with the POST, the POST is not successful. To get around this, we mimic a browser and send a GET to the page right before adding the user, parse the source for the hash, then perform the POST while passing the hash.
escape()function doesn’t escape everything necessary so we’ll need to manually do some replacing here in order to get the necessary data escaped. We add a little “but most of all, samy is my hero.” to the mix, append all the code right after, and voila. We have self-reproducing code, a worm if you will.
Other limits, such as a maximum length, imposed other problems and required tight code, no spaces, obfuscated names, reusable functions, etc…
There were a few other complications and things to get around. This was not by any means a straight forward process, and none of this was meant to cause any damage or piss anyone off. This was in the interest of…interest. It was interesting and fun!
And in the end, there was code:
Back to The MySpace Worm story