如何用cpp访问android中的服务?

发布于 2025-01-12 13:53:20 字数 421 浏览 4 评论 0原文

我正在为 Android 开发一个取证项目。该项目的目标是使用 adb 连接到手机并调查案例,有时我需要访问 android 服务。我正在使用 c/cpp 开发我的项目,我可以运行 popen("service call bla bla") 命令来访问服务并获取结果。方法很奇怪:DI找到了Service的源码。我可以编译它并将其添加到我的项目中吗?我该如何编译这个?

https://android.googlesource.com/平台/frameworks/native/+/master/cmds/service/service.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他是夢罘是命 2025-01-19 13:53:20

Android 使用 AIDL 来定义客户端和服务器之间的接口。 AIDL后端可以是java/cpp/rust。这样就可以通过cpp访问android服务了。你应该找到你要调用的服务,并找到相关的aidl,然后找到生成的头文件和客户端的包装impl。然后就可以通过wrapped impl来调用服务了。

我们以 ActivityManager 为例:

  1. AIDL 文件为 IActivityManager.aidl
  2. cpp 生成的头文件为 IActivityManager.h< /a>
  3. 包装的 impl 是 IActivityManager.cpp
  4. 然后你可以调用 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);
    }

请注意,在本例中,IActivityManager.h 仅包含 IActivityManager.aidl 中的几个方法。您可以自行使用所有方法生成。

首先,你应该检查是否可以在ndk中找到它。如果没有,检查是否可以在 AOSP 中找到,如果找到,请将 headers 和 impl 复制到您的项目中。如果没有,请使用 aidl-cpp 自己编写。

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:

  1. The AIDL file is IActivityManager.aidl
  2. The geneated header files for cpp is IActivityManager.h
  3. The wrapped impl is IActivityManager.cpp
  4. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文