Flash XSS mega nz
A Flash file on Mega.co.nz passed a user-controlled parameter directly into an ExternalInterface.call, allowing JavaScript injection. An attacker could execute arbitrary JavaScript in the context of mega.co.nz by crafting a URL to the SWF.
TwitterLinkedIn
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Mega.co.nz launched a Bug Bounty Program, and Frans Rosén set out to look for issues in it. The write-up covers the researcher's look at Mega's Flash files, in particular downloader.swf, which he tried to inject flashparams into to simulate a download, and OneClipboard.swf, which takes a filename and saves it into the clipboard.
Setup
The work was done by browsing Mega.co.nz and reading its source code. The main tool was JP ActionScript Decompiler (ASDec), used to examine the source of the SWF files. Testing was done in Safari and Chrome, because the download behaviour differed between them.
Finding it
Early in the digging, the researcher noticed that Mega had hijacked the alert function. This is visible by typing the following into the address field while visiting Mega.co.nz:
javascript:alert(/XSS/)
The confirm and prompt functions are not affected. The download behaviour also differed by browser: in Chrome the download happened automatically, but in Safari a flash segment was visible with a "Save File" link.
Going through the source code, the researcher started checking whether flashparams could be injected into the flash file downloader.swf to simulate a download. ASDec made it possible to examine the SWF source, but nothing insecure was found in that file. While searching for which flashparams were used for this SWF, another include caught the attention: a SWF that gets the filename and saves it into the clipboard. That file was downloaded and opened with ASDec.
Inside the constructor there was an ExternalInterface.call, and the flashparam called id was sent directly back to the function clipboardcopyready. A local test file simulating the initiation of OneClipboard.swf confirmed that the variable came back to the JS function.
The next question was how to inject code directly into the SWF without modifying any JS function. The answer came from an article by Soroush Dalili on ExternalInterface.call() JavaScript injection: an input to ExternalInterface.call combined with a try-catch can trigger your own JavaScript if the function being called does not exist.
Exploitation
Step 1: call the SWF with an injected id
In Safari, the researcher opened the following address:
https://mega.co.nz/OneClipboard.swf?id=%5C%22));%7Dcatch(e)%7Balert(document.domain)%7D//
The id parameter is URL-encoded. The injection worked and the JavaScript executed. The technique came from Soroush Dalili's article on ExternalInterface.call() JavaScript injection, where a try-catch can trigger your own JavaScript if the called function does not exist.
Step 2: report and observe the fix
At 10:16 PM GMT+1 the report was sent to Mega. The researcher then tried the vulnerability in other browsers, but it suddenly stopped working. Going back to Safari, it did not work there either: the file had been removed. A second email was sent to Mega at 10:53 saying that the vulnerability was gone. Five minutes later a reply arrived.
The file was removed in 35 minutes, which the researcher describes as the fastest fix he had seen, and a really nice bounty. Later that night the SWF was replaced with a fixed one that removed the id being sent back to the JS function.
Step 3: the earlier report
Mega's blog had a recent entry explaining the different levels of injections they had and what had been found during the week. It showed that another SWF issue had already been reported, on a SWF called ZeroClipboard.swf, almost the same as the one found here. Since the injection still worked, it looked like Mega had replaced their old third-party solution for the clipboard saver with a self-written one that had the same vulnerability.
Two days later, after some buzz around Mega's Bug Bounty Program and an interview by Adam Crouchley, Kim wrote to the researcher on Twitter.
Root cause
The OneClipboard.swf constructor passed the flashparam id straight into an ExternalInterface.call that targeted the JS function clipboardcopyready. When that function does not exist, the call throws, and an attacker who controls id can supply a catch block that runs their own JavaScript. The same class of bug had already appeared in the third-party ZeroClipboard.swf; replacing it with a self-written SWF kept the vulnerable pattern.
Impact
The payload executed JavaScript in the context of mega.co.nz, as demonstrated by alert(document.domain). The report does not describe any further actions taken with the execution, and it does not state which users or sessions could be reached beyond the researcher's own browser test.
Fix
The vulnerable file was removed within 35 minutes of the report, and later that night Mega replaced the SWF with a fixed one that no longer sends id back to the JS function.
Variants to test
- Look for other SWF files that pass request parameters into
ExternalInterface.call, especially where the called JS function may not exist. A positive result is your own JavaScript running when the parameter is crafted with a closing sequence and acatchblock. - Check whether a third-party clipboard or download SWF was replaced by an in-house one. A positive result is the same injection pattern surviving the replacement.
- Test the same flashparam injection in several browsers, since behaviour differed between Chrome and Safari here. A positive result is execution in at least one browser where the SWF is loaded.
What happened, in plain words
Mega.co.nz is a file-sharing website, a bit like an online locker where you store files and download them again later. In early 2013 Mega started a Bug Bounty Program: a scheme where a company pays people for finding security mistakes in its website and telling it privately, instead of keeping quiet or abusing them. Frans Rosén decided to look for such mistakes.
What he found was a way to make Mega's own website run JavaScript code that he wrote. JavaScript is the programming language that runs inside your browser and makes web pages interactive. If an attacker can make a site run their JavaScript, that code runs with the site's own permissions, which is the whole idea behind a class of bug called Cross-Site Scripting, or XSS. The trick here did not live in a normal web page but in a small Flash file. Flash was a browser plug-in (a helper program that added features to the browser) that could play animations, but also do things like copy text to your clipboard. The vulnerable file was called OneClipboard.swf, and it passed a flashparam called id into JavaScript in an unsafe way.
He reported it, Mega removed the file within 35 minutes, and he received a bounty. This is the story of how he got there, step by step, explained for someone who has never done security work before.
The target and the tools
The write-up focuses on two parts of Mega's website: how it handles downloads, and how it handles the clipboard (the temporary storage your computer uses when you copy and paste). Two Flash files are involved:
downloader.swf, which simulates a download.OneClipboard.swf, which takes a filename and saves it into your clipboard.
A .swf file is a Flash movie. It is a program that the Flash plug-in runs inside your browser. Like any program, it has source code, and that code can be read with the right tool. The main tool used here was JP ActionScript Decompiler, shortened to ASDec. A decompiler takes a compiled program and turns it back into something close to readable source code, the way a recipe can be reconstructed from a finished cake. ActionScript is the programming language Flash files are written in.
Testing was done in Safari and Chrome, because the download behaviour differed between the two browsers.
Finding it
A hijacked alert
Early in the digging, the researcher noticed that Mega had hijacked the alert function. alert is the browser's built-in way of showing a small pop-up box with a message. Websites can replace it with their own version, so that when any code calls alert, something else happens instead. You can see this yourself by typing the following into the address field while visiting Mega.co.nz:
javascript:alert(/XSS/)
Line by line: javascript: is a special prefix that tells the browser "run what follows as JavaScript instead of treating it as a web address". alert(...) is the call to the pop-up function. /XSS/ is a regular expression, a pattern-matching notation, used here simply as the text to display. Because Mega replaced the function, you see its own behaviour instead. The confirm and prompt functions, two other built-in pop-up functions, were not affected.
This matters because it shows the site was already customising the browser environment, and it is a hint that the site's JavaScript deserves a close look.
Different browsers, different downloads
The download behaviour also differed by browser. In Chrome the download happened automatically, but in Safari a flash segment was visible with a "Save File" link.
Reading the download file
Going through the source code, the researcher started checking whether flashparams could be injected into the flash file downloader.swf to simulate a download. A flashparam is simply a value passed to a Flash file through its address, the same way a web page can receive values after a question mark in its URL. "Injecting" means putting in a value the programmer did not expect, to see what the program does with it.
ASDec made it possible to examine the SWF source, but nothing insecure was found in that file. That is a normal outcome: most files you open turn out to be fine, and the point of reading them is to rule things out and to learn the patterns the developers use.
A second file catches the eye
While searching for which flashparams were used for this SWF, another include caught the attention: a SWF that gets the filename and saves it into the clipboard. That file was downloaded and opened with ASDec.
Inside the constructor there was an ExternalInterface.call. Two concepts need unpacking here. A constructor is the part of a program that runs first, when the object is created, a bit like the setup instructions at the start of a recipe. ExternalInterface.call is a Flash feature that lets a Flash file call a JavaScript function living in the web page around it. It is the bridge between the plug-in and the page.
The flashparam called id was sent directly back to the function clipboardcopyready. A local test file simulating the initiation of OneClipboard.swf confirmed that the variable came back to the JS function. Building a small test page on your own machine is a standard way to check your understanding of a piece of code before trying anything against the real site.
The trick that turns a value into code
The next question was how to inject code directly into the SWF without modifying any JS function. The answer came from an article by Soroush Dalili on ExternalInterface.call() JavaScript injection: an input to ExternalInterface.call combined with a try-catch can trigger your own JavaScript if the function being called does not exist.
A try-catch is a common programming structure for handling errors. You put risky instructions in the try block, and if something goes wrong, the program jumps to the catch block instead of crashing. The comparison is a safety net under a trapeze artist: if the jump fails, the net catches them. The subtle part is that the catch block is itself code, so if an attacker can supply the text of that block, the attacker's code runs exactly when an error happens. And an error is easy to cause: just call a JavaScript function that does not exist.
Exploitation
Step 1: call the SWF with an injected id
In Safari, the researcher opened the following address:
https://mega.co.nz/OneClipboard.swf?id=%5C%22));%7Dcatch(e)%7Balert(document.domain)%7D//
Line by line, in plain words. The address points at the Flash file OneClipboard.swf on mega.co.nz, and passes it one flashparam named id. The value after id= is URL-encoded, which means special characters are written in a percent form so they survive being put in an address. Decoding it gives the following pieces:
%5C%22decodes to a backslash followed by a double quote. The quote ends the text string the program was building, and the backslash is there to make sure the quote is treated as a real character rather than as part of the encoding.));closes the function call and the surrounding bracket, so the program's own instruction is finished early.%7Ddecodes to}, which closes the block of code.catch(e)starts an error handler, the safety net described above.%7Bdecodes to{, opening the block that the error handler will run.alert(document.domain)is the researcher's own JavaScript.document.domainis the browser's way of naming the site the page belongs to, so the pop-up proves which site the code is running as.%7Dcloses that block.//starts a comment, which makes the browser ignore whatever the program had written after this point, so the leftover code does not break the injection.
The technique came from Soroush Dalili's article on ExternalInterface.call() JavaScript injection, where a try-catch can trigger your own JavaScript if the called function does not exist. The injection worked and the JavaScript executed.
Step 2: report and observe the fix
At 10:16 PM GMT+1 the report was sent to Mega. The researcher then tried the vulnerability in other browsers, but it suddenly stopped working. Going back to Safari, it did not work there either: the file had been removed. A second email was sent to Mega at 10:53 saying that the vulnerability was gone. Five minutes later a reply arrived.
The file was removed in 35 minutes, which the researcher describes as the fastest fix he had seen, and a really nice bounty. Later that night the SWF was replaced with a fixed one that removed the id being sent back to the JS function.
Step 3: the earlier report
Mega's blog had a recent entry explaining the different levels of injections they had and what had been found during the week. It showed that another SWF issue had already been reported, on a SWF called ZeroClipboard.swf, almost the same as the one found here. Since the injection still worked, it looked like Mega had replaced their old third-party solution for the clipboard saver with a self-written one that had the same vulnerability. A third-party solution is a component made by another company and reused in your site; writing your own replacement is often done to reduce dependencies, but it does not automatically remove the old mistake.
Two days later, after some buzz around Mega's Bug Bounty Program and an interview by Adam Crouchley, Kim wrote to the researcher on Twitter.
Root cause
The OneClipboard.swf constructor passed the flashparam id straight into an ExternalInterface.call that targeted the JS function clipboardcopyready. When that function does not exist, the call throws, and an attacker who controls id can supply a catch block that runs their own JavaScript. The same class of bug had already appeared in the third-party ZeroClipboard.swf; replacing it with a self-written SWF kept the vulnerable pattern.
In everyday terms: the file took a note handed to it by a stranger and read it out loud as if it were an instruction, instead of treating it as plain text. The fix is to treat incoming values as data, never as code.
Impact
The payload executed JavaScript in the context of mega.co.nz, as demonstrated by alert(document.domain). "In the context of" means the code runs as if it came from Mega itself. The report does not describe any further actions taken with the execution, and it does not state which users or sessions could be reached beyond the researcher's own browser test.
Fix
The vulnerable file was removed within 35 minutes of the report, and later that night Mega replaced the SWF with a fixed one that no longer sends id back to the JS function.
Variants to test
- Look for other SWF files that pass request parameters into
ExternalInterface.call, especially where the called JS function may not exist. A positive result is your own JavaScript running when the parameter is crafted with a closing sequence and acatchblock. - Check whether a third-party clipboard or download SWF was replaced by an in-house one. A positive result is the same injection pattern surviving the replacement.
- Test the same flashparam injection in several browsers, since behaviour differed between Chrome and Safari here. A positive result is execution in at least one browser where the SWF is loaded.
Glossary
- Bug Bounty Program: a scheme where a company pays people for finding and privately reporting security mistakes in its systems.
- JavaScript: the programming language that runs inside web browsers and makes pages interactive.
- XSS (Cross-Site Scripting): a bug class where an attacker gets their own JavaScript to run as if it came from a trusted website.
- Flash: an old browser plug-in that ran interactive content such as animations and small programs.
- SWF: the file format for a Flash movie, a small program run by the Flash plug-in.
- ActionScript: the programming language Flash files are written in.
- Decompiler: a tool that turns a compiled program back into something close to readable source code.
- ASDec (JP ActionScript Decompiler): the decompiler used here to read the source of SWF files.
- Clipboard: the temporary storage a computer uses when you copy and paste.
- alert: a browser function that shows a small pop-up box with a message.
- confirm / prompt: two other built-in browser pop-up functions.
- javascript: prefix: a way of telling the browser to run what follows as JavaScript instead of treating it as a web address.
- Regular expression: a pattern-matching notation for text; here
/XSS/is used simply as text to display. - Flashparam: a value passed to a Flash file through its address, like values after a question mark in a URL.
- Inject / injection: putting in a value the programmer did not expect, to see what the program does with it.
- Constructor: the part of a program that runs first when an object is created.
- ExternalInterface.call: a Flash feature that lets a Flash file call a JavaScript function in the surrounding web page.
- Try-catch: a programming structure where risky instructions sit in a
tryblock and errors are handled in acatchblock instead of crashing the program. - URL-encoding: writing special characters in a percent form so they survive being placed in an address.
- document.domain: the browser's way of naming the site a page belongs to.
- Comment (
//): text the browser ignores, used here to neutralise leftover code. - Third-party solution: a component made by another company and reused in your site.
- Context (of execution): the identity and permissions under which code runs; code in the context of a site runs as if it came from that site.