{"id":11864,"date":"2018-03-27T08:10:11","date_gmt":"2018-03-27T16:10:11","guid":{"rendered":"http:\/\/www.palada.net\/index.php\/2018\/03\/27\/news-5633\/"},"modified":"2018-03-27T08:10:11","modified_gmt":"2018-03-27T16:10:11","slug":"news-5633","status":"publish","type":"post","link":"http:\/\/www.palada.net\/index.php\/2018\/03\/27\/news-5633\/","title":{"rendered":"Encryption 101: Decryptor&#8217;s thought process"},"content":{"rendered":"<p><strong>Credit to Author: Vasilios Hioureas| Date: Tue, 27 Mar 2018 15:00:00 +0000<\/strong><\/p>\n<p>In the previous parts <a href=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/02\/encryption-101-malware-analysts-primer\/\" target=\"_blank\" rel=\"noopener\">1<\/a>, <a href=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/02\/encryption-101-shione-ransomware-case-study\/\" target=\"_blank\" rel=\"noopener\">2<\/a> and <a href=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-how-to-break-encryption\/\" target=\"_blank\" rel=\"noopener\">3<\/a> of this series, we covered the basics of encryption, walked through a live example of a ransomware in detail, and talked about encryption weaknesses. In this part of the encryption 101 series, we will begin wrapping it up by going into detail on a ransomware with weak encryption and walking through step-by-step the thought process of creating a decryptor for it.<\/p>\n<p>This article is intended to help give a malware analyst a starting point for which to build off of in order to reverse and break ransomware encryptions. If nothing more, this article is to serve as an explanation why it is still worth it to inspect ransomware in detail to try to find opportunities for decryption.<\/p>\n<p>Even though encryption packages and API&#8217;s are built into modern day operating systems, still, many malware authors manage to misuse them and give us (analysts) the opportunity to exploit their poor coding skills.<\/p>\n<h3>Target ransomware intro<\/h3>\n<p>The sample we will be talking about in detail is PrincessLocker. Credit to <a href=\"https:\/\/twitter.com\/hasherezade\" target=\"_blank\" rel=\"noopener\">Hasherezade<\/a> for analyzing and creating keygen and decryptor for this. Her code and information on using the decryptor, should you need to, can be found here:<\/p>\n<p><a href=\"https:\/\/hshrzd.wordpress.com\/2016\/11\/17\/princess-locker-decryptor\/\">Princess Locker decryptor<\/a><\/p>\n<p>Before continuing on, I greatly urge you to read the analysis report on this ransomware so that you are familiar with some of its inner workings before talking about the decryption. The blog post can be found below:<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"Ft9QgFYdaB\">\n<p><a href=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2016\/11\/princess-ransomware\/\">PrincessLocker &#8211; ransomware with not so royal encryption<\/a><\/p>\n<\/blockquote>\n<p><iframe loading=\"lazy\"  src=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2016\/11\/princess-ransomware\/embed\/#?secret=Ft9QgFYdaB\" width=\"100%\" height=\"420\" frameborder=\"0\" ><\/iframe> <\/p>\n<p>&nbsp;<\/p>\n<h3>Analysis<\/h3>\n<p>Because this is not a lesson in unpacking, let&#8217;s assume that you have just read an analysis talking about <span style=\"background-color: #f6d5d9\">PrincessLocker\u00a0<\/span>being unpacked and some basic functionality, the following hash of the unpacked sample was provided:<\/p>\n<p><a href=\"https:\/\/virustotal.com\/en\/file\/710b0cd177cd0dbb5291e3b9538a8251f19fcf8753c757f3fb16ca2abf7cf2af\/analysis\/\" target=\"_blank\" rel=\"noopener\">4142a59be1f59dbd8e1be832df893d08<\/a><\/p>\n<p>as well as the original packed hash<\/p>\n<p><a href=\"https:\/\/virustotal.com\/en\/file\/b4824678b0b388260cdf7f1d70b868cc041b9865a295cb12fbdb868dd83ebc17\/analysis\/\" target=\"_blank\" rel=\"noopener\">14c32fd132942a0f3cc579adbd8a51ed<\/a><\/p>\n<p>We will start with the unpacked sample for our static analysis and will be using the fully packed sample for any dynamic analysis we have.<\/p>\n<h5>Static<\/h5>\n<p>Since we are focusing strictly on the crypto portion of the code, let&#8217;s start by doing a search and cross-reference for some crypto string or APIs, we see\u00a0<strong>CryptDeriveKey\u00a0<\/strong>being used in function address\u00a0<strong>10007980<\/strong>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22546\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/attachment\/cryptoapi\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/CryptoAPI.png\" data-orig-size=\"1768,1014\" 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=\"CryptoAPI\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/CryptoAPI-300x172.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/CryptoAPI-600x344.png\" class=\"size-full wp-image-22546 aligncenter\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/CryptoAPI.png\" alt=\"\" width=\"1768\" height=\"1014\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/CryptoAPI.png 1768w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/CryptoAPI-300x172.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/CryptoAPI-600x344.png 600w\" sizes=\"auto, (max-width: 1768px) 100vw, 1768px\" \/><\/p>\n<p>The parameter passed into CryptDeriveKey for algorithm ID is <strong>0x660E<\/strong>, which translates to\u00a0<strong>CALG_AES_128.<\/strong><\/p>\n<p>So we know that there is an AES key being generated right there, the &#8220;password&#8221; for this AES key is the pHash variable. If we trace it back, we see it being passed into 10007BB9, the <strong>call esi<\/strong> instruction. The strings are obfuscated so we cannot easily find them statically here.\u00a0 So we go to the debugger with the fully packed file\u00a014c32fd132942a0f3cc579adbd8a51ed.<\/p>\n<h5>Debug<\/h5>\n<p>When dealing with the packed file, to get to the unpacked code I am looking for, I typically find an API call from the unpacked sample and set a breakpoint in the library code. If we go to the main function who gets called\u00a0and leads us to this derive key call, we see it is the exported function &#8220;<strong>Zero<\/strong>&#8220;.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22548\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/attachment\/debug-2\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/Debug.png\" data-orig-size=\"1604,1326\" 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=\"Debug\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/Debug-300x248.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/Debug-600x496.png\" class=\"size-full wp-image-22548 aligncenter\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/Debug.png\" alt=\"\" width=\"1604\" height=\"1326\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/Debug.png 1604w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/Debug-300x248.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/Debug-600x496.png 600w\" sizes=\"auto, (max-width: 1604px) 100vw, 1604px\" \/><\/p>\n<p>Two candidates here for the breakpoint are <strong>TranslateMessage<\/strong> and <strong>OpenMutexW<\/strong>. I didn&#8217;t have luck with TranslateMessage, so I now try OpenMutexW.<\/p>\n<p>If you have the StringOD plugin in Olly, pressing control + G brings up this menu:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22549\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/attachment\/refernece\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/refernece.png\" data-orig-size=\"728,304\" 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=\"refernece\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/refernece-300x125.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/refernece-600x251.png\" class=\"size-medium wp-image-22549 aligncenter\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/refernece-300x125.png\" alt=\"\" width=\"300\" height=\"125\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/refernece-300x125.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/refernece-600x251.png 600w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/refernece.png 728w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Here I enter the API call I want to break and it will take me into the library code, I can set a breakpoint here and click run until I hit this breakpoint, then follow until the return of this function which will bring me to the point in this unpacked file now so we can debug.<\/p>\n<p>If we continue on and break at the <strong>call esi<\/strong>\u00a0where we were at before, we see that it is cryptHashData, so, we have lpString being hashed and the output hash of that string being used for input in generating AES key.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22550\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/attachment\/crypt_hash_data-2\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/crypt_hash_data.png\" data-orig-size=\"627,352\" 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=\"crypt_hash_data\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/crypt_hash_data-300x168.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/crypt_hash_data-600x337.png\" class=\"size-full wp-image-22550 aligncenter\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/crypt_hash_data.png\" alt=\"\" width=\"627\" height=\"352\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/crypt_hash_data.png 627w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/crypt_hash_data-300x168.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/crypt_hash_data-600x337.png 600w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/crypt_hash_data-400x225.png 400w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/p>\n<h5>Static<\/h5>\n<p>If we trace the <strong>lpString<\/strong> to where it comes from in IDA, we see that it is the parameter <strong>arg_0<\/strong> of this function, and it also looks to be a randomly generated string.<\/p>\n<p>Going out of this function one level, we see this line:<\/p>\n<p><em>10008B11 lea ecx, [esp+98h+<strong>var_54<\/strong>]<\/em><br \/> <em>10008B15 call sub_10007980<\/em><\/p>\n<p>The <strong>var_54<\/strong> is the argument which was being used in the function we just looked at. It is the string who gets hashed and used for AES key. Following it backward, I am trying to see where this variable might get populated. I can set a breakpoint on memory write for this address in olly, but also, in this case, it is easy enough to find by just looking in a few of the functions which use it as a parameter earlier.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22551\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/attachment\/rndgenfunc\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RndGenFunc.png\" data-orig-size=\"1642,722\" 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=\"RndGenFunc\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RndGenFunc-300x132.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RndGenFunc-600x264.png\" class=\"size-full wp-image-22551 aligncenter\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RndGenFunc.png\" alt=\"\" width=\"1642\" height=\"722\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RndGenFunc.png 1642w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RndGenFunc-300x132.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RndGenFunc-600x264.png 600w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RndGenFunc-195x85.png 195w\" sizes=\"auto, (max-width: 1642px) 100vw, 1642px\" \/><\/p>\n<p>I come across this function which is highlighted. I have commented it as RandomGenFunc. It takes as a parameter in <strong>edx<\/strong>, the variable which was used below to generate the key. So hopefully this is where the variable gets populated and the random password string gets created.<\/p>\n<p>We see off the bat, that it uses srand() with a seed value of time64(). I will include the description of what exactly time64 to make sure there is no confusion.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22557\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/attachment\/timefunc\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/timeFunc.png\" data-orig-size=\"2040,512\" 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=\"timeFunc\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/timeFunc-300x75.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/timeFunc-600x151.png\" class=\"size-full wp-image-22557 aligncenter\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/timeFunc.png\" alt=\"\" width=\"2040\" height=\"512\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/timeFunc.png 2040w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/timeFunc-300x75.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/timeFunc-600x151.png 600w\" sizes=\"auto, (max-width: 2040px) 100vw, 2040px\" \/><\/p>\n<p>If we look around in various sub-functions, we see this behavior being used in a loop.\u00a0srand() is called again with a seed of time64() and then in a loop,\u00a0rand is being called to generate a new number.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22552\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/attachment\/randomseed\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RandomSeed.png\" data-orig-size=\"1994,1154\" 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=\"RandomSeed\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RandomSeed-300x174.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RandomSeed-600x347.png\" class=\"size-full wp-image-22552 aligncenter\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RandomSeed.png\" alt=\"\" width=\"1994\" height=\"1154\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RandomSeed.png 1994w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RandomSeed-300x174.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/RandomSeed-600x347.png 600w\" sizes=\"auto, (max-width: 1994px) 100vw, 1994px\" \/><\/p>\n<p>What you are seeing here is the seeding of a number with the current time, and if you recall the functionality of srand() and rand(), the seed parameter is what determines the set of random numbers that each call to rand() will output. So, if the seed is the same, the sequence will also be the same. <strong>THIS IS THE KEY WEAKNESS FOR US TO EXPLOIT<\/strong>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22554\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/attachment\/stringbuildloop-2\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/stringBuildLoop-1.png\" data-orig-size=\"2176,1126\" 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=\"stringBuildLoop\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/stringBuildLoop-1-300x155.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/stringBuildLoop-1-600x310.png\" class=\"size-full wp-image-22554 aligncenter\" style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/stringBuildLoop-1.png\" alt=\"\" width=\"2176\" height=\"1126\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/stringBuildLoop-1.png 2176w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/stringBuildLoop-1-300x155.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/stringBuildLoop-1-600x310.png 600w\" sizes=\"auto, (max-width: 2176px) 100vw, 2176px\" \/><\/p>\n<p>As you can see, a random charset &#8220;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabc&#8230;&#8221; being used and inside of a loop, rand() is being called over and over again. A string is being built by taking that position as a substring of the charset,\u00a0char by char.<\/p>\n<p>So, now we have our starting point. We know that the AES key is being generated by a random string who is being created by a seed of current time. If we can find the current time which was used during the encryption, we know that we can reproduce the AES key. This can almost definitely be brute forced within a reasonable amount of time.<\/p>\n<p>If you recall the <a href=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-how-to-break-encryption\/\" target=\"_blank\" rel=\"noopener\">previous lesson<\/a>,\u00a0 <strong>we talked about almost exactly this scenario.\u00a0<\/strong>Please read that lesson if you have not done so already.<\/p>\n<p>&nbsp;<\/p>\n<h3>Overview of encryption code weakness<\/h3>\n<p>For now, our analysis phase is basically complete. We did not see any more calls to rand() or srand() aside from the one in the loop we spoke of in function\u00a010001CC0, so we can focus on those. As we can see, this function is being called 3 separate times.<\/p>\n<p>This makes sense because there are 3 sets of random strings being generated in this malware.<\/p>\n<ol>\n<li>AES key<\/li>\n<li>Victim ID<\/li>\n<li>File Extension<\/li>\n<\/ol>\n<p>The first call is for the loop which is generating the random string, used as a key. We can confirm this by debugging and seeing this memory region being passed into the function who generates AES key. The second call, we can confirm is the victim ID. We can verify this because the parameter passed in through edx for the length is 0x0C, or 12.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22555\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/attachment\/12len\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/12Len.png\" data-orig-size=\"1578,174\" 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=\"12Len\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/12Len-300x33.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/12Len-600x66.png\" class=\"size-full wp-image-22555 aligncenter\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/12Len.png\" alt=\"\" width=\"1578\" height=\"174\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/12Len.png 1578w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/12Len-300x33.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/12Len-600x66.png 600w\" sizes=\"auto, (max-width: 1578px) 100vw, 1578px\" \/><\/p>\n<p>If we look at the number and compare it to the website presented from the ransomware, we see that this for the number that gets generated for the victim ID is also 12 in length.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"22556\" data-permalink=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/attachment\/ransom_note-1\/\" data-orig-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/ransom_note-1.png\" data-orig-size=\"808,564\" 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=\"ransom_note-1\" data-image-description=\"\" data-medium-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/ransom_note-1-300x209.png\" data-large-file=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/ransom_note-1-600x419.png\" class=\"size-full wp-image-22556 aligncenter\" src=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/ransom_note-1.png\" alt=\"\" width=\"808\" height=\"564\" srcset=\"https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/ransom_note-1.png 808w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/ransom_note-1-300x209.png 300w, https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/ransom_note-1-600x419.png 600w\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" \/><\/p>\n<p>Which leaves the last call to this random string generator function to be the file extension.<\/p>\n<p>The difference in these 3 sets of random number seeds are close by in code so it can be assumed that they might be only a few seconds apart in execution.<\/p>\n<p>&nbsp;<\/p>\n<h3>Understanding the weakness<\/h3>\n<p>What all this means to us is that our saving grace here is the fact that the ransomware author used the same random number generating logic for the file extension and victim ID as they did the AES key. If this was not the case, (if they used a constant static extension and something else for victim ID) we would have nothing to go off of.<\/p>\n<p>I&#8217;ll explain what I mean here. It is not feasible to generate AES keys and test encrypt a file for thousands of iterations (potential seed seconds). The computation time is too great. We must find the string match for the victim ID, which is a relatively fast computational operation, and then we can begin generating test AES keys incrementally at that point because we then know that there will only be no more than 100 seconds of difference between the time that the victim ID was generated and the AES key. Generating an AES key 100 times is doable and reasonable.<\/p>\n<p>If the victim ID and extension were not also random strings, the only alternative would be that we might be able to decrypt the files only if we had a roughly accurate time of infection, which would work quite well too.<\/p>\n<p>We could then do exactly as I mentioned in the example from the previous blog part, and do a very targeted seconds seed. But this would require that every victim would have to be able to figure out roughly within a minute or two when they were infected. This is a perfectly good solution for a company building a decryptor after being hit by ransomware, but for building a generic tool that can work on any victim easily, hasherezade&#8217;s method is much better.<\/p>\n<p>So back to the situation at hand, if we can find the seed value (calendar seconds) used for generating the file extension or the victim ID, we can cycle back the counter from those seed values, and within a reasonably short range, we will hit the seed value used for generating the encryption key. Again, the reason this is true is because the time64() srand() was called first for the AES key, and then soon after called for the other two.<\/p>\n<p>So where does this leave us? We must now figure out a way to find the exact second that the victim ID or extension was generated.<\/p>\n<p>&nbsp;<\/p>\n<h3>Decryption algorithm steps<\/h3>\n<p>As the final part to the brainstorming of the algorithm, I will list out, step by step, what we want the algorithm to do and why.<\/p>\n<ol>\n<li>Create a seed value in calendar seconds, and call srand() with it.<\/li>\n<li>From the user, request either the victim ID from the ransom note, the encrypted file extension, or both.<\/li>\n<li>Start incrementally counting down in seconds from now calling srand()\n<ul>\n<li>For each srand() iteration, replicate the same rand() calls\/substring charset lookup as the ransomware.<\/li>\n<li>Generate a 12 character long string and compare it to the user provided victim ID<\/li>\n<li>If no match, decrement the calendar time variable and try again.<\/li>\n<li>If matched, this means we have found the second which the victim ID was seeded. Thus, we are most likely very close in the seed time for the AES key. lets set a test limit of 100 seconds of iteration.<br \/> (We know that the AES key was generated most likely within a few seconds before the victim ID)<\/p>\n<ul>\n<li>We decrement the time pointer now, but for each iteration, we are actually generating a password for the AES key, generating the actual test AES key, and performing an AES decryption of an encrypted file using that key,\u00a0 then comparing the output to a known original version of that file.<\/li>\n<li>If it matches,\u00a0 then it is highly likely we have found the key and we can apply it to the rest of the file on the hard disk.<\/li>\n<li>If it does not match, that might mean that there just happened to be a massive coincidence in that the seed generated a duplicate sequence of numbers. So, we go back and start the process over from step 3 -&gt; 1 continuing down in seconds.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>Some things worth mentioning here about this is that we are counting down in seconds from the time our decryption tool is run until the time the victim files were encrypted, the longer time passes, the longer the tool will take.<\/p>\n<p>But again, it is worth noting that the operation of generating a 12 character random string and doing a string comparison to victim ID is very fast, which is why it is not such a horrible thing to say that we might iterate 500,000 times because this comparison can be done 100&#8217;s if not 1000&#8217;s of times per second. So, even if the file was encrypted a month ago, it wouldn&#8217;t be an unreasonable wait time.<\/p>\n<p>Once it is matched, the computationally intensive AES generation and decryption operations are only performed 100 times or less. This is how we can keep decryption key search time closer to a constant.<\/p>\n<p>&nbsp;<\/p>\n<h3>Conclusion<\/h3>\n<p>In this, part 4 of the series, we now have a solid understanding of how the encryption code works for this sample and more importantly, what is wrong with the encryption scheme that allows us to exploit it. We walked through the thought process of reversing the decryption to the point of looking for weaknesses, and we now also have a game plan for building the decryption tool.<\/p>\n<p>Where do we go from here? Tune in for part 5, the final part of the encryption 101 series where we will put it all together and walk through the source code of the completed decryption tool in detail.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/\">Encryption 101: Decryptor&#8217;s thought process<\/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\/03\/encryption-101-decryptors-thought-process\/\" target=\"bwo\" >https:\/\/blog.malwarebytes.com\/feed\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p><strong>Credit to Author: Vasilios Hioureas| Date: Tue, 27 Mar 2018 15:00:00 +0000<\/strong><\/p>\n<table cellpadding='10'>\n<tr>\n<td valign='top' align='center'><a href='https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/' title='Encryption 101: Decryptor's thought process'><img src='https:\/\/blog.malwarebytes.com\/wp-content\/uploads\/2018\/03\/shutterstock_722763646.jpg' border='0'  width='300px'  \/><\/a><\/td>\n<\/tr>\n<tr>\n<td valign='top' align='left'>In the previous parts 1, 2 and 3 of this series, we covered the basics of encryption, walked through a live example of a ransomware in detail, and talked about encryption weaknesses. In this part of the encryption 101 series, we will begin wrapping it up by going into detail on a ransomware with weak&#8230;<\/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\/break-encryption\/\" rel=\"tag\">break encryption<\/a><a href=\"https:\/\/blog.malwarebytes.com\/tag\/decryptor\/\" rel=\"tag\">decryptor<\/a><a href=\"https:\/\/blog.malwarebytes.com\/tag\/encrpytion-functionality\/\" rel=\"tag\">encrpytion functionality<\/a><a href=\"https:\/\/blog.malwarebytes.com\/tag\/encryption\/\" rel=\"tag\">encryption<\/a><a href=\"https:\/\/blog.malwarebytes.com\/tag\/princesslocker\/\" rel=\"tag\">PrincessLocker<\/a><a href=\"https:\/\/blog.malwarebytes.com\/tag\/ransomware\/\" rel=\"tag\">ransomware<\/a><\/p>\n<table width='100%'>\n<tr>\n<td align=right>\n<p><b>(<a href='https:\/\/blog.malwarebytes.com\/threat-analysis\/2018\/03\/encryption-101-decryptors-thought-process\/' title='Encryption 101: Decryptor's thought process'>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\/03\/encryption-101-decryptors-thought-process\/\">Encryption 101: Decryptor&#8217;s thought process<\/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":[17709,10491,17919,10439,3764,14361,3765,10494],"class_list":["post-11864","post","type-post","status-publish","format-standard","hentry","category-malwarebytes","category-security","tag-break-encryption","tag-decryptor","tag-encrpytion-functionality","tag-encryption","tag-malware","tag-princesslocker","tag-ransomware","tag-threat-analysis"],"_links":{"self":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts\/11864","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=11864"}],"version-history":[{"count":0,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts\/11864\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/media?parent=11864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/categories?post=11864"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/tags?post=11864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}