android程序反編譯與二次打包

前言

android因爲平臺的緣由,未經加固的app是比較容易反編譯進行二次打包,好比加入廣告、植入木馬、去掉開發者加入的一些商業化的東西等等,這就致使了市面上出現了各類apk的破解版或者純淨版,做爲開發者,固然不但願本身的app脫離控制,這就有了所謂的app加固,正所謂知己知彼,百戰不殆。要想知道如何受,啊呸是守,咱們應該先要知道如何攻,攻主要分三步:反編譯、修改代碼或者資源文件、二次打包,下面以測試demo爲例,一步步演示。 先運行下demo,demo的名稱叫測試Demo,只是簡單的顯示了下Hello World! 以下圖:java

而後咱們開始反編譯

反編譯

工欲善其事必先利其器,準備的工具以下:apktool.jar、apktool.bat。官網地址:ibotpeaches.github.io/Apktool/android

將上面的文件以及測試demo的apk放在同一文件夾下面,cmd到文件夾下執行以下命令:git

apktool d com.lsj.decompile_v.1.0.apk,執行過程以下圖:github

注意路勁不要有中文不然會執行失敗。執行成功以後能夠看到在文件夾下生成了與apk同名的文件夾,打開能夠看到目錄結構以下:
是否是很熟悉?是的這裏面和咱們android代碼結果相似,只是java源碼看不到,以另一種形式smali的形式存在,這個下面再說。到這裏反編譯結束。

篡改

咱們首先修改資源文件試下,經過查看AndroidManifest.xm以下:bash

<?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:compileSdkVersion="28" android:compileSdkVersionCodename="9" package="com.lsj.decompile" platformBuildVersionCode="1" platformBuildVersionName="1.0">
    <application android:allowBackup="true" android:appComponentFactory="android.support.v4.app.CoreComponentFactory" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:name="android.support.multidex.MultiDexApplication" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name="decompile.lsj.com.decompile.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>
複製代碼

咱們知道app的的名稱是@string/app_name,接下來咱們先嚐試把app的名稱改掉,分別在上面反編譯產生的文件夾裏面找到對應的資源進行修改,好比咱們找到app_name這個字符串,改爲「反編譯測試Demo」。app

二次打包

執行以下命令ide

apktool b com.lsj.decompile_v.1.0 -o demofake.apk,執行過程以下圖:工具

能夠看到在當前文件夾下面生成了 demofake.apk,咱們看看能不能正常使用,結果悲劇了,忘了簽名,效果以下:

簽名以後把原來的卸載從新安裝,效果如圖:

接下來咱們繼續搞事情,嘗試串改佈局文件,加上本身的內容。,一樣經過查看AndroidManifest.xml,咱們知道當貝市場的入口activity是decompile.lsj.com.decompile.MainActivity,這個時候咱們就要祭出咱們的另外一個法寶Smali2Java,這個工具不但支持帶個smali文件裝成java代碼,還支持將apk轉換爲java代碼,咱們用Smali2Java打開測試demo apk,找到MainActivity,以下圖:

啥子都沒有,這個咋整?根據咱們的經驗既然layout通常和Activity有必定的關聯,咱們直接去layout文件夾裏面找,找到activity_main.xml,打開:佈局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
複製代碼

看到只有一個TextView,而且內容是「Hello World!」,應該就是它,接下來咱們作一個操做,在這個TextView上面蓋上另外一個TextView,顯示這是一個廣告,修改以下以下:測試

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="這是一個廣告"
        android:background="#ffff00"
        android:gravity="center"/>

</android.support.constraint.ConstraintLayout>
複製代碼

而後在從新打包簽名,安裝效果圖以下:

證實是有效果的。
相關文章
相關標籤/搜索