{"id":52034,"date":"2024-12-03T16:13:41","date_gmt":"2024-12-03T08:13:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52034\/"},"modified":"2024-12-03T16:13:41","modified_gmt":"2024-12-03T08:13:41","slug":"linux-nx%e6%8c%87%e7%9a%84%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52034\/","title":{"rendered":"linux nx\u6307\u7684\u662f\u4ec0\u4e48"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>linux nx\u6307\u7684\u662f\u4ec0\u4e48<\/h1>\n<p>\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300alinux nx\u6307\u7684\u662f\u4ec0\u4e48\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<\/p>\n<blockquote>\n<p>linux nx\u662f\u6307\u201cNo-eXecute\u201d\uff0c\u662flinux\u4e2d\u7684\u4e00\u79cd\u4fdd\u62a4\u673a\u5236\uff0c\u4e5f\u5c31\u662f\u6570\u636e\u4e0d\u53ef\u6267\u884c\uff0c\u9632\u6b62\u56e0\u4e3a\u7a0b\u5e8f\u8fd0\u884c\u51fa\u73b0\u6ea2\u51fa\u800c\u4f7f\u5f97\u653b\u51fb\u8005\u7684shellcode\u53ef\u80fd\u4f1a\u5728\u6570\u636e\u533a\u5c1d\u8bd5\u6267\u884c\u7684\u60c5\u51b5\u3002<\/p>\n<\/blockquote>\n<p>Linux\u7a0b\u5e8f\u5e38\u89c1\u7528\u7684\u4e00\u4e9b\u4fdd\u62a4\u673a\u5236<\/p>\n<h4>\u4e00\u3001NX\uff08Windows\u4e2d\u7684DEP\uff09<\/h4>\n<p><strong>NX<\/strong>\uff1aNo-eXecute\u3001<strong>DEP<\/strong>\uff1aData Execute Prevention<\/p>\n<ul>\n<li>\n<p>\u4e5f\u5c31\u662f\u6570\u636e\u4e0d\u53ef\u6267\u884c\uff0c\u9632\u6b62\u56e0\u4e3a\u7a0b\u5e8f\u8fd0\u884c\u51fa\u73b0\u6ea2\u51fa\u800c\u4f7f\u5f97\u653b\u51fb\u8005\u7684shellcode\u53ef\u80fd\u4f1a\u5728\u6570\u636e\u533a\u5c1d\u8bd5\u6267\u884c\u7684\u60c5\u51b5\u3002<\/p>\n<\/li>\n<li>\n<p>gcc\u9ed8\u8ba4\u5f00\u542f\uff0c\u9009\u9879\u6709\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre>gcc&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f00\u542fNX\u4fdd\u62a4\ngcc&nbsp;-z&nbsp;execstack&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;\/\/&nbsp;\u7981\u7528NX\u4fdd\u62a4\ngcc&nbsp;-z&nbsp;noexecstack&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;\/\/&nbsp;\u5f00\u542fNX\u4fdd\u62a4<\/pre>\n<h4>\u4e8c\u3001PIE\uff08ASLR\uff09<\/h4>\n<p><strong>PIE<\/strong>\uff1aPosition-Independent Excutable\u3001<strong>ASLR<\/strong>\uff1aAddress Space Layout Randomization<\/p>\n<ul>\n<li>\n<p>fpie\/fPIE\uff1a\u9700\u8981\u548c\u9009\u9879<code>-pie<\/code>\u4e00\u8d77\u4f7f\u7528\u5f00\u542fpie\u9009\u9879\u7f16\u8bd1\u53ef\u6267\u884c\u6587\u4ef6\u4f7f\u5f97elf\u62e5\u6709\u5171\u4eab\u5e93\u5c5e\u6027\uff0c\u53ef\u4ee5\u5728\u5185\u5b58\u4efb\u4f55\u5730\u65b9\u52a0\u8f7d\u8fd0\u884c\u3002\u4e0e\u4e4b\u76f8\u4f3c\u7684\u8fd8\u6709fpic\/fPIC\uff0c\u5173\u4e8e\u5176\u8bf4\u660ehttps:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Code-Gen-Options.html<\/p>\n<\/li>\n<\/ul>\n<pre>-fpic\n\n\tGenerate&nbsp;position-independent&nbsp;code&nbsp;(PIC)&nbsp;suitable&nbsp;for&nbsp;use&nbsp;in&nbsp;a&nbsp;shared&nbsp;library,&nbsp;if&nbsp;supported&nbsp;for&nbsp;the&nbsp;target&nbsp;machine.&nbsp;Such&nbsp;code&nbsp;accesses&nbsp;all&nbsp;constant&nbsp;addresses&nbsp;through&nbsp;a&nbsp;global&nbsp;offset&nbsp;table&nbsp;(GOT).&nbsp;The&nbsp;dynamic&nbsp;loader&nbsp;resolves&nbsp;the&nbsp;GOT&nbsp;entries&nbsp;when&nbsp;the&nbsp;program&nbsp;starts&nbsp;(the&nbsp;dynamic&nbsp;loader&nbsp;is&nbsp;not&nbsp;part&nbsp;of&nbsp;GCC;&nbsp;it&nbsp;is&nbsp;part&nbsp;of&nbsp;the&nbsp;operating&nbsp;system).&nbsp;If&nbsp;the&nbsp;GOT&nbsp;size&nbsp;for&nbsp;the&nbsp;linked&nbsp;executable&nbsp;exceeds&nbsp;a&nbsp;machine-specific&nbsp;maximum&nbsp;size,&nbsp;you&nbsp;get&nbsp;an&nbsp;error&nbsp;message&nbsp;from&nbsp;the&nbsp;linker&nbsp;indicating&nbsp;that&nbsp;-fpic&nbsp;does&nbsp;not&nbsp;work;&nbsp;in&nbsp;that&nbsp;case,&nbsp;recompile&nbsp;with&nbsp;-fPIC&nbsp;instead.&nbsp;(These&nbsp;maximums&nbsp;are&nbsp;8k&nbsp;on&nbsp;the&nbsp;SPARC,&nbsp;28k&nbsp;on&nbsp;AArch74&nbsp;and&nbsp;32k&nbsp;on&nbsp;the&nbsp;m68k&nbsp;and&nbsp;RS\/6000.&nbsp;The&nbsp;x86&nbsp;has&nbsp;no&nbsp;such&nbsp;limit.)\n\n\tPosition-independent&nbsp;code&nbsp;requires&nbsp;special&nbsp;support,&nbsp;and&nbsp;therefore&nbsp;works&nbsp;only&nbsp;on&nbsp;certain&nbsp;machines.&nbsp;For&nbsp;the&nbsp;x86,&nbsp;GCC&nbsp;supports&nbsp;PIC&nbsp;for&nbsp;System&nbsp;V&nbsp;but&nbsp;not&nbsp;for&nbsp;the&nbsp;Sun&nbsp;386i.&nbsp;Code&nbsp;generated&nbsp;for&nbsp;the&nbsp;IBM&nbsp;RS\/6000&nbsp;is&nbsp;always&nbsp;position-independent.\n\n\tWhen&nbsp;this&nbsp;flag&nbsp;is&nbsp;set,&nbsp;the&nbsp;macros&nbsp;`__pic__`&nbsp;and&nbsp;`__PIC__`&nbsp;are&nbsp;defined&nbsp;to&nbsp;1.\n\n-fPIC\n\n\tIf&nbsp;supported&nbsp;for&nbsp;the&nbsp;target&nbsp;machine,&nbsp;emit&nbsp;position-independent&nbsp;code,&nbsp;suitable&nbsp;for&nbsp;dynamic&nbsp;linking&nbsp;and&nbsp;avoiding&nbsp;any&nbsp;limit&nbsp;on&nbsp;the&nbsp;size&nbsp;of&nbsp;the&nbsp;global&nbsp;offset&nbsp;table.This&nbsp;option&nbsp;makes&nbsp;a&nbsp;difference&nbsp;on&nbsp;AArch74,&nbsp;m68k,&nbsp;PowerPC&nbsp;and&nbsp;SPARC.\n\n\tPosition-independent&nbsp;code&nbsp;requires&nbsp;special&nbsp;support,&nbsp;and&nbsp;therefore&nbsp;works&nbsp;only&nbsp;on&nbsp;certain&nbsp;machines.\n\n\tWhen&nbsp;this&nbsp;flag&nbsp;is&nbsp;set,&nbsp;the&nbsp;macros&nbsp;`__pic__`&nbsp;and&nbsp;`__PIC__`&nbsp;are&nbsp;defined&nbsp;to&nbsp;2.\n\n-fpie\n-fPIE\n\n\tThese&nbsp;options&nbsp;are&nbsp;similar&nbsp;to&nbsp;-fpic&nbsp;and&nbsp;-fPIC,&nbsp;but&nbsp;the&nbsp;generated&nbsp;position-independent&nbsp;code&nbsp;can&nbsp;be&nbsp;only&nbsp;linked&nbsp;into&nbsp;executables.&nbsp;Usually&nbsp;these&nbsp;options&nbsp;are&nbsp;used&nbsp;to&nbsp;compile&nbsp;code&nbsp;that&nbsp;will&nbsp;be&nbsp;linked&nbsp;using&nbsp;the&nbsp;&nbsp;-pie&nbsp;&nbsp;GCC&nbsp;option.\n\n\t-fpie&nbsp;and&nbsp;-fPIE&nbsp;both&nbsp;define&nbsp;the&nbsp;macros&nbsp;`__pie__`&nbsp;and&nbsp;`__PIE__`.&nbsp;The&nbsp;macros&nbsp;have&nbsp;the&nbsp;value&nbsp;1&nbsp;for&nbsp;`-fpie`&nbsp;and&nbsp;2&nbsp;for&nbsp;`-fPIE`.<\/pre>\n<ul>\n<li>\n<p>\u533a\u522b\u5728\u4e8efpic\/fPIC\u7528\u4e8e\u5171\u4eab\u5e93\u7684\u7f16\u8bd1\uff0cfpie\/fPIE\u5219\u662fpie\u6587\u4ef6\u7f16\u8bd1\u7684\u9009\u9879\u3002\u6587\u6863\u4e2d\u8bf4 pic\uff08\u4f4d\u7f6e\u65e0\u5173\u4ee3\u7801\uff09\u751f\u6210\u7684\u5171\u4eab\u5e93\u53ea\u80fd\u94fe\u63a5\u4e8e\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4e4b\u540e\u6839\u636e\u81ea\u5df1\u7f16\u8bd1\u7b80\u5355C\u7a0b\u5e8f\uff0cpie\u6b63\u5e38\u8fd0\u884c\uff0c\u5373\u5982\u7f51\u4e0a\u8bb8\u591a\u6587\u7ae0\u8bf4\u7684 pie \u9009\u9879\u751f\u6210\u7684\u4f4d\u7f6e\u65e0\u5173\u4ee3\u7801\u53ef\u5047\u5b9a\u4e8e\u672c\u7a0b\u5e8f\uff0c\u4f46\u662f\u6211\u4e5f\u6ca1\u770b\u51fafpie\/fPIE\u6709\u5565\u533a\u522b\uff0c\u53ea\u662f\u5b8f\u5b9a\u4e49\u53ea\u4e3a1\u548c2\u7684\u533a\u522b\uff0c\u8c8c\u4f3c&#8230;<br \/>\u7f16\u8bd1\u547d\u4ee4\uff08\u9ed8\u8ba4\u4e0d\u5f00\u542fPIE\uff09\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre>gcc&nbsp;-fpie&nbsp;-pie&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5f00\u542fPIE\ngcc&nbsp;-fPIE&nbsp;-pie&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5f00\u542fPIE\ngcc&nbsp;-fpic&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5f00\u542fPIC\ngcc&nbsp;-fPIC&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5f00\u542fPIC\ngcc&nbsp;-no-pie&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5173\u95edPIE<\/pre>\n<ul>\n<li>\n<p>\u800cASLR\uff08\u5730\u5740\u7a7a\u95f4\u968f\u673a\u5316\uff09\uff0c\u5f53\u521d\u8bbe\u8ba1\u65f6\u53ea\u8d1f\u8d23\u6808\u3001\u5e93\u3001\u5806\u7b49\u6bb5\u7684\u5730\u5740\u968f\u673a\u5316\u3002ASLR\u7684\u503c\u5b58\u4e8e<code>\/proc\/sys\/kernel\/randomize_va_space<\/code>\u4e2d\uff0c\u5982\u4e0b\uff1a<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p>0 &#8211; \u8868\u793a\u5173\u95ed\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u968f\u673a\u5316\u3002<br \/>1 &#8211; \u8868\u793a\u5c06mmap\u7684\u57fa\u5740\uff0cstack\u548cvdso\u9875\u9762\u968f\u673a\u5316\u3002<br \/>2 &#8211; \u8868\u793a\u57281\u7684\u57fa\u7840\u4e0a\u589e\u52a0\u6808\uff08heap\uff09\u7684\u968f\u673a\u5316\u3002\uff08\u9ed8\u8ba4\uff09<\/p>\n<\/blockquote>\n<blockquote>\n<p>\u66f4\u6539\u5176\u503c\u65b9\u5f0f\uff1aecho &nbsp;0 &gt; &nbsp;\/proc\/sys\/kernel\/randomize_va_space<\/p>\n<\/blockquote>\n<p>vDSO\uff1avirtual dynamic shared object\uff1b<br \/>mmap\uff1a\u5373\u5185\u5b58\u7684\u6620\u5c04\u3002<br \/><code>PIE<\/code> \u5219\u662f\u8d1f\u8d23\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u57fa\u5740\u968f\u673a\u3002<br \/>\u4ee5\u4e0b\u6458\u81eaWiki\uff1a<\/p>\n<blockquote>\n<p>Position-independent executable (PIE) implements a random base address for the main executable binary and has been in place since 2003. It provides the same address randomness to the main executable as being used for the shared libraries.<\/p>\n<\/blockquote>\n<p>PIE\u4e3aASLR\u7684\u4e00\u90e8\u5206\uff0cASLR\u4e3a\u7cfb\u7edf\u529f\u80fd\uff0cPIE\u5219\u4e3a\u7f16\u8bd1\u9009\u9879\u3002<br \/><strong>\u6ce8\uff1a<\/strong> \u5728heap\u5206\u914d\u65f6\uff0c\u6709<code>mmap()<\/code>\u548c<code>brk()<\/code>\u4e24\u79cd\u65b9\u5f0f\uff0c\u7531<code>malloc()<\/code>\u5206\u914d\u5185\u5b58\u65f6\u8c03\u7528\uff0c\u5206\u914d\u8f83\u5c0f\u65f6brk\uff0c\u5426\u5219mmap\uff0c128k\u533a\u522b\u3002<\/p>\n<h4>\u4e09\u3001Canary\uff08\u6808\u4fdd\u62a4\uff09<\/h4>\n<p>\u2003\u2003Canary\u5bf9\u4e8e\u6808\u7684\u4fdd\u62a4\uff0c\u5728\u51fd\u6570\u6bcf\u4e00\u6b21\u6267\u884c\u65f6\uff0c\u5728\u6808\u4e0a\u968f\u673a\u4ea7\u751f\u4e00\u4e2aCanary\u503c\u3002\u4e4b\u540e\u5f53\u51fd\u6570\u6267\u884c\u7ed3\u675f\u8fd4\u56de\u65f6\u68c0\u6d4bCanary\u503c\uff0c\u82e5\u4e0d\u4e00\u81f4\u7cfb\u7edf\u5219\u62a5\u51fa\u5f02\u5e38\u3002<\/p>\n<ul>\n<li>\n<p>Wiki\uff1a<\/p>\n<\/li>\n<li>\n<p><em>Canaries<\/em> or <em>canary words<\/em> are known values that are placed between a buffer and control data on the stack to monitor buffer overflows. When the buffer overflows, the first data to be corrupted will usually be the canary, and a failed verification of the canary data will therefore alert of an overflow, which can then be handled, for example, by invalidating the corrupted data. A canary value should not be confused with a sentinel value.<\/p>\n<\/li>\n<\/ul>\n<p>\u2003\u2003\u5982\u4e0a\u6240\u8ff0\uff0cCanary\u503c\u7f6e\u4e8e\u7f13\u51b2\u533a\u548c\u63a7\u5236\u6570\u636e\u4e4b\u95f4\uff0c\u5f53\u7f13\u51b2\u533a\u6ea2\u51fa\uff0c\u8be5\u503c\u88ab\u8986\u5199\uff0c\u4ece\u800c\u53ef\u4ee5\u68c0\u6d4b\u4ee5\u5224\u65ad\u662f\u5426\u8fd0\u884c\u51fa\u9519\u6216\u662f\u53d7\u5230\u653b\u51fb\u3002\u7f13\u89e3\u7f13\u51b2\u533a\u6ea2\u51fa\u653b\u51fb\u3002<\/p>\n<ul>\n<li>\n<p>\u7f16\u8bd1\u9009\u9879\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre>gcc&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u9ed8\u8ba4\u5173\u95ed\ngcc&nbsp;-fno-stack-protector&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;\/\/\u7981\u7528\u6808\u4fdd\u62a4\ngcc&nbsp;-fstack-protector&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u542f\u7528\u5806\u6808\u4fdd\u62a4\uff0c\u4e0d\u8fc7\u53ea\u4e3a\u5c40\u90e8\u53d8\u91cf\u4e2d\u542b\u6709&nbsp;char&nbsp;\u6570\u7ec4\u7684\u51fd\u6570\u63d2\u5165\u4fdd\u62a4\u4ee3\u7801\ngcc&nbsp;-fstack-protector-all&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;\/\/\u542f\u7528\u5806\u6808\u4fdd\u62a4\uff0c\u4e3a\u6240\u6709\u51fd\u6570\u63d2\u5165\u4fdd\u62a4\u4ee3\u7801<\/pre>\n<h4>\u56db\u3001RELRO\uff08RELocation Read Only\uff09<\/h4>\n<p>\u5728Linux\u4e2d\u6709\u4e24\u79cdRELRO\u6a21\u5f0f\uff1a<code>\u201dPartial RELRO\u201c<\/code> \u548c <code>\u201dFull RELRO\u201c<\/code>\u3002Linux\u4e2dPartical RELRO\u9ed8\u8ba4\u5f00\u542f\u3002<\/p>\n<p><strong>Partial RELRO\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u7f16\u8bd1\u547d\u4ee4\uff1a<br \/>gcc -o test test.c &nbsp;\/\/ \u9ed8\u8ba4\u90e8\u5206\u5f00\u542f<br \/>gcc -Wl,-z,relro -o test test.c \/\/ \u5f00\u542f\u90e8\u5206RELRO<br \/>gcc -z lazy -o test test.c \/\/ \u90e8\u5206\u5f00\u542f<\/p>\n<\/li>\n<li>\n<p>\u8be5ELF\u6587\u4ef6\u7684\u5404\u4e2a\u90e8\u5206\u88ab\u91cd\u65b0\u6392\u5e8f\u3002\u5185\u6570\u636e\u6bb5\uff08internal data sections\uff09\uff08\u5982.got\uff0c.dtors\u7b49\uff09\u7f6e\u4e8e\u7a0b\u5e8f\u6570\u636e\u6bb5\uff08program&#8217;s data sections\uff09\uff08\u5982.data\u548c.bss\uff09\u4e4b\u524d\uff1b<\/p>\n<\/li>\n<li>\n<p>\u65e0 plt \u6307\u5411\u7684GOT\u662f\u53ea\u8bfb\u7684\uff1b<\/p>\n<\/li>\n<li>\n<p>GOT\u8868\u53ef\u5199\uff08\u5e94\u8be5\u662f\u4e0e\u4e0a\u9762\u6709\u6240\u533a\u522b\u7684\uff09\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>Full RELRO\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u7f16\u8bd1\u547d\u4ee4\uff1a<br \/>gcc -Wl,-z,relro,-z,now -o test test.c &nbsp; &nbsp;\/\/ \u5f00\u542fFull RELRO<br \/>gcc -z now -o test test.c &nbsp;\/\/ \u5168\u90e8\u5f00\u542f<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301Partial\u6a21\u5f0f\u7684\u6240\u6709\u529f\u80fd\uff1b<\/p>\n<\/li>\n<li>\n<p>\u6574\u4e2aGOT\u8868\u6620\u5c04\u4e3a\u53ea\u8bfb\u7684\u3002<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p>gcc -z norelro -o a a.c \/\/ RELRO\u5173\u95ed\uff0c\u5373No RELRO<\/p>\n<\/blockquote>\n<p><strong>Note\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>.dtors\uff1a\u5f53\u5b9a\u4e49\u6709.dtors\u7684\u5171\u4eab\u5e93\u88ab\u52a0\u8f7d\u65f6\u8c03\u7528\uff1b<\/p>\n<\/li>\n<li>\n<p>\u5728bss\u6216\u6570\u636e\u6ea2\u51fa\u9519\u8bef\u7684\u60c5\u51b5\u4e0b\uff0cPartial\u548cFull RELRO\u4fdd\u62a4ELF\u5185\u6570\u636e\u6bb5\u4e0d\u88ab\u8986\u76d6\u3002 \u4f46\u53ea\u6709Full RELRO\u53ef\u4ee5\u7f13\u89e3GOT\u8868\u8986\u5199\u653b\u51fb\uff0c\u4f46\u662f\u76f8\u6bd4\u8f83\u800c\u8a00\u5f00\u9500\u8f83\u5927\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u5728\u542f\u52a8\u4e4b\u524d\u9700\u8981\u89e3\u6790\u6240\u6709\u7684\u7b26\u53f7\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>linux nx\u6307\u7684\u662f\u4ec0\u4e48 \u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300alinux nx\u6307\u7684\u662f\u4ec0\u4e48\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ linux nx\u662f\u6307\u201cNo-eXecute\u201d\uff0c\u662flinux\u4e2d\u7684\u4e00\u79cd\u4fdd\u62a4\u673a\u5236\uff0c\u4e5f\u5c31\u662f\u6570\u636e\u4e0d\u53ef\u6267\u884c\uff0c\u9632\u6b62\u56e0\u4e3a\u7a0b\u5e8f\u8fd0\u884c\u51fa\u73b0\u6ea2\u51fa\u800c\u4f7f\u5f97\u653b\u51fb\u8005\u7684shellcode\u53ef\u80fd\u4f1a\u5728\u6570\u636e\u533a\u5c1d\u8bd5\u6267\u884c\u7684\u60c5\u51b5\u3002 Linux\u7a0b\u5e8f\u5e38\u89c1\u7528\u7684\u4e00\u4e9b\u4fdd\u62a4\u673a\u5236 \u4e00\u3001NX\uff08Windows\u4e2d\u7684DEP\uff09 NX\uff1aNo-eXecute\u3001DEP\uff1aData Execute Prevention \u4e5f\u5c31\u662f\u6570\u636e\u4e0d\u53ef\u6267\u884c\uff0c\u9632\u6b62\u56e0\u4e3a\u7a0b\u5e8f\u8fd0\u884c\u51fa\u73b0\u6ea2\u51fa\u800c\u4f7f\u5f97\u653b\u51fb\u8005\u7684shellcode\u53ef\u80fd\u4f1a\u5728\u6570\u636e\u533a\u5c1d\u8bd5\u6267\u884c\u7684\u60c5\u51b5\u3002 gcc\u9ed8\u8ba4\u5f00\u542f\uff0c\u9009\u9879\u6709\uff1a gcc&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f00\u542fNX\u4fdd\u62a4 gcc&nbsp;-z&nbsp;execstack&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;\/\/&nbsp;\u7981\u7528NX\u4fdd\u62a4 gcc&nbsp;-z&nbsp;noexecstack&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;\/\/&nbsp;\u5f00\u542fNX\u4fdd\u62a4 \u4e8c\u3001PIE\uff08ASLR\uff09 PIE\uff1aPosition-Independent Excutable\u3001ASLR\uff1aAddress Space Layout Randomization fpie\/fPIE\uff1a\u9700\u8981\u548c\u9009\u9879-pie\u4e00\u8d77\u4f7f\u7528\u5f00\u542fpie\u9009\u9879\u7f16\u8bd1\u53ef\u6267\u884c\u6587\u4ef6\u4f7f\u5f97elf\u62e5\u6709\u5171\u4eab\u5e93\u5c5e\u6027\uff0c\u53ef\u4ee5\u5728\u5185\u5b58\u4efb\u4f55\u5730\u65b9\u52a0\u8f7d\u8fd0\u884c\u3002\u4e0e\u4e4b\u76f8\u4f3c\u7684\u8fd8\u6709fpic\/fPIC\uff0c\u5173\u4e8e\u5176\u8bf4\u660ehttps:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Code-Gen-Options.html -fpic Generate&nbsp;position-independent&nbsp;code&nbsp;(PIC)&nbsp;suitable&nbsp;for&nbsp;use&nbsp;in&nbsp;a&nbsp;shared&nbsp;library,&nbsp;if&nbsp;supported&nbsp;for&nbsp;the&nbsp;target&nbsp;machine.&nbsp;Such&nbsp;code&nbsp;accesses&nbsp;all&nbsp;constant&nbsp;addresses&nbsp;through&nbsp;a&nbsp;global&nbsp;offset&nbsp;table&nbsp;(GOT).&nbsp;The&nbsp;dynamic&nbsp;loader&nbsp;resolves&nbsp;the&nbsp;GOT&nbsp;entries&nbsp;when&nbsp;the&nbsp;program&nbsp;starts&nbsp;(the&nbsp;dynamic&nbsp;loader&nbsp;is&nbsp;not&nbsp;part&nbsp;of&nbsp;GCC;&nbsp;it&nbsp;is&nbsp;part&nbsp;of&nbsp;the&nbsp;operating&nbsp;system).&nbsp;If&nbsp;the&nbsp;GOT&nbsp;size&nbsp;for&nbsp;the&nbsp;linked&nbsp;executable&nbsp;exceeds&nbsp;a&nbsp;machine-specific&nbsp;maximum&nbsp;size,&nbsp;you&nbsp;get&nbsp;an&nbsp;error&nbsp;message&nbsp;from&nbsp;the&nbsp;linker&nbsp;indicating&nbsp;that&nbsp;-fpic&nbsp;does&nbsp;not&nbsp;work;&nbsp;in&nbsp;that&nbsp;case,&nbsp;recompile&nbsp;with&nbsp;-fPIC&nbsp;instead.&nbsp;(These&nbsp;maximums&nbsp;are&nbsp;8k&nbsp;on&nbsp;the&nbsp;SPARC,&nbsp;28k&nbsp;on&nbsp;AArch74&nbsp;and&nbsp;32k&nbsp;on&nbsp;the&nbsp;m68k&nbsp;and&nbsp;RS\/6000.&nbsp;The&nbsp;x86&nbsp;has&nbsp;no&nbsp;such&nbsp;limit.) Position-independent&nbsp;code&nbsp;requires&nbsp;special&nbsp;support,&nbsp;and&nbsp;therefore&nbsp;works&nbsp;only&nbsp;on&nbsp;certain&nbsp;machines.&nbsp;For&nbsp;the&nbsp;x86,&nbsp;GCC&nbsp;supports&nbsp;PIC&nbsp;for&nbsp;System&nbsp;V&nbsp;but&nbsp;not&nbsp;for&nbsp;the&nbsp;Sun&nbsp;386i.&nbsp;Code&nbsp;generated&nbsp;for&nbsp;the&nbsp;IBM&nbsp;RS\/6000&nbsp;is&nbsp;always&nbsp;position-independent. When&nbsp;this&nbsp;flag&nbsp;is&nbsp;set,&nbsp;the&nbsp;macros&nbsp;`__pic__`&nbsp;and&nbsp;`__PIC__`&nbsp;are&nbsp;defined&nbsp;to&nbsp;1. -fPIC If&nbsp;supported&nbsp;for&nbsp;the&nbsp;target&nbsp;machine,&nbsp;emit&nbsp;position-independent&nbsp;code,&nbsp;suitable&nbsp;for&nbsp;dynamic&nbsp;linking&nbsp;and&nbsp;avoiding&nbsp;any&nbsp;limit&nbsp;on&nbsp;the&nbsp;size&nbsp;of&nbsp;the&nbsp;global&nbsp;offset&nbsp;table.This&nbsp;option&nbsp;makes&nbsp;a&nbsp;difference&nbsp;on&nbsp;AArch74,&nbsp;m68k,&nbsp;PowerPC&nbsp;and&nbsp;SPARC. Position-independent&nbsp;code&nbsp;requires&nbsp;special&nbsp;support,&nbsp;and&nbsp;therefore&nbsp;works&nbsp;only&nbsp;on&nbsp;certain&nbsp;machines. When&nbsp;this&nbsp;flag&nbsp;is&nbsp;set,&nbsp;the&nbsp;macros&nbsp;`__pic__`&nbsp;and&nbsp;`__PIC__`&nbsp;are&nbsp;defined&nbsp;to&nbsp;2. -fpie -fPIE These&nbsp;options&nbsp;are&nbsp;similar&nbsp;to&nbsp;-fpic&nbsp;and&nbsp;-fPIC,&nbsp;but&nbsp;the&nbsp;generated&nbsp;position-independent&nbsp;code&nbsp;can&nbsp;be&nbsp;only&nbsp;linked&nbsp;into&nbsp;executables.&nbsp;Usually&nbsp;these&nbsp;options&nbsp;are&nbsp;used&nbsp;to&nbsp;compile&nbsp;code&nbsp;that&nbsp;will&nbsp;be&nbsp;linked&nbsp;using&nbsp;the&nbsp;&nbsp;-pie&nbsp;&nbsp;GCC&nbsp;option. -fpie&nbsp;and&nbsp;-fPIE&nbsp;both&nbsp;define&nbsp;the&nbsp;macros&nbsp;`__pie__`&nbsp;and&nbsp;`__PIE__`.&nbsp;The&nbsp;macros&nbsp;have&nbsp;the&nbsp;value&nbsp;1&nbsp;for&nbsp;`-fpie`&nbsp;and&nbsp;2&nbsp;for&nbsp;`-fPIE`. \u533a\u522b\u5728\u4e8efpic\/fPIC\u7528\u4e8e\u5171\u4eab\u5e93\u7684\u7f16\u8bd1\uff0cfpie\/fPIE\u5219\u662fpie\u6587\u4ef6\u7f16\u8bd1\u7684\u9009\u9879\u3002\u6587\u6863\u4e2d\u8bf4 pic\uff08\u4f4d\u7f6e\u65e0\u5173\u4ee3\u7801\uff09\u751f\u6210\u7684\u5171\u4eab\u5e93\u53ea\u80fd\u94fe\u63a5\u4e8e\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4e4b\u540e\u6839\u636e\u81ea\u5df1\u7f16\u8bd1\u7b80\u5355C\u7a0b\u5e8f\uff0cpie\u6b63\u5e38\u8fd0\u884c\uff0c\u5373\u5982\u7f51\u4e0a\u8bb8\u591a\u6587\u7ae0\u8bf4\u7684 pie \u9009\u9879\u751f\u6210\u7684\u4f4d\u7f6e\u65e0\u5173\u4ee3\u7801\u53ef\u5047\u5b9a\u4e8e\u672c\u7a0b\u5e8f\uff0c\u4f46\u662f\u6211\u4e5f\u6ca1\u770b\u51fafpie\/fPIE\u6709\u5565\u533a\u522b\uff0c\u53ea\u662f\u5b8f\u5b9a\u4e49\u53ea\u4e3a1\u548c2\u7684\u533a\u522b\uff0c\u8c8c\u4f3c&#8230;\u7f16\u8bd1\u547d\u4ee4\uff08\u9ed8\u8ba4\u4e0d\u5f00\u542fPIE\uff09\uff1a gcc&nbsp;-fpie&nbsp;-pie&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5f00\u542fPIE gcc&nbsp;-fPIE&nbsp;-pie&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5f00\u542fPIE gcc&nbsp;-fpic&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5f00\u542fPIC gcc&nbsp;-fPIC&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5f00\u542fPIC gcc&nbsp;-no-pie&nbsp;-o&nbsp;test&nbsp;test.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5173\u95edPIE \u800cASLR\uff08\u5730\u5740\u7a7a\u95f4\u968f\u673a\u5316\uff09\uff0c\u5f53\u521d\u8bbe\u8ba1\u65f6\u53ea\u8d1f\u8d23\u6808\u3001\u5e93\u3001\u5806\u7b49\u6bb5\u7684\u5730\u5740\u968f\u673a\u5316\u3002ASLR\u7684\u503c\u5b58\u4e8e\/proc\/sys\/kernel\/randomize_va_space\u4e2d\uff0c\u5982\u4e0b\uff1a 0 &#8211; \u8868\u793a\u5173\u95ed\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u968f\u673a\u5316\u30021 &#8211; \u8868\u793a\u5c06mmap\u7684\u57fa\u5740\uff0cstack\u548cvdso\u9875\u9762\u968f\u673a\u5316\u30022 &#8211; \u8868\u793a\u57281\u7684\u57fa\u7840\u4e0a\u589e\u52a0\u6808\uff08heap\uff09\u7684\u968f\u673a\u5316\u3002\uff08\u9ed8\u8ba4\uff09 \u66f4\u6539\u5176\u503c\u65b9\u5f0f\uff1aecho &nbsp;0 &gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-52034","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52034"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52034\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}