Accessing hidden System Service APIs in Android

來源:互聯網
上載者:User

 

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 fromhttp://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 theLocationManager 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 theLocationManagerService, 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 theLocationManagerService, 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 (viarequestLocationUpdates()) 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 calledregisterGpsStatusListener(). 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 theLocationManagerService 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 theLocationManagerService is GpsStatusListenerTransport. This is an extension of the IGpsStatusListener.Stub class, which was generated from an AIDL specification –IGpsStatusListener.aidl. It's possible to copyIGpsStatusListener.aidl from the Android source code and add to your project to generate theIGpsListenerStatus.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.aidl, ILocationListener.aidl, andAddress.aidl are also needed to do what we want. Once these classes are available in our project (I put them in theandroid.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 theLocationManagerService 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());<br />Field f = c.getDeclaredField("mService");<br />f.setAccessible(true);<br />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 toaddGpsStatusListener() will result in us gettingonSvStatusChanged() 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 regularLocationManager API to do this by adding aLocationListener. 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 theACCESS_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 athttp://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.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.