{"id":522,"date":"2013-10-01T01:14:29","date_gmt":"2013-10-01T01:14:29","guid":{"rendered":"http:\/\/www.gironsec.com\/blog\/?p=522"},"modified":"2014-11-21T13:09:09","modified_gmt":"2014-11-21T13:09:09","slug":"anti-sandboxing-ideas","status":"publish","type":"post","link":"https:\/\/www.gironsec.com\/blog\/2013\/10\/anti-sandboxing-ideas\/","title":{"rendered":"Anti-Sandboxing Ideas"},"content":{"rendered":"<p>Hello loyal readers.<\/p>\n<p>Good news! I&#8217;ve been picked to speak at ToorCon in San Diego next month in October. I will be going over my findings in malware that manages to slip by FireEye undetected. A chink in the armor of one of the most powerful (and expensive) malware appliances out there. <\/p>\n<p>But enough about that, today I&#8217;m going over ideas I&#8217;ve had about sandboxes and their methods.<\/p>\n<p>I&#8217;m sure you&#8217;ve all seen or at least know of sites like malwr.com &#038; virustotal.com which do a scan of malware that users can upload. These are great tools. But how do people go about bypassing them? Anti-sandboxing methods aren&#8217;t mentioned very often in talks and blogs. How do the pro&#8217;s do it?<\/p>\n<p>My idea:<\/p>\n<p>Check for the existence of a small data file using InternetOpenUrl() and compare it with a hash.<br \/>\nExample: http:\/\/www.microsoft.com\/favicon.ico<\/p>\n<p>17,174 bytes in size<br \/>\nfavicon.ico md5 &#8211; 12E3DAC858061D088023B2BD48E2FA96<\/p>\n<p>Assuming the sandbox in question blocks out Internet access probes or attempts to simulate them, this could work. <\/p>\n<p>Now for some code. We&#8217;ll be using wininet functions for downloading, and win32 crypto api&#8217;s for MD5 sums. <\/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: #557799\">#include &lt;stdio.h&gt;<\/span>\r\n<span style=\"color: #557799\">#include &lt;windows.h&gt;<\/span>\r\n<span style=\"color: #557799\">#include &lt;wincrypt.h&gt;<\/span>\r\n<span style=\"color: #557799\">#pragma comment(lib, &quot;advapi32.lib&quot;)<\/span>\r\n<span style=\"color: #557799\">#include &lt;wininet.h&gt;<\/span>\r\n<span style=\"color: #557799\">#pragma comment(lib, &quot;wininet.lib&quot;)<\/span>\r\n<span style=\"color: #557799\">#define DEFAULT_AGENT\t\t\t\t&quot;Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.17 (KHTML, like Gecko) Chrome\/24.0.1312.57 Safari\/537.17&quot;<\/span>\r\n\r\n\r\nBOOL <span style=\"color: #0066BB; font-weight: bold\">HashData<\/span>(LPTSTR szOut, DWORD cchOut, LPCBYTE lpIn, DWORD cbIn, ALG_ID hash_algorithm)\r\n{\r\n    HCRYPTPROV hProv      <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n    HCRYPTHASH hHash      <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n    BYTE       raw[<span style=\"color: #0000DD; font-weight: bold\">64<\/span>];\r\n    DWORD      raw_len    <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">sizeof<\/span>(raw);\r\n    BOOL       bResult    <span style=\"color: #333333\">=<\/span> FALSE;\r\n    UINT       i;\r\n \r\n    <span style=\"color: #008800; font-weight: bold\">if<\/span>(CryptAcquireContext(<span style=\"color: #333333\">&amp;<\/span>hProv, <span style=\"color: #007020\">NULL<\/span>, <span style=\"color: #007020\">NULL<\/span>, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT <span style=\"color: #333333\">|<\/span> CRYPT_SILENT) <span style=\"color: #333333\">&amp;&amp;<\/span>\r\n       CryptCreateHash(hProv, hash_algorithm, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>, <span style=\"color: #333333\">&amp;<\/span>hHash) <span style=\"color: #333333\">&amp;&amp;<\/span>\r\n       CryptHashData(hHash, lpIn, cbIn, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>) <span style=\"color: #333333\">&amp;&amp;<\/span>\r\n       CryptGetHashParam(hHash, HP_HASHVAL, raw, <span style=\"color: #333333\">&amp;<\/span>raw_len, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>) <span style=\"color: #333333\">&amp;&amp;<\/span>\r\n       (raw_len <span style=\"color: #333333\">*<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">+<\/span> <span style=\"color: #0000DD; font-weight: bold\">1<\/span> <span style=\"color: #333333\">&lt;=<\/span> cchOut)\r\n    {\r\n        <span style=\"color: #008800; font-weight: bold\">for<\/span> (i <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;i <span style=\"color: #333333\">&lt;<\/span> raw_len;i<span style=\"color: #333333\">++<\/span>)\r\n        {\r\n            wsprintf(<span style=\"color: #333333\">&amp;<\/span>szOut[i <span style=\"color: #333333\">*<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>], TEXT(<span style=\"background-color: #fff0f0\">&quot;%02.2x&quot;<\/span>), raw[i]);\r\n        }\r\n \r\n        bResult <span style=\"color: #333333\">=<\/span> TRUE;\r\n    }\r\n \r\n    <span style=\"color: #008800; font-weight: bold\">if<\/span> (hHash) CryptDestroyHash(hHash);\r\n    <span style=\"color: #008800; font-weight: bold\">if<\/span> (hProv) CryptReleaseContext(hProv, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>);\r\n \r\n    <span style=\"color: #008800; font-weight: bold\">return<\/span> bResult;\r\n}\r\n<span style=\"color: #333399; font-weight: bold\">void<\/span> <span style=\"color: #0066BB; font-weight: bold\">DownFile<\/span>()\r\n{\r\nHINTERNET IntOpen  <span style=\"color: #333333\">=<\/span> InternetOpen(DEFAULT_AGENT, LOCAL_INTERNET_ACCESS, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>);\r\nHINTERNET handle  <span style=\"color: #333333\">=<\/span> InternetOpenUrl(IntOpen, <span style=\"background-color: #fff0f0\">&quot;http:\/\/www.microsoft.com\/favicon.ico&quot;<\/span>, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>);\r\nHANDLE hFile    <span style=\"color: #333333\">=<\/span> CreateFile(<span style=\"background-color: #fff0f0\">&quot;test.ico&quot;<\/span>, GENERIC_WRITE, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>);\r\n<span style=\"color: #333399; font-weight: bold\">char<\/span> Buffer[<span style=\"color: #0000DD; font-weight: bold\">4096<\/span>];\r\nDWORD dwRead <span style=\"color: #333333\">=<\/span><span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n<span style=\"color: #008800; font-weight: bold\">while<\/span>(InternetReadFile(handle, Buffer, <span style=\"color: #008800; font-weight: bold\">sizeof<\/span>(Buffer), <span style=\"color: #333333\">&amp;<\/span>dwRead) <span style=\"color: #333333\">==<\/span> TRUE)\r\n{\r\n  <span style=\"color: #008800; font-weight: bold\">if<\/span> ( dwRead <span style=\"color: #333333\">==<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>) \r\n    <span style=\"color: #008800; font-weight: bold\">break<\/span>;\r\n  DWORD dwWrite <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n  WriteFile(hFile, Buffer, dwRead, <span style=\"color: #333333\">&amp;<\/span>dwWrite, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>);\r\n}\r\nCloseHandle(hFile);\r\nInternetCloseHandle(handle);\r\n}\r\n<span style=\"color: #333399; font-weight: bold\">char<\/span><span style=\"color: #333333\">*<\/span> <span style=\"color: #0066BB; font-weight: bold\">ReadIconFile<\/span>(<span style=\"color: #333399; font-weight: bold\">void<\/span>)\r\n{\r\n    <span style=\"color: #333399; font-weight: bold\">char<\/span> <span style=\"color: #333333\">*<\/span>readbuff;\r\n\t<span style=\"color: #333399; font-weight: bold\">FILE<\/span> <span style=\"color: #333333\">*<\/span>lolfile; \r\n\tlolfile <span style=\"color: #333333\">=<\/span> fopen(<span style=\"background-color: #fff0f0\">&quot;test.ico&quot;<\/span>,<span style=\"background-color: #fff0f0\">&quot;rb&quot;<\/span>);\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(lolfile<span style=\"color: #333333\">!=<\/span><span style=\"color: #007020\">NULL<\/span>)\r\n\t{\r\n\tfseek(lolfile,<span style=\"color: #0000DD; font-weight: bold\">0<\/span>,SEEK_END);\r\n\t<span style=\"color: #333399; font-weight: bold\">long<\/span> fsize <span style=\"color: #333333\">=<\/span> ftell(lolfile);rewind(lolfile);\r\n\treadbuff <span style=\"color: #333333\">=<\/span> (<span style=\"color: #333399; font-weight: bold\">char<\/span><span style=\"color: #333333\">*<\/span>) malloc (<span style=\"color: #008800; font-weight: bold\">sizeof<\/span>(<span style=\"color: #333399; font-weight: bold\">char<\/span>)<span style=\"color: #333333\">*<\/span>fsize);\r\n\tfread (readbuff,<span style=\"color: #0000DD; font-weight: bold\">1<\/span>,fsize,lolfile);\r\n\tfclose(lolfile);\r\n\t<span style=\"color: #008800; font-weight: bold\">return<\/span> readbuff;\r\n\t}\r\n}\r\n<span style=\"color: #333399; font-weight: bold\">int<\/span> <span style=\"color: #0066BB; font-weight: bold\">main<\/span>(<span style=\"color: #333399; font-weight: bold\">void<\/span>)\r\n{\r\n\t<span style=\"color: #333399; font-weight: bold\">char<\/span> szHash[<span style=\"color: #0000DD; font-weight: bold\">64<\/span>];\r\n\tDownFile();\r\n\t<span style=\"color: #333399; font-weight: bold\">char<\/span> <span style=\"color: #333333\">*<\/span>lol <span style=\"color: #333333\">=<\/span> ReadKeyFile();\r\n    <span style=\"color: #008800; font-weight: bold\">if<\/span> (HashData(szHash, <span style=\"color: #0000DD; font-weight: bold\">33<\/span>, lol, strlen(lol), CALG_MD5))\r\n\t{\r\n\t\tMessageBox(<span style=\"color: #007020\">NULL<\/span>,szHash,<span style=\"background-color: #fff0f0\">&quot;lol&quot;<\/span>,MB_OK);\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(<span style=\"color: #333333\">!<\/span>strcmp(szHash,<span style=\"background-color: #fff0f0\">&quot;12E3DAC858061D088023B2BD48E2FA96&quot;<\/span>)) <span style=\"color: #888888\">\/\/ md5 of icon file<\/span>\r\n\t\t{\r\n\t\tMessageBox(<span style=\"color: #007020\">NULL<\/span>,<span style=\"background-color: #fff0f0\">&quot;Not in a sandbox!&quot;<\/span>,<span style=\"background-color: #fff0f0\">&quot;yah&quot;<\/span>,MB_OK);\r\n\t\t}\r\n\t\t<span style=\"color: #008800; font-weight: bold\">else<\/span> <span style=\"color: #888888\">\/\/ else just crash<\/span>\r\n\t\t{\r\n\t\t<span style=\"color: #008800; font-weight: bold\">__asm<\/span> int3\r\n\t\t}\r\n\t}\r\n  \r\n    <span style=\"color: #008800; font-weight: bold\">return<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>I tested this with Pelles C compiler, no idea how well this works with CodeBlocks \/ Mingw \/ Visual Studio. <\/p>\n<p>Since C# is awesome and needs some love, here is the .net version of the above code done last week (November 2014)<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.IO<\/span>;\r\n<span style=\"color: #0000aa\">using<\/span> <span style=\"color: #00aaaa; text-decoration: underline\">System.Security.Cryptography<\/span>;\r\n<span style=\"color: #0000aa\">using<\/span> <span style=\"color: #00aaaa; text-decoration: underline\">System.Net<\/span>;\r\n\r\n<span style=\"color: #0000aa\">namespace<\/span> <span style=\"color: #00aaaa; text-decoration: underline\">AmISandBoxMode<\/span>\r\n{\r\n    <span style=\"color: #0000aa\">class<\/span> <span style=\"color: #00aa00; text-decoration: underline\">Program<\/span>\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            <span style=\"color: #0000aa\">if<\/span>(CheckYahoo())\r\n                Console.Write(<span style=\"color: #aa5500\">&quot;You are in a sandbox!&quot;<\/span>);\r\n            <span style=\"color: #0000aa\">else<\/span>\r\n                Console.Write(<span style=\"color: #aa5500\">&quot;You are legit&quot;<\/span>);\r\n        }\r\n        <span style=\"color: #0000aa\">private<\/span> <span style=\"color: #0000aa\">static<\/span> <span style=\"color: #00aaaa\">bool<\/span> <span style=\"color: #00aa00\">CheckYahoo<\/span>()\r\n        {\r\n            <span style=\"color: #00aaaa\">string<\/span> curdir = AppDomain.CurrentDomain.BaseDirectory;\r\n            WebClient wc = <span style=\"color: #0000aa\">new<\/span> WebClient();\r\n            wc.DownloadFile(<span style=\"color: #aa5500\">&quot;https:\/\/www.yahoo.com\/favicon.ico&quot;<\/span>, curdir + <span style=\"color: #aa5500\">&quot;testing.joe&quot;<\/span>);\r\n            <span style=\"color: #00aaaa\">string<\/span> yahoomd5 = <span style=\"color: #aa5500\">&quot;9796ed786d95606d51be9dab54fb5350&quot;<\/span>;\r\n            <span style=\"color: #00aaaa\">string<\/span> testval = GetMd5Hash(curdir + <span style=\"color: #aa5500\">&quot;testing.joe&quot;<\/span>);\r\n            <span style=\"color: #0000aa\">if<\/span>(yahoomd5 == testval)\r\n            {\r\n                <span style=\"color: #0000aa\">return<\/span> <span style=\"color: #0000aa\">false<\/span>;\r\n            }\r\n            \r\n            <span style=\"color: #0000aa\">return<\/span> <span style=\"color: #0000aa\">true<\/span>;\r\n        }\r\n        <span style=\"color: #0000aa\">public<\/span> <span style=\"color: #0000aa\">static<\/span> <span style=\"color: #00aaaa\">string<\/span> <span style=\"color: #00aa00\">GetMd5Hash<\/span>(<span style=\"color: #00aaaa\">string<\/span> filePath)\r\n        {\r\n            <span style=\"color: #0000aa\">using<\/span> (<span style=\"color: #00aaaa\">var<\/span> kek = MD5.Create())\r\n            {\r\n                <span style=\"color: #0000aa\">using<\/span> (FileStream fs = File.OpenRead(filePath))\r\n                {\r\n                    <span style=\"color: #0000aa\">return<\/span> BitConverter.ToString(kek.ComputeHash(fs)).Replace(<span style=\"color: #aa5500\">&quot;-&quot;<\/span>, <span style=\"color: #aa5500\">&quot;&quot;<\/span>).ToLower(); ;\r\n                }\r\n            }\r\n        }\r\n\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello loyal readers. Good news! I&#8217;ve been picked to speak at ToorCon in San Diego next month in October. I will be going over my findings in malware that manages to slip by FireEye undetected. A chink in the armor of one of the most powerful (and expensive) malware appliances out there. But enough about [&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":[69,48,68],"_links":{"self":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/522"}],"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=522"}],"version-history":[{"count":2,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/522\/revisions"}],"predecessor-version":[{"id":965,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/522\/revisions\/965"}],"wp:attachment":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/media?parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/categories?post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/tags?post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}