Android開源:利用Xposed框架hook系統方法 改變GPS定位 實現Android模擬定位

jeipee 7年前發布 | 54K 次閱讀 安卓開發 Android開發 移動開發

最近需要對微信,陌陌等程序進行位置模擬 實現世界各地發朋友圈,搜索附近人的功能,本著站在巨人肩膀上的原則 愛網上搜索一番,也找到一些 代碼和文章,但是代碼大都雷同而且都有一個弊端 比如說 微信 對目標函數實現hook之后第一次打開微信 第一次定位是可以改變的 但是 我如果想更換地址的話 就需要重啟手機了,重新加載hook了,試了很多次都是這樣滿足不了需求,為了改進這個地方我們從gps定義的源代碼流程開始看尋找hook系統函數的突破口
關于gps的工作流程 這位大大寫的很清楚 http://blog.csdn.net/xnwyd/article/details/7198728我也是看完之后才找到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);
}</code></pre> 

那么我們可以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());
               }

        }
    });</code></pre> 

如果我想主動調用這個函數 必須要得到這個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://github.com/mark-ypq/GPSHook

 

 

 

 本文由用戶 jeipee 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!