{"id":963,"date":"2014-11-22T13:51:43","date_gmt":"2014-11-22T13:51:43","guid":{"rendered":"http:\/\/www.gironsec.com\/blog\/?p=963"},"modified":"2014-11-22T13:52:02","modified_gmt":"2014-11-22T13:52:02","slug":"too-much-stuff","status":"publish","type":"post","link":"https:\/\/www.gironsec.com\/blog\/2014\/11\/too-much-stuff\/","title":{"rendered":"assembly, c-sharp, anti-sandbox, anti-antivirus, anti-debug, and malware research"},"content":{"rendered":"<p>Hello fellow readers!<\/p>\n<p>You all are probably wondering what the hell I&#8217;ve been up to this past month. Lot&#8217;s of stuff. This post is all over the place with code and slides and malware and general wackiness. Rather than spreading it out over several blog posts, I decided to just get it all over with so I can focus on cooler things in the future. <\/p>\n<p>I saw an interesting webinar on sandbox detection techniques employed by malware by Cyphort. They haven&#8217;t released their slides like they said they would, so here are the <a href=\"http:\/\/gironsec.com\/img\/sandbox.html\" target=\"_blank\">ones I took<\/a>. These are cool and all, but I felt like I could <a href=\"http:\/\/www.gironsec.com\/blog\/2013\/10\/anti-sandboxing-ideas\/\">contribute.<\/a><\/p>\n<p>I read an awesome paper on bypassing antiviruses by employing a number of code based tricks. The idea behind them was that AV&#8217;s will skip binaries based on certain behaviors. One thing missing though &#8211; an AV will skip the &#8220;dropper&#8221; heuristic if the file ends in &#8216;.msi&#8217;. All the code I saw was in C\/C++. I figured why not try and convert it to assembly? Next thing to do is make a patcher that can inject these into pre-compiled binaries. A future project perhaps? Anyways, I only did 2 before I lost interest. Read the <a href=\"http:\/\/www.gironsec.com\/code\/BypassAVDynamics.pdf\" target=\"_blank\">article here<\/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%\"><span style=\"color: #aaaaaa; font-style: italic\">;AV bypass 1<\/span>\r\n<span style=\"color: #00aa00\">xor<\/span>     <span style=\"color: #00aaaa\">eax<\/span>, <span style=\"color: #00aaaa\">eax<\/span>\r\n<span style=\"color: #0000aa\">db<\/span> <span style=\"color: #aa0000\">Caption<\/span> <span style=\"color: #aa5500\">&quot;Joe&quot;<\/span>\r\n<span style=\"color: #0000aa\">db<\/span> <span style=\"color: #aa0000\">Text<\/span> <span style=\"color: #aa5500\">&quot;Giron&quot;<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">edx<\/span>, <span style=\"color: #009999\">5F5E100h<\/span>\r\njoe:\r\n<span style=\"color: #00aa00\">inc<\/span>     <span style=\"color: #00aaaa\">eax<\/span>\r\n<span style=\"color: #00aa00\">dec<\/span>     <span style=\"color: #00aaaa\">edx<\/span>\r\n<span style=\"color: #00aa00\">jnz<\/span>     <span style=\"color: #aa0000\">joe<\/span>\r\n<span style=\"color: #00aa00\">cmp<\/span>     <span style=\"color: #00aaaa\">eax<\/span>, <span style=\"color: #009999\">5F5E100h<\/span>\r\n<span style=\"color: #00aa00\">jnz<\/span>     <span style=\"color: #aa0000\">short<\/span> <span style=\"color: #aa0000\">urafag<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #009999\">0<\/span>               <span style=\"color: #aaaaaa; font-style: italic\">; MB_OK<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #aa0000\">offset<\/span> <span style=\"color: #aa0000\">Caption<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #aa0000\">offset<\/span> <span style=\"color: #aa0000\">Text<\/span>   \r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #009999\">0<\/span>               <span style=\"color: #aaaaaa; font-style: italic\">; hWnd <\/span>\r\n<span style=\"color: #00aa00\">call<\/span>    <span style=\"color: #aa0000\">MessageBoxA<\/span>\r\nurafag:\r\n<span style=\"color: #00aa00\">xor<\/span>     <span style=\"color: #00aaaa\">eax<\/span>, <span style=\"color: #00aaaa\">eax<\/span>\r\n<span style=\"color: #00aa00\">retn<\/span>\r\n\r\n<span style=\"color: #aaaaaa; font-style: italic\">;AV bypass 1.5<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">; same as above, just using the loop instruction instead of branching conditionals<\/span>\r\n<span style=\"color: #00aa00\">xor<\/span>     <span style=\"color: #00aaaa\">eax<\/span>, <span style=\"color: #00aaaa\">eax<\/span>\r\n<span style=\"color: #0000aa\">db<\/span> <span style=\"color: #aa0000\">Caption<\/span> <span style=\"color: #aa5500\">&quot;Joe&quot;<\/span>\r\n<span style=\"color: #0000aa\">db<\/span> <span style=\"color: #aa0000\">Text<\/span> <span style=\"color: #aa5500\">&quot;Giron&quot;<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">ecx<\/span>, <span style=\"color: #009999\">5F5E100h<\/span>\r\njoe: <span style=\"color: #aaaaaa; font-style: italic\">; essentially do nothing<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span> <span style=\"color: #00aaaa\">eax<\/span>,<span style=\"color: #009999\">10<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span> <span style=\"color: #00aaaa\">ebx<\/span>,<span style=\"color: #009999\">20<\/span>\r\n<span style=\"color: #00aa00\">xchg<\/span> <span style=\"color: #00aaaa\">eax<\/span>,<span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">loop<\/span> <span style=\"color: #aa0000\">joe<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">; now start code<\/span>\r\n<span style=\"color: #00aa00\">xor<\/span> <span style=\"color: #00aaaa\">eax<\/span>,<span style=\"color: #00aaaa\">eax<\/span>\r\n<span style=\"color: #00aa00\">xor<\/span> <span style=\"color: #00aaaa\">ebx<\/span>,<span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #009999\">0<\/span>               <span style=\"color: #aaaaaa; font-style: italic\">; MB_OK<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #aa0000\">offset<\/span> <span style=\"color: #aa0000\">Caption<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #aa0000\">offset<\/span> <span style=\"color: #aa0000\">Text<\/span>   \r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #009999\">0<\/span>               <span style=\"color: #aaaaaa; font-style: italic\">; hWnd <\/span>\r\n<span style=\"color: #00aa00\">call<\/span>    <span style=\"color: #aa0000\">MessageBoxA<\/span>\r\n<span style=\"color: #00aa00\">retn<\/span>\r\n\r\n<span style=\"color: #aaaaaa; font-style: italic\">;AV bypass 2<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #00aaaa\">edi<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #009999\">5F5E100h<\/span>        <span style=\"color: #aaaaaa; font-style: italic\">; bytes to alloc<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #009999\">40h<\/span>             <span style=\"color: #aaaaaa; font-style: italic\">; zero init<\/span>\r\n<span style=\"color: #00aa00\">call<\/span>    <span style=\"color: #aa0000\">GlobalAlloc<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">ebx<\/span>, <span style=\"color: #00aaaa\">eax<\/span>\r\n<span style=\"color: #00aa00\">test<\/span>    <span style=\"color: #00aaaa\">ebx<\/span>, <span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">jz<\/span>      <span style=\"color: #aa0000\">short<\/span> <span style=\"color: #00aaaa\">cl<\/span><span style=\"color: #aa0000\">eanup<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">edi<\/span>, <span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">eax<\/span>, <span style=\"color: #009999\">0FFFFFFF1h<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">ecx<\/span>, <span style=\"color: #009999\">5F5E100h<\/span>\r\n<span style=\"color: #00aa00\">rep<\/span> <span style=\"color: #aa0000\">stosb<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #009999\">0<\/span>               <span style=\"color: #aaaaaa; font-style: italic\">; MB_OK<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #aa0000\">offset<\/span> <span style=\"color: #aa0000\">Caption<\/span>  <span style=\"color: #aaaaaa; font-style: italic\">; &quot;Joe&quot;<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #aa0000\">offset<\/span> <span style=\"color: #aa0000\">Text<\/span>     <span style=\"color: #aaaaaa; font-style: italic\">; &quot;Giron&quot;<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #009999\">0<\/span>               <span style=\"color: #aaaaaa; font-style: italic\">; hWnd<\/span>\r\n<span style=\"color: #00aa00\">call<\/span>    <span style=\"color: #aa0000\">MessageBoxA<\/span>\r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #00aaaa\">ebx<\/span>             <span style=\"color: #aaaaaa; font-style: italic\">; memory handler<\/span>\r\n<span style=\"color: #00aa00\">call<\/span>    <span style=\"color: #aa0000\">GlobalFree<\/span>\r\ncleanup:                             \r\n<span style=\"color: #00aa00\">xor<\/span>     <span style=\"color: #00aaaa\">eax<\/span>, <span style=\"color: #00aaaa\">eax<\/span>\r\n<span style=\"color: #00aa00\">pop<\/span>     <span style=\"color: #00aaaa\">edi<\/span>\r\n<span style=\"color: #00aa00\">pop<\/span>     <span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">retn<\/span>\r\n<\/pre>\n<\/div>\n<p>Feels good to put my crappy assembly skills to good use. Especially now that I figured out how to use inline assembly within C#. Sort of. The way it works is by utilizing delegates and cramming code inside an executable code page. Observe this piece of genius:<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: #0000aa\">using<\/span> <span style=\"color: #00aaaa; text-decoration: underline\">System<\/span>;\r\n<span style=\"color: #0000aa\">using<\/span> <span style=\"color: #00aaaa; text-decoration: underline\">System.Collections.Generic<\/span>;\r\n<span style=\"color: #0000aa\">using<\/span> <span style=\"color: #00aaaa; text-decoration: underline\">System.Text<\/span>;\r\n<span style=\"color: #0000aa\">using<\/span> <span style=\"color: #00aaaa; text-decoration: underline\">System.Runtime.InteropServices<\/span>;\r\n\r\n<span style=\"color: #0000aa\">namespace<\/span> <span style=\"color: #00aaaa; text-decoration: underline\">InLineAsm<\/span>\r\n{\r\n    <span style=\"color: #0000aa\">static<\/span> <span style=\"color: #0000aa\">class<\/span> <span style=\"color: #00aa00; text-decoration: underline\">Program<\/span>\r\n    {\r\n<span style=\"color: #1e90ff\">        [UnmanagedFunctionPointer(CallingConvention.StdCall)]<\/span>\r\n        <span style=\"color: #0000aa\">delegate<\/span> <span style=\"color: #0000aa\">void<\/span> <span style=\"color: #00aa00\">JoesAntiDebuggery<\/span>();\r\n\r\n<span style=\"color: #1e90ff\">        [DllImport(&quot;kernel32.dll&quot;, SetLastError = true)]<\/span>\r\n        <span style=\"color: #0000aa\">static<\/span> <span style=\"color: #0000aa\">extern<\/span> IntPtr <span style=\"color: #00aa00\">VirtualAlloc<\/span>(IntPtr lpAddress, UIntPtr dwSize, IntPtr flAllocationType, IntPtr flProtect);\r\n\r\n\t\t<span style=\"color: #0000aa\">static<\/span> <span style=\"color: #00aaaa\">byte<\/span>[] opcodez = {\r\n\t\t<span style=\"color: #009999\">0<\/span>x55, <span style=\"color: #009999\">0<\/span>x89, <span style=\"color: #009999\">0<\/span>xE5, <span style=\"color: #009999\">0<\/span>x31, <span style=\"color: #009999\">0<\/span>xC0, <span style=\"color: #009999\">0<\/span>xBA, <span style=\"color: #009999\">0<\/span>x00, <span style=\"color: #009999\">0<\/span>xE1, <span style=\"color: #009999\">0<\/span>xF5, <span style=\"color: #009999\">0<\/span>x05, <span style=\"color: #009999\">0<\/span>x40, <span style=\"color: #009999\">0<\/span>x4A, <span style=\"color: #009999\">0<\/span>x75, <span style=\"color: #009999\">0<\/span>xFC, <span style=\"color: #009999\">0<\/span>x3D, <span style=\"color: #009999\">0<\/span>x00,\r\n\t\t<span style=\"color: #009999\">0<\/span>xE1, <span style=\"color: #009999\">0<\/span>xF5, <span style=\"color: #009999\">0<\/span>x05, <span style=\"color: #009999\">0<\/span>x75, <span style=\"color: #009999\">0<\/span>x14, <span style=\"color: #009999\">0<\/span>x6A, <span style=\"color: #009999\">0<\/span>x00, <span style=\"color: #009999\">0<\/span>x68, <span style=\"color: #009999\">0<\/span>x12 ,<span style=\"color: #009999\">0<\/span>x70, <span style=\"color: #009999\">0<\/span>x40, <span style=\"color: #009999\">0<\/span>x00, <span style=\"color: #009999\">0<\/span>x68, <span style=\"color: #009999\">0<\/span>x0C, <span style=\"color: #009999\">0<\/span>x70, <span style=\"color: #009999\">0<\/span>x40,\r\n\t\t<span style=\"color: #009999\">0<\/span>x00, <span style=\"color: #009999\">0<\/span>x6A, <span style=\"color: #009999\">0<\/span>x00, <span style=\"color: #009999\">0<\/span>xFF, <span style=\"color: #009999\">0<\/span>x15, <span style=\"color: #009999\">0<\/span>xD0, <span style=\"color: #009999\">0<\/span>x80, <span style=\"color: #009999\">0<\/span>x40, <span style=\"color: #009999\">0<\/span>x00, <span style=\"color: #009999\">0<\/span>x31, <span style=\"color: #009999\">0<\/span>xC0, <span style=\"color: #009999\">0<\/span>x68, <span style=\"color: #009999\">0<\/span>x00, <span style=\"color: #009999\">0<\/span>x70, <span style=\"color: #009999\">0<\/span>x40, <span style=\"color: #009999\">0<\/span>x00,\r\n\t\t<span style=\"color: #009999\">0<\/span>xE8, <span style=\"color: #009999\">0<\/span>x3B, <span style=\"color: #009999\">0<\/span>x00, <span style=\"color: #009999\">0<\/span>x00, <span style=\"color: #009999\">0<\/span>x00, <span style=\"color: #009999\">0<\/span>x59, <span style=\"color: #009999\">0<\/span>x31, <span style=\"color: #009999\">0<\/span>xC0, <span style=\"color: #009999\">0<\/span>x89, <span style=\"color: #009999\">0<\/span>xEC, <span style=\"color: #009999\">0<\/span>x5D, <span style=\"color: #009999\">0<\/span>xC3 \r\n\t\t}\r\n\t\t<span style=\"color: #aaaaaa; font-style: italic\">\/\/ opcodes taken from disassembled program.<\/span>\r\n\t\t<span style=\"color: #aaaaaa; font-style: italic\">\/*<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\t__asm<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\t{<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\txor     eax, eax<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\tmov     edx, 5F5E100h<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\tjoe:<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\tinc     eax<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\tdec     edx<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\tjnz     joe<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\tcmp     eax, 5F5E100h<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\tjnz     short urafag<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\t}<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\tMessageBox(0,Text, Caption,0);<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\t__asm<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\t{<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\turafag:<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\txor     eax, eax<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\t\t<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\t}<\/span>\r\n<span style=\"color: #aaaaaa; font-style: italic\">\t\t*\/<\/span>\r\n\r\n\t\t<span style=\"color: #0000aa\">static<\/span> IntPtr codeBuffer = VirtualAlloc(IntPtr.Zero, <span style=\"color: #0000aa\">new<\/span> UIntPtr((<span style=\"color: #00aaaa\">uint<\/span>)opcodez.Length), (IntPtr)(<span style=\"color: #009999\">0<\/span>x1000 | <span style=\"color: #009999\">0<\/span>x2000), (IntPtr)<span style=\"color: #009999\">0<\/span>x40);\r\n\t\t<span style=\"color: #aaaaaa; font-style: italic\">\/\/ EXECUTE_READWRITE, MEM_COMMIT | MEM_RESERVE<\/span>\r\n\t\tMarshal.Copy(opcodez, <span style=\"color: #009999\">0<\/span>,codeBuffer, opcodez.Length);\r\n\t\tJoesAntiDebuggery JoeDbg = (JoesAntiDebuggery)\r\n\t\tMarshal.GetDelegateForFunctionPointer(codeBuffer, <span style=\"color: #0000aa\">typeof<\/span>(JoesAntiDebuggery));\r\n        \r\n        <span style=\"color: #0000aa\">static<\/span> <span style=\"color: #0000aa\">void<\/span> <span style=\"color: #00aa00\">Main<\/span>(<span style=\"color: #00aaaa\">string<\/span>[] args)\r\n        {\r\n           Console.Write(<span style=\"color: #aa5500\">&quot;lol&quot;<\/span>);\r\n           JoeDbg();\r\n        }\r\n\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n<p>It&#8217;s a thing of beauty &#8211; Assembly, C code, op codes \/ hex, delegates, and C#. <\/p>\n<p>Moving on to what else I&#8217;ve been up to &#8211; pulling apart malwarez. This one piece gave me trouble for a few days. Namely because of the weird anti-debugging counter measure I encountered. I&#8217;m unsure if its even anti-debug as the conditions always seem to equate to false. I mean it&#8217;s easy to get around when you see it, but you can&#8217;t get around it automatically &#8211; you have to patch it. I even took a video of the weird behavior.<br \/>\n<iframe loading=\"lazy\" width=\"640\" height=\"360\" src=\"\/\/www.youtube.com\/embed\/eP9pQJSzewM?feature=player_detailpage\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Took me some time, but I figured it out. <\/p>\n<p>The following is the sequence called not 5 instructions after the entry point<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/anti-debuggery.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/anti-debuggery.png\" alt=\"anti-debuggery\" width=\"618\" height=\"380\" class=\"alignnone size-full wp-image-968\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/anti-debuggery.png 618w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/anti-debuggery-300x184.png 300w\" sizes=\"(max-width: 618px) 100vw, 618px\" \/><\/a><\/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: #00aa00\">sub_4017CF<\/span>      <span style=\"color: #aa0000\">proc<\/span> <span style=\"color: #aa0000\">near<\/span>               \r\n<span style=\"color: #00aa00\">push<\/span>    <span style=\"color: #00aaaa\">ebp<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">edi<\/span>, <span style=\"color: #00aaaa\">edx<\/span>\r\n<span style=\"color: #00aa00\">add<\/span>     <span style=\"color: #00aaaa\">edi<\/span>, <span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">not<\/span>     <span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">ebp<\/span>, <span style=\"color: #00aaaa\">esp<\/span>\r\n<span style=\"color: #00aa00\">add<\/span>     <span style=\"color: #00aaaa\">edi<\/span>, <span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">add<\/span>     <span style=\"color: #00aaaa\">esp<\/span>, <span style=\"color: #009999\">0FFFFFF94h<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">edx<\/span>, <span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">inc<\/span>     <span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">ecx<\/span>, <span style=\"color: #00aaaa\">esp<\/span>\r\n<span style=\"color: #00aa00\">dec<\/span>     <span style=\"color: #00aaaa\">ebx<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">edi<\/span>, <span style=\"color: #00aaaa\">eax<\/span>\r\n<span style=\"color: #00aa00\">add<\/span>     <span style=\"color: #00aaaa\">ecx<\/span>, <span style=\"color: #009999\">48h<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">ebx<\/span>, <span style=\"color: #00aaaa\">ecx<\/span>\r\n<span style=\"color: #00aa00\">dec<\/span>     <span style=\"color: #00aaaa\">edi<\/span>\r\n<span style=\"color: #00aa00\">cmp<\/span>     <span style=\"color: #00aaaa\">eax<\/span>, <span style=\"color: #00aaaa\">ecx<\/span>\r\n<span style=\"color: #00aa00\">jz<\/span>      <span style=\"color: #aa0000\">short<\/span> <span style=\"color: #aa0000\">labelforyou<\/span>\r\n<span style=\"color: #00aa00\">neg<\/span>     <span style=\"color: #00aaaa\">edx<\/span>\r\n<span style=\"color: #00aa00\">leave<\/span>\r\n<span style=\"color: #00aa00\">not<\/span>     <span style=\"color: #00aaaa\">edx<\/span>\r\n<span style=\"color: #00aa00\">mov<\/span>     <span style=\"color: #00aaaa\">eax<\/span>, <span style=\"color: #00aaaa\">edi<\/span>\r\n<span style=\"color: #00aa00\">neg<\/span>     <span style=\"color: #00aaaa\">eax<\/span>\r\n<span style=\"color: #00aa00\">leave<\/span>\r\n<span style=\"color: #00aa00\">add<\/span>     <span style=\"color: #00aaaa\">edx<\/span>, <span style=\"color: #00aaaa\">edi<\/span>\r\n<span style=\"color: #00aa00\">not<\/span>     <span style=\"color: #00aaaa\">edx<\/span>\r\n<span style=\"color: #00aa00\">retn<\/span>\r\nlabelforyou:                          \r\n<span style=\"color: #00aa00\">leave<\/span>\r\n<span style=\"color: #00aa00\">retn<\/span>\r\n<\/pre>\n<\/div>\n<p>The first thing you may notice about this procedure is the weird stack frame setup. Most of the time, the intro stack frame will be &#8220;push ebp&#8221; followed directly by &#8220;mov ebp, esp&#8221;. This one is different in that it plays with the registers a little before the &#8220;mov ebp, esp&#8221; assembly codes. You may also notice the 2 &#8220;leave&#8221; instructions at the end of the procedure as opposed to the 1 for the &#8220;labelforyou&#8221; conditional.  The 2 &#8220;leave&#8221; instructions are why the program jumps to ExitThread. When you leave a stack frame twice and &#8216;ret&#8217;, any windows program jumps to ntdll.RtlExitUserThread. An interesting intrinsic way of quietly exiting without warning. <\/p>\n<p>But what about the code that leads up to the &#8216;JZ&#8217; branch and the 2 leaves? The comparison is EAX to ECX. Every time I run, EAX always ends up as 1 and ECX as some stack address. I&#8217;m postulating that the malware I grabbed was extracted from a dropper. That makes sense given the stack value \/ pointer points to nothing useful. <\/p>\n<p>If you&#8217;re curious what the malware does, it attempts to download and run a &#8216;doc&#8217; file from a russian host. Inside the &#8216;doc&#8217; file is HTML code with a meta redirect to a host my DNS server can&#8217;t seem to find:<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/what-it-do.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/what-it-do.png\" alt=\"what it do\" width=\"766\" height=\"673\" class=\"alignnone size-full wp-image-969\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/what-it-do.png 766w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/what-it-do-300x263.png 300w\" sizes=\"(max-width: 766px) 100vw, 766px\" \/><\/a><\/p>\n<p>You can download the malware <a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/062.7z\">here<\/a>. Pass in &#8216;infected&#8217;.<\/p>\n<p>The other piece of malware I went through lacked a DOS sub. Most exe&#8217;s have this little DOS application inside that reads &#8220;this program cannot be run in DOS mode&#8221; and is placed at the start of an exe just in case someone attempts to run an exe on an old DOS system. Its a forward compatibility thing Microsoft does. Compare a normal exe to the binary:<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/w1.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/w1-300x91.png\" alt=\"w1\" width=\"300\" height=\"91\" class=\"alignnone size-medium wp-image-971\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/w1-300x91.png 300w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/w1-1024x312.png 1024w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/w1.png 1290w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>So how the hell do you remove the DOS sub and still maintain functionality? According to <a href=\"http:\/\/www.phreedom.org\/research\/tinype\/\" target=\"_blank\">TinyPE<\/a>, you do it in assembly via zeroing out the MZ header with the exception of the &#8216;e_magic&#8217; field &#8216;MZ&#8217; at the start and the &#8216;e_lfanew&#8217; field value at the bottom. The &#8216;e_lfanew&#8217; field is just a 4 byte offset to where the PE header is located.<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%\">mzhdr:\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #aa5500\">&quot;MZ&quot;<\/span>                       <span style=\"color: #aaaaaa; font-style: italic\">; e_magic<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_cblp UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_cp UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_crlc UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_cparhdr UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_minalloc UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_maxalloc UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_ss UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_sp UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_csum UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_ip UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_cs UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_lsarlc UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_ovno UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">times<\/span> <span style=\"color: #009999\">4<\/span> <span style=\"color: #aa0000\">dw<\/span> <span style=\"color: #009999\">0<\/span>                  <span style=\"color: #aaaaaa; font-style: italic\">; e_res UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_oemid UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dw<\/span> <span style=\"color: #009999\">0<\/span>                          <span style=\"color: #aaaaaa; font-style: italic\">; e_oeminfo UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">times<\/span> <span style=\"color: #009999\">10<\/span> <span style=\"color: #aa0000\">dw<\/span> <span style=\"color: #009999\">0<\/span>                 <span style=\"color: #aaaaaa; font-style: italic\">; e_res2 UNUSED<\/span>\r\n    <span style=\"color: #0000aa\">dd<\/span> <span style=\"color: #aa0000\">pesig<\/span>                      <span style=\"color: #aaaaaa; font-style: italic\">; e_lfanew<\/span>\r\n<\/pre>\n<\/div>\n<p>But what about doing it to a pre-compiled binary? I just used CFF explorer and HXD. Jot down the &#8216;e_lfanew&#8217; field offset and zero out the entries between the PE header, the MZ field, and the &#8216;e_lfanew&#8217; field:<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/remove_dos_sub.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/remove_dos_sub.png\" alt=\"remove_dos_sub\" width=\"656\" height=\"905\" class=\"alignnone size-full wp-image-972\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/remove_dos_sub.png 656w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/remove_dos_sub-217x300.png 217w\" sizes=\"(max-width: 656px) 100vw, 656px\" \/><\/a><\/p>\n<p>The malware does code running modification and is surprisingly sophisticated, but this blog post is long enough. I&#8217;m done for now. <\/p>\n<p>The next post will be much more interesting, however its unfinished and needs more research. Except it soon.<\/p>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/BiNUecP.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/BiNUecP.jpg\" alt=\"BiNUecP\" width=\"600\" height=\"378\" class=\"alignnone size-full wp-image-964\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/BiNUecP.jpg 600w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/11\/BiNUecP-300x189.jpg 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello fellow readers! You all are probably wondering what the hell I&#8217;ve been up to this past month. Lot&#8217;s of stuff. This post is all over the place with code and slides and malware and general wackiness. Rather than spreading it out over several blog posts, I decided to just get it all over with [&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,7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/963"}],"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=963"}],"version-history":[{"count":3,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/963\/revisions"}],"predecessor-version":[{"id":973,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/963\/revisions\/973"}],"wp:attachment":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/media?parent=963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/categories?post=963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/tags?post=963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}