{"id":8273,"date":"2017-07-08T04:40:04","date_gmt":"2017-07-08T12:40:04","guid":{"rendered":"http:\/\/www.palada.net\/index.php\/2017\/07\/08\/news-2047\/"},"modified":"2017-07-08T04:40:04","modified_gmt":"2017-07-08T12:40:04","slug":"news-2047","status":"publish","type":"post","link":"http:\/\/www.palada.net\/index.php\/2017\/07\/08\/news-2047\/","title":{"rendered":"Petya&#039;s Master Boot Record Infection"},"content":{"rendered":"<p><strong>Credit to Author: Gabriel Hung and Margarette Joven| Date: Sat, 08 Jul 2017 12:00:00 +0000<\/strong><\/p>\n<div class=\"entry\">\n<p>Last week we started our <a href=\"http:\/\/blog.fortinet.com\/2017\/06\/28\/a-technical-analysis-of-the-petya-ransomworm\">technical analysis<\/a> on Petya (also called NotPetya) and its so-called &ldquo;killswitch.&rdquo; In that blog post we mentioned that Petya looks for a file in the Windows folder that has the same filename (no extension) as itself (for example: <em>C:WindowsPetya<\/em>). If it exists, it terminates by calling <em>ExitProcess<\/em>. If it doesn&#39;t exist, it creates a file with the attribute DELETE_ON_CLOSE. This seems to imply that instead of a killswitch, this file is meant to be a marker to check and see if the system has already been infected.<\/p>\n<p>After this file is created, Petya proceeds to perform its disk accessing functions.<\/p>\n<p>Petya first opens the C: volume and calls the DeviceIoControl API with the IOCTL_DISK_GET_DRIVE_GEOMETRY control code, which retrieves information about the physical disk&rsquo;s geometry &#8211; specifically the number of bytes per sector. It allocates fixed memory by calling LocalAlloc, then overwrites the second sector of the C: volume with the newly allocated bytes. This is where the volume boot record runs the bootloader (NTLDR), at least in Windows XP systems, which will corrupt the boot sequence.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/PetyaMBR001.png\" style=\"width: 1068px; height: 520px;\" \/><\/p>\n<p align=\"center\"><em>Figure 1. The 2<sup>nd<\/sup> sector, before and after being overwritten<\/em><\/p>\n<p>Next, it checks its process flag to determine whether to overwrite the MBR with its code, or to just simply corrupt it. This process flag was set earlier when it searched for certain processes in memory. If this flag indicates that the process &ldquo;avp.exe&rdquo; is found, it proceeds to the function that we call <em>corrupt_mbr<\/em>. If its process flag indicates that the process &ldquo;avp.exe&rdquo; is not found, it enters the function that we call <em>overwrite_mbr_func<\/em>, which replaces the MBR.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/PetyaMBR003.png\" \/><\/p>\n<p align=\"center\"><em>Figure 2. Checking the process flag<\/em><\/p>\n<p>The function <em>corrupt_mbr<\/em>, seen in Figure 2 above, just writes bytes from uninitialized memory (0xBAADF00D) to the first 10 sectors of the disk. This effectively renders the disk unbootable.<\/p>\n<p>The other function, <em>overwrite_mbr_func<\/em>, is where the malware attempts to overwrite the MBR with its own code.<\/p>\n<p>Petya begins by calling the <em>CreateFileA<\/em> API with the filename <em>\\.PhysicalDrive0<\/em>, which corresponds to the Master Boot Record. It then calls <em>DeviceIoControl<\/em> with the IOCTL_DISK_GET_PARTITION_INFO_EX control code to retrieve extended information about the type, size, and nature of the MBR partition. It uses this information to check if the <em>PartitionStyle<\/em> member of the PARTITION_INFORMATION_EX structure is indeed an MBR.<\/p>\n<p>Petya then generates 60 (0x3C) random bytes using the <em>CryptGenRandom<\/em> API. Using an index and these 60 random bytes, it generates a personal installation key.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/PetyaMBR005.png\" style=\"width: 1226px; height: 408px;\" \/><\/p>\n<p align=\"center\"><em>Figure 3: Indexing function to generate the personal installation key<\/em><\/p>\n<p>Note that this generated key is what is displayed to the user as part of the ransom message.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/PetyaMBR007.png\" style=\"width: 729px; height: 480px;\" \/><\/p>\n<p align=\"center\"><em>Figure 4. Generated personal installation key<\/em><\/p>\n<p>Next, the malware reads 0x200 bytes from the first sector of the physical drive, and XORs the bytes by 7. This is stored later back into the disk at Sector 34. To check if the disk is big enough to store its malicious boot loader, it reads the last partition&rsquo;s LBA value (which gives the position of the cylinder, head, and sector of the disk) making sure it is greater than 0x28.<\/p>\n<p>In the next few steps, Petya replaces the various sectors of the physical drive:<\/p>\n<p>&#8211; Sectors 0-18 are replaced with its own malicious MBR code, making sure to keep the original disk signature and partition entries<\/p>\n<p>&#8211; Sector 32 is replaced with the Salsa20 key\/nonce, personal installation key, and bitcoin address (shown in Figure 5, below)<\/p>\n<p>&#8211; Sector 33 is filled with &lsquo;07&rsquo; hex bytes<\/p>\n<p>&#8211; Sector 34 is filled with the original MBR first sector, XORed by 7<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/PetyaMBR009.png\" style=\"width: 604px; height: 210px;\" \/><\/p>\n<p align=\"center\">\u2028<em>Figure 5: Bytes written to Sector 33<\/em><\/p>\n<p>If any step of the overwriting process fails, Petya retaliates by calling its <em>corrupt_mbr<\/em> function (see Figure 2). As we mentioned, this function corrupts the first 10 sectors of the disk, making it unbootable.<\/p>\n<p>The news on Petya may have slowed down for now, but this does not mean that we can be complacent with the security of our systems. As this recent outbreak has shown, many systems remained unpatched even after the damage created by WannaCry was made known all around the world. After this latest outbreak, there really is no excuse for not applying available patches. But let&#39;s not stop there. A good defense system (as outlined in the blog post <a href=\"http:\/\/blog.fortinet.com\/2017\/07\/06\/wannacry-and-petya-the-circus-comes-to-town\">here<\/a>) can greatly reduce the points of entry that new malware may take.<\/p>\n<p style=\"margin-left:.5in;\"><strong>MD5:<\/strong> 71b6a493388e7d0b40c83ce903bc6b04<\/p>\n<h2>Fortinet protections<\/h2>\n<p style=\"margin-left:.5in;\"><strong>AV Signature:<\/strong><\/p>\n<p style=\"margin-left:1.0in;\">W32\/Petya.EOB!tr<\/p>\n<p style=\"margin-left:1.0in;\">W32\/Agent.YXH!tr<\/p>\n<p style=\"margin-left:1.0in;\">Other signatures are being investigated.<\/p>\n<p style=\"margin-left:.5in;\"><strong>IPS Signature:<\/strong><\/p>\n<p style=\"margin-left:1.0in;\">MS.Office.RTF.File.OLE.autolink.Code.Execution<\/p>\n<p style=\"margin-left:1.0in;\">MS.SMB.Server.SMB1.Trans2.Secondary.Handling.Code.Execution<\/p>\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"width:254px;\" width=\"190\">\n<tbody>\n<tr>\n<td style=\"width:123px;\">\n<p>Created<\/p>\n<\/td>\n<td>\n<p>Mar 14, 2017<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width:123px;\">\n<p>Last Updated<\/p>\n<\/td>\n<td>\n<p>Jun 05, 2017<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"margin-left:1.0in;\"><em>In addition, Fortinet&rsquo;s WannaCry IPS rules appear to protect against exploits targeting thse vulnerabilities. Fortinet teams are verifying this claim. <\/em><\/p>\n<p style=\"margin-left:.5in;\"><strong>Sandbox Detection:<\/strong><\/p>\n<p style=\"margin-left:.5in;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Fortinet Sandbox (FSA) detects this attack.<\/p>\n<p style=\"margin-left:.5in;\"><strong>TOR Communications:<\/strong><\/p>\n<p style=\"margin-left:1.0in;\">Block TOR Outbound traffic via AppControl signatures.<\/p>\n<h2>Critical Microsoft updates for Petya<\/h2>\n<p style=\"margin-left:.5in;\"><a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/security\/ms17-010.aspx\">Security Update for Microsoft Windows SMB Server: March 14, 2017<\/a><\/p>\n<p style=\"margin-left:.5in;\"><a href=\"https:\/\/support.microsoft.com\/en-us\/help\/3178703\/description-of-the-security-update-for-office-2016-april-11-2017\">Security update for Office 2016: April 11, 2017<\/a><\/p>\n<p>&nbsp;<\/p>\n<\/div<br \/><a href=\"https:\/\/blog.fortinet.com\/2017\/07\/08\/petya-s-master-boot-record-infection\" target=\"bwo\" >https:\/\/blog.fortinet.com\/feed<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/PetyaMBR001.png\"\/><\/p>\n<p><strong>Credit to Author: Gabriel Hung and Margarette Joven| Date: Sat, 08 Jul 2017 12:00:00 +0000<\/strong><\/p>\n<p>Last week we started our technical analysis on Petya (also called NotPetya) and its so-called \u201ckillswitch.\u201d In that blog post we mentioned that Petya looks for a file in the Windows folder that has the same filename (no extension) as itself (for example: C:WindowsPetya). If it exists, it terminates by calling ExitProcess. If it doesn&#039;t exist, it creates a file with the attribute DELETE_ON_CLOSE. This seems to imply that instead of a killswitch, this file is meant to be a marker to check and see if the system has already been infected.    After&#8230;<\/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":[10424,10378],"tags":[],"class_list":["post-8273","post","type-post","status-publish","format-standard","hentry","category-fortinet","category-security"],"_links":{"self":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts\/8273","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=8273"}],"version-history":[{"count":0,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts\/8273\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/media?parent=8273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/categories?post=8273"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/tags?post=8273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}