{"id":13230,"date":"2018-08-30T08:10:05","date_gmt":"2018-08-30T16:10:05","guid":{"rendered":"http:\/\/www.palada.net\/index.php\/2018\/08\/30\/news-6997\/"},"modified":"2018-08-30T08:10:05","modified_gmt":"2018-08-30T16:10:05","slug":"news-6997","status":"publish","type":"post","link":"http:\/\/www.palada.net\/index.php\/2018\/08\/30\/news-6997\/","title":{"rendered":"Reversing malware in a custom format: Hidden Bee elements"},"content":{"rendered":"<p><strong>Credit to Author: hasherezade| Date: Thu, 30 Aug 2018 15:41:34 +0000<\/strong><\/p>\n<p>Malware can be made of many components. Often, we encounter macros and scripts that work as malicious downloaders. Some functionalities can also be achieved by position-independent code\u2014so-called shellcode. But when it comes to more complex elements or core modules, we almost take it for granted that it will be a PE file that is a native Windows executable format.<\/p>\n<p>The reason for this is simple: It is much easier to provide complex functionality within a PE file than within a shellcode. PE format has a well-defined structure, allowing for much more flexibility. We have certain headers that define what imports should be loaded and where, as well as how the relocations should be applied. This is a default format generated when we compile applications for Windows, and its structure is then used by Windows Loader to load and execute our application. Even when the malware authors write custom loaders, they are mostly for the PE format.<\/p>\n<p>However, sometimes we find exceptions. Last time, when we analyzed payloads related to <a href=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/07\/hidden-bee-miner-delivered-via-improved-drive-by-download-toolkit\/\" target=\"_blank\" rel=\"noopener\">Hidden Bee (dropped by the Underminer exploit kit)<\/a>, we noticed something unusual. There were two payloads dropped that didn&#8217;t follow the PE format. Yet, their structure looked well organized and more complex than we usually encounter dealing with pieces of shellcode. We decided to take a closer look and discovered that the authors of this malware actually created their own executable format, following a consistent structure.<\/p>\n<h3>Overview<\/h3>\n<p>The first payload: <a href=\"https:\/\/www.virustotal.com\/#\/file\/76b70f1dfd64958fca7ab3e18fffe6d551474c2b25aaa9515181dec6ae112895\/details\" target=\"_blank\" rel=\"noopener\">b3eb576e02849218867caefaa0412ccd<\/a> (with .wasm extension, imitating Web Assembly) is a loader, downloading and unpacking a Cabinet file:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/07\/dlls_.png\" \/><\/p>\n<p>The second payload: <a href=\"https:\/\/www.virustotal.com\/#\/file\/c1a6df241239359731c671203925a8265cf82a0c8c20c94d57a6a1ed09dec289\/details\" target=\"_blank\" rel=\"noopener\">11310b509f8bf86daa5577758e9d1eb5<\/a>, unpacked from the Cabinet:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/07\/coresdb_.png\" \/><\/p>\n<p>We can see at first that in contrast to most shellcodes, it does not start from a code, but from some headers. Comparing both modules, we can see that the header has the same structure in both cases.<\/p>\n<h3>Headers<\/h3>\n<p>We took a closer look to decipher the meaning of particular fields in the header.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25245\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/custom_hdr\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/custom_hdr.png\" data-orig-size=\"627,213\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"custom_hdr\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/custom_hdr-300x102.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/custom_hdr-600x204.png\" class=\"alignnone size-full wp-image-25245\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/custom_hdr.png\" alt=\"\" width=\"627\" height=\"213\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/custom_hdr.png 627w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/custom_hdr-300x102.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/custom_hdr-600x204.png 600w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/p>\n<p>The first DWORD: 0x10000301 is the same in both. We didn&#8217;t find this number corresponding to any of the pieces within the module. So, we assume it is a magic number that makes an identifier of this format.<\/p>\n<p>Next, two WORDs are offsets to elements related to loading the imports. The first one (0x18) points to the list of DLLs. The second block (0x60) looks more mysterious at first. Its meaning can be understood when we load the module in IDA. We can see the cross-references to those fields:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25257\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/call_via\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/call_via.png\" data-orig-size=\"628,197\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"call_via\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/call_via-300x94.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/call_via-600x188.png\" class=\"alignnone size-full wp-image-25257\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/call_via.png\" alt=\"\" width=\"628\" height=\"197\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/call_via.png 628w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/call_via-300x94.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/call_via-600x188.png 600w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/p>\n<p>We see that they are used as IAT\u2014they are supposed to be filled with the addresses to the imported functions:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25258\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/calls\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/calls.png\" data-orig-size=\"406,184\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"calls\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/calls-300x136.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/calls.png\" class=\"alignnone size-full wp-image-25258\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/calls.png\" alt=\"\" width=\"406\" height=\"184\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/calls.png 406w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/calls-300x136.png 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/p>\n<p>The next value is a DWORD (0x2A62). If we follow it in IDA, we see that it leads to the beginning of a new function:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25259\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/mod_ep\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/mod_ep.png\" data-orig-size=\"652,208\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"mod_ep\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/mod_ep-300x96.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/mod_ep-600x191.png\" class=\"alignnone size-full wp-image-25259\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/mod_ep.png\" alt=\"\" width=\"652\" height=\"208\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/mod_ep.png 652w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/mod_ep-300x96.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/mod_ep-600x191.png 600w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/p>\n<p>This function is not referenced by any other functions so we can suspect that it is the program&#8217;s Entry Point.<\/p>\n<p>The meaning of the next value (0x509C) is easy to guess because it is the same as the size of the full module.<\/p>\n<p>Then, we have the last two DWORDs of the header. The second DWORD (0x4D78) leads to the structure that is very similar to the PE&#8217;s relocations. We can guess that it must be a relocation table of the module, and the previous DWORD specifies its size.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25260\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/reloc_table\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/reloc_table.png\" data-orig-size=\"610,146\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"reloc_table\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/reloc_table-300x72.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/reloc_table-600x144.png\" class=\"alignnone size-full wp-image-25260\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/reloc_table.png\" alt=\"\" width=\"610\" height=\"146\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/reloc_table.png 610w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/reloc_table-300x72.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/reloc_table-600x144.png 600w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/reloc_table-604x146.png 604w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/p>\n<p>This is how we were able to reconstruct the full header:<\/p>\n<pre>typedef struct {  \tDWORD magic;    \tWORD dll_list;  \tWORD iat;  \tDWORD ep;  \tDWORD mod_size;    \tDWORD relocs_size;  \tDWORD relocs;  } t_bee_hdr;  <\/pre>\n<h3>Imports<\/h3>\n<p>As we know from the header, the list of the DLLs starts at the offset 0x18. We can see that each of the DLL&#8217;s names are prepended with a number:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25250\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/functions-3\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions.png\" data-orig-size=\"620,138\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"functions\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions-300x67.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions-600x134.png\" class=\"alignnone size-full wp-image-25250\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions.png\" alt=\"\" width=\"620\" height=\"138\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions.png 620w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions-300x67.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions-600x134.png 600w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>The numbers are not corresponding with a DLL name: In two different modules, the same DLL had different numbers assigned. But if we sum up all the numbers, we find that their total sum is the same as the number of DWORDs in the IAT. So, we can make an educated guess that those numbers are specifying how many functions will be imported from a particular DLL.<\/p>\n<p>We can describe it as the following structure (where the name&#8217;s length is not specified):<\/p>\n<pre>typedef struct {  \tWORD func_count;  \tchar name;  } t_dll_name;  <\/pre>\n<p>Then, the IAT comes as a list of DWORDs:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25251\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/iat-2\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/iat.png\" data-orig-size=\"617,308\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"iat\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/iat-300x150.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/iat-600x300.png\" class=\"alignnone size-full wp-image-25251\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/iat.png\" alt=\"\" width=\"617\" height=\"308\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/iat.png 617w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/iat-300x150.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/iat-600x300.png 600w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/p>\n<p>It is common in malware that when the function&#8217;s names are not given as an explicit string, they are imported by checksum. The same is done in this case. Guessing the appropriate function that was used for calculating the checksum can be more difficult. Fortunately, we found it in the loader component:<\/p>\n<pre>DWORD checksum(char *func_name)  {    DWORD result = 0x1505;    while ( *func_name )      result = *func_name++ + 33 * result;    return result;  }  <\/pre>\n<p>Knowing that we paired appropriate checksums with the function&#8217;s names:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25256\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/functions-4\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions-1.png\" data-orig-size=\"303,183\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"functions\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions-1-300x181.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions-1.png\" class=\"alignnone size-full wp-image-25256\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions-1.png\" alt=\"\" width=\"303\" height=\"183\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions-1.png 303w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/functions-1-300x181.png 300w\" sizes=\"auto, (max-width: 303px) 100vw, 303px\" \/><\/p>\n<p>Once the address of the function is retrieved, it is stored in the IAT in place of the checksum.<\/p>\n<h3>Relocations<\/h3>\n<p>Creating a relocation table is simple. It consists of the list of DWORDs that are identifying the offsets of the places in the code to which we should add the base where the module has been loaded. Without relocations applied, the module will crash (so, it is not position-independent like a typical shellcode).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25261\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/relocations-2\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/relocations.png\" data-orig-size=\"122,222\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"relocations\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/relocations.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/relocations.png\" class=\"alignnone size-full wp-image-25261\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/relocations.png\" alt=\"\" width=\"122\" height=\"222\" \/><\/p>\n<h3>Comparison to PE format<\/h3>\n<p>While the PE format is complex, with a variety of headers, this one contains only essentials. Most of the information that is usually stored in a PE header is completely omitted here.<\/p>\n<p>You can see a PE format visualized by Ange Albertini <a href=\"https:\/\/raw.githubusercontent.com\/corkami\/pics\/master\/binary\/PE101.png\" target=\"_blank\" rel=\"noopener\" data-rel=\"lightbox-0\" title=\"\">here<\/a>.<\/p>\n<p>Compare it with the visualization of the currently analyzed format:<\/p>\n<p><a href=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/format.png\" target=\"_blank\" rel=\"noopener\" data-rel=\"lightbox-1\" title=\"\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25335\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/format\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/format.png\" data-orig-size=\"817,613\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"format\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/format-300x225.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/format-600x450.png\" class=\"alignnone size-full wp-image-25335\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/format.png\" alt=\"\" width=\"817\" height=\"613\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/format.png 817w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/format-300x225.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/format-600x450.png 600w\" sizes=\"auto, (max-width: 817px) 100vw, 817px\" \/><\/a><\/p>\n<h3>Static analysis<\/h3>\n<p>We can load this code into IDA as a blob of raw code. However, we will be missing important information. Due to the fact that the file doesn&#8217;t follow a PE structure, and its import table is non-standard, we will have a hard time understanding which API calls are being made at which offset. To solve this problem, I made <a href=\"https:\/\/github.com\/hasherezade\/bee_parser\" target=\"_blank\" rel=\"noopener\">a tool<\/a> that resolves hashes into function names and generates a TAG file to mark the offsets where each function&#8217;s address is going to be filled.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25330\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/resolved_thunks\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/resolved_thunks.png\" data-orig-size=\"211,186\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"resolved_thunks\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/resolved_thunks.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/resolved_thunks.png\" class=\"alignnone size-full wp-image-25330\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/resolved_thunks.png\" alt=\"\" width=\"211\" height=\"186\" \/><\/p>\n<p>Those tags can be loaded into IDA using an\u00a0<a href=\"https:\/\/github.com\/hasherezade\/ida_ifl\" target=\"_blank\" rel=\"noopener\">IFL plugin<\/a>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25331\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/loaded_tags\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/loaded_tags.png\" data-orig-size=\"629,239\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"loaded_tags\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/loaded_tags-300x114.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/loaded_tags-600x228.png\" class=\"alignnone size-full wp-image-25331\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/loaded_tags.png\" alt=\"\" width=\"629\" height=\"239\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/loaded_tags.png 629w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/loaded_tags-300x114.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/loaded_tags-600x228.png 600w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/p>\n<p>Having all the API functions tagged, it is much easier to understand which actions are performed by the module. Here, for example, we can see that it will be establishing the connection with the C2 server:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25332\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/attachment\/get_host\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/get_host.png\" data-orig-size=\"443,280\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"get_host\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/get_host-300x190.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/get_host.png\" class=\"alignnone size-full wp-image-25332\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/get_host.png\" alt=\"\" width=\"443\" height=\"280\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/get_host.png 443w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/get_host-300x190.png 300w\" sizes=\"auto, (max-width: 443px) 100vw, 443px\" \/><\/p>\n<h3>Dynamic analysis<\/h3>\n<p>This format is custom, so it is not supported by the typical tools for analysis. However, after understanding it, we can write our own tools, such as the parser for the headers and loader that will help to run this format and analyze it dynamically.<\/p>\n<p>In contrast to PE, the module doesn&#8217;t have any sections. So, we need to load it in a continuous memory region with RWX (read-write-execute) access. Walking through the relocations list, we will add the value of the base at which the module was loaded to the listed addresses. Then, we have to resolve the imported functions by their hashes and fill the addresses in the thunks. After preparing the stage, it just needs to jump at the Entry Point of the module. We will load the prepared loaded under the debugger and follow to the entry point of the loaded module.<\/p>\n<h3>Simple but rare<\/h3>\n<p>The elements described here are pretty simple\u2014they serve as a first stage of the full malware package, downloading other pieces and injecting them into processes. However, what makes them interesting is the fact that their authors have shown some creativity and decided to invent a custom structure that is less complex than a full-fledged PE format, but goes a step further than a typical piece of shellcode.<\/p>\n<p>This structure, in contrast to independent shellcode, is not self-sufficient and cannot be loaded in a trivial way, but must be parsed first. Given the fact that the format is custom, it is not supported by existing tools. This is where programming skills come in handy for a malware analyst.<\/p>\n<p>Fortunately, fully custom formats are rather uncommon in the malware world; usually, authors rely heavily on existing formats, from time to time corrupting or customizing selected parts of PE headers.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/\">Reversing malware in a custom format: Hidden Bee elements<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/blog.malwarebytes.com\">Malwarebytes Labs<\/a>.<\/p>\n<p><a href=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/\" target=\"bwo\" >https:\/\/blog.malwarebytes.com\/feed\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p><strong>Credit to Author: hasherezade| Date: Thu, 30 Aug 2018 15:41:34 +0000<\/strong><\/p>\n<table cellpadding='10'>\n<tr>\n<td valign='top' align='center'><a href='https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/' title='Reversing malware in a custom format: Hidden Bee elements'><img src='https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/08\/shutterstock_767184319.jpg' border='0'  width='300px'  \/><\/a><\/td>\n<\/tr>\n<tr>\n<td valign='top' align='left'>When we recently analyzed payloads related to Hidden Bee (dropped by the Underminer EK), we noticed something unusual. After reversing the malware, we discovered that its authors actually created their own executable format. Follow our step-by-step analysis for a closer look.<\/p>\n<p>Categories: <\/p>\n<ul class=\"post-categories\">\n<li><a href=\"https:\/\/blog.malwarebytes.com\/category\/threat-analysis\/malware-threat-analysis\/\" rel=\"category tag\">Malware<\/a><\/li>\n<li><a href=\"https:\/\/blog.malwarebytes.com\/category\/threat-analysis\/\" rel=\"category tag\">Threat analysis<\/a><\/li>\n<\/ul>\n<p>Tags: <a href=\"https:\/\/blog.malwarebytes.com\/tag\/custom-malware\/\" rel=\"tag\">custom malware<\/a><a href=\"https:\/\/blog.malwarebytes.com\/tag\/hidden-bee\/\" rel=\"tag\">hidden bee<\/a><a href=\"https:\/\/blog.malwarebytes.com\/tag\/hidden-bee-miner\/\" rel=\"tag\">hidden bee miner<\/a><a href=\"https:\/\/blog.malwarebytes.com\/tag\/payload\/\" rel=\"tag\">payload<\/a><a href=\"https:\/\/blog.malwarebytes.com\/tag\/reverse-engineering\/\" rel=\"tag\">reverse engineering<\/a><a href=\"https:\/\/blog.malwarebytes.com\/tag\/reversing-malware\/\" rel=\"tag\">reversing malware<\/a><\/p>\n<table width='100%'>\n<tr>\n<td align=right>\n<p><b>(<a href='https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/' title='Reversing malware in a custom format: Hidden Bee elements'>Read more&#8230;<\/a>)<\/b><\/p>\n<\/td>\n<\/tr>\n<\/table>\n<\/td>\n<\/tr>\n<\/table>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/08\/reversing-malware-in-a-custom-format-hidden-bee-elements\/\">Reversing malware in a custom format: Hidden Bee elements<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/blog.malwarebytes.com\">Malwarebytes Labs<\/a>.<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[10488,10378],"tags":[19356,19070,19071,3764,13001,19357,19358,10494],"class_list":["post-13230","post","type-post","status-publish","format-standard","hentry","category-malwarebytes","category-security","tag-custom-malware","tag-hidden-bee","tag-hidden-bee-miner","tag-malware","tag-payload","tag-reverse-engineering","tag-reversing-malware","tag-threat-analysis"],"_links":{"self":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts\/13230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/comments?post=13230"}],"version-history":[{"count":0,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts\/13230\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/media?parent=13230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/categories?post=13230"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/tags?post=13230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}