Monday, September 27, 2010

SWF Encryption / Content Protection

What is SWF Obfuscation?
An obfuscator is a program used to transform program code / ByteArray. The output of an obfuscator is program code/ByteArray, that is more difficult to understand but is functionally equivalent to the original code.
Obfuscation transformations are classified into the Layout, Control, Data, Preventive, Splitting, Merging, Reordering etc. transformations.

Process of creating obfuscation SWF:
STEP 1
: Create Utility on server to Obfuscate SWF. It can be command line utility created in using Java/PHP/.NET etc
1. Read original SWF in binary Mode
2. Change Binary combination of SWF. Rearranging or sequencing
Sample Pseudo Code in PHP. We

$filename = "sample.swf";
$fp = fopen($filename, "rb");
$image = fread($fp, filesize($filename));
$image2 = ~$image;
fclose($fp);
echo $image2;
?>
3. Save obfuscation SWF on SERVER / content folder on server
Step 2: Process of Loading SWF in Container
1. Load obfuscated SWF using Loader object.
2. SWF will be loaded into local cache. But when user try to copy and run it, it will not run without our container. To avoid caching set 'cacheResponse' to true.
3. Convert SWF into Binary Mode-
4. Reverser the obfuscated SWF/ deobfuscate. Sample AS 3.0 Pseudo code
for ( var i:int = 0; i < b.length; i++ )
{
b.writeByte(~b[i]);
}
5. Now add loader object to stage
Step 3: Protect Container SWF
We need to use encryption tool to Obfuscate, Protect & Secure which contains reverse engineering/ deobfuscate logic for original SWF. Means we are adding two level of security.

Tuesday, July 7, 2009

Flex/ Flash Performance Optimization

Here are few techniques and tips which can be used to increase performance/speed of the flex application. These are generic steps which can be used by any flex/flash(AS3.0) applications.

Reduce Memory Utilization: "garbage collection" implies that objects no longer needed by the program are "garbage" and can be thrown away. When an object is no longer referenced by the program, the heap space it occupies can be recycled so that the space is made available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed. Here are few methods which can help in optimizing application.
1.1 Remove event listeners when there role is done. Generally Loader, URLLoader, are few class which generally we don't remove.
1.2 Instantiate Array when it is no longer needed. It is easy way to free memory. If array contains objects/array as item then application will
Eg. a = new Array();
1.3 Call gc() after some regular. It forces the Flash VM to explicitly free the memory.
1.4 Keep watch on Bitmap and BitmapData object this object occupy huge memory.
1.5 Object/Variable Typing: Always remember to type the object to correct type; avoid using variables without type. It is bad habit is to use the Object class. In ActionScript 3 the AVM2can make use of defined objects that are not dynamic.
---------------------------------------------------------------------


Iterations Vector[ms] Object[ms]

---------------------------------------------------------------------
10^2 0.1 0.1
10^3 1:3 2:2
10^4 13:3 17:2
10^5 73:4 109:2
10^6 672:8 1033:7

---------------------------------------------------------------------
From above table we can see that time complexity of the object increases exponential index. So try using int instead of Number for loop counters.
SLOW Version
var obj: Object = new Object();
obj.x = 1;
obj.y = 2;
FAST VERSION

class MyMatrix{
public var x:Number;
public var y: Number;
}
var mat:MyMatrix = new MyMatrix();

mat.x = 1;
mat.y = 2;
MORE FAST VERISON

class MyMatrix{
public var x:int;
public var y:int;
}

var mat:MyMatrix = new MyMatrix();
mat.x = 1;
mat.y = 2;