Accessing hidden System Service APIs in Android

转自:http://blog.codetastrophe.com/2008/12/accessing-hidden-system-service-apis-in.html

 

Android's SDK allows developers to do a lot with the platform, but there are some interesting capabilities of the system that aren't accessible through the public API. It's still possible to access these capabilities with a little bit of work and the Android source code, available from http://source.android.com. The source code I wrote is  here.

The capability I wanted to expose is collecting GPS satellite information - data on the satellites (SVs - space vehicles) themselves. There are hidden methods in the LocationManager service API to set up callbacks to collect this data, but they aren't accessible through the public API. I considered the various ways I could get this data and the easiest one I came up with is to send messages directly to the  LocationManager service itself using the IPC interface.

LocationManager and LocationManagerService

The  LocationManager API in the  android.locationnamespace is the public interface to the LocationManagerService, in  com.android.server. The service runs in a separate process, and the API communicates with it using Android's IPC mechanism ( Binder). When an Android application wants to communicate with the LocationManagerService, it uses this API call to get a reference to the API object: LocationManager mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  Normally, an application will then use the  LocationManagerAPI to register for location events (via requestLocationUpdates()) and then use the data to do really fun and interesting things, like  this.
If you dig into the  LocationManager source code, you'll see that there's a method called registerGpsStatusListener(). This sets up a listener that's able to get updates on GPS satellite information – PRNs, elevations, azimuths, etc, of each of the satellites in view of the GPS. This is what I want. Unfortunately, this method, as well as the argument type ( GpsStatusListenerTransport), aren't visible in the android.jar that comes with the SDK. It's still possible to get at this information by communicating directly with the LocationManagerService itself.

The APIs available in the Android SDK communicate with system services using IPC. Specifically, they use interfaces defined with  AIDL to communicate with the service. The AIDL specifications for the service IPC API's isn't available in the SDK, but you can get them from the Android source code. The AIDL is used to generate a client stub that Java classes can use to send messages to and receive messages from the service.

Accessing LocationManagerService directly

The class in  LocationManager that communicates with the LocationManagerService is  GpsStatusListenerTransport. This is an extension of the  IGpsStatusListener.Stub class, which was generated from an AIDL specification – IGpsStatusListener.aidl. It's possible to copy IGpsStatusListener.aidl from the Android source code and add to your project to generate the IGpsListenerStatus.Stub class. This project can then use this to communicate directly with the service that implements that interface.
IGpsStatusListener isn't the only AIDL interface we'll need –  ILocationManager.aidlILocationListener.aidl, and Address.aidl are also needed to do what we want. Once these classes are available in our project (I put them in the android.location namespace to avoid changing the code, but it doesn't really matter which namespace they belong to). Once these interfaces are available to our project, it's really easy to get what we want.

While it is possible to write a totally new client for the LocationManagerService using the AIDL interfaces, it's actually easier for us to re-use an existing client. Setting up a new connection involves a bunch of lines of code and my carpal tunnels hate when I write too much code. The easy way is to use the Java reflection API to get the handle to the system service: Class c = Class.forName(mLocationManager.getClass().getName()); Field f = c.getDeclaredField("mService"); f.setAccessible(true); ILocationManager mILM = (ILocationManager)f.get(mLocationManager);  Accessing private fields like this in Java is generally frowned upon in production code, but we're hackers and we want the data and we can do whatever we want to get it and the establishment can't stop us.

So now that we have access to the  ILocationManager API, we can see that there is an  addGpsStatusListener()method we can call to add a listener that retrieves the GPS satellite status updates. This takes an  IGpsStatusListenerobject, which we can create by instantiating a class that extends  IGpsStatusListener.stub. Passing that object to addGpsStatusListener() will result in us getting onSvStatusChanged() callbacks with the satellite status. Nice.

Before we get any of this spiffy satellite info, the GPS must be enabled. In my sample app, I used the regular LocationManager API to do this by adding a LocationListener. The result of all of my efforts is this useless and boring application that shows some information about some satellites:
Oops...

If you start digging into the service code, you might notice that no permissions are required to add a listener for GPS status updates. This is a minor and low-risk information leak vulnerability. Applications shouldn't have access to GPS location data unless they have the correct permission. If an application creates a service that listens for GPS status updates (but not GPS location updates, which require the ACCESS_FINE_LOCATION permission), it will get these updates when any other application turns on the GPS. It's possible, with some fancy math, to determine the location of the phone with this data. You'll need to know the exact time, the relative locations of the GPS satellites to the user, and the absolute locations of the GPS satellites in space at that time. The time and relative locations can come from the phone, and the precise locations of the satellites can be found at http://www.navcen.uscg.gov/GPS/almanacs.htm. I'll leave doing the fancy math as an exercise to the reader.
Update: Proposed patch to add permission check, http://review.source.android.com/5124.

 

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)实现全屏显示的几种方法整
  2. Android(安卓)Theme的设置
  3. android中调试之日志
  4. android sdk setup时出现:Failed to fetc
  5. Android(安卓)XML解析
  6. Android不依赖Activity的全局悬浮窗实现
  7. Android图像开源视图:SmartImageView
  8. Android中做一个无标题窗口
  9. 系出名门Android(7) - 控件(View)之ZoomC
  10. Android联系人数据库全解析(5)