{"id":6640,"date":"2024-11-30T09:17:17","date_gmt":"2024-11-30T01:17:17","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6640"},"modified":"2024-11-30T09:17:17","modified_gmt":"2024-11-30T01:17:17","slug":"android%e7%83%ad%e4%bf%ae%e5%a4%8d%e6%9c%80%e7%bb%88%e7%9a%84%e6%96%b9%e6%b3%95%e6%9b%bf%e6%8d%a2%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6640","title":{"rendered":"[Android]\u70ed\u4fee\u590d\u6700\u7ec8\u7684\u65b9\u6cd5\u66ff\u6362\u5b9e\u73b0"},"content":{"rendered":"<p>\u5728 Android \u4e2d\uff0c\u70ed\u4fee\u590d\uff08Hotfix\uff09\u6280\u672f\u662f\u4e3a\u4e86\u5728\u5e94\u7528\u53d1\u5e03\u540e\u8fdb\u884c\u4fee\u590d\u548c\u66f4\u65b0 bug\uff0c\u800c\u4e0d\u9700\u8981\u901a\u8fc7\u5e94\u7528\u7684\u7248\u672c\u66f4\u65b0\u3002\u70ed\u4fee\u590d\u7684\u6838\u5fc3\u662f\u5bf9\u5df2\u7ecf\u7f16\u8bd1\u597d\u7684\u5e94\u7528\u8fdb\u884c\u52a8\u6001\u4fee\u590d\uff0c\u5176\u4e2d <strong>\u6700\u7ec8\u7684\u70ed\u4fee\u590d\u65b9\u6cd5\u66ff\u6362<\/strong> \u4e3b\u8981\u662f\u901a\u8fc7 <strong>\u52a8\u6001\u5b57\u8282\u7801\u66ff\u6362<\/strong> \u6216 <strong>\u7c7b\u52a0\u8f7d\u673a\u5236<\/strong> \u6765\u5b9e\u73b0\u7684\u3002\u4ee5\u4e0b\u662f\u70ed\u4fee\u590d\u8fc7\u7a0b\u4e2d\uff0c\u5982\u4f55\u66ff\u6362\u65b9\u6cd5\u7684\u5173\u952e\u673a\u5236\uff1a<\/p>\n<h3>1. <strong>\u901a\u8fc7 <code>DexClassLoader<\/code> \u52a8\u6001\u52a0\u8f7d\u8865\u4e01\u5305<\/strong><\/h3>\n<p>\u70ed\u4fee\u590d\u7684\u8865\u4e01\u5305\u901a\u5e38\u4ee5 <code>.dex<\/code> \u6587\u4ef6\u7684\u5f62\u5f0f\u5b58\u5728\u3002\u8865\u4e01\u5305\u4e2d\u5305\u542b\u4e86\u4fee\u590d\u65b9\u6cd5\u7684\u5b9e\u73b0\u6216\u8005\u4fee\u590d\u7684\u7c7b\u4ee3\u7801\u3002Android \u7684 <code>DexClassLoader<\/code> \u53ef\u4ee5\u7528\u6765\u52a8\u6001\u52a0\u8f7d\u8fd9\u4e9b <code>.dex<\/code> \u6587\u4ef6\u5e76\u5c06\u5176\u52a0\u8f7d\u5230\u5e94\u7528\u7684\u8fd0\u884c\u65f6\u73af\u5883\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u4ee3\u7801\u7684\u4fee\u590d\u548c\u66f4\u65b0\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u6b65\u9aa4<\/strong>\uff1a<\/p>\n<ol>\n<li><strong>\u4e0b\u8f7d\u8865\u4e01\u5305<\/strong>\uff1a\u70ed\u4fee\u590d\u6846\u67b6\u901a\u8fc7\u7f51\u7edc\u4e0b\u8f7d <code>.dex<\/code> \u6587\u4ef6\uff08\u8865\u4e01\u5305\uff09\u3002<\/li>\n<li><strong>\u52a0\u8f7d\u8865\u4e01<\/strong>\uff1a\u901a\u8fc7 <code>DexClassLoader<\/code> \u6216 <code>PathClassLoader<\/code> \u52a8\u6001\u52a0\u8f7d\u8fd9\u4e2a\u8865\u4e01\u5305\uff0c\u52a0\u8f7d\u5230\u5e94\u7528\u7684\u5185\u5b58\u4e2d\u3002<\/li>\n<li><strong>\u66ff\u6362\u65b9\u6cd5<\/strong>\uff1a\u901a\u8fc7\u53cd\u5c04\u6216\u8005\u76f4\u63a5\u4fee\u6539\u5185\u5b58\u4e2d\u7684\u65b9\u6cd5\u6307\u9488\uff0c\u5c06\u4fee\u590d\u540e\u7684\u65b9\u6cd5\u66ff\u6362\u6389\u539f\u6765\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<p><strong><code>DexClassLoader<\/code><\/strong>: \u5141\u8bb8\u52a0\u8f7d\u6307\u5b9a\u8def\u5f84\u4e0b\u7684 <code>.dex<\/code> \u6587\u4ef6\uff0c\u5e76\u80fd\u591f\u628a\u5176\u7c7b\u52a0\u8f7d\u5230\u5f53\u524d\u5e94\u7528\u7684\u8fd0\u884c\u65f6\u73af\u5883\u4e2d\u3002<\/p>\n<pre><code class=\"language-java\"> DexClassLoader classLoader = new DexClassLoader(dexPath, optimizedDirectory, librarySearchPath, getClassLoader());<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u901a\u8fc7\u4f20\u5165\u8865\u4e01\u6587\u4ef6\u8def\u5f84\u3001\u4f18\u5316\u76ee\u5f55\u7b49\u53c2\u6570\uff0c\u52a8\u6001\u52a0\u8f7d <code>.dex<\/code> \u6587\u4ef6\u3002\u52a0\u8f7d\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u53cd\u5c04\u6216\u8005\u76f4\u63a5\u4fee\u6539\u76ee\u6807\u7c7b\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u70ed\u4fee\u590d\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>2. <strong>\u901a\u8fc7\u53cd\u5c04\u66ff\u6362\u65b9\u6cd5<\/strong><\/h3>\n<p>\u5728\u70ed\u4fee\u590d\u6846\u67b6\u4e2d\uff0c\u4e00\u65e6\u65b0\u7684 <code>.dex<\/code> \u6587\u4ef6\u88ab\u52a0\u8f7d\u5230\u7c7b\u52a0\u8f7d\u5668\u4e2d\uff0c\u4fee\u590d\u7684\u65b9\u6cd5\u4f1a\u901a\u8fc7\u53cd\u5c04\u673a\u5236\u52a8\u6001\u66ff\u6362\u539f\u6765\u7684\u65b9\u6cd5\u3002\u5e38\u89c1\u7684\u53cd\u5c04\u8c03\u7528\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u76ee\u6807\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>Class<\/code> \u5bf9\u8c61\u83b7\u53d6\u539f\u7c7b\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\n<p><strong>\u66ff\u6362\u539f\u65b9\u6cd5<\/strong>\uff1a\u901a\u8fc7\u53cd\u5c04\u76f4\u63a5\u66ff\u6362\u76ee\u6807\u65b9\u6cd5\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-java\">Method targetMethod = targetClass.getDeclaredMethod(\"methodName\", paramTypes);\ntargetMethod.setAccessible(true);\n\/\/ \u901a\u8fc7\u53cd\u5c04\u8c03\u7528\u65b0\u7684\u4fee\u590d\u65b9\u6cd5\u66ff\u6362\u539f\u6765\u7684\u65b9\u6cd5<\/code><\/pre>\n<p>\u7136\u540e\u901a\u8fc7 <code>Method<\/code> \u7684\u53cd\u5c04\u8c03\u7528\uff0c\u5c06\u539f\u65b9\u6cd5\u6307\u5411\u65b0\u7684\u65b9\u6cd5\uff08\u5373\u8865\u4e01\u4e2d\u7684\u4fee\u590d\u65b9\u6cd5\uff09\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>3. <strong>\u901a\u8fc7 <code>MethodHook<\/code> \u6216\u5b57\u8282\u7801\u64cd\u4f5c\u5de5\u5177<\/strong><\/h3>\n<p>\u6709\u4e00\u4e9b\u70ed\u4fee\u590d\u6846\u67b6\uff0c\u5982 <strong>Tinker<\/strong>\u3001<strong>AndFix<\/strong>\uff0c\u4f1a\u4f7f\u7528\u7c7b\u4f3c <strong>\u5b57\u8282\u7801\u6ce8\u5165<\/strong> \u6216 <strong>\u5b57\u8282\u7801\u64cd\u4f5c<\/strong> \u7684\u6280\u672f\u6765\u66ff\u6362\u65b9\u6cd5\u3002\u8fd9\u4e9b\u6846\u67b6\u5728\u8fd0\u884c\u65f6\u64cd\u4f5c\u5b57\u8282\u7801\uff0c\u901a\u8fc7\u63d2\u6869\uff08instrumentation\uff09\u6280\u672f\u4fee\u6539\u7c7b\u7684\u5b57\u8282\u7801\uff0c\u5728\u65b9\u6cd5\u8c03\u7528\u65f6\u52a8\u6001\u66ff\u6362\u539f\u6709\u7684\u5b9e\u73b0\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u5b57\u8282\u7801\u6ce8\u5165<\/strong>\uff1a\u5b57\u8282\u7801\u5de5\u5177\uff08\u5982 ASM \u6216 Xposed\uff09\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u4fee\u6539\u5e94\u7528\u7684\u5b57\u8282\u7801\u3002\u8fd9\u662f\u901a\u8fc7\u4fee\u6539 <code>.dex<\/code> \u6587\u4ef6\u4e2d\u7684\u5b57\u8282\u7801\u6765\u8fbe\u5230\u66ff\u6362\u65b9\u6cd5\u7684\u6548\u679c\u3002<\/p>\n<\/li>\n<li>\n<p><strong>Xposed \u6846\u67b6<\/strong>\uff1aXposed \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5b57\u8282\u7801\u94a9\u5b50\u63a5\u53e3\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u6302\u94a9\u65b9\u6cd5\uff0c\u5e76\u5728\u8fd0\u884c\u65f6\u66ff\u6362\u65b9\u6cd5\u5b9e\u73b0\u3002Xposed \u53ef\u4ee5\u62e6\u622a\u539f\u59cb\u65b9\u6cd5\uff0c\u5e76\u66ff\u6362\u4e3a\u8865\u4e01\u4e2d\u7684\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-java\"> XposedHelpers.findAndHookMethod(\"com.example.app.TargetClass\", \n     lpparam.classLoader, \"targetMethod\", paramTypes, new XC_MethodHook() {\n     @Override\n     protected void beforeHookedMethod(MethodHookParam param) throws Throwable {\n         \/\/ \u66ff\u6362\u524d\u7684\u903b\u8f91\n     }\n\n     @Override\n     protected void afterHookedMethod(MethodHookParam param) throws Throwable {\n         \/\/ \u66ff\u6362\u540e\u7684\u903b\u8f91\n     }\n });<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>ASM<\/strong>\uff1a\u4f7f\u7528\u5b57\u8282\u7801\u64cd\u4f5c\u6846\u67b6\u5982 ASM \u6765\u4fee\u6539\u5e94\u7528\u7684 <code>.dex<\/code> \u6587\u4ef6\u3002ASM \u53ef\u4ee5\u76f4\u63a5\u4fee\u6539 <code>.class<\/code> \u6587\u4ef6\u6216 <code>.dex<\/code> \u6587\u4ef6\u4e2d\u7684\u5b57\u8282\u7801\uff0c\u5b9e\u73b0\u65b9\u6cd5\u66ff\u6362\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>4. <strong>Tinker \u70ed\u4fee\u590d\u6846\u67b6<\/strong><\/h3>\n<p>Tinker \u662f\u817e\u8baf\u5f00\u6e90\u7684 Android \u70ed\u4fee\u590d\u6846\u67b6\uff0c\u4f7f\u7528\u4e86\u52a8\u6001\u52a0\u8f7d\u548c\u5b57\u8282\u7801\u4fee\u6539\u7684\u6280\u672f\u3002Tinker \u6846\u67b6\u7684\u6838\u5fc3\u5b9e\u73b0\u662f\u901a\u8fc7 <strong>\u52a8\u6001\u52a0\u8f7d\u8865\u4e01\u5305\u4e2d\u7684 dex \u6587\u4ef6<\/strong>\uff0c\u7136\u540e <strong>\u4fee\u590d\u65b9\u6cd5\u7684\u4ee3\u7801<\/strong>\uff0c\u5e76\u901a\u8fc7\u5b57\u8282\u7801\u4fee\u6539\u548c\u65b9\u6cd5\u66ff\u6362\u5b9e\u73b0\u70ed\u4fee\u590d\u3002<\/p>\n<ul>\n<li>Tinker \u4f1a\u5148\u4e0b\u8f7d\u65b0\u7684 <code>.apk<\/code> \u8865\u4e01\u5305\uff0c\u7136\u540e\u5c06\u5176\u4e2d\u7684 <code>.dex<\/code> \u6587\u4ef6\u3001\u8d44\u6e90\u6587\u4ef6\u7b49\u4e0e\u5f53\u524d\u5e94\u7528\u7684\u8d44\u6e90\u8fdb\u884c\u5408\u5e76\u548c\u66ff\u6362\u3002<\/li>\n<li>\n<p>\u5b83\u8fd8\u4f1a\u4f7f\u7528\u5b57\u8282\u7801\u64cd\u4f5c\u6280\u672f\uff08\u5982 ASM\uff09\u6765\u66ff\u6362\u539f\u6709\u7c7b\u4e2d\u7684\u65b9\u6cd5\uff0c\u5e76\u6ce8\u5165\u65b0\u7684\u4ee3\u7801\u5b9e\u73b0\u3002<\/p>\n<p>Tinker \u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\u4e86\u70ed\u4fee\u590d\uff1a<\/p>\n<ol>\n<li><strong>Dex \u63d2\u6869<\/strong>\uff1a\u901a\u8fc7\u5bf9 <code>dex<\/code> \u6587\u4ef6\u7684\u63d2\u6869\uff08\u5b57\u8282\u7801\u6ce8\u5165\uff09\u548c\u4fee\u6539\u6765\u5b9e\u73b0\u65b9\u6cd5\u7684\u66ff\u6362\u3002<\/li>\n<li><strong>Method Hooking<\/strong>\uff1a\u901a\u8fc7\u53cd\u5c04\u6216\u8005\u4f7f\u7528\u4e00\u4e9b\u5e95\u5c42\u6280\u672f\uff08\u5982 Xposed\uff09\u66ff\u6362\u65b9\u6cd5\u7684\u5b9e\u73b0\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h3>5. <strong>AndFix \u70ed\u4fee\u590d\u6846\u67b6<\/strong><\/h3>\n<p>AndFix \u662f\u4e00\u4e2a\u8f83\u65e9\u7684 Android \u70ed\u4fee\u590d\u6846\u67b6\uff0c\u91c7\u7528\u4e86\u4e0e Tinker \u7c7b\u4f3c\u7684\u65b9\u5f0f\u6765\u8fdb\u884c\u4fee\u590d\u3002AndFix \u4f7f\u7528 <strong>\u52a8\u6001\u52a0\u8f7d\u548c\u5b57\u8282\u7801\u4fee\u6539<\/strong> \u6765\u66ff\u6362\u5e94\u7528\u4e2d\u7684\u65b9\u6cd5\u3002\u5b83\u901a\u8fc7 <strong>\u7c7b\u52a0\u8f7d\u5668<\/strong> \u52a0\u8f7d\u8865\u4e01\uff0c\u5e76\u901a\u8fc7 <code>MethodHook<\/code> \u6765\u66ff\u6362\u539f\u6709\u7684\u65b9\u6cd5\u3002<\/p>\n<ul>\n<li>\u5b83\u7684\u5b9e\u73b0\u4e3b\u8981\u662f\u901a\u8fc7 <strong>\u53cd\u5c04<\/strong> \u548c <strong>\u5b57\u8282\u7801\u64cd\u4f5c<\/strong> \u5b8c\u6210\u65b9\u6cd5\u66ff\u6362\u3002\u4e0e Tinker \u7c7b\u4f3c\uff0c\u5b83\u53ef\u4ee5\u5728\u4e0d\u5378\u8f7d\u5e94\u7528\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u4fee\u6539\u5b57\u8282\u7801\u6765\u5b9e\u73b0\u70ed\u4fee\u590d\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u70ed\u4fee\u590d\u65b9\u6cd5\u66ff\u6362\u7684\u6838\u5fc3\u673a\u5236\u662f\u901a\u8fc7 <strong>\u52a8\u6001\u7c7b\u52a0\u8f7d<\/strong> \u548c <strong>\u5b57\u8282\u7801\u4fee\u6539<\/strong> \u5b8c\u6210\u7684\uff0c\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u901a\u5e38\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li><strong>DexClassLoader<\/strong>\uff1a\u52a8\u6001\u52a0\u8f7d\u8865\u4e01\u4e2d\u7684 <code>.dex<\/code> \u6587\u4ef6\u3002<\/li>\n<li><strong>\u53cd\u5c04<\/strong>\uff1a\u901a\u8fc7\u53cd\u5c04\u6765\u83b7\u53d6\u76ee\u6807\u65b9\u6cd5\u5e76\u66ff\u6362\u3002<\/li>\n<li><strong>\u5b57\u8282\u7801\u6ce8\u5165\uff08ASM\/Xposed\uff09<\/strong>\uff1a\u901a\u8fc7\u5b57\u8282\u7801\u64cd\u4f5c\u6216\u94a9\u5b50\u6280\u672f\u76f4\u63a5\u4fee\u6539\u65b9\u6cd5\u7684\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u70ed\u4fee\u590d\u6846\u67b6\uff08\u5982 Tinker\u3001AndFix\uff09<\/strong>\uff1a\u5229\u7528\u5b57\u8282\u7801\u64cd\u4f5c\u548c\u65b9\u6cd5\u66ff\u6362\u6280\u672f\u5b9e\u73b0\u70ed\u4fee\u590d\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u6280\u672f\u53ef\u4ee5\u786e\u4fdd\u5728\u5e94\u7528\u8fd0\u884c\u65f6\u52a8\u6001\u4fee\u590d bug\u3001\u66f4\u65b0\u529f\u80fd\uff0c\u800c\u4e0d\u9700\u8981\u7528\u6237\u91cd\u65b0\u4e0b\u8f7d\u6574\u4e2a APK\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Android \u4e2d\uff0c\u70ed\u4fee\u590d\uff08Hotfix\uff09\u6280\u672f\u662f\u4e3a\u4e86\u5728\u5e94\u7528\u53d1\u5e03\u540e\u8fdb\u884c\u4fee\u590d\u548c\u66f4\u65b0 bug\uff0c\u800c\u4e0d\u9700\u8981\u901a\u8fc7\u5e94\u7528\u7684&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6640\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[],"class_list":["post-6640","post","type-post","status-publish","format-standard","hentry","category-135"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6640"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6640\/revisions"}],"predecessor-version":[{"id":6641,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6640\/revisions\/6641"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}