{"id":220233,"date":"2024-05-11T18:13:53","date_gmt":"2024-05-11T10:13:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/220233.html"},"modified":"2024-05-11T18:14:00","modified_gmt":"2024-05-11T10:14:00","slug":"c%e8%af%ad%e8%a8%80%e5%a6%82%e4%bd%95%e6%89%a7%e8%a1%8cbuf%e4%b8%ad%e7%9a%84%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/220233.html","title":{"rendered":"C\u8bed\u8a00\u5982\u4f55\u6267\u884cbuf\u4e2d\u7684\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140521\/0ac8a9ea-fe2d-402d-9333-a442bca8ec12.webp\" alt=\"C\u8bed\u8a00\u5982\u4f55\u6267\u884cbuf\u4e2d\u7684\u4ee3\u7801\" \/><\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\u6267\u884cbuf\u4e2d\u7684\u4ee3\u7801\u4e3b\u8981\u6d89\u53ca\u5230\u51e0\u4e2a\u5173\u952e\u6280\u672f\u70b9\uff1a<strong>\u5185\u5b58\u7ba1\u7406\u3001\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\u3001\u4ee3\u7801\u7684\u52a8\u6001\u751f\u6210\u4ee5\u53ca\u6743\u9650\u63a7\u5236<\/strong>\u3002\u5728\u8fd9\u4e9b\u6280\u672f\u70b9\u4e2d\uff0c<strong>\u6307\u5411\u51fd\u6570\u7684\u6307\u9488<\/strong>\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e3a\u6267\u884c\u5b58\u50a8\u5728buf\u4e2d\u7684\u4ee3\u7801\u63d0\u4f9b\u4e86\u53ef\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5185\u5b58\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u4efb\u4f55\u5f62\u5f0f\u7684\u6267\u884c\u4ee3\u7801\u90fd\u9700\u8981\u9996\u5148\u628a\u4ee3\u7801\u5b58\u653e\u5728\u53ef\u6267\u884c\u7684\u5185\u5b58\u533a\u57df\u91cc\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u5185\u5b58\u7684\u5206\u914d\u548c\u8bbe\u7f6e\u5185\u5b58\u533a\u57df\u7684\u5c5e\u6027\uff08\u53ef\u6267\u884c\uff09\u3002<\/p>\n<\/p>\n<p><h4>\u5206\u914d\u5185\u5b58<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>malloc<\/code>\u6216<code>mmap<\/code>\u51fd\u6570\u53ef\u4ee5\u4e3a\u5373\u5c06\u6267\u884c\u7684\u4ee3\u7801\u5206\u914d\u5185\u5b58\u3002<code>mmap<\/code>\u5728\u8fd9\u91cc\u66f4\u52a0\u5408\u9002\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u5206\u914d\u4e00\u5757\u53ef\u6267\u884c\u7684\u5185\u5b58\u533a\uff0c\u800c<code>malloc<\/code>\u5206\u914d\u7684\u5185\u5b58\u9ed8\u8ba4\u4e0d\u662f\u53ef\u6267\u884c\u7684\u3002<\/p>\n<\/p>\n<p><h4>\u8bbe\u7f6e\u5185\u5b58\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u501f\u52a9\u4e8e<code>mprotect<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u6539\u53d8\u5df2\u5206\u914d\u5185\u5b58\u533a\u57df\u7684\u5c5e\u6027\u3002\u4e3a\u4e86\u6267\u884c<code>buf<\/code>\u4e2d\u7684\u4ee3\u7801\uff0c\u5fc5\u987b\u786e\u4fdd\u8fd9\u6bb5\u5185\u5b58\u662f\u53ef\u6267\u884c\u7684\u3002\u8fd9\u901a\u5e38\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u5c06\u5176\u8bbe\u7f6e\u4e3a<code>PROT_EXEC<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6307\u5411\u51fd\u6570\u7684\u6307\u9488<\/h3>\n<\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u5c06buf\u4e2d\u7684\u4e8c\u8fdb\u5236\u4ee3\u7801\u89c6\u4f5c\u51fd\u6570\u5e76\u6267\u884c\u5173\u952e\u662f\u6784\u5efa\u4e00\u4e2a\u6b63\u786e\u7c7b\u578b\u7684\u51fd\u6570\u6307\u9488\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u6307\u9488\u6307\u5411buf\u6240\u5728\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>\u6784\u5efa\u51fd\u6570\u6307\u9488<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5fc5\u987b\u77e5\u9053buf\u4e2d\u4ee3\u7801\u7684\u6b63\u786e\u7b7e\u540d\uff0c\u4e5f\u5c31\u662f\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u548c\u53c2\u6570\u7c7b\u578b\u3002\u5047\u8bbebuf\u4e2d\u7684\u4ee3\u7801\u5bf9\u5e94\u7684\u51fd\u6570\u7b7e\u540d\u662f<code>void func()<\/code>\uff0c\u90a3\u4e48\u53ef\u4ee5\u8fd9\u6837\u58f0\u660e\u4e00\u4e2a\u6307\u5411\u8fd9\u7c7b\u51fd\u6570\u7684\u6307\u9488\uff1a<code>void (*func_ptr)();<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u6307\u9488\u8d4b\u503c<\/h4>\n<\/p>\n<p><p>\u5c06<code>func_ptr<\/code>\u6307\u5411buf\u4e2d\u4ee3\u7801\u7684\u5730\u5740\uff1a<code>func_ptr = (void (*)())buf_address;<\/code>\u3002\u8fd9\u6837\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7<code>func_ptr()<\/code>\u8c03\u7528buf\u4e2d\u7684\u4ee3\u7801\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4ee3\u7801\u7684\u52a8\u6001\u751f\u6210<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u52a8\u6001\u4ee3\u7801\u6d89\u53ca\u5230\u5bf9\u76ee\u6807\u673a\u5668\u6307\u4ee4\u96c6\u7684\u4e86\u89e3\u3002\u5728\u4e00\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u5982JIT\u7f16\u8bd1\u5668\uff0c\u4f1a\u5728\u8fd0\u884c\u65f6\u6839\u636e\u9700\u8981\u751f\u6210\u673a\u5668\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u751f\u6210\u673a\u5668\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u9002\u5408\u76ee\u6807\u673a\u5668\u6267\u884c\u7684\u4e8c\u8fdb\u5236\u4ee3\u7801\uff0c\u5e76\u5c06\u8fd9\u4e9b\u4ee3\u7801\u5b58\u50a8\u5230\u524d\u9762\u5206\u914d\u5e76\u8bbe\u7f6e\u597d\u7684\u5185\u5b58\u533a\u57df\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u8c03\u8bd5\u548c\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e00\u6b65\u9aa4\uff0c\u975e\u5e38\u91cd\u8981\u7684\u662f\u786e\u4fdd\u751f\u6210\u7684\u4ee3\u7801\u662f\u6b63\u786e\u7684\u5e76\u4e14\u53ef\u4ee5\u5982\u9884\u671f\u90a3\u6837\u6267\u884c\u3002\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\uff0c\u5982GDB\uff0c\u53ef\u4ee5\u5e2e\u52a9\u68c0\u67e5\u548c\u6d4b\u8bd5\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6743\u9650\u63a7\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u6267\u884c\u5185\u5b58\u4e2d\u4ee3\u7801\u7684\u505a\u6cd5\u53d7\u5230\u4e25\u683c\u7684\u6743\u9650\u63a7\u5236\u3002\u8fd9\u4e3b\u8981\u662f\u4e3a\u4e86\u9632\u6b62\u6076\u610f\u8f6f\u4ef6\u5229\u7528\u8fd9\u4e00\u6280\u672f\u6765\u6267\u884c\u653b\u51fb\u3002<\/p>\n<\/p>\n<p><h4>\u7406\u89e3\u64cd\u4f5c\u7cfb\u7edf\u5b89\u5168\u673a\u5236<\/h4>\n<\/p>\n<p><p>\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5982Linux\u548cWindows\uff0c\u63d0\u4f9b\u4e86NX\uff08No-Execute\uff09\u6216DEP\uff08Data Execution Prevention\uff09\u7b49\u673a\u5236\uff0c\u6765\u9632\u6b62\u975e\u53ef\u6267\u884c\u5185\u5b58\u533a\u57df\u7684\u4ee3\u7801\u88ab\u6267\u884c\u3002\u56e0\u6b64\uff0c\u6b63\u786e\u5730\u914d\u7f6e\u5185\u5b58\u5c5e\u6027\u53d8\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h4>\u7ed5\u8fc7\u9650\u5236\uff08\u614e\u7528\uff09<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u67d0\u4e9b\u6280\u672f\uff0c\u5982ROP\uff08Return-Oriented Programming\uff09\u653b\u51fb\uff0c\u53ef\u4ee5\u7ed5\u8fc7\u8fd9\u4e9b\u5b89\u5168\u673a\u5236\uff0c\u4f46\u5b83\u4eec\u5c5e\u4e8e\u5b89\u5168\u6f0f\u6d1e\u5229\u7528\u8303\u7574\uff0c\u5f00\u53d1\u8005\u5e94\u907f\u514d\u5728\u6b63\u89c4\u8f6f\u4ef6\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u624b\u6bb5\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u6267\u884c\u5b58\u50a8\u5728buf\u4e2d\u7684C\u8bed\u8a00\u4ee3\u7801\u662f\u4e00\u9879\u6d89\u53ca\u591a\u4e2a\u6280\u672f\u70b9\u7684\u9ad8\u7ea7\u6280\u672f\uff0c\u6b63\u786e\u5b9e\u73b0\u5b83\u4e0d\u4ec5\u9700\u8981\u5bf9C\u8bed\u8a00\u6709\u6df1\u5165\u7684\u7406\u89e3\uff0c\u8fd8\u9700\u8981\u5bf9\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u5b58\u7ba1\u7406\u548c\u5b89\u5168\u7b56\u7565\u6709\u5145\u5206\u7684\u8ba4\u8bc6\u3002\u5728\u7b26\u5408\u6cd5\u5f8b\u548c\u9053\u5fb7\u7684\u524d\u63d0\u4e0b\uff0c\u8fd9\u9879\u6280\u672f\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u9ad8\u6548\u3001\u52a8\u6001\u7684\u8f6f\u4ef6\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728C\u8bed\u8a00\u4e2d\u6267\u884c\u5b58\u50a8\u5728\u7f13\u51b2\u533a\uff08buf\uff09\u4e2d\u7684\u4ee3\u7801\uff1f<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u4e3a\u4ec0\u4e48\u4f1a\u9700\u8981\u5728C\u8bed\u8a00\u4e2d\u6267\u884c\u5b58\u50a8\u5728\u7f13\u51b2\u533a\u4e2d\u7684\u4ee3\u7801\uff1f<\/strong><br \/>\n\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5730\u6267\u884c\u4e00\u4e9b\u4ee3\u7801\uff0c\u800c\u4e0d\u662f\u5728\u7f16\u8bd1\u65f6\u5c31\u786e\u5b9a\u597d\u3002\u8fd9\u79cd\u9700\u6c42\u5e38\u89c1\u4e8e\u89e3\u91ca\u5668\u3001\u811a\u672c\u8bed\u8a00\u7b49\u73af\u5883\uff0c\u4e5f\u53ef\u80fd\u51fa\u73b0\u5728\u4e00\u4e9b\u7279\u5b9a\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u5c06\u4ee3\u7801\u5b58\u50a8\u5728\u7f13\u51b2\u533a\u4e2d\uff1f<\/strong><br \/>\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u6765\u5b58\u50a8\u4ee3\u7801\uff0c\u7136\u540e\u5c06\u5176\u8bfb\u5165\u7f13\u51b2\u533a\u4e2d\u3002\u5728C\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>fgets()<\/code>\u51fd\u6570\u4ece\u6807\u51c6\u8f93\u5165\u6216\u6587\u4ef6\u4e2d\u8bfb\u53d6\u4ee3\u7801\u884c\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u6267\u884c\u7f13\u51b2\u533a\u4e2d\u7684\u4ee3\u7801\uff1f<\/strong><br \/>\n\u6267\u884c\u7f13\u51b2\u533a\u4e2d\u7684\u4ee3\u7801\u9700\u8981\u4f7f\u7528C\u8bed\u8a00\u7684\u52a8\u6001\u5e93\u52a0\u8f7d\u529f\u80fd\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5c06\u7f13\u51b2\u533a\u4e2d\u7684\u4ee3\u7801\u7f16\u8bd1\u6210\u53ef\u6267\u884c\u7684\u5bf9\u8c61\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528C\u8bed\u8a00\u7684\u52a8\u6001\u94fe\u63a5\u5668\uff08\u5982<code>dlopen()<\/code>\u51fd\u6570\uff09\u52a0\u8f7d\u5df2\u7f16\u8bd1\u7684\u4ee3\u7801\uff0c\u5e76\u8c03\u7528\u5176\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u4e8eJIT\uff08\u5373\u65f6\u7f16\u8bd1\uff09\u7684\u6280\u672f\u5c06C\u4ee3\u7801\u52a8\u6001\u7f16\u8bd1\u6210\u673a\u5668\u7801\uff0c\u7136\u540e\u6267\u884c\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5728\u6267\u884c\u7f13\u51b2\u533a\u4e2d\u7684\u4ee3\u7801\u65f6\uff0c\u5e94\u683c\u5916\u6ce8\u610f\u5b89\u5168\u6027\u548c\u9519\u8bef\u5904\u7406\u3002\u786e\u4fdd\u53ea\u6267\u884c\u53ef\u4fe1\u4efb\u7684\u4ee3\u7801\uff0c\u5e76\u907f\u514d\u6f5c\u5728\u7684\u6f0f\u6d1e\u548c\u5b89\u5168\u98ce\u9669\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728C\u8bed\u8a00\u4e2d\u6267\u884cbuf\u4e2d\u7684\u4ee3\u7801\u4e3b\u8981\u6d89\u53ca\u5230\u51e0\u4e2a\u5173\u952e\u6280\u672f\u70b9\uff1a\u5185\u5b58\u7ba1\u7406\u3001\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\u3001\u4ee3\u7801\u7684\u52a8\u6001\u751f\u6210\u4ee5\u53ca\u6743\u9650\u63a7\u5236\u3002\u5728 [&hellip;]","protected":false},"author":3,"featured_media":220242,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/220233"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=220233"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/220233\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/220242"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=220233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=220233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=220233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}