阿里系App抓包分析(一)

阿里移動端產品網絡層大多使用Mtop、ANetwork框架,Mtop是阿里雲的移動端網關SDk,ANetwork是比較底層的網絡實現庫,分析這兩個框架就能找出App抓不到包的緣由。本文將逆向分析smali文件,找出阿里系App網絡的開關,最終能使用Charles實現抓包的功能。java

以《大麥網》App爲例子,從應用寶將APK下載下來將文件後綴改成zip而後解壓,而後使用baksmail將dex文件轉成smali文件:android

java -jar baksmali-2.3.4.jar  d classes.dex -o damai
java -jar baksmali-2.3.4.jar  d classes2.dex -o damai
...
複製代碼

注意classes.dex會有多個,每一個文件都執行一次上面的命令,確保全部的dex文件都被反編譯再來分析。shell

《大麥網》的網絡是使用Service的形式,因此首先打開anetwork/channel/aidl/NetworkService.smali這個文件,分析裏面的onBind方法:bash

.method public onBind(Landroid/content/Intent;)Landroid/os/IBinder;
 
 ...
    
    new-instance v1, Lanetwork/channel/degrade/DegradableNetworkDelegate;

    iget-object v2, p0, Lanetwork/channel/aidl/NetworkService;->context:Landroid/content/Context;

    invoke-direct {v1, v2}, Lanetwork/channel/degrade/DegradableNetworkDelegate;-><init>(Landroid/content/Context;)V

    iput-object v1, p0, Lanetwork/channel/aidl/NetworkService;->degradeableNetwork:Lanetwork/channel/aidl/RemoteNetwork$Stub;
 
    new-instance v1, Lanetwork/channel/http/HttpNetworkDelegate;

    iget-object v2, p0, Lanetwork/channel/aidl/NetworkService;->context:Landroid/content/Context;

    invoke-direct {v1, v2}, Lanetwork/channel/http/HttpNetworkDelegate;-><init>(Landroid/content/Context;)V

    iput-object v1, p0, Lanetwork/channel/aidl/NetworkService;->httpNetwork:Lanetwork/channel/aidl/RemoteNetwork$Stub;
 
    const-class v1, Lanetwork/channel/aidl/IRemoteNetworkGetter;
 ... .end method
複製代碼

去掉一些無用的代碼,能夠看到幾個關鍵的類:網絡

  • IRemoteNetworkGetter
  • DegradableNetworkDelegate
  • HttpNetworkDelegate

IRemoteNetworkGetter看名字大概就能猜想是獲取某個東西的類,它是一個AIDL生成的類,它就只有一個方法:框架

.method public abstract get(I)Lanetwork/channel/aidl/RemoteNetwork;
複製代碼

接收一個Int型參數,用於獲取RemoteNetwork實例。它的實現類在anetwork/channel/aidl/NetworkService$1.smali文件中:學習

.method public get(I)Lanetwork/channel/aidl/RemoteNetwork;
    .registers 3
    .param p1, "type"    # I
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Landroid/os/RemoteException;
        }
    .end annotation

    .prologue
    .line 36
    const/4 v0, 0x1

    if-ne p1, v0, :cond_a

    iget-object v0, p0, Lanetwork/channel/aidl/NetworkService$1;->this$0:Lanetwork/channel/aidl/NetworkService;

    # getter for: Lanetwork/channel/aidl/NetworkService;->degradeableNetwork:Lanetwork/channel/aidl/RemoteNetwork$Stub;
    invoke-static {v0}, Lanetwork/channel/aidl/NetworkService;->access$000(Lanetwork/channel/aidl/NetworkService;)Lanetwork/channel/aidl/RemoteNetwork$Stub;

    move-result-object v0

    :goto_9
    return-object v0

    :cond_a
    iget-object v0, p0, Lanetwork/channel/aidl/NetworkService$1;->this$0:Lanetwork/channel/aidl/NetworkService;

    # getter for: Lanetwork/channel/aidl/NetworkService;->httpNetwork:Lanetwork/channel/aidl/RemoteNetwork$Stub;
    invoke-static {v0}, Lanetwork/channel/aidl/NetworkService;->access$100(Lanetwork/channel/aidl/NetworkService;)Lanetwork/channel/aidl/RemoteNetwork$Stub;

    move-result-object v0

    goto :goto_9
.end method
複製代碼

大概意思就是判斷type是0就返回httpNetwork其餘的就返回degradeableNetwork,這兩個實例都是NetworkServie中的onBind方法中建立的。ui

通過上面的分析就能肯定《大麥網》中有兩種網絡實現一種是比較熟悉的httpNetwork和目前不知道有啥區別的degradeableNetwork,因爲我們的主要目的是實現抓包,具體的網絡實現啥的就不去探索了,有興趣的能夠去研究哈。this

如今都知道《大麥網》的網絡請求是使用AIDL的方式發送的,你們都知道AIDL會自動生成ProxyStubStub就不去分析了,他的實現類就是DegradableNetworkDelegateHttpNetworkDelegate,下面得找出綁定NetworkService服務的代碼,要使用網絡那也要綁定服務才行,打開AndroidManifest.xml文件,找到服務的聲明以下:阿里雲

<service android:exported="false" android:name="anetwork.channel.aidl.NetworkService">
            <intent-filter>
                <action android:name="anetwork.channel.aidl.IRemoteNetworkGetter"/>
            </intent-filter>
 </service>
複製代碼

如今要去找到綁定NetworkService服務的代碼,全局搜索anetwork.channel.aidl.IRemoteNetworkGetter這action沒有發現有相關的代碼,後來發現這個action其實就是IRemoteNetworkGetter這個類的類名,因此經過全局搜索找到了一個RemoteGetterHelper的類,這個類主要用來綁定服務,獲取IRemoteNetworkGetter實例,而後獲取網絡實現類:

invoke-virtual {v3}, Ljava/lang/Class;->getName()Ljava/lang/String;

move-result-object v3

invoke-virtual {v2, v3}, Landroid/content/Intent;->setAction(Ljava/lang/String;)Landroid/content/Intent;
 
const-string/jumbo v3, "android.intent.category.DEFAULT"

invoke-virtual {v2, v3}, Landroid/content/Intent;->addCategory(Ljava/lang/String;)Landroid/content/Intent;
 
sget-object v3, Lanetwork/channel/aidl/adapter/RemoteGetterHelper;->conn:Landroid/content/ServiceConnection;

invoke-virtual {p0, v2, v3, v0}, Landroid/content/Context;->bindService(Landroid/content/Intent;Landroid/content/ServiceConnection;I)Z
複製代碼

自此能夠肯定獲取網絡實例會通過如下步驟:

  1. 使用RemoteGetterHelper綁定服務
  2. 獲取到IRemoteNetworkGetter
  3. 調用IRemoteNetworkGetter.get(int)方法獲取實例

網絡就分析到這裏啦,要想實現抓包還得分析Mtop,第二部分再來分析Mtop而後實現抓包,最後簡單畫個圖,理一理:

最後歡迎你們關注個人公衆號,獲取最新的進展。

免責聲明: 本文章僅限用於學習和研究目的;不得將上述內容用於商業或者非法用途,不然,一切後果請讀者自負。

相關文章
相關標籤/搜索