'How to access services in android with cpp?
i am developing a forensic project for android. The goal of the project is to connect to the phone with adb and investigate cases, sometimes i need to access android services. I am developing my project with c/cpp, i can run the popen("service call bla bla") command to access the services and get the result. The method is very strange :D I found the source code of the Service. Can i compile this and add it to my project? How can i compile this?
https://android.googlesource.com/platform/frameworks/native/+/master/cmds/service/service.cpp
Solution 1:[1]
Android use AIDL to define the interface between the client and the server. And the AIDL backend can be java/cpp/rust. So you can access android serices with cpp. You should find the service you will call, and find the related aidl, then find the geneated header files and wrapped impl for client. Then you can call the service by wrapped impl.
Let's take ActivityManager as example:
- The AIDL file is IActivityManager.aidl
- The geneated header files for cpp is IActivityManager.h
- The wrapped impl is IActivityManager.cpp
- Then you can call ActivityManager:
sp<IServiceManager> sm = defaultServiceManager();
sp<IBinder> binder = sm->getService(String16("activity"));
sp<IActivityManager> am = interface_cast<IActivityManager>(binder);
if (am != NULL) {
fd = am->openContentUri(uri);
}
Note that in this example, IActivityManager.h only include a few method in IActivityManager.aidl. You can generated with all method by yourself.
Fistly, you should check whether it can be found in ndk. If not, check whether it can be found in AOSP, if found, copy the headers and the impl to your project. If not, generated it with aidl-cpp by yourself.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yong |
