среда, 19 октября 2011 г.

Hacking into AS3 bytecode

Some days ago during the work I've faced following issue with flash player security policy.
There was attempt to stage access from non open source library which I was using, but I really don't want it because my swf was aimed to be loaded and used by the 3rd-party swf.

Obviously the was security exceptions thrown by flash player, and there was no standard methods to hook into this. The library which I was using was Google Maps Flash API library (maps_1_20.swc). It have setup a timer event listener function in which attempt to access to stage were triggered. So there was no hope with simply using try/catch block as a workaround.

The solution come with investigating into apparat project (http://code.google.com/p/apparat/). This great open source project is written in Scala 2.8.0 and AS3, and aimed to optimize compiled AS3 code, and to add some cool features to AS3 programming language. It lets you even write some of your functions in AVM assembler code.

It has some tools such as trace stripper available from the box. But the feature wich I was need from it is AS3 byte code replacement.

So I had written a simple Scala program which takes a file (.swf, or .swc) and then either does code replacements or dumps it to the screen in human readable form.

You can find the sources of the tool on github.

The byte code replacement logic is rather simple it looks into each method to find calls of addEventListener method on any object, then it replaces this code with a call to proxy static method, which is hardcoded in the Tool object constants.

There was two issues which I've met during the work:
1. There is a need to update Abc.cpool (contant pool) after bytecode replacement.
2. Method body maximum stack size should be increased by 1 (as we add additional parameter of event dispatcher)

If you want to run this, please follow instructions in README.textile.

Have happy byte code hacking! :)

Комментариев нет:

Отправить комментарий