linux chroot<\/strong>\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\/proc\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u68c0\u6d4b\u8fdb\u7a0b\u7684\u4e2d\u7684\u6839\u76ee\u5f55\uff0c\u4f8b\u5982\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u8fb9\u7684\u4ee3\u7801\u68c0\u6d4b\u91cc\u9762\u8fd0\u884c\u7684\/bin\/sh\u547d\u4ee4\u7684\u6839\u76ee\u5f55(\u8bf7\u5728\u53e6\u5916\u4e00\u4e2ashell\u4e2d\u6267\u884c)\uff1a$pid=$(pidof-ssh)<\/p>\n$sudols-ld\/proc\/$pid\/root<\/p>\n
\u8f93\u51fa\u4e2d\u7684\u5185\u5bb9\u660e\u6670\u7684\u5f3a\u8c03PID\u4e3a46644\u7684\u8fdb\u7a0b\u7684\u6839\u76ee\u5f55\u88ab\u6620\u5c04\u5230\u4e86\/tmp\/rootfs\u76ee\u5f55\u3002<\/p>\n
\u901a\u8fc7\u4ee3\u7801\u7406\u89e3chroot\u547d\u4ee4<\/p>\n
\u4e0b\u8fb9\u6211\u4eec\u5c1d\u8bd5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2achroot\u7a0b\u5e8f\uff0c\u4ee3\u7801\u4e2d\u6d89\u53ca\u5230\u4e24\u4e2a\u51fd\u6570\uff0c\u5206\u522b\u662fchroot()\u51fd\u6570\u548cchdir()\u51fd\u6570\uff0c\u867d\u7136\u771f\u6b63\u7684chroot\u547d\u4ee4\u4e5f\u662f\u901a\u8fc7\u8c03\u7528\u5b83\u4eec\u5b9e\u73b0\u7684\uff1a<\/p>\n
#include#include#include<\/p>\n
intmain(intargc,char*argv[])<\/p>\n
{if(argc<\/p>\n
printf(\"Usage:chrootNEWROOT[COMMAND...]n\");<\/p>\n
return1;<\/p>\n
}if(chroot(argv[1])){<\/p>\n
perror(\"chroot\");<\/p>\n
<\/p>\n
return1;<\/p>\n
if(chdir(\"\/\")){<\/p>\n
perror(\"chdir\");<\/p>\n
return1;<\/p>\n
if(argc==2){\/\/hardcode\/bin\/shformybusyboxtools.<\/p>\n
argv[0]=(char*)\"\/bin\/sh\";<\/p>\n
argv[1]=(char*)\"-i\";<\/p>\n
argv[2]=NULL;<\/p>\n
}else{<\/p>\n
argv+=2;<\/p>\n
execvp(argv[0],argv);<\/p>\n
printf(\"chroot:cannotruncommand`%s`n\",*argv);<\/p>\n
return0;<\/p>\n
\u628a\u91cc\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u5230\u6587\u4ef6mychroot.c\u6587\u4ef6\u4e2d\uff0c\u5e76\u6267\u884c\u4e0b\u8fb9\u7684\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\uff1a$gcc-Wallmychroot.c-omychroot<\/p>\n
<\/p>\n
mychroot\u7684\u7528\u6cd5\u548cchroot\u57fa\u672c\u76f8\u540c\uff1a$sudo.\/mychroot.\/rootfs<\/p>\n
\u975e\u5e38\u4e4b\u5904\u662f\u6211\u4eec\u7684mychroot\u5728\u6ca1\u6709\u4f20\u9012\u547d\u4ee4\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u4e86\/bin\/sh\uff0c\u7f18\u7531\u5176\u5b9e\u662f\u4e3a\u4e86\u652f\u6301\u6211\u4eec\u7684busybox\u5de5\u5177\u96c6linux apache \u865a\u62df\u4e3b\u673a\uff0c\u7b14\u8005\u5728\u4ee3\u7801\u4e2dhardcode\u4e86\u9ed8\u8ba4\u7684shell\uff1aargv[0]=(char*)\"\/bin\/sh\";<\/p>\n
\u4ece\u4ee3\u7801\u4e2d\u6211\u4eec\u4e5f\u53ef\u4ee5\u89c1\u5230\uff0c\u5b9e\u73b0chroot\u547d\u4ee4\u7684\u6838\u5fc3\u903b\u8f91\u867d\u7136\u5e76\u4e0d\u590d\u6742\u3002<\/p>\n
\u5b9e\u4f8b\uff1a\u901a\u8fc7chroot\u91cd\u65b0\u8bbe\u7f6eroot\u5bc6\u7801<\/p>\n
\u5fd8\u6389\u4e86root\u5bc6\u7801\u8be5\u5982\u4f55\u529e\uff1f\u63a5\u51fa\u6765\u7684demo\u5c06\u6f14\u793a\u600e\u4e48\u901a\u8fc7chroot\u547d\u4ee4\u91cd\u65b0\u8bbe\u7f6ecentos7\u4e2d\u88ab\u5fd8\u6389\u4e86\u7684root\u5bc6\u7801\u3002<\/p>\n
systemd\u7684\u7ba1\u7406\u673a\u5236\u4e2d\uff0crescure\u6a21\u5f0f\u548cemeryency\u6a21\u5f0f\u662f\u96be\u4ee5\u76f4\u63a5\u53d6\u5f97root\u6743\u9650\u7684\uff0c\u987b\u8981\u4f7f\u7528root\u5bc6\u7801\u80fd\u591f\u6b65\u5165rescure\u548cemeryency\u73af\u5883\u3002\u6240\u4ee5\u6211\u4eec\u987b\u8981\u901a\u8fc7\u5176\u4ed6\u65b9\u6cd5\u6765\u8bbe\u7f6eroot\u5bc6\u7801\u3002\u6211\u4eec\u53ef\u4ee5\u4e3a\u5185\u6838\u7684\u542f\u52a8\u6307\u5b9a\"rd.break\"\u53c2\u6570\uff0c\u8fdb\u800c\u8ba9\u7cfb\u7edf\u5728\u542f\u52a8\u7684\u521d\u671f\u505c\u51fa\u6765\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528root\u6743\u9650\u5e76\u7ed3\u5408chroot\u547d\u4ee4\u5b8c\u6210\u8bbe\u7f6eroot\u5bc6\u7801\u7684\u64cd\u4f5c\u3002\u4e0b\u8fb9\u6211\u4eec\u4e00\u4e0a\u53bb\u770b\u5177\u4f53\u7684\u64cd\u4f5c\u8fc7\u7a0b\u3002<\/p>\n
\u5728\u7cfb\u7edf\u542f\u52a8\u8fc7\u7a0b\u4e2d\u6b65\u5165\u5f00\u673a\u83dc\u5355\u65f6\u6309\u4e0b\u5b57\u6bcd\u952ee\u8fdb\u7a0b\u5f00\u673a\u83dc\u5355\u7684\u7f16\u8f91\u6a21\u5f0f\uff1a<\/p>\n
\u8fd9\u5c31\u662f\u7cfb\u7edf\u7684\u5f00\u673a\u83dc\u5355\uff0c\u6309\u4e0be\u540e\u6b65\u5165\u7f16\u8f91\u754c\u9762\uff1a<\/p>\n
\u627e\u5230\u4ee5\"linux16\/vmlinuz-\"\u5f00\u5934\u7684\u884c\u3002\u5047\u5982\u9ed8\u8ba4\u6ca1\u6709\u770b\u89c1\u8be5\u884c\uff0c\u987b\u8981\u6309\u5411\u4e0a\u952e\u628a\u5b83\u6eda\u52a8\u4e0b\u6765\u3002<\/p>\n
\u4e4b\u540e\u5b9a\u4f4d\u5230\u8be5\u884c\u7ed3\u5c3e\u5904\uff0c\u8f93\u5165\u4e00\u4e2a\u7a7a\u683c\u548c\u5b57\u7b26\u4e32\"rd.break\"\uff0c\u5982\u53f3\u56fe\u6240\u793a\uff1a<\/p>\n
<\/p>\n
\u63a5\u7740\u6309\u4e0bctrl+x\u4ee5\u8be5\u8bbe\u7f6e\u7ee7\u7eed\u542f\u52a8\uff0c\u542f\u52a8\u8fc7\u7a0b\u4e2d\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u505c\u51fa\u6765linux chroot<\/strong>\uff0c\u8fd9\u662f\u7cfb\u7edf\u542f\u52a8\u8fc7\u7a0b\u4e2d\u7684\u4e00\u4e2a\u7279\u522b\u65e9\u7684\u65f6\u95f4\u70b9\uff1a<\/p>\n\u6240\u4ee5\u7cfb\u7edf\u7684\u6839\u76ee\u5f55\u8fd8\u6302\u8f7d\u5728RAMdisk\u4e0a(\u5c31\u662f\u663e\u5b58\u4e2d\u7684\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf)\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7mount\u547d\u4ee4\u68c0\u6d4b\u7cfb\u7edf\u5f53\u524d\u6302\u8f7d\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u4e0b\u8fb9\u662f\u6211\u4eec\u6bd4\u8f83\u5173\u5fc3\u7684\u4e24\u6761\uff1a<\/p>\n
\u4e0a\u56fe\u4e2dmount\u547d\u4ee4\u8f93\u51fa\u7684\u7b2c\u4e00\u884c\u8bf4\u660e\u6b64\u65f6\u7684\u6839\u76ee\u5f55\u5728\u4e00\u4e2aRAMdisk\u4e2d,\u5373rootfs\u3002<\/p>\n
\u56fe\u4e2d\u8f93\u51fa\u7684\u7b2c\u4e8c\u884c\u8bf4\u660e\u6211\u4eec\u7684\u6587\u4ef6\u7cfb\u7edf\u6b64\u65f6\u88ab\u6302\u8f7d\u5230\u4e86\/sysroot\u76ee\u5f55\uff0c\u4f46\u662f\u662f\u53ea\u8bfb\u7684\u6a21\u5f0f\uff1a\/dev\/mapper\/centos-rooton\/sysroottypexfs(ro,relatime,attr2,inode64,noquota)<\/p>\n
\u800c\u5728\u6211\u4eec\u6b63\u5e38\u767b\u5f55\u7cfb\u7edf\u7684\u60c5\u51b5\u4e0b\uff0c\u7cfb\u7edf\u6839\u76ee\u5f55\u7684\u6302\u8f7d\u60c5\u51b5\u5982\u4e0b\uff1a\/dev\/mapper\/centos-rooton\/typexfs(rw,relatime,seclabel,attr2,inode64,noquota)<\/p>\n
\u8be5\u65f6\u95f4\u70b9\u7684\u6700\u5927\u4f18\u52bf\u662f\u6211\u4eec\u5177\u6709root\u6743\u9650\uff01\u6240\u4ee5\u8ba9\u6211\u4eec\u5f00\u59cb\u8bbe\u7f6e\u65b0\u7684root\u5bc6\u7801\u5427\u3002<\/p>\n
\u5148\u901a\u8fc7\u4e0b\u8fb9\u7684\u547d\u4ee4\u628a\/sysroot\u91cd\u65b0\u6302\u8f7d\u4e3a\u53ef\u8bfb\u5199\u7684\u6a21\u5f0f\uff1aswitch_root:\/#mount-oremount,rw\/sysroot<\/p>\n
\u4e4b\u540e\u7528\u4e0b\u8fb9chroot\u547d\u4ee4\u628a\u6839\u76ee\u5f55\u5207\u6362\u5230\u6211\u4eec\u539f\u5148\u7684\u73af\u5883\u4e2d\uff1aswitch_root:\/#chroot\/sysroot<\/p>\n
\u6b64\u65f6\u53ef\u4ee5\u7406\u89e3\u4e3a\uff1a\u6211\u4eec\u4ee5root\u6743\u9650\u767b\u5165\u4e86\u539f\u5148\u7684\u7cfb\u7edf\uff0c\u66f4\u6539\u5bc6\u7801\u5c31\u5f88\u5bb9\u6613\u4e86\uff01\u7528\u4e0b\u8fb9\u7684\u547d\u4ee4\u4e3aroot\u7528\u6237\u8bbe\u7f6e\u65b0\u7684\u5bc6\u7801\uff1ash-4.2#echo\"new_root_pw\"|passwd--stdinroot<\/p>\n
\u63a5\u51fa\u6765\u8fd8\u8981\u5904\u7406SELinux\u76f8\u5173\u7684\u95ee\u9898\u3002\u56e0\u4e3a\u5f53\u524d\u7684\u73af\u5883\u4e2dSELinux\u5e76\u672a\u542f\u52a8\uff0c\u6240\u4ee5\u6211\u4eec\u5bf9\u6587\u4ef6\u7684\u66f4\u6539\u53ef\u80fd\u5bfc\u81f4\u6587\u4ef6\u7684context\u4e0d\u6b63\u786e\u3002\u4e3a\u4e86\u786e\u4fdd\u5f00\u673a\u65f6\u91cd\u65b0\u8bbe\u5b9aSELinuxcontext\uff0c\u5fc5\u9808\u5728\u6839\u76ee\u5f55\u4e0b\u6dfb\u52a0\u9690\u85cf\u6587\u4ef6.autorelabel\uff1ash-4.2#touch\/.autorelabel<\/p>\n
\u6700\u540e\u4ecechroot\u4e2d\u9000\u51fa\uff0c\u5e76\u91cd\u542f\u7cfb\u7edf\uff1ash-4.2#exit<\/p>\n
switch_root:\/#reboot<\/p>\n
\u91cd\u65b0\u6b65\u5165\u767b\u5f55\u754c\u9762\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528\u521a\u521a\u8bbe\u7f6e\u7684\u5bc6\u7801\u4ee5root\u767b\u5f55\u4e86\uff01<\/p>\n
\u603b\u7ed3<\/p>\n
chroot\u662f\u4e00\u4e2a\u5f88\u6709\u610f\u601d\u7684\u547d\u4ee4\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u6765\u7b80\u5355\u7684\u5b9e\u73b0\u6587\u4ef6\u7cfb\u7edf\u7684\u9694\u79bb\u3002\u4f46\u5728\u4e00\u4e2a\u5bb9\u5668\u6280\u672f\u7e41\u8363\u7684\u65f6\u4ee3\uff0c\u7528chroot\u6765\u8fdb\u884c\u8d44\u6e90\u7684\u9694\u79bb\u5b9e\u5728\u662flow\u4e86\u70b9\u3002\u6240\u4ee5chroot\u7684\u4e3b\u8981\u7528\u9014\u8fd8\u662f\u96c6\u4e2d\u5728\u7cfb\u7edf\u641c\u6551\u3001\u7ef4\u62a4\u7b49\u4e00\u4e9b\u7279\u6b8a\u7684\u573a\u666f\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u6587\u4ef6\u4e2d\uff0c\u5e76\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\uff1a$gcc-Wallmychroot.c-omychroot\u57fa\u672c\u76f8\u540c\uff1a$sudo.\/mychroot.\/rootfs\u547d\u4ee4\u628a\u6839\u76ee\u5f55\u5207\u6362\u5230\u6211\u4eec\u539f\u6765\u7684\u73af\u5883\u4e2d\uff1aswitch_root:\/#chroot\/sysroot<\/p>\n","protected":false},"author":1,"featured_media":290369,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-290368","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/290368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=290368"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/290368\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/290369"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=290368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=290368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=290368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}