{"id":127,"date":"2024-06-19T01:47:31","date_gmt":"2024-06-18T17:47:31","guid":{"rendered":"https:\/\/code.lingapi.top\/?p=127"},"modified":"2024-06-19T01:47:31","modified_gmt":"2024-06-18T17:47:31","slug":"%e4%bd%bf%e7%94%a8libsu%e6%9d%a5%e5%9c%a8android%e4%b8%ad%e8%ae%bf%e9%97%ae%e8%b6%85%e7%ba%a7%e7%94%a8%e6%88%b7%e6%9d%83%e9%99%90","status":"publish","type":"post","link":"https:\/\/code.lingapi.top\/?p=127","title":{"rendered":"\u4f7f\u7528libsu\u6765\u5728Android\u4e2d\u8bbf\u95ee\u8d85\u7ea7\u7528\u6237\u6743\u9650"},"content":{"rendered":"\n<p>\u5728Android\u4e2d\uff0cAPP\u81ea\u8eab\u65e0\u6cd5\u83b7\u5f97root\u6743\u9650\uff08uid\u53d8\u62100\uff09\u3002<\/p>\n\n\n\n<p>\u6211\u5c1d\u8bd5\u8fc7\u5728\u5185\u6838\u6001\u4fee\u6539APP\u7684uid\uff0c\u4f46\u662f\u8fd9\u6837\u505a\u4f1a\u5bfc\u81f4APP\u8fdb\u7a0b\u5361\u6b7b\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u9700\u8981\u4f7f\u7528root\u6743\u9650\u8fdb\u884c\u4e00\u4e9b\u590d\u6742\u64cd\u4f5c\u65f6\uff0c\u6700\u597d\u7684\u65b9\u6cd5\u5c31\u662f\u542f\u52a8\u4e00\u4e2a\u5177\u5907root\u6743\u9650\u7684\u5b88\u62a4\u8fdb\u7a0b\uff0c\u7531\u5b88\u62a4\u8fdb\u7a0b\u6765\u64cd\u4f5c\u3002\u800c\u8fd9\u6837\u542f\u52a8\u7684\u5b88\u62a4\u8fdb\u7a0b\u65e0\u6cd5\u7ed8\u5236UI\uff0c\u867d\u7136\u6709\u4e00\u4e9b\u624b\u6bb5\u53ef\u4ee5\u7ed8\u5236\uff0c\u4f46\u662f\u5b58\u5728\u5f88\u591a\u95ee\u9898\uff0c\u6bd4\u5982\u89e6\u6478\u7a7f\u900f\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u901a\u884c\u7684\u65b9\u6cd5\u662f\uff0c\u7531APP\u542f\u52a8\u5b88\u62a4\u8fdb\u7a0b\uff08\u4e0b\u6587\u79f0\u4e3aCore\uff09\uff0c\u7136\u540e\u548c\u5b88\u62a4\u8fdb\u7a0b\u4e4b\u95f4\u5efa\u7acb\u67d0\u79cd\u8fde\u63a5\uff0c\u901a\u8fc7\u8fdb\u7a0b\u95f4\u901a\u4fe1\u6765\u6307\u6325Core\u884c\u9a76\u6743\u9650\uff0c\u6570\u636e\u5728Core\u4e2d\u5206\u6790\u5b8c\u6bd5\u540e\uff0c\u4ea4\u7531APP\u6765\u5448\u73b0\u7ed9\u7528\u6237\u3002<\/p>\n\n\n\n<p>\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u624b\u6bb5\u6709\u5f88\u591a\uff0c\u5171\u4eab\u5185\u5b58\uff0cUnix Domain Socket\uff0c\u7ba1\u9053\u7b49\u7b49\u7b49\u7b49\uff0c\u4f46\u662f\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u4e0d\u592a\u65b9\u4fbf\uff0c\u9700\u8981\u81ea\u5df1\u624b\u52a8\u7ef4\u62a4\u8fde\u63a5\uff0c\u5e76\u4e14\u7f16\u7801\u5c06\u53d8\u6210C\/S\u7ed3\u6784\uff0c\u4ee3\u7801\u4e2d\u5c06\u5927\u91cf\u4f7f\u7528\u56de\u8c03\uff0c\u53d8\u5f97\u5f88\u4e0d\u76f4\u89c2\u3002<\/p>\n\n\n\n<p>\u800c<a href=\"https:\/\/github.com\/topjohnwu\/libsu\">libsu<\/a>\u53ef\u4ee5\u65b9\u4fbf\u7684\u81ea\u52a8\u5b8c\u6210\u4ee5\u4e0a\u64cd\u4f5c\uff0c\u5e76\u4e14\u5f97\u76ca\u4e8eAndroid\u7684AIDL\u673a\u5236\uff0c\u53ef\u4ee5\u50cf\u5de5\u4f5c\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u4e00\u6837\u7f16\u7801\u3002<\/p>\n\n\n\n<p>libsu\u662f\u7531Magisk\u7684\u4e3b\u8981\u7ef4\u62a4\u8005<a href=\"https:\/\/github.com\/topjohnwu\">John Wu<\/a>\u5f00\u53d1\u7684\u4e00\u6b3e\u5f00\u6e90\u5e93\uff0c\u65e8\u5728\u7b80\u5316root\u5f00\u53d1\u6d41\u7a0b\u3002\u5f15\u5165libsu\u5e93\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728\u9879\u76ee\u8bbe\u7f6e\u4e2d\u5f15\u5165\u5982\u4e0b\u4ed3\u5e93<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>maven(\"https:\/\/jitpack.io\")<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u5728\u9700\u8981\u4f7f\u7528\u7684\u6a21\u5757\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies {\n    val libsuVersion = \"5.3.0\"\n\n    \/\/ The core module that provides APIs to a shell\n    implementation(\"com.github.topjohnwu.libsu:core:$libsuVersion\")\n\n    \/\/ Optional: APIs for creating root services. Depends on \":core\"\n    implementation(\"com.github.topjohnwu.libsu:service:$libsuVersion\")\n\n    \/\/ Optional: Provides remote file system support\n    implementation(\"com.github.topjohnwu.libsu:nio:$libsuVersion\")\n}<\/code><\/pre>\n\n\n\n<p>libsu\u672c\u8d28\u4e0a\u4f9d\u7136\u662f\u5728\u8fdb\u7a0b\u95f4\u8fdb\u884c\u901a\u4fe1\uff0c\u4f60\u5fc5\u987b\u5148\u638c\u63e1AIDL\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u672c\u6587\u4e0d\u505a\u8d58\u8ff0\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u5b9a\u4e49\u5982\u4e0bAIDL<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package ling.core;\n\n\ninterface IRootProcess {\n    int getUid();\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u9700\u8981\u5148build\u4e00\u4e0b\u9879\u76ee\uff0c\u624d\u4f1a\u751f\u6210\u5b83\u7684\u4ee3\u7406\u7c7b\u3002<\/p>\n\n\n\n<div class=\"wp-block-argon-admonition admonition shadow-sm\" style=\"border-left-color:#7889e8\"><div class=\"admonition-body\">\u4f60\u9700\u8981\u5728\u8fd0\u884c\u914d\u7f6e\u4e2d\uff0c\u542f\u7528 Always install with package manager \u9009\u9879\uff0c\u5426\u5219\u66f4\u6539\u53ef\u80fd\u4e0d\u4f1a\u751f\u6548\u3002<\/div><\/div>\n\n\n\n<p>\u5b9e\u73b0IRootProcess\uff0c\u8fd9\u5c06\u6210\u4e3aAPP\u8c03\u7528\u7684\u63a5\u53e3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class RootProcess extends IRootProcess.Stub {\n    @Override\n    public int getUid() throws RemoteException {\n        \/\/\u6b64\u5904\u5df2\u7ecf\u5728root\u8fdb\u7a0b\u4e2d\n        return Process.myUid();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u5b9a\u4e49\u4e00\u4e2aService\uff0c\u7ee7\u627f\u81eaRootService\u3002<\/p>\n\n\n\n<p>RootService\u76f8\u8f83Android\u7684Service\u6709\u5f88\u591a\u6539\u52a8\uff0c\u4e0d\u8981\u5c06\u5b83\u7528\u5728\u5176\u4ed6\u5730\u65b9\uff01<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class RootService extends com.topjohnwu.superuser.ipc.RootService {\n    static {\n        \/\/\u7531\u4e8e\u5c06\u8981\u5728APP\u4e2d\u8bf7\u6c42\u542f\u52a8\u6b64\u670d\u52a1\uff0c\n        \/\/\u6240\u4ee5APP\u4e2d\u7684\u7c7b\u52a0\u8f7d\u5668\u4e5f\u4f1a\u8f7d\u5165\u5b83\n        \/\/\u4ec5\u5728root\u8fdb\u7a0b\u4e2d\u52a0\u8f7d\u9700\u8981\u7684\u5e93\n        if (Process.myUid() == 0) {\n            System.loadLibrary(\"CoreTask\");\n        }\n    }\n\n    @Override\n    public IBinder onBind(@NonNull Intent intent) {\n        \/\/\u6b64\u5904\u5df2\u7ecf\u5728Root\u8fdb\u7a0b\u4e2d\uff01\n        \/\/\u8fd9\u662f\u521a\u521aIRootProcess\u7684\u5b9e\u73b0\u7c7b\n        \/\/\u6b64\u5bf9\u8c61\u5c06\u4f5c\u4e3a\u63a5\u53e3\u4ea4\u7531APP\u8c03\u7528\n        return new RootProcess();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fde\u63a5Core\u4e4b\u524d\uff0c\u9700\u8981\u4e00\u4e2a\u7c7b\u6765\u83b7\u53d6\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class RootConnection implements ServiceConnection {\n\n    @Override\n    public void onServiceConnected(ComponentName name, IBinder service) {\n        \/\/\u8fde\u63a5\u6210\u529f\uff0croot\u5c31\u662f\u521a\u521a\u8fd4\u56de\u7684RootProcess\u7684\u4ee3\u7406\u7c7b\n        \/\/\u50cf\u5e73\u5e38\u4e00\u6837\u8c03\u7528\u5176\u4e2d\u7684\u65b9\u6cd5\uff0c\u5b9e\u9645\u6267\u884c\u5c06\u5728Core\u4e2d\u5b8c\u6210\u3002\n        IRootProcess root = IRootProcess.Stub.asInterface(service);\n    }\n\n    @Override\n    public void onServiceDisconnected(ComponentName name) {\n         \/\/\u8fde\u63a5\u65ad\u5f00\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u5728\u4e00\u4e2a\u5408\u9002\u7684\u4f4d\u7f6e\u542f\u52a8\u8fd9\u4e00\u5207\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RootService.bind(new Intent(this, RootService.class), new RootConnection());<\/code><\/pre>\n\n\n\n<p>\u5927\u529f\u544a\u6210\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Android\u4e2d\uff0cAPP\u81ea\u8eab\u65e0\u6cd5\u83b7\u5f97root\u6743\u9650\uff08uid\u53d8\u62100\uff09\u3002 \u6211\u5c1d\u8bd5\u8fc7\u5728\u5185\u6838\u6001\u4fee\u6539APP\u7684uid\uff0c\u4f46\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/code.lingapi.top\/index.php?rest_route=\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.lingapi.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.lingapi.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.lingapi.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code.lingapi.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=127"}],"version-history":[{"count":5,"href":"https:\/\/code.lingapi.top\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/code.lingapi.top\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions\/132"}],"wp:attachment":[{"href":"https:\/\/code.lingapi.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.lingapi.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.lingapi.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}