Accessing hidden System Service APIs in Android

Source: Internet
Author: User

 

Accessing hidden System Service APIs in Android

From: 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 cocould 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 Mechanic (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 be 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 ). 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're 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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.