Crack Android apk with Apktool & Apksigner

As my mom has got a certain requirement on one of her most common-used apps in her Huawei Mate9, I resolved to make a difference on that app which will consequently be called "anonymity.apk" by adapting the source code of Apktool, a versatile and powerful toolkit for reverse engineering Android apk files.

Okey. Let's hustle to make it.

1. Utilize a Shell tool to verify if the apk has been shelled.

2. Download the featured actor —— Latest Apktool.

You'd better read through the instructions and documentations listed on the official website though it'd be just not enough for the following steps.

It'd be a jar file that you may have downloaded that can be invoked by java -jar directive.

java -jar --help

Apktool is mainly comprised of smali and xpp3

  • Smali is a disassemblerfor the dex format used by dalvik, Android's Java VM implementation, in a way that transforms .dex into .smali code.
  • Xpp3 is a lightweight XML parser used to promptly decode the encrypted xml resources.

Now, decompile the zip file. Yep, apk is actually a zip file.

java -jar apktool.jar d -f anonymity.apk -o anonymity

image.png

Let's take a glance at the outputed anonymity folder.

  • assets

    Major resources consisting of audio, video, raw pictures, fonts, models and so forth.

  • kotlin

    Kotlin related files.

  • lib

    Android native .so libraries.

  • original

    • META-INF

    Several degist and public certificate files constitute the meta security information.

    • AndroidManifest.xml

    An encrypted source AndroidManifest.xml.

  • res

    Essential resources consisting of widget pictures and xml files which comprise id folder (values folder) and resource folders (layout, drawable, color, menu, anim, etc)

  • smali, smali1, smali2, smali3 ... folders

    Involving android disassembled files that are derived from .dex byte-code files by smali.jar.

    Smali is quite a human readable disassembly code based on Dalvik directives than pure Intel/arm disassembly code.

  • unknown

    Files that can't be correctly identified like a file that has got a name without a extension. These files will be put back in their original places in the building process.

  • AndroidManifest.xml

    Decrypted AndroidManifest.xml.

  • apktool.yaml

    An inventory of the other folders and files that is not belonging to the apk archive. Apktool take advantage of such a .yaml to record something that is important to take notes in the building phase.

3. Locating the target code in smali files

A) Search the characterized string in the res\values\strings.xml as a crack-in point.

<string name="au7">出错</string>

By the way, Files that have been obfuscated by the Android proguard module involve the confusing names such as a, b, c, aa, bb, cc, a1, b2, c3, etc. Here is the item that is named "au7".

B) Map the id of the string we found in the res\values\public.xml.

<public type="drawable" name="au7" id="0x7f020d70" />
<public type="string" name="au7" id="0x7f080d79" />
<public type="id" name="au7" id="0x7f0f0f95" />

Be cautious that there'd be various types under an identical name, so it'd make sense to choose the one whose type attribute is marked as "string". Here is the item that has an id equaling "0x7f080d79".

C) Search the id globally in the project in order to find out which file has referenced the string.

D) Take whatever actions you need to achieve your aims by modifying the source smali code which won't be exhibited here in detail.

4. Rebuilt the whole of project

java -jar apktool.jar b -f anonymity

"-f" option is not forced to exist in a condition that the smali files have not been altered. Apktool will not recompile the smali files into new dex files if no alteration has been detected.

But some weird building errors encounter me.

A) No resource identifier found for attribute "XXX" in package "com.XXX.XXX"

B) No resource found that matches the given name (at "XXX" with value "XXX")

I will set it aside and move ahead in here. It's time to dive into these exceptions and apktool source code in next article.

There'd be two brand new folder generated in the root directory.

  1. "build" folder would be stored intermediate files to be recompiled as an apk like encrypted AndroidManifest.xml, classesX.dex, resource files and their archived resources.arsc, kotlin supported files, native .so libraries.

  2. The outputed apk would be stashed in the "dist" folder. But it can't be installed for now as it has not been re-signed.Otherwise, you should have received an error named "INSTALL_PARSE_FAILED_NO_CERTIFICATES"

If we open apk file through WinRAR or something, we'll notice that there is an extra "META-INF" folder in the root directory that we have mentioned before that serves as an security method within public certificate and Base64 hashed fingerprints mostly implemented by SHA-1 algorithm.

The folder has to be re-created by a signer like "Jarsign" or "Apksigner". I will utilize Apksigner as signer because it is just placed in the Android sdk under path %SDK Root%\build-tools\%SDK Version%\

Then we need a keystore in which involves a pair of asymmetrical keys which is analogous to the old-fashioned .pk8 (aks the private key) and X509.pem
(aks the public certificate) duo.

We can generate a custom keystore by keytool in JDK.

keytool -genkeypair -v -keyalg DSA -keysize 1024 -sigalg SHA1withDSA -validity 20000 -keystore my.keystore -storepass my_storepass -alias my_key_alias -keypass my_key_pass

keytool -genkeypair [OPTION]...

Generates a key pair

Options:

 -alias <alias>                  alias name of the entry to process
 -keyalg <keyalg>                key algorithm name
 -keysize <keysize>              key bit size
 -sigalg <sigalg>                signature algorithm name
 -destalias <destalias>          destination alias
 -dname <dname>                  distinguished name
 -startdate <startdate>          certificate validity start date/time
 -ext <value>                    X.509 extension
 -validity <valDays>             validity number of days
 -keypass <arg>                  key password
 -keystore <keystore>            keystore name
 -storepass <arg>                keystore password
 -storetype <storetype>          keystore type
 -providername <providername>    provider name
 -providerclass <providerclass>  provider class name
 -providerarg <arg>              provider argument
 -providerpath <pathlist>        provider classpath
 -v                              verbose output
 -protected                      password through protected mechanism

And it'd be recommended to migrate the proprietary JKS format to an industrially standard PKCS12 format.

keytool -importkeystore -srckeystore my.keystore -destkeystore my.keystore -deststoretype pkcs12

Then the final apk can be generated by a re-sign
process. Note that key_alias is the position in which the private key and certificate are stored in the KeyStore. This must be specified if the KeyStore contains multiple keys.

apksigner sign --ks my.keystore --ks-pass pass:keystore_pass --ks-key-alias key_alias --key-pass pass:key_pass --out output.apk anonymity.apk

The successfully re-signed apk will override the historical apk.

Finally, install and enjoy it.

By the way, if the apk provider has set a re-signed barrier, you would do more jobs to bypass it. But in fact, this particular approach is usually only adopted by mobile game providers.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,099评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,473评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,229评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,570评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,427评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,335评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,737评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,392评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,693评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,730评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,512评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,349评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,750评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,017评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,290评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,706评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,904评论 2 335