{"id":630,"date":"2022-04-02T13:37:43","date_gmt":"2022-04-02T05:37:43","guid":{"rendered":"https:\/\/dreamc.top\/?p=630"},"modified":"2025-03-14T22:47:23","modified_gmt":"2025-03-14T14:47:23","slug":"pwn_notes","status":"publish","type":"post","link":"https:\/\/dreamc.top\/index.php\/2022\/04\/02\/pwn_notes\/","title":{"rendered":"\u300aCTF\u6743\u5a01\u6307\u5357(pwn)\u300b\u5b66\u4e60\u7b14\u8bb0"},"content":{"rendered":"<h2 class=\"wp-block-heading\" id=\"\u7b2c\u4e00\u7ae0\"><span class=\"ez-toc-section\" id=\"%E7%AC%AC%E4%B8%80%E7%AB%A0\"><\/span>\u7b2c\u4e00\u7ae0<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3 class=\"wp-block-heading\" id=\"\u77e5\u540d\u5b89\u5168\u4f1a\u8bae\"><span class=\"ez-toc-section\" id=\"%E7%9F%A5%E5%90%8D%E5%AE%89%E5%85%A8%E4%BC%9A%E8%AE%AE\"><\/span>\u77e5\u540d\u5b89\u5168\u4f1a\u8bae<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-size: large\"><br>RSA\u3001Black Hat\u3001DEFCON\u3001ISC(\u4e2d\u56fd\u4e92\u8054\u7f51\u5b89\u5168\u5927\u4f1a)<br>\u5b66\u672f\u9876\u4f1a<br>CCS(A): ACM Conference on Computer and Communications Security<br>NDSS(B): Network and Distributed System Security Symposium<br>Oakland S&amp;P(A): IEEE Symposium on Security &amp; Privacy<br>USENIX(A): USENIX Security Symposium<\/span><\/p>\n<h3 class=\"wp-block-heading\" id=\"\u5b66\u4e60\u8def\u7ebf\"><span class=\"ez-toc-section\" id=\"%E5%AD%A6%E4%B9%A0%E8%B7%AF%E7%BA%BF\"><\/span>\u5b66\u4e60\u8def\u7ebf<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4 class=\"wp-block-heading\" id=\"\u8ba1\u7b97\u673a\u57fa\u7840\"><span class=\"ez-toc-section\" id=\"%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80\"><\/span>\u8ba1\u7b97\u673a\u57fa\u7840<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u4e2d\u6587\uff1a\u7f51\u6613\u4e91\u8bfe\u5802\u7684\u5927\u5b66\u8ba1\u7b97\u673a\u4e13\u4e1a\u8bfe\u7a0b\u4f53\u7cfb<br>\u82f1\u6587\uff1aHarvard CS50 Introduction to Computer Science<br>CMU 18-447 Introduction to Computer Architecture<br>MIT 6.828 Operating System Engineering<br>Stanford CS143 Compilers<\/p>\n<h4 class=\"wp-block-heading\" id=\"\u9006\u5411\u5de5\u7a0b\"><span class=\"ez-toc-section\" id=\"%E9%80%86%E5%90%91%E5%B7%A5%E7%A8%8B\"><\/span>\u9006\u5411\u5de5\u7a0b<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u638c\u63e1\u5404\u5e73\u53f0\u7684\u9759\u6001\u53cd\u6c47\u7f16(IDA\u3001Radare2)\u548c\u52a8\u6001\u8c03\u8bd5\uff08GDB\u3001x64dbg\uff09\u5de5\u5177\uff0c\u719f\u7ec3\u9605\u8bfb\u53cd\u6c47\u7f16\u4ee3\u7801\uff0c\u7406\u89e3x86\u3001ARM\u548cMIPS\u4e8c\u8fdb\u5236\u7a0b\u5e8f\uff0c\u6ce8\u610f\u7a0b\u5e8f\u7ed3\u6784\u7ec4\u6210\u548c\u7f16\u8bd1\u8fd0\u884c\u7684\u7ec6\u8282\u3002<br>\u63a8\u8350\u4e66\u7c4d\uff1a<br>Secure Coding in C and C++\uff0c2nd Edition<br>The Intel 64 and IA-32 Architectures Software Developer&#8217;s Manual<br>ARM Cortex-A Series Programmer&#8217;s Guide<br>See MIPS Run.2nd Edition<br>Reverse Engineering for Beginners<br>\u300a\u7a0b\u5e8f\u5458\u7684\u81ea\u6211\u4fee\u517b&#8211;\u94fe\u63a5\u3001\u88c5\u8f7d\u4e0e\u5e93\u300b<br>\u300a\u52a0\u5bc6\u4e0e\u89e3\u5bc6\uff0c\u7b2c4\u7248\u300b<\/p>\n<h4 class=\"wp-block-heading\" id=\"\u6f0f\u6d1e\u5b66\u4e60\"><span class=\"ez-toc-section\" id=\"%E6%BC%8F%E6%B4%9E%E5%AD%A6%E4%B9%A0\"><\/span>\u6f0f\u6d1e\u5b66\u4e60<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u4eceCTF\u5207\u5165\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u601d\u8def\u3002\u5b66\u4e60\u5e38\u89c1\u7684\u6f0f\u6d1e(\u6ea2\u51fa\u3001UAF\u3001double-free\u7b49)\u7684\u539f\u7406\u3001Linux\u6f0f\u6d1e\u7f13\u89e3\u673a\u5236\uff08stack canaries\u3001NX\u3001ASLR\u7b49\uff09\u4ee5\u53ca\u9488\u5bf9\u8fd9\u4e9b\u673a\u5236\u7684\u6f0f\u6d1e\u5229\u7528\u65b9\u6cd5(stack smashing\u3001shellcoding\u3001ROP\u7b49)\uff0c\u6b64\u9636\u6bb5\u8fd8\u53ef\u4ee5\u8bfbwp\u6765\u5b66\u4e60\u3002\u5728\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u77e5\u8bc6\u4e4b\u540e\u5c31\u53ef\u4ee5\uff0c\u5c1d\u8bd5\u5206\u6790\u771f\u5b9e\u73af\u5883\u4e2d\u7684\u6f0f\u6d1e\uff0c\u6216\u8005\u5206\u6790\u4e00\u4e9b\u6076\u610f\u6837\u672c\u4e86\u3002<br>\u63a8\u8350\u8d44\u6599\uff1a<br>RPI CSCI-4968 Modern Binary Exploitation<br>Hacking: The Art of Exploitation,2nd Edition<br>The Shellcode r&#8217; s Handbook,2nd Edition<br>Practical Malware Analysis<br>\u300a\u6f0f\u6d1e\u6218\u4e89\uff1a\u8f6f\u4ef6\u6f0f\u6d1e\u5206\u6790\u7cbe\u8981\u300b<\/p>\n<p>\u6709\u4e86\u5b9e\u8df5\u7684\u57fa\u7840\u540e\uff0c\u53ef\u4ee5\u5b66\u4e60\u5206\u6790\u4e00\u4e9b\u7a0b\u5e8f\u5206\u6790\u7406\u8bba\uff0c\u6bd4\u5982\u6570\u636e\u6d41\u5206\u6790(\u5de5\u5177\u5982Soot)\u3001\u503c\u96c6\u5206\u6790(BAP)\u3001\u53ef\u6ee1\u8db3\u6027\u7406\u8bba(Z3)\u3001\u52a8\u6001\u4e8c\u8fdb\u5236\u63d2\u6869(DynamoRio\u3001Pin)\u3001\u7b26\u53f7\u6267\u884c(KLEE\u3001angr)\u3001\u6a21\u7cca\u6d4b\u8bd5(Peach\u3001AFL)\u7b49\u3002\u8fd9\u4e9b\u6280\u672f\u5bf9\u4e8e\u7a0b\u5e8f\u5206\u6790\u548c\u6f0f\u6d1e\u6316\u6398\u81ea\u52a8\u5316\u975e\u5e38\u91cd\u8981\uff0c\u662f\u5b66\u672f\u754c\u548c\u5de5\u4e1a\u754c\u90fd\u5728\u7814\u7a76\u7684\u70ed\u70b9\u3002\u611f\u5174\u8da3\u7684\u8fd8\u53ef\u4ee5\u5173\u6ce8\u4e0b\u81ea\u52a8\u5316\u7f51\u7edc\u653b\u9632\u7684CGC\u7ade\u8d5b\u3002\u63a8\u8350\u8d44\u6599\u5982\u4e0b\uff1a<br>UT Dallas CS-6V81 System Security and Binary Code Analysis<br>AU static Program Analysis Lecture notes<\/p>\n<p>\u5982\u679c\u662f\u8d70\u5b66\u672f\u8def\u7ebf\uff0c\u9605\u8bfb\u8bba\u6587\u5fc5\u4e0d\u53ef\u5c11\uff0c\u4e00\u5f00\u59cb\u53ef\u4ee5\u8bfb\u7efc\u8ff0\u7c7b\u7684\u6587\u7ae0\uff0c\u5bf9\u67d0\u4e2a\u9886\u57df\u7684\u7814\u7a76\u60c5\u51b5\u6709\u5168\u9762\u7684\u4e86\u89e3\uff0c\u7136\u540e\u8ddf\u968f\u7efc\u8ff0\u53bb\u627e\u76f8\u5e94\u7684\u8bba\u6587\u3002\u6bd4\u8f83\u63a8\u8350\u4f1a\u8bae\u8bba\u6587\uff0c\u56e0\u4e3a\u901a\u5e38\u53ef\u4ee5\u5728\u4f5c\u8005\u4e2a\u4eba\u4e3b\u9875\u4e0a\u627e\u5230\u5e7b\u706f\u7247\uff0c\u751a\u81f3\u4f1a\u8bae\u5f55\u50cf\u89c6\u9891\uff0c\u5bf9\u5b66\u4e60\u7406\u89e3\u8bba\u6587\u5f88\u6709\u5e2e\u52a9\u3002\u5982\u679c\u76f4\u63a5\u8bfb\u8bba\u6587\u5219\u611f\u89c9\u4f1a\u6709\u4e9b\u56f0\u96be\uff0c\u8fd9\u91cc\u63a8\u8350\u4e0a\u4ea4\u5927&#8221;\u871a\u8bed&#8221;\u5b89\u5168\u5c0f\u7ec4\u7684\u8bba\u6587\u7b14\u8bb0\u3002\u575a\u6301\u8bfb\u3001\u591a\u601d\u8003\uff0c\u76f8\u4fe1\u91cf\u53d8\u7ec8\u4f1a\u4ea7\u751f\u8d28\u53d8\u3002<\/p>\n<p>\u4e3a\u4e86\u6301\u7eed\u5b66\u4e60\u548c\u63d0\u5347\uff0c\u8fd8\u9700\u8981\u6536\u96c6\u548c\u8ba2\u9605\u4e00\u4e9b\u5b89\u5168\u8d44\u8baf(FreeBuf\u3001SecWiki\u3001\u5b89\u5168\u5ba2)\u3001\u6f0f\u6d1e\u62ab\u9732(exploit-db\u3001CVE)\u3001\u6280\u672f\u8bba\u575b(\u770b\u96ea\u8bba\u575b\u3001\u543e\u7231\u7834\u89e3\u3001\u5148\u77e5\u793e\u533a)\u548c\u5927\u725b\u7684\u6280\u672f\u535a\u5ba2\uff0c\u8fd9\u4e00\u6b65\u53ef\u4ee5\u901a\u8fc7RSS Feed\u6765\u5b8c\u6210\u3002\u968f\u7740\u793e\u4f1a\u5a92\u4f53\u7684\u53d1\u5c55\uff0c\u5f88\u591a\u5b89\u5168\u56e2\u961f\u548c\u4e2a\u4eba\u90fd\u8f6c\u6218\u5230\u4e86Twitter\u5fae\u535a\u3001\u5fae\u4fe1\u516c\u4f17\u53f7\u7b49\u65b0\u5a92\u4f53\u4e0a\uff0c\u8bf7\u679c\u65ad\u5173\u6ce8\u4ed6\u4eec(\u64cd\u4f5c\u6280\u5de7:\u4ece\u67d0\u4e2a\u5b89\u5168\u7814\u7a76\u8005\u5f00\u59cb\uff0c\u904d\u5386\u5176\u5173\u6ce8\u5217\u8868\uff0c\u7136\u540e\u9012\u5f52\uff0c\u5373\u53ef\u83b7\u5f97\u5927\u91cf\u76f8\u5173\u8d44\u6e90)\uff0c\u901a\u5e38\u53ef\u4ee5\u83b7\u5f97\u6700\u65b0\u7684\u7814\u7a76\u6210\u679c\u3001\u6f0f\u6d1e\u3001PoC\u3001\u4f1a\u8bae\u6f14\u8bb2\u7b49\u4fe1\u606f\u751a\u81f3\u8d44\u6e90\u94fe\u63a5\u7b49\u3002<br>\u76ee\u524d\u4e8c\u8fdb\u5236\u65b9\u5411\u5f88\u96be\u5c31\u4e1a\uff0c\u5982\u679c\u4e0d\u662f\u7279\u522b\u6709\u5174\u8da3\u548c\u6b7b\u78d5\u4e00\u8f88\u5b50\u7684\u51b3\u5fc3\uff0c\u5efa\u8bae\u8fd8\u662f\u9009\u62e9web\u5b89\u5168\u3001\u5b89\u5168\u7ba1\u7406\u7b49\u65b9\u5411\u3002<\/p>\n<h3 class=\"wp-block-heading\" id=\"\u5bf9\u4ece\u4e1a\u8005\u7684\u5efa\u8bae\"><span class=\"ez-toc-section\" id=\"%E5%AF%B9%E4%BB%8E%E4%B8%9A%E8%80%85%E7%9A%84%E5%BB%BA%E8%AE%AE\"><\/span>\u5bf9\u4ece\u4e1a\u8005\u7684\u5efa\u8bae<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4 class=\"wp-block-heading\" id=\"1\u5173\u4e8e\u4e2a\u4eba\u6210\u957f\"><span class=\"ez-toc-section\" id=\"1%E5%85%B3%E4%BA%8E%E4%B8%AA%E4%BA%BA%E6%88%90%E9%95%BF\"><\/span>1.\u5173\u4e8e\u4e2a\u4eba\u6210\u957f<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>1\uff09\u786e\u7acb\u65b9\u5411\uff0c\u7ed3\u5408\u5de5\u4f5c\uff0c\u627e\u51fa\u77ed\u677f<br>\u8be5\u9886\u57df\u4e3b\u8981\u4e13\u5bb6\u4eec\u7684\u5de5\u4f5c\u662f\u5426\u90fd\u4e86\u89e3\u8fc7\uff1f<br>\u76f8\u5173\u534f\u8bae\u3001\u6587\u4ef6\u683c\u5f0f\u662f\u5426\u90fd\u719f\u6089\uff1f<br>\u76f8\u5173\u6280\u672f\u548c\u4e3b\u8981\u5de5\u5177\u662f\u5426\u770b\u8fc7\u3001\u7528\u8fc7\uff1f<br>2\uff09\u9605\u8bfb\u53ea\u662f\u5b66\u4e60\u8fc7\u7a0b\u7684\u8d77\u70b9\uff0c\u4e0d\u80fd\u6b62\u4e8e\u9605\u8bfb<br>\u5de5\u5177\u7684\u6bcf\u4e2a\u53c2\u6570\u6bcf\u4e2a\u83dc\u5355\u90fd\u8981\u770b\u3001\u8981\u8bd5<br>\u5b66\u4e60\u7f51\u7edc\u534f\u8bae\u8981\u5b9e\u9645\u6293\u5305\u5206\u6790\uff0c\u5b66\u4e60\u6587\u4ef6\u683c\u5f0f\u8981\u8bfb\u4ee3\u7801\u5b9e\u73b0.\u5b66\u4e60\u8001\u6f0f\u6d1e\u4e00\u5b9a\u8981\u8c03\u8bd5\uff0c\u641e\u61c2\u6bcf\u4e00\u4e2a\u5b57\u8282\u7684\u610f\u4e49\uff0c \u4e4b\u540e\u8981\u5b8c\u5168\u81ea\u5df1\u91cd\u5199\u4e00\u4e2aExploit<br>\u7ec6\u8282\u3001\u7ec6\u8282\u3001\u7ec6\u8282\uff0c\u5228\u6839\u95ee\u5e95<\/p>\n<h4 class=\"wp-block-heading\" id=\"2\u5efa\u7acb\u5b66\u4e60\u53c2\u8003\u76ee\u6807\"><span class=\"ez-toc-section\" id=\"2%E5%BB%BA%E7%AB%8B%E5%AD%A6%E4%B9%A0%E5%8F%82%E8%80%83%E7%9B%AE%E6%A0%87\"><\/span>2.\u5efa\u7acb\u5b66\u4e60\u53c2\u8003\u76ee\u6807<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>(1)\u77ed\u671f\u53c2\u8003\u6bd4\u81ea\u5df1\u4f18\u79c0\u7684\u540c\u9f84\u4eba\u3002\u9605\u8bfb\u4ed6\u4eec\u7684\u6587\u7ae0\u548c\u5de5\u4f5c\u6210\u679c\uff0c\u4ece\u7ec6\u8282\u4e2d\u89c2\u5bdf\u4ed6\u4eec\u7684\u5b66\u4e60\u65b9\u5f0f\u548c\u5de5\u4f5c\u65b9\u5f0f\u3002<br>(2)\u4e2d\u671f\u53c2\u8003\u4f60\u7684\u65b9\u5411\u4e0a\u7684\u4e1a\u5185\u4e13\u5bb6\u3002\u4e86\u89e3\u4ed6\u4eec\u7684\u6210\u957f\u8f68\u8ff9\uff0c\u8ddf\u8e2a\u4ed6\u4eec\u5173\u6ce8\u7684\u5185\u5bb9\u3002<br>(3)\u957f\u671f\u53c2\u8003\u4e1a\u5185\u8001\u724c\u4f01\u4e1a\u548c\u5148\u950b\u4f01\u4e1a\u3002\u628a\u63e1\u884c\u4e1a\u53d1\u5c55\u3001\u6280\u672f\u8d8b\u52bf\uff0c\u4e3a\u672a\u6765\u505a\u79ef\u7d2f\u3002<\/p>\n<h4 class=\"wp-block-heading\" id=\"3\u63a8\u8350\u7684\u5b66\u4e60\u65b9\u5f0f\"><span class=\"ez-toc-section\" id=\"3%E6%8E%A8%E8%8D%90%E7%9A%84%E5%AD%A6%E4%B9%A0%E6%96%B9%E5%BC%8F\"><\/span>3.\u63a8\u8350\u7684\u5b66\u4e60\u65b9\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>(1)\u4ee5\u5de5\u5177\u4e3a\u7ebf\u7d22<br>\u4e00\u4e2a\u6bd4\u8f83\u7701\u4e8b\u7684\u5b66\u4e60\u76ee\u5f55: Kali Linux<br>\u5b66\u4e60\u601d\u8def\uff0c\u4ee5Metasploi\u4e3a\u4f8b: \u904d\u5386\u6bcf\u4e2a\u5b50\u76ee\u5f55\uff0c\u9664\u4e86Exploi\u91cc\u9762\u8fd8\u6709\u4ec0\u4e48?\u6bcf\u4e2a\u5de5\u5177\u600e\u4e48\u7528?\u539f\u7406\u662f\u4ec0\u4e48?\u6d89\u53ca\u54ea\u4e9b\u77e5\u8bc6?\u80fd\u5426\u6539\u8fdb\u4f18\u5316?\u80fd\u5426\u53d1\u5c55\u3001\u7ec4\u5408\u51fa\u65b0\u7684\u529f\u80fd?<br>(2)\u4ee5\u4e13\u5bb6\u4e3a\u7ebf\u7d22<br>\u4f60\u7684\u6280\u672f\u65b9\u5411\u4e0a\u6709\u54ea\u4e9b\u4e13\u5bb6?\u4ed6\u4eec\u7684\u90ae\u7bb1\u3001\u4e3b\u9875\u3001\u793e\u4ea4\u7f51\u7edc\u8d26\u53f7\u662f\u4ec0\u4e48?\u4ed6\u4eec\u5728\u8be5\u65b9\u5411\u4e0a\u6709\u54ea\u4e9b\u4f5c\u54c1\uff0c\u53d1\u8868\u8fc7\u54ea\u4e9b\u6f14\u8bb2?\u8ddf\u8e2a\u5173\u6ce8\uff0c\u4e00\u4e2a\u4e00\u4e2a\u5730\u5b66\u3002<\/p>\n<h4 class=\"wp-block-heading\" id=\"4\u5982\u4f55\u63d0\u9ad8\u6548\u7387\"><span class=\"ez-toc-section\" id=\"4%E5%A6%82%E4%BD%95%E6%8F%90%E9%AB%98%E6%95%88%E7%8E%87\"><\/span>4.\u5982\u4f55\u63d0\u9ad8\u6548\u7387<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u505a\u597d\u9884\u7814\uff0c\u6536\u96c6\u76f8\u5173\u524d\u4eba\u6210\u679c\uff0c\u907f\u514d\u65e0\u8c13\u7684\u91cd\u590d\u52b3\u52a8<br>\u5728\u53ef\u884c\u6027\u5224\u65ad\u9636\u6bb5\uff0c\u80fd\u627e\u5230\u5de5\u5177\u5c31\u4e0d\u5199\u4ee3\u7801\uff0c\u80fd\u7528\u811a\u672c\u8bed\u8a00\u5199\u5c31\u4e0d\u8981\u7528\u7f16\u8bd1\u8bed\u8a00\uff0c\u628a\u5b8c\u7f8e\u4e3b\u4e49\u653e\u5728\u6700\u7ec8\u5b9e\u73b0\u9636\u6bb5<br>\u505a\u597d\u7b14\u8bb0\u5e76\u5b9a\u671f\u6574\u7406\uff0c\u9057\u5fd8\u4f1a\u8ba9\u6240\u6709\u7684\u6295\u5165\u90fd\u767d\u767d\u6d6a\u8d39<br>\u591a\u548c\u540c\u4e8b\u4ea4\u6d41\uff0c\u522b\u4eba\u8bf4\u4e00\u4e2a\u5de5\u5177\u7684\u540d\u5b57\u53ef\u80fd\u8ba9\u4f60\u8282\u7ea6\u6570\u5c0f\u65f6<br>\u5904\u7406\u597d\u5b66\u4e60\u3001\u5de5\u4f5c\u548c\u751f\u6d3b<br>\u65e0\u8bba\u600e\u4e48\u63d0\u9ad8\u6548\u7387\uff0c\u8981\u6210\u4e3a\u4e13\u5bb6\uff0c\u90fd\u9700\u8981\u5927\u91cf\u7684\u65f6\u95f4\u6295\u5165<\/p>\n<h2 class=\"wp-block-heading\" id=\"\u7b2c\u4e8c\u7ae0\"><span class=\"ez-toc-section\" id=\"%E7%AC%AC%E4%BA%8C%E7%AB%A0\"><\/span>\u7b2c\u4e8c\u7ae0<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h4 class=\"wp-block-heading\" id=\"211-\u7f16\u8bd1\u539f\u7406\"><span class=\"ez-toc-section\" id=\"211_%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86\"><\/span>2.1.1 \u7f16\u8bd1\u539f\u7406<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>5\u4e2a\u6b65\u9aa4\uff1a<br>\u8bcd\u6cd5\u5206\u6790<br>\u8bed\u6cd5\u5206\u6790<br>\u8bed\u4e49\u5206\u6790<br>\u4e2d\u95f4\u4ee3\u7801\u751f\u6210\u548c\u4f18\u5316<br>\u4ee3\u7801\u751f\u6210\u548c\u4f18\u5316<\/p>\n<h4 class=\"wp-block-heading\" id=\"212-gcc\u7f16\u8bd1\u8fc7\u7a0b\"><span class=\"ez-toc-section\" id=\"212_GCC%E7%BC%96%E8%AF%91%E8%BF%87%E7%A8%8B\"><\/span>2.1.2 GCC\u7f16\u8bd1\u8fc7\u7a0b<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>GCC\u547d\u4ee4\u52a0&#8221;-save-temps&#8221;\u548c&#8221;&#8211;verbose&#8221;\u7f16\u8bd1\u9009\u9879\uff0c\u524d\u8005\u7528\u4e8e\u5c06\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u751f\u6210\u7684\u4e2d\u95f4\u6587\u4ef6\u4fdd\u5b58\u4e0b\u6765\uff0c\u540e\u8005\u67e5\u770bGCC\u7f16\u8bd1\u7684\u8be6\u7ec6\u6d41\u7a0b\u3002<br>\u4e3b\u8981\u5305\u542b\u56db\u4e2a\u9636\u6bb5\uff1a\u9884\u5904\u7406\u3001\u7f16\u8bd1\u3001\u6c47\u7f16\u548c\u94fe\u63a5<\/p>\n<h4 class=\"wp-block-heading\" id=\"213-\u9884\u5904\u7406\u9636\u6bb5\"><span class=\"ez-toc-section\" id=\"213_%E9%A2%84%E5%A4%84%E7%90%86%E9%98%B6%E6%AE%B5\"><\/span>2.1.3 \u9884\u5904\u7406\u9636\u6bb5<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5904\u7406\u6e90\u4ee3\u7801\u4e2d&#8221;#&#8221;\u5f00\u5934\u7684\u9884\u5904\u7406\u6307\u4ee4\uff0c\u5c06\u5176\u8f6c\u6362\u540e\u76f4\u63a5\u63d2\u5165\u7a0b\u5e8f\u6587\u672c\uff0c\u5f97\u5230\u53e6\u4e00\u4e2aC\u7a0b\u5e8f\uff0c\u901a\u5e38\u4ee5&#8221;.i&#8221;\u4f5c\u4e3a\u6587\u4ef6\u6269\u5c55\u540d\u3002&#8221;-E&#8221;\u9009\u9879\u53ef\u4ee5\u5355\u72ec\u6267\u884c\u9884\u5904\u7406\u3002<br>\u81ea\u884c\u4e86\u89e3\u4e00\u4e9b\u5e38\u89c1\u7684\u9884\u5904\u7406\u89c4\u5219\u3002<\/p>\n<h4 class=\"wp-block-heading\" id=\"214-\u7f16\u8bd1\u9636\u6bb5\"><span class=\"ez-toc-section\" id=\"214_%E7%BC%96%E8%AF%91%E9%98%B6%E6%AE%B5\"><\/span>2.1.4 \u7f16\u8bd1\u9636\u6bb5<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5b8c\u6210\u5206\u6790\u53ca\u4f18\u5316\uff0c\u751f\u6210\u6c47\u7f16\u4ee3\u7801\u3002&#8221;-S&#8221;\u9009\u9879\u5355\u72ec\u6267\u884c\u3002\u4f8b\uff1a&#8221;gcc -S hello.c -o hello.s&#8221;<br>GCC\u9ed8\u8ba4\u662f\u4f7f\u7528AT&amp;T\u683c\u5f0f\u7684\u6c47\u7f16\uff0c\u53ef\u4ee5\u52a0\u9009\u9879&#8221;-masm=intel&#8221;\u4f7f\u5176\u6307\u5b9a\u4e3a \u6211\u4eec\u719f\u6089\u7684intel\u683c\u5f0f\u3002&#8221;-fno-asynchronous-unwind-tables&#8221; \u5219\u7528\u4e8e\u751f\u6210\u6ca1\u6709cfi\u5b8f\u7684\u6c47\u7f16\u6307\u4ee4\uff0c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n<h4 class=\"wp-block-heading\" id=\"215-\u6c47\u7f16\u9636\u6bb5\"><span class=\"ez-toc-section\" id=\"215_%E6%B1%87%E7%BC%96%E9%98%B6%E6%AE%B5\"><\/span>2.1.5 \u6c47\u7f16\u9636\u6bb5<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5c06\u6c47\u7f16\u6307\u4ee4\u7ffb\u8bd1\u6210\u673a\u5668\u6307\u4ee4\u3002&#8221;-c&#8221;\u5355\u72ec\u6267\u884c\uff0c\u4f8b\uff1a&#8221;gcc -c hello.s -o hello.o&#8221;\u3002<\/p>\n<h4 class=\"wp-block-heading\" id=\"216-\u94fe\u63a5\u9636\u6bb5\"><span class=\"ez-toc-section\" id=\"216_%E9%93%BE%E6%8E%A5%E9%98%B6%E6%AE%B5\"><\/span>2.1.6 \u94fe\u63a5\u9636\u6bb5<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u53ef\u5206\u4e3a\u9759\u6001\u94fe\u63a5\u548c\u52a8\u6001\u94fe\u63a5\uff0c\u9ed8\u8ba4\u662f\u52a8\u6001\u94fe\u63a5,&#8221;-static&#8221;\u9009\u9879\u53ef\u6307\u5b9a\u4f7f\u7528\u9759\u6001\u94fe\u63a5\u3002\u8fd9\u4e00\u9636\u6bb5\u5c06\u76ee\u6807\u6587\u4ef6\u53ca\u5176\u4f9d\u8d56\u5e93\u8fdb\u884c\u94fe\u63a5\uff0c\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4e3b\u8981\u5305\u62ec\u5730\u5740\u548c\u7a7a\u95f4\u5206\u914d(Address and Storage Allocation)\u3001\u7b26\u53f7\u7ed1\u5b9a(Symbol Binding)\u548c\u91cd\u5b9a\u4f4d(Relocation)\u7b49\u64cd\u4f5c\u3002<br>\u94fe\u63a5\u64cd\u4f5c\u7531\u94fe\u63a5\u5668(<a href=\"http:\/\/Id.so\">Id.so<\/a>)\u5b8c\u6210\uff0c\u7ed3\u679c\u5c31\u5f97\u5230\u4e86hello\u6587\u4ef6\uff0c\u8fd9\u662f\u4e00\u4e2a\u9759\u6001\u94fe\u63a5\u7684\u53ef\u6267\u884c\u6587\u4ef6(Executable File)\uff0c\u5305\u542b\u4e86\u5927\u91cf\u7684\u5e93\u6587\u4ef6\u3002<\/p>\n<h3 class=\"wp-block-heading\" id=\"22elf\u6587\u4ef6\u683c\u5f0f\"><span class=\"ez-toc-section\" id=\"22ELF%E6%96%87%E4%BB%B6%E6%A0%BC%E5%BC%8F\"><\/span>2.2ELF\u6587\u4ef6\u683c\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre class=\"wp-block-code\"><code>&lt;span class=&quot;hljs-constructor&quot;&gt;ELF(Executable &lt;span class=&quot;hljs-params&quot;&gt;and&lt;\/span&gt; Linkable Format)&lt;\/span&gt;,\u5373&ldquo;\u53ef\u6267\u884c\u53ef\u94fe\u63a5\u683c\u5f0f&rdquo;\uff0c\u662f&lt;span class=&quot;hljs-constructor&quot;&gt;COFF(Common &lt;span class=&quot;hljs-params&quot;&gt;file&lt;\/span&gt; &lt;span class=&quot;hljs-params&quot;&gt;format&lt;\/span&gt;)&lt;\/span&gt;\u683c\u5f0f\u7684\u53d8\u79cd\u3002Linux\u76f8\u5173\u5b9a\u4e49\u5728&lt;span class=&quot;hljs-string&quot;&gt;&quot;\/usr\/include\/elf.h&quot;&lt;\/span&gt;\u6587\u4ef6\u91cc\u3002\n<\/code><\/pre>\n<h4 class=\"wp-block-heading\" id=\"221-elf\u6587\u4ef6\u7684\u7c7b\u578b\"><span class=\"ez-toc-section\" id=\"221_ELF%E6%96%87%E4%BB%B6%E7%9A%84%E7%B1%BB%E5%9E%8B\"><\/span>2.2.1 ELF\u6587\u4ef6\u7684\u7c7b\u578b<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>ELF\u6587\u4ef6\u4e3b\u8981\u5206\u4e09\u79cd\u7c7b\u578b\uff0c\u53ef\u6267\u884c\u6587\u4ef6(.exec)\u3001\u53ef\u91cd\u5b9a\u4f4d\u6587\u4ef6(.rel)\u548c\u5171\u4eab\u76ee\u6807\u6587\u4ef6(.dyn)\uff0c\u6b64\u5916\uff0c\u8fd8\u6709\u6838\u5fc3\u8f6c\u50a8\u6587\u4ef6(Core Dump file)\u4f5c\u4e3a\u8fdb\u7a0b\u610f\u5916\u7ec8\u6b62\u65f6\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u7684\u8f6c\u50a8\u3002<\/p>\n<h4 class=\"wp-block-heading\" id=\"222-elf\u6587\u4ef6\u7684\u7ed3\u6784\"><span class=\"ez-toc-section\" id=\"222_ELF%E6%96%87%E4%BB%B6%E7%9A%84%E7%BB%93%E6%9E%84\"><\/span>2.2.2 ELF\u6587\u4ef6\u7684\u7ed3\u6784<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>ELF\u6587\u4ef6\u7edf\u79f0\u4e3aObiect file.\u8fd9\u4e0e\u6211\u4eec\u901a\u5e38\u7406\u89e3\u7684&#8221;.o&#8221;\u6587\u4ef6\u4e0d\u540c\u3002\u672c\u7b14\u8bb0\u4e2d\u63d0\u5230\u76ee\u6807\u6587\u4ef6\u65f6\uff0c\u6307\u5404\u79cd\u7c7b\u578b\u7684ELF\u6587\u4ef6\u3002\u5bf9\u4e8e&#8221;.o&#8221;\u6587\u4ef6\uff0c\u5219\u8868\u793a\u4e3a\u53ef\u91cd\u5b9a\u4f4d\u6587\u4ef6\uff0c\u6b64\u7c7b\u6587\u4ef6\u5305\u542b\u4e86\u4ee3\u7801\u548c\u6570\u636e\uff0c\u53ef\u4ee5\u88ab\u7528\u4e8e\u94fe\u63a5\u6210\u53ef\u6267\u884c\u6587\u4ef6\u6216\u8005\u5171\u4eab\u76ee\u6807\u6587\u4ef6\u3002<br>\u4ece\u94fe\u63a5\u89c6\u89d2\u770b\uff0c\u53ef\u4ee5\u5c06\u76ee\u6807\u6587\u4ef6\u7528\u8282(Section)\u6765\u5212\u5206\uff1b\u53e6\u4e00\u79cd\u662f\u8fd0\u884c\u89c6\u89d2\uff0c\u901a\u8fc7\u6bb5(Segment)\u6765\u8fdb\u884c\u5212\u5206\u3002\u672c\u8282\u5148\u5b66\u4e60\u94fe\u63a5\u89c6\u89d2\u3002\u901a\u5e38\u76ee\u6807\u6587\u4ef6\u4f1a\u5305\u542b\u4ee3\u7801(.text)\u3001\u6570\u636e(.data)\u548cBSS(.bss)\u4e09\u4e2a\u8282\u3002\u5176\u4e2d\u4ee3\u7801\u8282\u7528\u4e8e\u4fdd\u5b58\u673a\u5668\u6307\u4ee4\uff0c\u6570\u636e\u8282\u4fdd\u5b58\u5df2\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u9759\u6001\u53d8\u91cf\uff0cBSS\u8282\u4fdd\u5b58\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u3002\u76ee\u6807\u6587\u4ef6\u8fd8\u5305\u542b\u4e00\u4e2a\u6587\u4ef6\u5934\u3002<br>ELF\u6587\u4ef6\u5934\u4f4d\u4e8e\u76ee\u6807\u6587\u4ef6\u6700\u5f00\u59cb\u4f4d\u7f6e\uff0c\u5305\u542bELF\u6587\u4ef6\u7c7b\u578b\u3001\u7248\u672c\/ABI\u7248\u672c\u3001\u76ee\u6807\u673a\u5668\u3001\u7a0b\u5e8f\u5165\u53e3\u3001\u6bb5\u8868\u548c\u8282\u8868\u7684\u4f4d\u7f6e\u548c\u957f\u5ea6\u7b49\u3002<\/p>\n<p>64\u4f4dELF\u6587\u4ef6<br>ELF \u6587\u4ef6\u89e3\u6790 3-\u6bb5 &#8211; \u77e5\u4e4e (<a href=\"http:\/\/zhihu.com\">zhihu.com<\/a>)<br>&lt;&gt;<\/p>\n<h2 class=\"wp-block-heading\" id=\"linux\u5b89\u5168\u673a\u5236\"><span class=\"ez-toc-section\" id=\"Linux%E5%AE%89%E5%85%A8%E6%9C%BA%E5%88%B6\"><\/span>Linux\u5b89\u5168\u673a\u5236<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3 class=\"wp-block-heading\" id=\"linux\u57fa\u7840\"><span class=\"ez-toc-section\" id=\"Linux%E5%9F%BA%E7%A1%80\"><\/span>Linux\u57fa\u7840<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>procfs\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u3002\u901a\u8fc7procfs\u67e5\u770b\u7cfb\u7edf\u786c\u4ef6\u53ca\u6b63\u5728\u8fd0\u884c\u8fdb\u7a0b\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u5176\u4e2d\u67d0\u4e9b\u5185\u5bb9\u6765\u6539\u53d8\u5185\u6838\u7684\u8fd0\u884c\u72b6\u6001\u3002<br>\u6bcf\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\u90fd\u5bf9\u5e94\/proc\u4e0b\u7684\u4e00\u4e2a\u76ee\u5f55\uff0c\u76ee\u5f55\u540d\u5c31\u7b97\u8fdb\u7a0b\u7684PID\u3002<\/p>\n<p>\u6bd4\u8f83\u91cd\u8981\u7684\u6587\u4ef6\uff08cat\u547d\u4ee4\u4e3a\u4f8b\uff09\uff1a<\/p>\n<pre class=\"wp-block-code\"><code>&lt;span class=&quot;hljs-attr&quot;&gt;auxv&lt;\/span&gt;                       &lt;span class=&quot;hljs-string&quot;&gt;\u4f20\u9012\u7ed9\u8fdb\u7a0b\u7684\u89e3\u91ca\u5668\u4fe1\u606f&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;cmdline&lt;\/span&gt;                     &lt;span class=&quot;hljs-string&quot;&gt;\u542f\u52a8\u8fdb\u7a0b\u7684\u547d\u4ee4\u884c&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;cwd&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;-&gt; \/home\/firmy \u5f53\u524d\u5de5\u4f5c\u76ee\u5f55&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;environ&lt;\/span&gt;                      &lt;span class=&quot;hljs-string&quot;&gt;\u8fdb\u7a0b\u7684\u73af\u5883\u53d8\u91cf&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;exe&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;-&gt; \/bin\/cat          \u6700\u521d\u7684\u53ef\u6267\u884c\u6587\u4ef6&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;fd&lt;\/span&gt;                               &lt;span class=&quot;hljs-string&quot;&gt;\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6  &lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;fdinfo&lt;\/span&gt;                         &lt;span class=&quot;hljs-string&quot;&gt;\u6bcf\u4e2a\u6253\u5f00\u6587\u4ef6\u7684\u4fe1\u606f&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;maps&lt;\/span&gt;                          &lt;span class=&quot;hljs-string&quot;&gt;\u5185\u5b58\u6620\u5c04\u4fe1\u606f&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;mem&lt;\/span&gt;                          &lt;span class=&quot;hljs-string&quot;&gt;\u5185\u5b58\u7a7a\u95f4&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;root&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;-&gt; \/                    \u8fdb\u7a0b\u7684\u6839\u76ee\u5f55&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;stack&lt;\/span&gt;                          &lt;span class=&quot;hljs-string&quot;&gt;\u5185\u6838\u8c03\u7528\u6808&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;status&lt;\/span&gt;                         &lt;span class=&quot;hljs-string&quot;&gt;\u8fdb\u7a0b\u7684\u57fa\u672c\u4fe1 &lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;syscall&lt;\/span&gt;                        &lt;span class=&quot;hljs-string&quot;&gt;\u6b63\u5728\u6267\u884c\u7684\u7cfb\u7edf\u8c03\u7528&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;task&lt;\/span&gt;                           &lt;span class=&quot;hljs-string&quot;&gt;\u8fdb\u7a0b\u5305\u542b\u7684\u6240\u6709\u7ebf\u7a0b&lt;\/span&gt;\n&lt;span class=&quot;hljs-meta&quot;&gt;\/proc\/PID\/mem&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;\u7531open\u3001read\u548cseek\u7b49\u7cfb\u7edf\u8c03\u7528\u4f7f\u7528\uff0c\u65e0\u6cd5\u7531\u7528\u6237\u76f4\u63a5\u8bfb\u53d6\uff0c\u4f46\u5176\u5185\u5bb9\u53ef\u4ee5\u901a\u8fc7\/proc\/PID\/maps \u67e5\u770b\uff0c\u8fdb\u7a0b\u7684\u5e03\u5c40\u901a\u8fc7\u5185\u5b58\u6620\u5c04\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u53ef\u6267\u884c\u6587\u4ef6\u3001\u5171\u4eab\u5e93\u3001\u6808\u3001\u5806\u7b49\u3002&lt;\/span&gt;\n&lt;span class=&quot;hljs-meta&quot;&gt;\u770b\u6808(cat&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;\/proc\/xx\/stack)\u9700\u8981\u5728\u7f16\u8bd1\u5185\u6838\u65f6\u542f\u7528CONFIG_STACKTRACE\u9009\u9879&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;auxv&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;(AUXiliary Vector)\u7684\u6bcf\u4e00\u9879\u90fd\u662f\u7531\u4e00\u4e2aunsigned long \u7684ID\u52a0\u4e0a\u4e00\u4e2aunsigned long\u7684\u503c\u6784\u6210\uff0c\u6bcf\u4e2a\u503c\u5177\u4f53\u7684\u7528\u9014\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf LD_SHOW_AUXV=1\u663e\u793a\u51fa\u6765\u3002\u8f85\u52a9\u5411\u91cf\u5b58\u653e\u5728\u6808\u4e0a\uff0c\u9644\u5e26\u4e86\u4f20\u9012\u7ed9\u52a8\u6001\u94fe\u63a5\u5668\u7684\u7a0b\u5e8f\u76f8\u5173\u7684\u7279\u5b9a\u4fe1\u606f\u3002&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;task&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u4fe1\u606f\u5206\u522b\u653e\u5728\u4e00\u4e2a\u7531\u7ebf\u7a0b\u53f7(TID)\u547d\u540d\u7684\u76ee\u5f55\u4e2d.&lt;\/span&gt;\n&lt;span class=&quot;hljs-attr&quot;&gt;syscall&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;\u7b2c\u4e00\u4e2a\u503c\u662f\u7cfb\u7edf\u8c03\u7528\u53f7\uff0c\u540e\u9762\u8ddf\u7740\u516d\u4e2a\u53c2\u6570\uff0c\u6700\u540e\u4e24\u4e2a\u503c\u5206\u522b\u662f\u5806\u6808\u6307\u9488\u548c\u6307\u4ee4\u8ba1\u6570\u5668\u3002&lt;\/span&gt;\n<\/code><\/pre>\n<h4 class=\"wp-block-heading\" id=\"\u8c03\u7528\u7ea6\u5b9a\"><span class=\"ez-toc-section\" id=\"%E8%B0%83%E7%94%A8%E7%BA%A6%E5%AE%9A\"><\/span>\u8c03\u7528\u7ea6\u5b9a<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>(1)\u5185\u6838\u63a5\u53e3<br>x86-32\u7cfb\u7edf\u8c03\u7528\u7ea6\u5b9a\uff1aLinux\u7cfb\u7edf\u8c03\u7528\u4f7f\u7528\u4f7f\u7528\u5bc4\u5b58\u5668\u4f20\u9012\u53c2\u6570\u3002eax\u4e3asyscall_nnumber,ebx\u3001ecx\u3001edx\u3001esi\u548cebp\u7528\u4e8e\u5c066\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9\u7cfb\u7edf\u8c03\u7528\uff0c\u8fd4\u56de\u503c\u4fdd\u5b58\u5728eax\u4e2d\u3002\u5176\u4ed6\u5bc4\u5b58\u5668\u90fd\u4fdd\u7559\u5728int 0x80\u4e2d\u3002<br>x86-64\u7cfb\u7edf\u8c03\u7528\u7ea6\u5b9a\uff1a\u5185\u6838\u63a5\u53e3\u4f7f\u7528\u7684\u5bc4\u5b58\u5668\u6709rdi\u3001rsi\u3001rdx\u3001r10\u3001r8\u548cr9\u3002\u901a\u8fc7\u6307\u4ee4syscall\u5b8c\u6210\u3002\u7cfb\u7edf\u8c03\u7528\u7684\u7f16\u53f7\u5fc5\u987b\u5728\u5bc4\u5b58\u5668rax\u4e2d\u4f20\u9012\u3002\u7cfb\u7edf\u8c03\u7528\u7684\u53c2\u6570\u9650\u5236\u4e3a6\u4e2a\uff0c\u4e0d\u76f4\u63a5\u4ece\u5806\u6808\u4e0a\u4f20\u9012\u4efb\u4f55\u53c2\u6570\u3002\u8fd4\u56de\u65f6\uff0crax\u5305\u542b\u4e86\u7ed3\u679c\uff0c\u4e14\u53ea\u6709integer\u6216\u8005memory\u7c7b\u578b\u7684\u503c\u624d\u4f1a\u88ab\u4f20\u9012\u7ed9\u5185\u6838\u3002<br>(2)\u7528\u6237\u63a5\u53e3<br>x86-32\u51fd\u6570\u8c03\u7528\u7ea6\u5b9a\uff1a\u53c2\u6570\u901a\u8fc7\u6808\u8fdb\u884c\u4f20\u9012\u3002\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u7b2c\u4e00\u4e2a\u88ab\u653e\u5165\u6808\u4e2d\uff0c\u76f4\u5230\u6240\u6709\u7684\u53c2\u6570\u90fd\u653e\u7f6e\u5b8c\u6bd5\uff0c\u7136\u540e\u6267\u884ccall\u6307\u4ee4\u3002\u8fd9\u4e5f\u662fLinux\u4e0aC\u8bed\u8a00\u9ed8\u8ba4\u7684\u65b9\u5f0f\u3002<br>x86-64\u51fd\u6570\u8c03\u7528\u7ea6\u5b9a\uff1ax86-64\u4e0b\u901a\u8fc7\u5bc4\u5b58\u5668\u4f20\u9012\u53c2\u6570\uff0c\u8fd9\u6837\u505a\u6bd4\u901a\u8fc7\u6808\u5177\u6709\u66f4\u9ad8\u7684\u6548\u7387\u3002\u5b83\u907f\u514d\u4e86\u5185\u5b58\u4e2d\u53c2\u6570\u7684\u5b58\u53d6\u548c\u989d\u5916\u7684\u6307\u4ee4\u3002\u6839\u636e\u53c2\u6570\u7c7b\u578b\u7684\u4e0d\u540c\uff0c\u4f1a\u4f7f\u7528\u5bc4\u5b58\u5668\u6216\u4f20\u53c2\u65b9\u5f0f\u3002\u5982\u679c\u53c2\u6570\u7684\u7c7b\u578b\u662fMEMORY,\u5219\u5728\u6808\u4e0a\u4f20\u9012\u53c2\u6570\u3002<br>\u5982\u679c\u7c7b\u578b\u662fINTEGER,\u5219\u987a\u5e8f\u4f7f\u7528rdi\u3001rsi\u3001rdx\u3001rcx\u3001r8\u548cr9\u3002\u6240\u4ee5\u5982\u679c\u6709\u591a\u4e8e6\u4e2a\u7684INTEGER\u53c2\u6570\uff0c\u5219\u540e\u9762\u7684\u53c2\u6570\u5728\u6808\u4e0a\u4f20\u9012\u3002<\/p>\n<h4 class=\"wp-block-heading\" id=\"\u6838\u5fc3\u8f6c\u50a8\"><span class=\"ez-toc-section\" id=\"%E6%A0%B8%E5%BF%83%E8%BD%AC%E5%82%A8\"><\/span>\u6838\u5fc3\u8f6c\u50a8<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5f53\u7a0b\u5e8f\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u5f02\u5e38\u7ec8\u6b62\u6216\u5d29\u6e83\uff0c\u7cfb\u7edf\u5c31\u4f1a\u5c06\u7a0b\u5e8f\u5d29\u6e83\u65f6\u7684\u5185\u5b58\u3001\u5bc4\u5b58\u5668\u72b6\u6001\u3001\u5806\u6808\u6307\u9488\u3001\u5185\u5b58\u7ba1\u7406\u4fe1\u606f\u7b49\u8bb0\u5f55\u4e0b\u6765\uff0c\u4fdd\u5b58\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u53eb\u4f5c\u6838\u5fc3\u8f6c\u50a8(Core Dump)<\/p>\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4fe1\u53f7<\/th>\n<th>\u52a8\u4f5c<\/th>\n<th>\u89e3\u91ca<\/th><\/tr><\/thead>\n<tbody><tr><td>SIGQUIT<\/td>\n<td>Core<\/td>\n<td>\u901a\u8fc7\u952e\u76d8\u9000\u51fa\u65f6<\/td><\/tr>\n<tr><td>SIGILL<\/td>\n<td>Core<\/td>\n<td>\u9047\u5230\u4e0d\u5408\u6cd5\u7684\u6307\u4ee4\u65f6<\/td><\/tr>\n<tr><td>SIGABRT<\/td>\n<td>Core<\/td>\n<td>\u4eceabort\u4e2d\u4ea7\u751f\u7684\u4fe1\u53f7<\/td><\/tr>\n<tr><td>SIGSEGV<\/td>\n<td>Core<\/td>\n<td>\u65e0\u6548\u7684\u5185\u5b58\u8bbf\u95ee<\/td><\/tr>\n<tr><td>SIGTRAP<\/td>\n<td>Core<\/td>\n<td>trace\/breakpoint\u9677\u9631<\/td><\/tr><\/tbody><\/table><\/figure>\n<h4 class=\"wp-block-heading\" id=\"\u7cfb\u7edf\u8c03\u7528\"><span class=\"ez-toc-section\" id=\"%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8\"><\/span>\u7cfb\u7edf\u8c03\u7528<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5728Linux\u4e2d\uff0c\u7cfb\u7edf\u8c03\u7528\u662f\u4e00\u4e9b\u5185\u6838\u7a7a\u95f4\u51fd\u6570\uff0c\u662f\u7528\u6237\u7a7a\u95f4\u8bbf\u95ee\u5185\u6838\u7684\u552f\u4e00\u624b\u6bb5\u3002\u8fd9\u4e9b\u51fd\u6570\u4e0eCPU\u67b6\u6784\u6709\u5173\uff0c86\u63d0\u4f9b\u4e86358\u4e2a\u7cfb\u7edf\u8c03\u7528\uff0c86-64\u63d0\u4f9b\u4e86322\u4e2a\u7cfb\u7edf\u8c03\u7528\u300232\u4f4d\u548c64\u4f4d\u6709\u533a\u522b\u3002<br>\u5148\u770b\u770b32\u4f4d\u7684\uff1a<\/p>\n<pre class=\"wp-block-code\"><code>.data\nmsg:\n    .ascii &lt;span class=&quot;hljs-string&quot;&gt;&quot;hello 32-bit!\\n&quot;&lt;\/span&gt;\n    len = . - msg\n\n.&lt;span class=&quot;hljs-keyword&quot;&gt;text&lt;\/span&gt;\n    .global_start\n\n_start:\n    movl $len,%edx\n    movl Smsg,%ecx\n    movl $1,%ebx\n    movl $4,%eax\n    &lt;span class=&quot;hljs-keyword&quot;&gt;int&lt;\/span&gt; $0x80\n\n    movl $0,%ebx\n    mov1 $1,%eax\n    &lt;span class=&quot;hljs-keyword&quot;&gt;int&lt;\/span&gt; $0x80\n<\/code><\/pre>\n<p>\u7a0b\u5e8f\u5c06\u8c03\u7528\u53f7\u4fdd\u5b58\u5728eax\u4e2d\uff0c\u53c2\u6570\u4f20\u9012\u7684\u987a\u5e8f\u4f9d\u6b21\u662febx\uff0cecx\uff0cedx\uff0c\uff0cesi\u548cedi\u3002\u901a\u8fc7int $0x80\u6765\u6267\u884c\u7cfb\u7edf\u8c03\u7528\uff0c\u8fd4\u56de\u503c\u5b58\u653e\u5728eax\u3002<br>\uff08\u53ef\u4ee5\u88ab\u7f16\u8bd1\u621064\u4f4d\u7a0b\u5e8f\uff09<br>\u8f6f\u4e2d\u65ad int 0x80 \u65e9\u671f2.6\u53ca\u66f4\u65e9\u7248\u672c\u7684\u5185\u6838\u90fd\u4f7f\u7528\u8fd9\u79cd\u673a\u5236\u8fdb\u884c\u7cfb\u7edf\u8c03\u7528\uff0c\u4f46\u56e0\u5176\u6027\u80fd\u8f83\u5dee\uff0c\u5728\u5f80\u540e\u7684\u5185\u6838\u4e2d\u88ab\u5feb\u901f\u7cfb\u7edf\u8c03\u7528\u66ff\u4ee3\uff0c\u598232\u4f4d\u7cfb\u7edf\u4f7f\u7528sysenter\u6307\u4ee4\uff0c64\u4f4dsyscall\u6307\u4ee4\u3002<\/p>\n<h3 class=\"wp-block-heading\" id=\"\u6808\u4fdd\u62a4\u673a\u5236-stack-canaries\"><span class=\"ez-toc-section\" id=\"%E6%A0%88%E4%BF%9D%E6%8A%A4%E6%9C%BA%E5%88%B6_stack_canaries\"><\/span>\u6808\u4fdd\u62a4\u673a\u5236 stack canaries<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>canary\u7684\u503c\u662f\u6808\u4e0a\u7684\u4e00\u4e2a\u968f\u673a\u6570\uff0c\u7a0b\u5e8f\u542f\u52a8\u65f6\u968f\u673a\u751f\u6210\u5e76\u4fdd\u5b58\u5728\u6bd4\u51fd\u6570\u8fd4\u56de\u5730\u5740\u66f4\u4f4e\u7684\u4f4d\u7f6e\u3002\u7531\u4e8e\u6808\u6ea2\u51fa\u662f\u4ece\u9ad8\u5730\u5740\u8fdb\u884c\u8986\u76d6\uff0c\u56e0\u6b64\u653b\u51fb\u8005\u60f3\u8981\u63a7\u5236\u51fd\u6570\u7684\u8fd4\u56de\u6307\u9488\uff0c\u5c31\u4e00\u5b9a\u8981\u5148\u8986\u76d6canary\uff0c\u7a0b\u5e8f\u4e4b\u9700\u8981\u5728\u51fd\u6570\u8fd4\u56de\u524d\u68c0\u67e5canary\u662f\u5426\u88ab\u7be1\u6539\uff0c\u5c31\u53ef\u4ee5\u8fbe\u5230\u4fdd\u62a4\u6808\u7684\u76ee\u7684\u3002<\/p>\n<h4 class=\"wp-block-heading\" id=\"\u7b80\u4ecb\"><span class=\"ez-toc-section\" id=\"%E7%AE%80%E4%BB%8B\"><\/span>\u7b80\u4ecb<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u901a\u5e38\u53ef\u5206\u4e3a3\u7c7b\uff1aterminator\u3001random\u548crandom XOR\uff0c\u5177\u4f53\u5b9e\u73b0\u6709StackGuard\u3001StackShield\u3001ProPoliced\u7b49\u3002<br>\u5173\u4e8eStackGuard\u7684\u8bba\u6587\uff1a<a href=\"https:\/\/www.usenix.org\/legacy\/publications\/library\/proceedings\/sec98\/full_papers\/cowan\/cowan.pdf\">StackGuard:Automatic Adaptive Detection and Prevention of Buffer-Overflow Attacks.<\/a><\/p>\n<p>Terminator canaries:\u7531\u4e8e\u8bb8\u591a\u6808\u6ea2\u51fa\u662f\u5b57\u7b26\u4e32\u64cd\u4f5c\u4e0d\u5f53\u6240\u4ea7\u751f\u7684\uff0c\u800c\u8fd9\u4e9b\u5b57\u7b26\u4e32\u4ee5&#8221;\\x00&#8243;\u622a\u65ad\uff0c\u57fa\u4e8e\u8fd9\u4e00\u70b9\uff0cterminator canaries \u5c06\u4f4e\u4f4d\u8bbe\u7f6e\u4e3a&#8221;\\x00&#8243;,\u65e2\u53ef\u4ee5\u9632\u6b62\u88ab\u6cc4\u9732\uff0c\u4e5f\u53ef\u4ee5\u9632\u6b62\u88ab\u4f2a\u9020\u3002\u622a\u65ad\u5b57\u7b26\u8fd8\u5305\u62ecCR(0x0d)\u3001LF(0x0a)\u548cEOF(0xff)\u3002<\/p>\n<p>Random canaries:\u4e3a\u9632\u6b62canaries\u88ab\u653b\u51fb\u8005\u731c\u5230\uff0crandom canaries\u901a\u5e38\u5728\u7a0b\u5e8f\u521d\u59cb\u5316\u65f6\u968f\u673a\u751f\u6210\uff0c\u5e76\u4fdd\u5b58\u5728\u4e00\u4e2a\u76f8\u5bf9\u5b89\u5168\u7684\u5730\u65b9\u3002\u968f\u673a\u6570\u901a\u5e38\u7531\/dev\/urandom\u751f\u6210\uff0c\u6709\u65f6\u4e5f\u4f7f\u7528\u5f53\u524d\u65f6\u95f4\u7684\u54c8\u5e0c\u3002<\/p>\n<p>Random XOR canaries: \u4e0erandom canaries \u7c7b\u4f3c\uff0c\u4f46\u591a\u4e00\u4e2aXOR\u64cd\u4f5c\uff0c\u8fd9\u6837\u65e0\u8bbacanaries\u88ab\u7be1\u6539\u8fd8\u662f\u4e0e\u4e4bXOR\u7684\u63a7\u5236\u6570\u636e\u88ab\u7be1\u6539\uff0c\u90fd\u4f1a\u53d1\u751f\u9519\u8bef\uff0c\u8fd9\u5c31\u589e\u52a0\u4e86\u653b\u51fb\u96be\u5ea6\u3002<\/p>\n<p>GCC\u5305\u542b\u591a\u4e2acanaries\u6709\u5173\u53c2\u6570\uff0c\u53ef\u4ee5\u7528\u5e2e\u52a9\u547d\u4ee4\u67e5\u770b\u3002<br><code>-fstack-protector \u5bf9alloca\u7cfb\u5217\u51fd\u6570\u548c\u5185\u90e8\u7f13\u51b2\u533a\u5927\u4e8e8\u5b57\u8282\u7684\u51fd\u6570\u542f\u7528\u4fdd\u62a4<\/code><br><code>-fno-stack-protector \u7981\u7528\u4fdd\u62a4<\/code><br>\u53ef\u4ee5\u81ea\u5df1\u6572\u4e2a\u5b9e\u4f8b\u8bd5\u8bd5\uff0c\u770b\u770b\u53cd\u6c47\u7f16\u4ee3\u7801\u5206\u6790\u5206\u6790\u3002<\/p>\n<pre class=\"wp-block-code\"><code>gef&gt; disassemble main\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005b6&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;push&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;rbp&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005b7&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;1&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;mov&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;rbp&lt;\/span&gt;,&lt;span class=&quot;hljs-built_in&quot;&gt;rsp&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005ba&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;4&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;sub&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;rsp&lt;\/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;0x20&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005be&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;8&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;mov&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;rax&lt;\/span&gt;,&lt;span class=&quot;hljs-built_in&quot;&gt;QWORD&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;PTR&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;fs&lt;\/span&gt;:&lt;span class=&quot;hljs-number&quot;&gt;0x28&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005c7&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;17&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;mov&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;QWORD&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;PTR&lt;\/span&gt; [&lt;span class=&quot;hljs-built_in&quot;&gt;rbp&lt;\/span&gt;-&lt;span class=&quot;hljs-number&quot;&gt;0x8&lt;\/span&gt;],&lt;span class=&quot;hljs-built_in&quot;&gt;rax&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005cb&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;21&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;xor&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;eax&lt;\/span&gt;,&lt;span class=&quot;hljs-built_in&quot;&gt;eax&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005cd&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;23&lt;\/span&gt;&gt;: 1ea &lt;span class=&quot;hljs-built_in&quot;&gt;rax&lt;\/span&gt;,[&lt;span class=&quot;hljs-built_in&quot;&gt;rbp&lt;\/span&gt;-&lt;span class=&quot;hljs-number&quot;&gt;0x20&lt;\/span&gt;]\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005d1&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;27&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;mov&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;rsi&lt;\/span&gt;,&lt;span class=&quot;hljs-built_in&quot;&gt;rax&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005d4&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;30&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;mov&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;edi&lt;\/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;0x400684&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005d9&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;35&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;mov&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;eax&lt;\/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;0x0&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005de&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;40&lt;\/span&gt;&gt;: ca11 &lt;span class=&quot;hljs-number&quot;&gt;0x4004a0&lt;\/span&gt; &lt;_isoc99_scanf@plt&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005e3&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;45&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;nop&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005e4&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;46&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;mov&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;rax&lt;\/span&gt;,&lt;span class=&quot;hljs-built_in&quot;&gt;QWORD&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;PTR&lt;\/span&gt; [&lt;span class=&quot;hljs-built_in&quot;&gt;rbp&lt;\/span&gt;-&lt;span class=&quot;hljs-number&quot;&gt;0x8&lt;\/span&gt;]\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005e8&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;50&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;xor&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;rax&lt;\/span&gt;,&lt;span class=&quot;hljs-built_in&quot;&gt;QWORD&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;PTR&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;fs&lt;\/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;0x28&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005f1&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;59&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;je&lt;\/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;0x4005f8&lt;\/span&gt; &lt;main+&lt;span class=&quot;hljs-number&quot;&gt;66&lt;\/span&gt;&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005f3&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;61&lt;\/span&gt;&gt;: ca11 &lt;span class=&quot;hljs-number&quot;&gt;0x400480&lt;\/span&gt; &lt;stack_chk_fail@plt&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005f8&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;66&lt;\/span&gt;&gt;: 1eave\n&lt;span class=&quot;hljs-number&quot;&gt;0x00000000004005f9&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;67&lt;\/span&gt;&gt;: &lt;span class=&quot;hljs-keyword&quot;&gt;ret&lt;\/span&gt;\n<\/code><\/pre>\n<p>\u5728Linux\u4e2d\uff0cfs\u5bc4\u5b58\u5668\u88ab\u7528\u4e8e\u5b58\u653e\u7ebf\u7a0b\u5c40\u90e8\u5b58\u50a8(Thread Local Storage, TLS)\uff0cTLS\u4e3b\u8981\u662f\u4e3a\u4e86\u907f\u514d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u5b58\u540c\u4e00\u5168\u5c40\u53d8\u91cf\u6216\u9759\u6001\u53d8\u91cf\u65f6\u6240\u5bfc\u81f4\u7684\u51b2\u7a81\uff0c\u5c24\u5176\u662f\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u9700\u8981\u4fee\u6539\u8fd9\u4e00\u53d8\u91cf\u65f6\u3002TLS\u4e3a\u6bcf\u4e00\u4e2a\u4f7f\u7528\u8be5\u5168\u5c40\u53d8\u91cf\u7684\u7ebf\u7a0b\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53d8\u91cf\u503c\u7684\u526f\u672c\uff0c\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u5747\u53ef\u4ee5\u72ec\u7acb\u5730\u6539\u53d8\u81ea\u5df1\u7684\u526f\u672c\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u7ebf\u7a0b\u7684\u526f\u672c\u51b2\u7a81\u3002\u4ece\u7ebf\u7a0b\u7684\u89d2\u5ea6\u770b\uff0c\u5c31\u597d\u50cf\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u90fd\u5b8c\u5168\u62e5\u6709\u8be5\u53d8\u91cf\u3002\u4ece\u5168\u5c40\u53d8\u91cf\u7684\u89d2\u5ea6\u770b\uff0c\u5c31\u50cf\u88ab\u590d\u5236\u4e86\u5f88\u591a\u526f\u672c\uff0c\u6bcf\u4e00\u4e2a\u526f\u672c\u90fd\u88ab\u4e00\u4e2a\u7ebf\u7a0b\u72ec\u7acb\u5360\u6709\u3002\u5728glibc\u7684\u5b9e\u73b0\u4e2d\uff0cTLS\u7ed3\u6784\u4f53tcbhead_t\u7684\u5b9a\u4e49\u5982\u4e0b\u6240\u793a\uff0c\u504f\u79fb0x28\u6b63\u662fstack_guard\u3002<\/p>\n<pre class=\"wp-block-code\"><code>&lt;span class=&quot;hljs-keyword&quot;&gt;typedef&lt;\/span&gt; &lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;struct&lt;\/span&gt;{&lt;\/span&gt;\n&lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;\/span&gt; *tcb; &lt;span class=&quot;hljs-comment&quot;&gt;\/* Pointer to the TCB.Not necessarily the  \n           thread descriptor used by libpthread.*\/&lt;\/span&gt;\n&lt;span class=&quot;hljs-keyword&quot;&gt;dtv_t&lt;\/span&gt; *dtv;\n&lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;\/span&gt; *self; &lt;span class=&quot;hljs-comment&quot;&gt;\/*Pointer to the thread descriptor.*\/&lt;\/span&gt;\n&lt;span class=&quot;hljs-keyword&quot;&gt;int&lt;\/span&gt; multiple_threads;\n&lt;span class=&quot;hljs-keyword&quot;&gt;int&lt;\/span&gt; gscope_flag;\n&lt;span class=&quot;hljs-keyword&quot;&gt;uintptr_t&lt;\/span&gt; sysinfo;\n&lt;span class=&quot;hljs-keyword&quot;&gt;uintptr_t&lt;\/span&gt; stack_guard;\n&lt;span class=&quot;hljs-keyword&quot;&gt;uintptr_t&lt;\/span&gt; pointer_guard;\n......\n} &lt;span class=&quot;hljs-keyword&quot;&gt;tcbhead_t&lt;\/span&gt;;\n<\/code><\/pre>\n<p>\u4eceTLS\u53d6\u51facanary\u540e\uff0c\u7a0b\u5e8f\u5c31\u5c06\u5176\u63d2\u5165rbp-0x8\u7684\u4f4d\u7f6e\u6682\u65f6\u4fdd\u5b58\u3002\u5728\u51fd\u6570\u8fd4\u56de\u524d\uff0c\u53c8\u4ece\u6808\u4e0a\u5c06\u5176\u53d6\u51fa\uff0c\u5e76\u4e8eTLS\u4e2d\u7684canary\u8fdb\u884c\u5f02\u6216\u6bd4\u8f83\uff0c\u4ece\u800c\u786e\u5b9a\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u4e0d\u76f8\u7b49\u5c31\u8bf4\u660e\u53d1\u751f\u4e86\u6808\u6ea2\u51fa\uff0c\u7136\u540e\u8f6c\u5230__stack_chk_fail()\u51fd\u6570\u4e2d\uff0c\u7a0b\u5e8f\u7ec8\u6b62\u5e76\u629b\u51fa\u9519\u8bef;\u5426\u5219\u7a0b\u5e8f\u6b63\u5e38\u9000\u51fa\u3002<br>\u5982\u679c\u662f32\u4f4d\u7a0b\u5e8f\uff0c\u90a3\u4e48canary\u5c31\u53d8\u6210\u4e86gs\u5bc4\u5b58\u5668\u504f\u79fb0x14\u7684\u5730\u65b9\u3002<\/p>\n<pre class=\"wp-block-code\"><code>&lt;span class=&quot;hljs-keyword&quot;&gt;typedef&lt;\/span&gt; struct{\n&lt;span class=&quot;hljs-built_in&quot;&gt;void&lt;\/span&gt; *tcb; &lt;span class=&quot;hljs-comment&quot;&gt;\/*Pointer to the TCB.Not necessarily the\n            thread descriptor used by libpthread.*\/&lt;\/span&gt;\ndtv_t *dtv;\n&lt;span class=&quot;hljs-built_in&quot;&gt;void&lt;\/span&gt; *self; &lt;span class=&quot;hljs-comment&quot;&gt;\/*Pointer to the thread descriptor.*\/&lt;\/span&gt;\n&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;\/span&gt; multiple_threads;\n&lt;span class=&quot;hljs-built_in&quot;&gt;uint&lt;\/span&gt;ptr_t sysinfo;\n&lt;span class=&quot;hljs-built_in&quot;&gt;uint&lt;\/span&gt;ptr_t stack_guard;\n&lt;span class=&quot;hljs-built_in&quot;&gt;uint&lt;\/span&gt;ptr_t pointer_guard;\n......\n} tcbhead_t;\ngef &gt; disassemble main\n......\n&lt;span class=&quot;hljs-number&quot;&gt;0x0804849c&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;17&lt;\/span&gt;&gt;:mov eax,gs:&lt;span class=&quot;hljs-number&quot;&gt;0x14&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x080484a2&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;23&lt;\/span&gt;&gt;:mov DWORD PTR [ebp&lt;span class=&quot;hljs-number&quot;&gt;-0xc&lt;\/span&gt;],eax\n......\n&lt;span class=&quot;hljs-number&quot;&gt;0x080484bc&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;49&lt;\/span&gt;&gt;:mov eax,DWORD PTR [ebp&lt;span class=&quot;hljs-number&quot;&gt;-0xc&lt;\/span&gt;]\n&lt;span class=&quot;hljs-number&quot;&gt;0x080484bf&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;52&lt;\/span&gt;&gt;:&lt;span class=&quot;hljs-keyword&quot;&gt;xor&lt;\/span&gt; eax,DWORD PTR gs:&lt;span class=&quot;hljs-number&quot;&gt;0x14&lt;\/span&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x080484c6&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;59&lt;\/span&gt;&gt;:je &lt;span class=&quot;hljs-number&quot;&gt;0x80484cd&lt;\/span&gt;&lt;main+&lt;span class=&quot;hljs-number&quot;&gt;66&lt;\/span&gt;&gt;\n&lt;span class=&quot;hljs-number&quot;&gt;0x080484c8&lt;\/span&gt;&lt;+&lt;span class=&quot;hljs-number&quot;&gt;61&lt;\/span&gt;&gt;:ca11 &lt;span class=&quot;hljs-number&quot;&gt;0x8048350&lt;\/span&gt; &lt;&lt;span class=&quot;hljs-symbol&quot;&gt;__stack_chk_fail@&lt;\/span&gt;plt&gt;\n......\n<\/code><\/pre>\n<p>checksec.sh\u5bf9Canary\u7684\u68c0\u6d4b\u4e5f\u662f\u6839\u636e\u662f\u5426\u5b58\u5728<code>__stack_chk_fail<\/code>\u6216<code>__intel_security_cookie<\/code>\u6765\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<p>\u81f3\u6b64\uff0c\u6211\u53d1\u73b0\u6211\u7684\u7b14\u8bb0\u51e0\u4e4e\u90fd\u662f\u4ece\u4e66\u4e0a\u6458\u6284\u4e0b\u6765\uff0c\u53ea\u6709\u5f88\u5c11\u81ea\u5df1\u7684\u7406\u89e3\u3002\u6211\u4e0d\u613f\u518d\u6284\u4e66\uff0c\u540e\u9762\u53ea\u6982\u62ec\u4e3b\u8981\u5185\u5bb9\uff0c\u52a0\u4e0a\u81ea\u5df1\u7684\u7406\u89e3\u3002<\/p>\n<h4 class=\"wp-block-heading\" id=\"\u5b9e\u73b0\"><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E7%8E%B0\"><\/span>\u5b9e\u73b0<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u4ee564\u4f4d\u7a0b\u5e8f\u4e3a\u4f8b\uff0c\u7a0b\u5e8f\u52a0\u8f7d\u65f6\uff0cglibc\u4e2d\u7684ld.so\u9996\u5148\u521d\u59cb\u5316TLS\uff0c\u4e3a\u5176\u5206\u914d\u7a7a\u95f4\uff0c\u901a\u8fc7arch_prctl\u7cfb\u7edf\u8c03\u7528\u8bbe\u7f6efs\u5bc4\u5b58\u5668\u6307\u5411TLS\u3002<br>\u7136\u540e\u7a0b\u5e8f\u8c03\u7528security_init()\u51fd\u6570\uff0c\u751f\u6210Canary\u7684\u503cstack_chk_guard,\u5e76\u653e\u5165fs:0x28\u3002<\/p>\n<pre class=\"wp-block-code\"><code>static void security_init (void)\n{\n    &lt;span class=&quot;hljs-comment&quot;&gt;\/* Set up the stack checker&#039;s canary.  *\/&lt;\/span&gt;\n    uintptr_t stack_chk_guard = &lt;span class=&quot;hljs-variable&quot;&gt;_dl_setup_stack_chk_guard&lt;\/span&gt; (&lt;span class=&quot;hljs-variable&quot;&gt;_dl_random&lt;\/span&gt;);\n&lt;span class=&quot;hljs-meta&quot;&gt;#&lt;span class=&quot;hljs-meta-keyword&quot;&gt;ifdef&lt;\/span&gt; THREAD_SET_STACK_GUARD&lt;\/span&gt;\n    THREAD_SET_STACK_GUARD (stack_chk_guard);\n&lt;span class=&quot;hljs-meta&quot;&gt;#&lt;span class=&quot;hljs-meta-keyword&quot;&gt;else&lt;\/span&gt;&lt;\/span&gt;\n    &lt;span class=&quot;hljs-variable&quot;&gt;__stack_chk_guard&lt;\/span&gt; = stack_chk_guard;\n&lt;span class=&quot;hljs-meta&quot;&gt;#&lt;span class=&quot;hljs-meta-keyword&quot;&gt;endif&lt;\/span&gt;&lt;\/span&gt;\n\n    &lt;span class=&quot;hljs-comment&quot;&gt;\/* Set up the pointer guard as well, if necessary.  *\/&lt;\/span&gt;\n    uintptr_t pointer_chk_guard\n    = &lt;span class=&quot;hljs-variable&quot;&gt;_dl_setup_pointer_guard&lt;\/span&gt; (&lt;span class=&quot;hljs-variable&quot;&gt;_dl_random&lt;\/span&gt;, stack_chk_guard);\n&lt;span class=&quot;hljs-meta&quot;&gt;#&lt;span class=&quot;hljs-meta-keyword&quot;&gt;ifdef&lt;\/span&gt; THREAD_SET_POINTER_GUARD&lt;\/span&gt;\n    THREAD_SET_POINTER_GUARD (pointer_chk_guard);\n&lt;span class=&quot;hljs-meta&quot;&gt;#&lt;span class=&quot;hljs-meta-keyword&quot;&gt;endif&lt;\/span&gt;&lt;\/span&gt;\n    &lt;span class=&quot;hljs-variable&quot;&gt;__pointer_chk_guard_local&lt;\/span&gt; = pointer_chk_guard;\n\n    &lt;span class=&quot;hljs-comment&quot;&gt;\/* We do not need the _dl_random value anymore.  The less\n       information we leave behind, the better, so clear the\n       variable.  *\/&lt;\/span&gt;\n    &lt;span class=&quot;hljs-variable&quot;&gt;_dl_random&lt;\/span&gt; = NULL;\n}\n<\/code><\/pre>\n<p>glibc\/elf\/rtld.c<br>security_init()\u51fd\u6570\u751f\u6210canary\u503c\u3002<\/p>\n<pre class=\"wp-block-code\"><code>STATIC int LIBC_START_MAIN {\n......\n    &lt;span class=&quot;hljs-comment&quot;&gt;\/* Set up the stack checker&#039;s canary.  *\/&lt;\/span&gt;\n    uintptr_t stack_chk_guard = &lt;span class=&quot;hljs-variable&quot;&gt;_dl_setup_stack_chk_guard&lt;\/span&gt; (&lt;span class=&quot;hljs-variable&quot;&gt;_dl_random&lt;\/span&gt;);\n&lt;span class=&quot;hljs-meta&quot;&gt;# &lt;span class=&quot;hljs-meta-keyword&quot;&gt;ifdef&lt;\/span&gt; THREAD_SET_STACK_GUARD&lt;\/span&gt;\n    THREAD_SET_STACK_GUARD (stack_chk_guard);\n&lt;span class=&quot;hljs-meta&quot;&gt;# &lt;span class=&quot;hljs-meta-keyword&quot;&gt;else&lt;\/span&gt;&lt;\/span&gt;\n    &lt;span class=&quot;hljs-variable&quot;&gt;__stack_chk_guard&lt;\/span&gt; = stack_chk_guard;\n&lt;span class=&quot;hljs-meta&quot;&gt;# &lt;span class=&quot;hljs-meta-keyword&quot;&gt;endif&lt;\/span&gt;&lt;\/span&gt;\n<\/code><\/pre>\n<p>glibc\/csu\/libc-start.c<br>__libc_start_main()\u51fd\u6570\u751f\u6210canary\u503c<\/p>\n<pre class=\"wp-block-code\"><code>&lt;span class=&quot;hljs-comment&quot;&gt;\/* Random data provided by the kernel.  *\/&lt;\/span&gt;\n&lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;\/span&gt; *_dl_random;\n<\/code><\/pre>\n<p>glibc\/elf\/ld-support.c<br>_dl_random\u6307\u5411\u4e00\u4e2a\u7531\u5185\u6838\u63d0\u4f9b\u7684\u968f\u673a\u6570\u3002<\/p>\n<p>\u00a0<\/p>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>\u7b2c\u4e00\u7ae0\n\u77e5\u540d\u5b89\u5168\u4f1a\u8bae\nRSA\u3001Black Hat\u3001DEFCON\u3001ISC(\u4e2d\u56fd\u4e92\u8054\u7f51\u5b89\u5168\u5927\u4f1a) \u5b66\u672f\u9876\u4f1a CCS(A): ACM Conference on Computer and Communica<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-630","post","type-post","status-publish","format-standard","hentry","category-studyrecords"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/posts\/630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/comments?post=630"}],"version-history":[{"count":1,"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/posts\/630\/revisions"}],"predecessor-version":[{"id":641,"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/posts\/630\/revisions\/641"}],"wp:attachment":[{"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/media?parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/categories?post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/tags?post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}