{"id":1640,"date":"2019-06-01T09:40:18","date_gmt":"2019-06-01T16:40:18","guid":{"rendered":"http:\/\/www.gironsec.com\/blog\/?p=1640"},"modified":"2019-06-30T21:01:44","modified_gmt":"2019-07-01T04:01:44","slug":"expiring-shellcode-update","status":"publish","type":"post","link":"https:\/\/www.gironsec.com\/blog\/2019\/06\/expiring-shellcode-update\/","title":{"rendered":"Expiring Shellcode update"},"content":{"rendered":"<p>Howdy dudey!<\/p>\n<p>I&#8217;m back with an update. A quickey mind you, but I&#8217;m out here posting real UPDATES for once.<\/p>\n<p>The Metasploit Framework expiration of shellcode commit that was never taken into master had me thinking &#8211; how come I never made POC code for Windows?<br \/>\nWhy the fuck not right?<\/p>\n<p>Well, let&#8217;s fucking do it!<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%;\"><span style=\"color: #00aa00;\">.486<\/span>\n<span style=\"color: #00aa00;\">.model<\/span> <span style=\"color: #aa0000;\">flat<\/span>, <span style=\"color: #aa0000;\">stdcall<\/span>\n<span style=\"color: #00aa00;\">option<\/span> <span style=\"color: #aa0000;\">casemap<\/span> :<span style=\"color: #aa0000;\">none<\/span>\n<span style=\"color: #00aa00;\">include<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\\<\/span><span style=\"color: #aa0000;\">masm32<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">\\<\/span><span style=\"color: #aa0000;\">include<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">\\<\/span><span style=\"color: #aa0000;\">windows.inc<\/span>\n<span style=\"color: #00aa00;\">include<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\\<\/span><span style=\"color: #aa0000;\">masm32<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">\\<\/span><span style=\"color: #aa0000;\">include<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">\\<\/span><span style=\"color: #aa0000;\">kernel32.inc<\/span>\n<span style=\"color: #00aa00;\">includelib<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">\\<\/span><span style=\"color: #aa0000;\">masm32<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">\\<\/span><span style=\"color: #aa0000;\">lib<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">\\<\/span><span style=\"color: #aa0000;\">kernel32.lib<\/span>\n<span style=\"color: #00aa00;\">.code<\/span>\nstart:\n<span style=\"color: #00aa00;\">push<\/span>    <span style=\"color: #00aaaa;\">ebp<\/span>\n<span style=\"color: #00aa00;\">mov<\/span>     <span style=\"color: #00aaaa;\">ebp<\/span>, <span style=\"color: #00aaaa;\">esp<\/span>\n<span style=\"color: #00aa00;\">xor<\/span> <span style=\"color: #00aaaa;\">eax<\/span>,<span style=\"color: #00aaaa;\">eax<\/span>\n<span style=\"color: #00aa00;\">push<\/span> <span style=\"color: #009999;\">40h<\/span>     <span style=\"color: #aaaaaa; font-style: italic;\">; PAGE_EXECUTE_READWRITE<\/span>\n<span style=\"color: #00aa00;\">mov<\/span> <span style=\"color: #00aaaa;\">ax<\/span>,<span style=\"color: #009999;\">4095<\/span><span style=\"color: #aa0000;\">d<\/span> <span style=\"color: #aaaaaa; font-style: italic;\">; FFF in hex, masm bug?<\/span>\n<span style=\"color: #00aa00;\">inc<\/span> <span style=\"color: #00aaaa;\">ax<\/span> <span style=\"color: #aaaaaa; font-style: italic;\">; <\/span>\n<span style=\"color: #00aa00;\">push<\/span> <span style=\"color: #00aaaa;\">eax<\/span> <span style=\"color: #aaaaaa; font-style: italic;\">; MEM_COMMIT 0x00001000<\/span>\n<span style=\"color: #00aa00;\">push<\/span> <span style=\"color: #009999;\">10h<\/span> <span style=\"color: #aaaaaa; font-style: italic;\">; 16 bytes needed<\/span>\n<span style=\"color: #00aa00;\">xor<\/span> <span style=\"color: #00aaaa;\">eax<\/span>,<span style=\"color: #00aaaa;\">eax<\/span>\n<span style=\"color: #00aa00;\">push<\/span> <span style=\"color: #00aaaa;\">eax<\/span> <span style=\"color: #aaaaaa; font-style: italic;\">; NULL as we dont care where the allocation is.<\/span>\n<span style=\"color: #00aa00;\">call<\/span> <span style=\"color: #aa0000;\">VirtualAlloc<\/span><span style=\"color: #aaaaaa; font-style: italic;\">; VirtualAlloc<\/span>\n<span style=\"color: #00aa00;\">mov<\/span> <span style=\"color: #00aaaa;\">eax<\/span>,<span style=\"color: #00aaaa;\">ebx<\/span>      <span style=\"color: #aaaaaa; font-style: italic;\">; VVVVV<\/span>\n<span style=\"color: #00aa00;\">push<\/span>    <span style=\"color: #00aaaa;\">eax<\/span> <span style=\"color: #aaaaaa; font-style: italic;\">; ( NULL,dwLength,MEM_COMMIT,PAGE_EXECUTE_READWRITE);     <\/span>\n<span style=\"color: #00aa00;\">call<\/span> <span style=\"color: #aa0000;\">GetLocalTime<\/span> <span style=\"color: #aaaaaa; font-style: italic;\">; GetLocalTime with chunk from VirtualAlloc<\/span>\n<span style=\"color: #00aa00;\">xor<\/span> <span style=\"color: #00aaaa;\">ecx<\/span>,<span style=\"color: #00aaaa;\">ecx<\/span>\n<span style=\"color: #00aa00;\">mov<\/span>     <span style=\"color: #00aaaa;\">cl<\/span>, <span style=\"color: #009999;\">6h<\/span> <span style=\"color: #aaaaaa; font-style: italic;\">; MONTH converted to hex (june)<\/span>\n<span style=\"color: #00aa00;\">cmp<\/span>     <span style=\"color: #00aaaa;\">cx<\/span>, [<span style=\"color: #00aaaa;\">ebx<\/span>+<span style=\"color: #009999;\">2<\/span>]\n<span style=\"color: #00aa00;\">jnz<\/span>     <span style=\"color: #aa0000;\">short<\/span> <span style=\"color: #aa0000;\">exitpart<\/span>\n<span style=\"color: #00aa00;\">mov<\/span>     <span style=\"color: #00aaaa;\">cx<\/span>, <span style=\"color: #009999;\">7e3h<\/span> <span style=\"color: #aaaaaa; font-style: italic;\">; YEAR converted to hex (2019)<\/span>\n<span style=\"color: #00aa00;\">cmp<\/span>     <span style=\"color: #00aaaa;\">cx<\/span>, [<span style=\"color: #00aaaa;\">ebx<\/span>]\n<span style=\"color: #00aa00;\">jz<\/span>      <span style=\"color: #aa0000;\">short<\/span> <span style=\"color: #aa0000;\">wegood<\/span>\nexitpart:\n<span style=\"color: #aaaaaa; font-style: italic;\">; FAILED date check, exit gracefully<\/span>\n<span style=\"color: #00aa00;\">xor<\/span> <span style=\"color: #00aaaa;\">ebx<\/span>,<span style=\"color: #00aaaa;\">ebx<\/span>\n<span style=\"color: #00aa00;\">push<\/span> <span style=\"color: #00aaaa;\">ebx<\/span>\n<span style=\"color: #00aa00;\">call<\/span> <span style=\"color: #aa0000;\">ExitProcess<\/span> \nwegood:\n<span style=\"color: #aaaaaa; font-style: italic;\">; passed checks, can start shellcode now<\/span>\n<span style=\"color: #00aa00;\">nop<\/span>\n<span style=\"color: #00aa00;\">fnop<\/span>\n<span style=\"color: #aaaaaa; font-style: italic;\">; shellcode start<\/span>\n<span style=\"color: #00aa00;\">end<\/span> <span style=\"color: #aa0000;\">start<\/span>\n<\/pre>\n<\/div>\n<p>How does it work?<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2019\/07\/1536599534431.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-1641\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2019\/07\/1536599534431-950x1024.png\" alt=\"\" width=\"640\" height=\"690\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2019\/07\/1536599534431-950x1024.png 950w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2019\/07\/1536599534431-278x300.png 278w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2019\/07\/1536599534431-768x828.png 768w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2019\/07\/1536599534431.png 1600w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>I have a simple list of what&#8217;s happening:<\/p>\n<ol>\n<li>\nSetup the stack with the required parameters for <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/desktop\/api\/memoryapi\/nf-memoryapi-virtualalloc\" target=\"_blank\" rel=\"noopener noreferrer\">VirtualAlloc<\/a>. Store the memory block&#8217;s address in EBX as it will be lost in EAX after the call to <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/desktop\/api\/sysinfoapi\/nf-sysinfoapi-getlocaltime\" target=\"_blank\" rel=\"noopener noreferrer\">GetLocalTime<\/a>.<\/li>\n<li>\nPush our returned memory block for use with our function which takes a single argument &#8211; a pointer to a <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/desktop\/api\/minwinbase\/ns-minwinbase-systemtime\" target=\"_blank\" rel=\"noopener noreferrer\">SYSTEMTIME<\/a> structure to receive the current local date and time. It&#8217;s 16 bytes in size.<\/li>\n<li>\nStore the value of the selected month in the &#8216;cl&#8217; register. We use 8 bit registers when the value is tiny to avoid null bytes and save precious space.<\/li>\n<li>\nWe then compare this MONTH value with the address of the memory location we passed to GetLocalTime, plus 2 bytes forward to get to the place of the MONTH in &#8216;SYSTEMTIME&#8217;.<\/li>\n<li>\nIf we&#8217;re not in the right month, we exit gracefully, if we&#8217;re in the right month, then we continue by moving the value of the current YEAR into the CX register (16 bits).<\/li>\n<li>\nWe compare again the value of the chosen YEAR with the address of the memory location we passed to GetLocalTime as &#8216;SYSTEMTIME&#8217; starts with the YEAR<\/li>\n<li>\nIf satisfied, we jump to our shellcode start. If not, we exit gracefully.<\/li>\n<\/ol>\n<p>Hope that was easy enough of an explanation. Coded to avoid null bytes, except for calling functions, but you can piece that shit together yourself. Or just use my <a href=\"https:\/\/www.gironsec.com\/blog\/2019\/07\/a-shellcode-idea\/\" target=\"_blank\" rel=\"noopener noreferrer\">earlier blog post<\/a> as a skeleton template wherein we derive the functions from the Process Environment and Thread Environment Block(s) to search for function names.<\/p>\n<p>More to come soon, I swear.<\/p>\n<p>Stay tuned!<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2019\/07\/1403119591037.gif\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1642\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2019\/07\/1403119591037.gif\" alt=\"\" width=\"240\" height=\"240\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Howdy dudey! I&#8217;m back with an update. A quickey mind you, but I&#8217;m out here posting real UPDATES for once. The Metasploit Framework expiration of shellcode commit that was never taken into master had me thinking &#8211; how come I never made POC code for Windows? Why the fuck not right? Well, let&#8217;s fucking do [&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":[119],"_links":{"self":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1640"}],"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=1640"}],"version-history":[{"count":9,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1640\/revisions"}],"predecessor-version":[{"id":1648,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1640\/revisions\/1648"}],"wp:attachment":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/media?parent=1640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/categories?post=1640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/tags?post=1640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}