Android开源:利用Xposed框架hook系统方法 改变GPS定位 实现Android模拟定位

jeipee 7年前
   <pre>  <code class="language-java">最近需要对微信,陌陌等程序进行位置模拟 实现世界各地发朋友圈,搜索附近人的功能,本着站在巨人肩膀上的原则 爱网上搜索一番,也找到一些 代码和文章,但是代码大都雷同而且都有一个弊端 比如说 微信 对目标函数实现hook之后第一次打开微信 第一次定位是可以改变的 但是 我如果想更换地址的话 就需要重启手机了,重新加载hook了,试了很多次都是这样满足不了需求,为了改进这个地方我们从gps定义的源代码流程开始看寻找hook系统函数的突破口  关于gps的工作流程 这位大大写的很清楚 http://blog.csdn.net/xnwyd/article/details/7198728我也是看完之后才找到hook的地方 LocationMangerService  这个类</code></pre>    <pre>  <code class="language-java">@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>    <pre>  <code class="language-java">那么我们可以hook掉这个location的参数 修改为我们想要定位的地方就可以实现效果了,</code></pre>    <pre>  <code class="language-java">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>    <pre>  <code class="language-java">如果我想主动调用这个函数 必须要得到这个LocationMangerService 的对象 获取这个对象可以通过hook LocationManager 的构造函数获取,</code></pre>    <pre>  <code class="language-java">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];                  }              }          });</code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/6e2a871f67b4a469c92739ec9297b14a.png"> <img src="https://simg.open-open.com/show/ef21f780610fa4598aaf9fcb60a95f66.png"></p>    <p>当然还需要hook一些其他的辅助函数 ,这些函数都可以在 android studio 中看到java的代码 我们就无需过多解释了 上 源代码连接 <a href="/misc/goto?guid=4959741334558603129" rel="nofollow,noindex">https://github.com/mark-ypq/GPSHook</a></p>    <p> </p>    <p> </p>    <p> </p>