Android11系统 adb添加访问密码

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)) {

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值