<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CyberShadow’s blog &#187; SWF</title>
	<atom:link href="http://blog.thecybershadow.net/tag/swf/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thecybershadow.net</link>
	<description>Code and miscellanea</description>
	<lastBuildDate>Wed, 05 Oct 2011 15:01:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Announcing: RABCDAsm</title>
		<link>http://blog.thecybershadow.net/2010/05/05/announcing-rabcdasm/</link>
		<comments>http://blog.thecybershadow.net/2010/05/05/announcing-rabcdasm/#comments</comments>
		<pubDate>Wed, 05 May 2010 07:47:49 +0000</pubDate>
		<dc:creator>CyberShadow</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[D]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[open-source]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[SWF]]></category>

		<guid isPermaLink="false">http://blog.thecybershadow.net/?p=187</guid>
		<description><![CDATA[RABCDAsm (Robust ABC (ActionScript Bytecode) [Dis-]Assembler) is a collection of utilities including an ActionScript 3 assembler/disassembler, and a few tools to manipulate SWF files. This package was created due to lack of similar software out there. Particularly, I needed an utility which would allow me to edit ActionScript 3 bytecode (used in Flash 9 and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/CyberShadow/RABCDAsm">RABCDAsm</a> (<b>R</b>obust <b>ABC</b> (<b>A</b>ctionScript <b>B</b>yte<b>c</b>ode) [<b>D</b>is-]<b>As</b>se<b>m</b>bler) is a collection of utilities including an ActionScript 3 assembler/disassembler, and a few tools to manipulate SWF files.</p>
<p>This package was created due to lack of similar software out there.<br />
Particularly, I needed an utility which would allow me to edit ActionScript 3 bytecode (used in Flash 9 and newer) with the following properties:</p>
<ul>
<li>Speed. Less waiting means more productivity. <tt>rabcasm</tt> can assemble large projects (&gt;200000 LOC) in under a second on modern machines.</li>
<li>Comfortably-editable output. Each class is decompiled to its own file, with files arranged in subdirectories representing the package hierarchy. Class files are <tt>#include</tt>d from the main file.</li>
<li>Most importantly &#8211; robustness! If the Adobe AVM can load and run the file, then it must be editable &#8211; no matter if the file is obfuscated or otherwise mutilated to prevent reverse-engineering. RABCDAsm achieves this by using a textual representation closer to the ABC file format, rather than to what an ActionScript compiler would generate.</li>
</ul>
<p>Read more on the <a href="http://github.com/CyberShadow/RABCDAsm#readme">project&#8217;s homepage on GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thecybershadow.net/2010/05/05/announcing-rabcdasm/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Reverse-engineering and deobfuscation of Flash apps</title>
		<link>http://blog.thecybershadow.net/2010/02/12/reverse-engineering-and-deobfuscation-of-flash-apps/</link>
		<comments>http://blog.thecybershadow.net/2010/02/12/reverse-engineering-and-deobfuscation-of-flash-apps/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 11:18:24 +0000</pubDate>
		<dc:creator>CyberShadow</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[deobfuscation]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[open-source]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[SWF]]></category>

		<guid isPermaLink="false">http://blog.thecybershadow.net/?p=80</guid>
		<description><![CDATA[I probably should have known better when I started down this path with &#8220;cracking&#8221; a certain flash game. This has taken way more of my time than I had initially planned. On the other hand, now I know how to use Adobe Flash Builder, Adobe Flex SDK, XML schemas and JAXB and brushed up on [...]]]></description>
			<content:encoded><![CDATA[<p>I probably should have known better when I started down this path with &#8220;cracking&#8221; a certain flash game. This has taken way more of my time than I had initially planned. On the other hand, now I know how to use Adobe Flash Builder, Adobe Flex SDK, XML schemas and <a href="http://en.wikipedia.org/wiki/Java_Architecture_for_XML_Binding">JAXB</a> and brushed up on my Java as well.</p>
<p>My previous versions of the cheat consisted of a Mozilla Firefox extension which redirected requests for the game SWF (and data.xml, a configuration file) to my server. The server (configured as a HTTP proxy) sent back an older version of the game SWF, which still had some debugging code left in which allowed you to edit the game board using hotkeys. The game developer has once changed the MD5 salt used to calculate validation checksums (after removing the debugging code), but I got away with it once by uncompressing (gzip) the SWF file, hex-editing the salt, and compressing it back. However, the latest build of the game was obfuscated, so no such tricks would pass this time.</p>
<p>In retrospect (as I wrote on the cheat page), I could have used simpler techniques such as memory editing and replay attacks, but all these could eventually be &#8220;patched up&#8221;. It would probably even have been simpler if I had written a bot (or just updated the old one) &#8211; there really isn&#8217;t much to do against screen-scraping bots, other than heuristics and changing the UI every once in a while. Still, I have written an ActionScript 3 deobfuscator.<br />
<span id="more-80"></span><br />
I began my research with the Adobe Flex SDK. Considering that it includes a full open-source ActionScript/SWF compiler and utilities, I thought it would have some code which I could adapt to my needs. I was right, in fact there was actually an overlap/duplication of functionality in some classes. I have created the following &#8220;map&#8221; during my spelunking of the SDK source:</p>
<h3>SWF</h3>
<p>flash.swf.TagHandler: interface to handle SWF tags<br />
flash.swf.TagDecoder: InputStream -&gt; TagHandler<br />
flash.swf.TagEncoder: TagHandler -&gt; OutputStream/byte[]<br />
flash.swf.MovieDecoder: TagHandler -&gt; Movie<br />
flash.swf.MovieEncoder: Movie -&gt; TagHandler<br />
flash.swf.tools.SwfxPrinter: TagHandler -&gt; PrintWriter (Swfx)<br />
flash.swf.tools.SwfxParser: XML (Swfx) -&gt; TagHandler</p>
<h3>Actions</h3>
<p>flash.swf.ActionHandler: interface to handle actions(?)<br />
flash.swf.tools.Disassembler: ActionHandler -&gt; PrintWriter<br />
flash.swf.ActionEncoder: ActionHandler -&gt; SwfEncoder<br />
flash.swf.ActionDecoder: SwfDecoder -&gt; ActionList</p>
<h3>ABC</h3>
<p>macromedia.abc.Visitor: interface to handle ABC instructions<br />
macromedia.abc.Decoder: BytecodeBuffer -&gt; Visitor<br />
macromedia.abc.Encoder: Visitor -&gt; byte[]<br />
macromedia.abc.DefaultVisitor: Visitor -&gt; higher-level interface<br />
<span style="padding-left: 30px; font-style: italic;">Visitors (DefaultVisitor at least) still need reference to the original decoder</span><br />
macromedia.abc.Printer.ABCVisitor: Visitor -&gt; System.out<br />
flash.swf.tools.AbcPrinter: byte[] -&gt; PrintWriter<br />
<span style="padding-left: 30px; font-style: italic;">Used by flash.swf.tools.SwfxPrinter</span></p>
<h3>Nodes</h3>
<p>macromedia.asc.parser.Evaluator: interface to handle macromedia.asc.parser.*Node<br />
flash.swf.tools.SyntaxTreeDumper: Evaluator -&gt; XML<br />
flash.swf.tools.as3.PrettyPrinter: Evaluator -&gt; PrintWriter<br />
macromedia.asc.semantics.Emitter: interface for handling ABC instructions?<br />
macromedia.asc.semantics.CodeGenerator: Evaluator -&gt; Emitter<br />
macromedia.asc.embedding.avmplus.ActionBlockEmitter: Emitter -&gt; ByteList</p>
<h3>Optimizer</h3>
<p>adobe.abc.GlobalOptimizer: reads/writes .abc files, uses adobe.abc.Expr/Node/Edge/etc.<br />
<span style="padding-left: 30px; font-style: italic;">It looks like the adobe.abc namespace is only used for optimization of already-compiled code.</span></p>
<p>Unfortunately, I haven&#8217;t found any code capable of robust deserialization of <abbr title="ActionScript Bytecode">ABC</abbr>. (macromedia.abc.Encoder/Decoder come close to that, however they choke on some obfuscated code and produce broken output.) I haven&#8217;t written a deserializer, but instead I <a href="http://github.com/CyberShadow/swfutilsex/blob/master/src/net/thecybershadow/swf/tools/AbcProcessor.java">wrote a class</a> (based on flash.swf.tools.AbcPrinter) that allows its subclasses to edit ABC structures &#8220;on the fly&#8221;.</p>
<p>The deobfuscator classes (the <a href="http://github.com/CyberShadow/swfutilsex/blob/master/src/net/thecybershadow/swf/tools/Deobfuscator.java">main class</a> extending TagEncoder, to also edit SWF tags &#8220;on the fly&#8221;) performed two functions: correct identifier names in SymbolClass tags and ABC string constant pools, and rearrange code blocks to eliminate dead &#8220;junk&#8221; code inserted by the obfuscator.</p>
<p>The first function is fairly self-explanatory: it searched for strings starting with &#8220;_-&#8221; (the obfuscator&#8217;s prefix for all obfuscated names) or containing ActionScript keywords, and replacing them with strings which an ActionScript compiler would be more happy with. This step wasn&#8217;t actually necessary as the decompiled code wasn&#8217;t directly reusable anyway. It also allowed replacing strings based on a dictionary read from a text file, which allowed me to &#8220;rename&#8221; identifiers (similar to IDA).</p>
<p>The second feature is more interesting. The gist is that it splits each function into blocks (delimited at jumps and jump targets), constructs a flow graph, and then rewrites the code by traveling across the graph starting with the first node, and writing out the blocks in visited order. Practically, this is done by in-place patching relative jump offsets then writing the same bytecode in a different order (inserting labels and jumps where appropriate). Thus, the code doesn&#8217;t actually create in-memory copies of the bytecode. This approach has some side effects though, in that all unconditional jumps are rewritten to be strictly backwards, and this seems to break the <abbr title="ActionScript Virtual Machine">AVM</abbr> as it will spit out a cryptic error such as &#8220;VerifyError: Error #1068: package.class and package.class cannot be reconciled&#8221; (yes, the very same class). However, it makes decompilers much more happy with the new code, and that&#8217;s what mattered to me most.</p>
<p>The deobfuscator allowed me to study the game&#8217;s code much easier, however that wasn&#8217;t enough to accomplish my goal. It looks like the obfuscator used for this game also had a &#8220;string encryption&#8221; feature, and the MD5 salt used for validating sent and received data was encrypted using this feature (namely, a class which contained an encrypted version of the script and allowed decrypting it at runtime). Code generated by a decompiler was unusable, and the encryption algorithm (which I later recognized as the <a href="http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm">Tiny Encryption Algorithm</a>) was too long to decipher from bytecode directly. I needed some way to execute that code without decompiling it. After initially planning to do some string replacement tricks and merging ABC code, I noticed that the respective class was in a separate DoABC tag (the deobfuscator must have written that block manually). Then, running the code was as simple as:</p>
<ol>
<li>extracting the contents of the DoABC tag to an .abc file (<a href="http://github.com/CyberShadow/swfutilsex/blob/master/src/net/thecybershadow/swf/tools/AbcExport.java">AbcExport</a>)</li>
<li>creating an ActionScript 3 application with the code:
<pre class="brush: actionscript3; title: ; toolbar: false; notranslate">var c:Class = getDefinitionByName(&quot;_-GF._-FA&quot;) as Class;
var method:Function = c[&quot;_-NR&quot;];
throw new Error(method(117, -75));</pre>
</li>
<li>injecting the .abc file from step 1 into the resulting SWF (<a href="http://github.com/CyberShadow/swfutilsex/blob/master/src/net/thecybershadow/swf/tools/AbcReplace.java">AbcReplace</a>)</li>
</ol>
<p>and you get the magic string in a Flash Player exception box <img src='http://blog.thecybershadow.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>You can find the source code for my deobfuscator and other utilities <a href="http://github.com/CyberShadow/swfutilsex/">on GitHub</a>. Please don&#8217;t ask for help in using the source code, you&#8217;re on your own.</p>
<p><strong>Continued in</strong>: <a href="http://blog.thecybershadow.net/2010/05/05/announcing-rabcdasm/">Announcing: RABCDAsm</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thecybershadow.net/2010/02/12/reverse-engineering-and-deobfuscation-of-flash-apps/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
	</channel>
</rss>

