1. 程式人生 > >Hook實現Android 微信,陌陌 ,探探位置模擬

Hook實現Android 微信,陌陌 ,探探位置模擬

 最近需要對微信,陌陌等程式進行位置模擬 實現世界各地發朋友圈,搜尋附近人的功能,本著站在巨人肩膀上的原則 愛網上搜索一番。
 也找到一些 程式碼和文章,但是程式碼大都雷同而且都有一個弊端 比如說 微信 對目標函式實現hook之後第一次開啟微信 第一次定位是可以改變的
  但是 我如果想更換地址的話 就需要重啟手機了,重新載入hook了,試了很多次都是這樣滿足不了需求。
 為了改進這個地方我們從gps定義的原始碼流程開始看尋找hook系統函式的突破口 http://www.ibm.com/developerworks/cn/opensource/os-cn-android-location/
 我也是看完之後才找到hook的地方 LocationMangerService  這個類 
@Override
    public void reportLocation(Location location, boolean passive) {
        checkCallerIsProvider(); //檢測許可權和uid

        if (!location.isComplete()) {
            Log.w(TAG, "Dropping incomplete location: " + location);
            return;
        }
            //傳送位置資訊
        mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
        Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
        m.arg1 = (passive ? 1
: 0); mLocationHandler.sendMessageAtFrontOfQueue(m); }
那麼我們可以hook掉這個location的引數 修改為我們想要定位的地方就可以實現效果了,
  XposedHelpers.findAndHookMethod("com.android.server.LocationManagerService", lpparam.classLoader, "reportLocation", Location.class, boolean.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod
(param); Location location = (Location) param.args[0]; XposedBridge.log("實際 系統 經度"+location.getLatitude() +" 系統 緯度"+location.getLongitude() +"系統 加速度 "+location.getAccuracy()); XSharedPreferences xsp =new XSharedPreferences("com.markypq.gpshook","markypq"); if (xsp.getBoolean("enableHook",true)){ double latitude = Double.valueOf(xsp.getString("lan","117.536246"))+ (double) new Random().nextInt(1000) / 1000000 ; double longtitude = Double.valueOf(xsp.getString("lon","36.681752"))+ (double) new Random().nextInt(1000) / 1000000 ; location.setLongitude(longtitude); location.setLatitude(latitude); XposedBridge.log("hook 系統 經度"+location.getLatitude() +" 系統 緯度"+location.getLongitude() +"系統 加速度 "+location.getAccuracy()); } } });
如果我想主動呼叫這個函式 必須要得到這個LocationMangerService 的物件 獲取這個物件可以通過hook LocationManager 的建構函式獲取,
 XposedBridge.hookAllConstructors(LocationManager.class,new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                if (param.args.length==2) {
                    Context context = (Context) param.args[0]; //這裡的 context
                    XposedBridge.log(" 對 "+getProgramNameByPackageName(context)+" 模擬位置");
                    //把許可權的檢查 hook掉
                    XposedHelpers.findAndHookMethod(context.getClass(), "checkCallingOrSelfPermission", String.class, new XC_MethodHook() {
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);
                            if (param.args[0].toString().contains("INSTALL_LOCATION_PROVIDER")){
                                param.setResult(PackageManager.PERMISSION_GRANTED);
                            }
                        }
                    });
                    XposedBridge.log("LocationManager : " + context.getPackageName() + " class:= " + param.args[1].getClass().toString());
                  //獲取到  locationManagerService 主動呼叫 物件的 reportLocation 方法  可以去模擬提供位置資訊
                    //這裡程式碼中並沒有涉及到主動呼叫
                  Object   locationManagerService = param.args[1];
                }
            }
        });

這裡寫圖片描述
這裡寫圖片描述
當然還需要hook一些其他的輔助函式 ,這些函式都可以在 android studio 中看到java的程式碼 我們就無需過多解釋了 上 新修改的程式碼
原始碼連線 連結: https://pan.baidu.com/s/1c23mLPm 密碼: rhbh
不好意思 gitHub一直沒傳上去 原始碼放百度雲了
新的程式碼取消了對 com.android.server.LocationManagerService 的直接hook
因為手機廠商修改原始碼的原因 直接hook LocationManagerService 會導致 classnotfind的錯誤
修改為hook LocationManager的構造方法
LocationManager的構造方法 第一個引數是Context引數 第二就是 LocationManagerService 的物件。

對HooK技術感興趣,或者有開發需求 ,或者Xposed安裝有問題 的朋友可以加Q群 125051266 一起交流