Android Binder Hook的實現

1. 簡述

Binder Hook 可以 Hook 掉當前進程用到的系統 Service 服務。
以 LocationManager 為例,在獲取一個 LocationManager 時分為兩步:
(1) 獲取 IBinder 對象;
(2) 通過 IBinder 的 asInterface() 方法轉化為 LocationMangerService 對象,接著初始化 LocationManager。

application 層用到的都是 LocationManager 對象。

原理:

  1. ServiceManager 在首次獲取某個 Service 的 Binder 后,會把 Binder 對象緩存在 ServiceManager#sCahce 映射表中。后續再獲取時,會先檢查 sCache 中是否已經存在緩存對象,如果有則返回這個緩存對象。所以我們可以通過反射的方式,往 sCahce 中 put 一個自定義的 Binder,這樣獲取到的 Binder 對象就會是我們自定義的 Binder 了。
  2. 上層代碼在調用 LocationManager 時,用到的都是 Service 對象,這個對象是在 ILocationManager.Stub.asInterface(CustomBinder) 方法返回的。 asInterface(customeBinder) 最終會調用到 CustomBinder # queryLocalInterface() 方法,我們需要重寫這個方法,返回自定義的 Service 對象。

整個過程需要利用反射設置一個自定義的 Binder 對象和一個自定義的 Service 對象。由于我們只 Hook 其中一部分的功能,其他功能還需要保留,所以要用動態代理的方式創建自定義對象。

在理解后面的內容前你需要了解這些知識點:

  1. 一點點 Binder 的知識,知道 IBinder 轉 IInterface 的大致流程;
  2. Java 的動態代理。

2. Context 獲取系統 Service 的流程

Activity 等類在獲取系統 Service 時,都是調用 getSystemService(serviceName) 方法獲取的。

屏幕快照 2019-02-13 19.39.00.png

這是一段不太重要的過程:
Context # getSystemService() 方法調用了 SystemServiceRegistry # getSystemService() 方法。
SystemServiceRegistry 中有一個常量 SYSTEM\_SERVICE\_FETCHERS,這是一個 Map。保存了 ServiceName 和對應的 ServiceFetcher。ServiceFetcher 是用于創建具體 Service 對象的類。ServiceFetcher 的關鍵方法是 createService() 方法。
ServiceFetcher # createService() 方法中,調用了 ServiceManager.getService(name) 方法。

Context # getSystemService() 方法最終會調用到 ServiceManager # getService() 方法中。以 LocationManager 對應的 ServiceFetcher 為例,它的 createService() 方法源碼如下:

// Android 8.0 android.app.SystemServiceRegistry.java

@Override
public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
    IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
    return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
}

假如我們要 Hook 掉 LocationManager # getLastKnownLocation() 方法(下文都是)。我們要做的就是讓
ServiceManager.getService(Context.LOCATION_SERVICE) 返回我們自定義的 Binder 對象。
先看一下這個方法的源碼:

// Android 8.0 android.os.ServiceManager.java

public static IBinder getService(String name) {
    try {
        IBinder service = sCache.get(name);
        if (service != null) {
            return service;
        } else {
            return Binder.allowBlocking(getIServiceManager().getService(name));
        }
    } catch (RemoteException e) {
        Log.e(TAG, "error in getService", e);
    }
    return null;
}

sCache 是一個 Map,緩存了已經向系統請求過的 Binder。如果需要讓這個方法返回我們自己的 binder 對象,只需要事先往 sCache 中 put 一個自定義的 Binder 對象就行了。
在 put 之前,需要先創建出一個自定義的 Binder。這個 Binder 在被 ILocationManager.Stub.asInterface 處理后,可以返回一個自定義的 LocationManagerService 對象。
先看一下 Binder 的 asInterface() 的實現:

// Android 8.0 android.location.ILocationManager.java

public static android.location.ILocationManager asInterface(android.os.IBinder obj) {
    if ((obj == null)) {
        return null;
    }
    android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
    if (((iin != null) && (iin instanceof android.location.ILocationManager))) {
        return ((android.location.ILocationManager) iin);
    }
    return new android.location.ILocationManager.Stub.Proxy(obj);
}

如果把 queryLocalInterface()方法返回一個自定義的Service,使得走 if 語句內部,不走 else,那就算是Hook 成功了。

誤區: asInterface(binder) 就是把 binder 做了一次類型轉換
實際上XXX service = XXX.Stub.asInterface(binder)的返回值根據 binder 的來源有兩種情況:

  1. 跨進程時,service 的類型是 XXX.Stub.Proxy
  2. 相同進程時,service 的類型是 XXX.Stub
    XXX.Stub.asInterface(binder);得到的返回值并不一定是binder自己,并且調用系統服務時肯定不是binder自己。

3 創建自定義的 Service 和 Binder 對象

3.1 自定義的 Service 對象

假設我們想讓系統的 LocationManager 返回的位置信息全是在天安門(116.23, 39.54)。那我們需要使得 LocatitionManagerService 的 getLastLocation() 方法 返回的全是 (116.23, 39.54)。
由于我們不能直接拿到系統的這個Service對象,可以先用反射的方式拿到系統的LocationManagerService。然后攔截 getLastLocation() 方法。

import android.location.Location;
import android.location.LocationManager;
import android.os.IBinder;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 動態代理時用到的 Handler
 * @author ZHP
 * @since 16/12/25 17:36
 */

public class ServiceHookHandler implements InvocationHandler {

    private Object mOriginService;

    /**
     * @param binder 系統原始的Binder對象
     */
    @SuppressWarnings("unchecked")
    public ServiceHookHandler(IBinder binder) {
        try {
            // 由于可以拿到 Binder 對象,但無法拿到 Service 的對象, 所以我們要手動獲取
            // 即: this.mOriginService = ILocationManager.Stub.asInterface(binder);
            Class ILocationManager$Stub = Class.forName("android.location.ILocationManager$Stub");
            Method asInterface = ILocationManager$Stub.getDeclaredMethod("asInterface", IBinder.class);
            this.mOriginService = asInterface.invoke(null, binder);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        switch(method.getName()) {
            case "getLastLocation":
                // 一直返回天安門的坐標
                Location location = new Location(LocationManager.GPS_PROVIDER);
                location.setLongitude(116.23);
                location.setLatitude(39.54);
                return location;

            default:
                return method.invoke(this.mOriginService, args);
        }
    }
}

3.2 自定義的 Binder 對象

原生的Binder對象在調用 queryLocalInterface() 方法時會返回原生的Service對象。我們希望返回3.1中的自定義Service。所以這里攔截 queryLocalInterface() 方法。

import android.os.IBinder;
import android.os.IInterface;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 動態代理時用到的Handler,用于創建自定義Binder
 * @author ZHP
 * @since 16/12/25 17:36
 */

public class BinderHookHandler implements InvocationHandler {

    private IBinder mOriginBinder;

    private Class ILocationManager;

    @SuppressWarnings("unchecked")
    public BinderHookHandler(IBinder binder) {
        this.mOriginBinder = binder;
        try {
            this.ILocationManager = Class.forName("android.location.ILocationManager");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        switch (method.getName()) {
            // 使得返回自定義的Service
            case "queryLocalInterface":
                ClassLoader classLoader = mOriginBinder.getClass().getClassLoader();
                Class[] interfaces = new Class[] {IInterface.class, IBinder.class, ILocationManager};
                ServiceHookHandler handler = new ServiceHookHandler(this.mOriginBinder);
                return Proxy.newProxyInstance(classLoader, interfaces, handler);

            default:
                return method.invoke(mOriginBinder, args);

        }
    }
}

4. 將自定義的 Binder 注入到 ServiceManager 中

有了自定義的 Binder 后,將它注入到 ServiceManger 的 sCache 變量中就完成 Hook 了~

import android.content.Context;
import android.os.IBinder;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

/**
 * @author ZHP
 * @since 16/12/25 17:56
 */

public class HookManager {

    @SuppressWarnings("unchecked")
    public static boolean hookLocationManager() {
        try {
            // 1. 獲取系統自己的Binder
            Class ServiceManager = Class.forName("android.os.ServiceManager");
            Method getService = ServiceManager.getDeclaredMethod("getService", String.class);
            IBinder binder = (IBinder) getService.invoke(null, Context.LOCATION_SERVICE);

            // 2. 創建我們自己的Binder,動態代理了 queryLocalInterface 方法。
            ClassLoader classLoader = binder.getClass().getClassLoader();
            Class[] interfaces = {IBinder.class};
            BinderHookHandler handler = new BinderHookHandler(binder);
            IBinder customBinder = (IBinder) Proxy.newProxyInstance(classLoader, interfaces, handler);

            // 3. 獲取 ServiceManager 中的 sCache
            Field sCache = ServiceManager.getDeclaredField("sCache");
            sCache.setAccessible(true);
            Map<String, IBinder> cache = (Map<String, IBinder>) sCache.get(null);

            // 4. 將自定義的 Binder 對象替換掉舊的系統 Binder
            cache.put(Context.LOCATION_SERVICE, customBinder);
            sCache.setAccessible(false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

5. 測試

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Hook
        HookManager.hookLocationManager();
    }

    /**
     * 請求定位信息
     */
    private void requestLocation() {
        // 定位權限檢測
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.i("鄭海鵬", "沒有定位權限");
            Toast.makeText(this, "沒有定位權限", Toast.LENGTH_SHORT).show();
            return;
        }

        // 獲取位置并顯示
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        String message = "(" + location.getLongitude() + ", " + location.getLatitude() + ")";
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        Log.i("鄭海鵬", message);
    }

    public void onClick(View view) {
        this.requestLocation();
    }
}

當onClick被調用的時候,Toast和Log都會顯示天安門的坐標(116.23, 39.54)。證明Hook成功!

你甚至可以用Binder Hook的方式Hook掉 ActivityManager

團隊博客同名文章:http://www.lxweimin.com/p/fcb832a2b411
轉載必須注明出處:http://www.lxweimin.com/p/5c2c3fc4286b`

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容