How recoverable is an APK source code made with Titanium ?

Posted by

Last Friday I had a meeting with a posible client, who had made an app with a freelance developer using Appcelerator Titanium. Now he wished to make upgrades to the app, but he realized he didn’t have the source code for it and the original developer was nowhere to be found.

I’ve heard these kind of stories a lot of times before, but I though as I’m creating compilers that generate Titanium code i wonder: how recoverable is the source code of a Titanium app from an existing APK ? I found it a nice challenge, so i started digging.

Almost any Android developer knows that there exist tools to unpack and decompile the assets and class files from a APK. It’s basically a zip file that contains a resources.arsc, classes.dex and a binary manifest.xml. Using apktool you can extract the assets (images, sounds, fonts, raw objects, xml layouts, decrypt the manifest) from almost any APK, and using dex2jar you can transform the classes.dex file into a normal ‘jar’ file, which you can then decompile using any class2java tool (example jd or jadx).

blog-pablo-npm-apk-unpack

Now for this task, or any like it, the first step is to get the resources, so I made my own one step tool called APK_unpack to do that job easier using NodeJS and a java-bridge. This small step was a challenge but solved quickly (i had never made before a nodejs app that consumed java classes directly).

Well, Titanium promotes itself as that it is javascript compiled as native, so it was no surprise that the JS files were not on the resources folder (as an ionic app could have – well they are there if the apk was compiled as developer mode). But, i found a particular slideshare on google telling that the Titanium code for the apps was actually on the main package directory, on a file called AssetCryptImpl. I follow the directions on it and created my javascript NodeJS version of an unpacker for that file (if you wish to see what i wrote, you can watch it on my github.com account, project ti_unpack).

2015-11-19_1447895727180

Surprisingly I was able to get the source code for the entire app !(at least into memory)

After playing a bit with the extracted code, i found that some versions of Titanium minify the JS compiled code in different ways, so i though it would be nicer if I could restore the original structure for the app instead of just having the compiled resources (very much readable by the way). For this, i made another NodeJS program called ti_recover, which uses all the packages made before and creates an output dir trying to restore the structure of a real Ti app. I’m making this for solving the requirement of my client, but thought it may be of use for other Titanium developers that have lost their code (which i have seen several times on quora).

 

To tell you the truth this frightened me, because i have made and published more than a dozen titanium apps, but helps me understand how can i better protect my code, and the ones of my clients. And don’t misunderstand me, I love Titanium, I find it a very good product, but sometimes happens you need to do get your source back, and now I know you actually can.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s