Sunday, March 19, 2017

Hardened Attacks: Surviving OS rewrites using NVRAM storage

Reading through the recent WikiLeaks tool dump has yielded a great deal of Post Exploitation material. While the drama continues to unfold around the exploit portion of the dump I wanted to dive right in to what already known. A lot of the tools and methods were known publicly already. However just because something is old does not mean it is useless. The one thing that was of particular interest outside of the better known methods was the ability to survive OS rewrites on UEFI-enabled computers. This is one of the highest aspirations for any Malware. Let's take a look at how this can be achieved using Hardware level persistent storage known as Non-Volatile RAM (NVRAM).

To be clear, this method isn't entirely new either. Similar methods were revealed as part of the Hacking Team tool dump a couple of years ago (http://www.intelsecurity.com/advanced-threat-research/ht_uefi_rootkit.html_7142015.html). However it is not as common as some of the other persistence menthods (Such as Run Keys or Scheduled Tasks).
The key to this method is the fact that, on boot, your Operating system needs to be able to read certain types of information from the Hardware. This requires the use of the firmware level. The particular firmware spec this targets is called the Unified Extensible Firmware Interface (UEFI). It is a specification that defines a software interface between an operating system and platform firmware. The documents describe it as such:
"NVRAM is non-volatile RAM that is used in EFI to store variables that need to persist between boots. Many of these NVRAM variables are architecturally defined, and setting invalid options to NVRAM could cause a machine to not be able to boot." (https://wikileaks.org/ciav7p1/cms/page_26968084.html)
I bolded the section on invalid options because I think it is important to note before going further....if you brick a device while trying to do any of these tests, I am not responsible. Only go through this if you already know what you are doing (or you are comfortable losing the system you are messing with.) With that in mind I highly recommend you go read the details on the spec at http://uefi.org/uefi

The Vault 7 documentation focuses on the potential use of the NVRAM as covert storage on Windows systems. So, like the articles, I will focus on the Windows.

Prerequisites

As I mentioned this is a method in the Post Exploitation phase, so we assume that the machine has already been compromised and that some basic form of persistent C2 has already been established (remember 2 is 1, 1 is none). This is just good practice, though. The only formal prerequisite is that you be operating in the context of a SYSTEM account with the SE_SYSTEM_ENVIRONMENT_NAME privilege. You can find out more about this privilege (and how to see if you have it) here https://msdn.microsoft.com/en-us/library/windows/desktop/bb530716(v=vs.85).aspx  


Setting Contents

The Windows API makes it easy to set this type of storage through one of two calls. The one of most use is SetFirmwareEnvironmentVariableEx, the structure of which is:
 
BOOL WINBASEAPI SetFirmwareEnvironmentVariableEx(
  _In_ LPCTSTR lpName,
  _In_ LPCTSTR lpGuid,
  _In_ PVOID   pValue,
  _In_ DWORD   nSize,
  _In_ DWORD   dwAttributes
);
More information can be found here https://msdn.microsoft.com/en-us/library/windows/desktop/jj204594(v=vs.85).aspx but a couple points to note are the lpName and lpGuid must not be NULL pointers, even though some systems may not actually support the GUID lookup. According to the docuemntation The GUID must be a string in the format "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" e.g. 8BE4DF61-93CA-11D2-AA0D-00E098032B8C which is defined as the GUID for the BootOrder variable. As mentioned previously, several of these are set aside by the specification, and then more yet are reserved for vendor-specific uses. As noted in the documents:
"NVRAM variables are a combination of a GUID of the variable owner, a name, and a value....These variables present interesting opportunities for our tools since they will survive a OS reinstall and are invisible to a forensic image of the hard drive.  What's also interesting is that there is no way to enumerate NVRAM variables from the OS...you have to know the exact GUID and name of the variable to even determine that it exists. The variables are part of the BIOS image mapped into kernel memory, so it is possible to enumerate them from the kernel if a PSP knew where to look."
The flow of such a persistence method would look something like this:
As mentioned before this isn't a new technique. Several relevant talks have been given in the recent past. At BH-EU Corey Kallenberg, Xeno Kovah, John Butterworth, and Sam Cornwell presented their analysis of the UEFI standard from an attacker's and defender's perspective https://www.blackhat.com/docs/eu-14/materials/eu-14-Kovah-Analyzing-UEFI-BIOSes-From-Attacker-And-Defender-Viewpoints.pdf The same group of authors presented a talk at BH-US on how to use this method for persistence on Windows https://www.blackhat.com/docs/us-14/materials/us-14-Kallenberg-Extreme-Privilege-Escalation-On-Windows8-UEFI-Systems-WP.pdf

Other OS families

Just because I focused on Windows doesn't mean Linux or OSX Users are in the clear. They are each discussed in turn in the Vault 7 Documentation of UEFI basics as well https://wikileaks.org/ciav7p1/cms/page_26968084.html
Each presents it's own uniques pros and cons to the mix as well. 

Mitigation

Setting aside the obvious blocking of initial exploitation vectors for a moment, I want to look at the detection and mitigation of this type of implant. First, as noted in the previous quote, there is no way to enumerate these variables from User land. The notes do say:
"The variables are part of the BIOS image mapped into kernel memory, so it is possible to enumerate them from the kernel if a PSP knew where to look."
The "place to look" is called the GUID Partition Table (GPT). As usual if you want more info on this, go to the docs https://support.microsoft.com/en-us/help/302873/frequently-asked-questions-about-the-guid-partitioning-table-disk-architecture. So, it may very well be possible to script the process of discovering suspicious NVRAM content. Regardless it would be impossible to completely reconstruct this from a forensic image of the machine (assuming the original machine were otherwise unobtainable).

Probably of more use is when you can physically access the potentially infected machine:
"If an adversary had physical access to the computer, then reading the NVRAM variables would be much easier.  They could turn off secure boot in the UEFI menu and boot to a UEFI shell, which has a command to list all NVRAM variables."
The command mentioned is the UEFI shell command dmpstore. Here is the usage specification from the UEFI Shell 2.0 documentation:

http://www.uefi.org/sites/default/files/resources/UEFI_Shell_Spec_2_0_Errata_A.pdf

No comments:

Post a Comment