Android11系统 adb添加访问密码

avatar
作者
筋斗云
阅读量:0

Android11上,通过命令行访问adb的时候增加密码。

修改以后,直接访问adb会报错,需要使用<adb shell 密码>来输入密码鉴权,默认密码为admin,即输入adb shell admin以后可以正常访问adb。

详细修改如下:

1、修改system/core/adb/adb.cpp

此处主要通过重置accept_shell的值,让adb断开以后需要重新输入密码进行访问 

diff --git a/system/core/adb/adb.cpp b/system/core/adb/adb.cpp index c3e9731a30..990dec445b 100644 --- a/system/core/adb/adb.cpp +++ b/system/core/adb/adb.cpp @@ -100,6 +100,11 @@ apacket* get_apacket(void)      return p;  }   + +#if !ADB_HOST +int accept_shell; +#endif +  void put_apacket(apacket *p)  {      delete p; @@ -110,6 +115,9 @@ void handle_online(atransport *t)      D("adb: online");      t->online = 1;      t->SetConnectionEstablished(true); +    #if !ADB_HOST +    accept_shell = 0; +    #endif  }    void handle_offline(atransport *t) @@ -126,6 +134,9 @@ void handle_offline(atransport *t)      // Close the associated usb      t->online = 0;   +    #if !ADB_HOST +    accept_shell = 0; +    #endif      // This is necessary to avoid a race condition that occurred when a transport closes      // while a client socket is still active.      close_all_sockets(t)
2、修改system/core/adb/services.cpp

访问密码通过读取property(persist.otg.passwd)的值获取, 默认为admin

diff --git a/system/core/adb/services.cpp b/system/core/adb/services.cpp index 853d658976..c1544f6ac6 100644 --- a/system/core/adb/services.cpp +++ b/system/core/adb/services.cpp @@ -27,6 +27,7 @@  #include <cstring>  #include <thread>   +#include <android-base/properties.h>  #include <android-base/stringprintf.h>  #include <android-base/strings.h>  #include <cutils/sockets.h> @@ -74,9 +75,41 @@ unique_fd create_service_thread(const char* service_name, std::function<void(uni      return unique_fd(s[0]);  }   +#if !ADB_HOST +extern int accept_shell; +static void accept_shell_success(unique_fd fd) { +    WriteFdExactly(fd, "success\n"); +} + +static void accept_shell_fail(unique_fd fd) { +    WriteFdExactly(fd, "failed\n"); +} +#endif +  unique_fd service_to_fd(std::string_view name, atransport* transport) {      unique_fd ret;   +#if !ADB_HOST +    // login +    char* temp; +    temp = (char*)strstr(name.data(), "raw"); +    if (accept_shell == 0) { +        if (temp != NULL) { +            std::string passwd = android::base::GetProperty("persist.otg.passwd", "admin"); +            if (!strncmp(temp + 4, passwd.c_str(), passwd.length())) { +                accept_shell = 1; +                ret = create_service_thread("accept_shell_success", accept_shell_success); +            } else { +                accept_shell = 0; +                ret = create_service_thread("accept_shell_fail", accept_shell_fail); +            } +            if (ret >= 0) { +                close_on_exec(ret); +            } +        } +        return ret; +    } +#endif      if (is_socket_spec(name)) {          std::string error;          if (!socket_spec_connect(&ret, name, nullptr, nullptr, &error)) {

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!