Persisting in the boot chain
MBR Infection: Replaced Boot Code, Traces and Offline Checks
How bootkits replace the master boot code in sector 0 to run before Windows, what TDL4, Gapz, ESPecter and FinSpy left on disk, and how to check it offline.
- Goal
- Persisting in the boot chain
- Layer
- Boot
- Where it acts
- MBROutside the partitionsOn the boot chain map
- Platforms
- Windows
- ATT&CK
- T1542.003
- Also known as
- MBR infection, MBR bootkit
- Checked by
- Disk boot sectors
Overview
On a computer that starts through the legacy BIOS, the firmware reads the first sector of the boot disk, the master boot record (MBR), and runs the code at its start. That master boot code, as Microsoft describes it, finds the active partition, loads its boot sector and hands over to it. An MBR bootkit replaces this code with its own, which then runs before the Windows boot manager, the kernel and any security product, and finally hands control to a copy of the original code so the machine boots as usual.
The technique is old. ESET's Virus Bulletin 2014 paper traces modern bootkits to eEye's BootRoot proof of concept (2005) and Mebroot (2007), the first MBR bootkit it lists in the wild. The same paper links their return to the kernel-mode code signing policy Windows Vista enforced on 64-bit systems from 2007: code that runs before Windows is not subject to it. In this catalogue, TDL4, the MBR variant of Gapz, the legacy version of ESPecter and the FinSpy bootkit for machines without UEFI all replace the MBR code.
It only works where MBR code runs. On a UEFI machine booting from a GPT disk, sector 0 holds a protective MBR that, in Microsoft's words, "is ignored by EFI; no MBR code is run"; ESPecter and FinSpy reach those machines through the EFI system partition instead (see boot manager replacement). Not every overwritten MBR is a bootkit: Microsoft reported WhisperGate overwriting it with a ransom note in 2022.
How it works
The MBR is one 512-byte sector. Microsoft's reference gives its layout:
| Offset | Size | Content | What a bootkit does with it |
|---|---|---|---|
0x000 | 440 bytes | Master boot code | Replaced with the bootkit's loader |
0x1B8 | 4 bytes | Disk signature, which Windows uses to identify the disk and its drive letters | Kept |
0x1BE | 64 bytes | Partition table: four 16-byte entries, 0x80 marks the active one | Usually kept; one TDL4 variant edits it instead of the code (see VBR and IPL infection) |
0x1FE | 2 bytes | 55 AA end-of-sector marker | Kept |
The infected MBR is only the first link of a chain:
- Load the next stage from sectors no file system covers, after the MBR or at the end of the disk (see hidden storage).
- Hook the BIOS disk service, interrupt 13h, to patch the boot manager and loader in memory as they are read (see int 13h hooking).
- Run the original MBR, kept elsewhere on the disk, so Windows starts normally.
- Load kernel code without the kernel's signature checks (see turning off driver signing from the boot chain).
Writing sector 0 needs administrator rights, and nothing more: Windows restricts direct writes inside mounted volumes, but lets software write sectors that "do not fall within a volume", as partitioning tools must. That is why ESET's advice after ESPecter includes restricting the privileged accounts bootkit installation needs.
How families use it
| Family | What the infected MBR does | Where the rest lives | Source |
|---|---|---|---|
| TDL4 | Finds the ldr16 component in the rootkit's encrypted store, loads it and passes control; ldr16 hooks int 13h and restores the original MBR, which is kept in the store | Encrypted file system in the last sectors of the disk | Kaspersky, 2011 |
| Gapz (MBR variant, Gapz.C) | Restores the original code in memory, reads the next-stage code from disk and passes control to it | Before the first partition or after the last one | ESET, 2013 |
| ESPecter (legacy version) | Hooks int 13h and decrypts code from sectors 2 to 4 | Original MBR in sector 1, configuration in sector 5, driver from sector 6 | ESET, 2021 |
| FinSpy (machines without UEFI) | Copies the first loader stage into memory, hooks int 13h and 15h, launches the original MBR | The last megabyte of the disk | Kaspersky, 2021 |
ESET's paper calls the bootkits that infect the volume boot record instead, such as Rovnix and the later Gapz, the more sophisticated and stealthier ones.
Traces it leaves
| Where | Trace | Notes |
|---|---|---|
| Sector 0, bytes 0 to 439 | Code that matches no known loader. The Windows MBR carries the messages "Invalid partition table", "Error loading operating system" and "Missing operating system" | Other boot managers and disk encryption products replace this code legitimately |
| Sectors after the MBR | Non-zero, often high-entropy data; a copy of the original MBR (ESPecter puts it in sector 1) | Windows leaves this gap empty; GRUB and some encryption products use it |
| End of the disk | An encrypted store (TDL4) or loader (FinSpy) after the last partition | Only a full image, or a copy of the disk's tail, shows it |
| Windows volume | Usually nothing: no driver file, no service key | The kernel code comes from raw sectors |
| TPM, on BIOS machines with BitLocker | A changed PCR 4 (MBR code) and PCR 5 (partition table) | Both are in BitLocker's default BIOS profile |
How to check a host
Can it apply? msinfo32 shows "BIOS Mode"; bcdedit /enum "{current}" shows winload.exe for a BIOS boot and winload.efi for UEFI; Get-Disk | Select-Object Number, PartitionStyle shows MBR or GPT.
Read the disk from outside the infected system. TDL4, Gapz and Rovnix filter disk reads on the running system and return the original sectors (see disk stack hooking), and booting a recovery environment from the same disk runs the infected MBR first. Boot from trusted external media, or attach the disk to another machine. From a Linux boot medium, with /dev/sda the system disk, these commands save the first 4 MiB and the last MiB under the names the checker expects:
D=/dev/sda
N=$(sudo blockdev --getsz "$D")
sudo dd if="$D" of=disk0.img bs=512 count=8192
sudo dd if="$D" of=disk0.tail-$(( (N - 2048) * 512 )).img bs=512 skip=$((N - 2048)) count=2048
A full image is better: it keeps everything between those two regions for later analysis.
Compare the boot code. Hash the first 440 bytes and compare them with a clean disk installed by the same Windows version, and read the strings of the sector:
dd if=disk0.img bs=440 count=1 2>/dev/null | sha256sum
dd if=disk0.img bs=512 count=1 2>/dev/null | strings -n 8
mmls disk0.img
dd if=disk0.img bs=512 skip=1 count=2047 2>/dev/null | gzip -c | wc -c
mmls (The Sleuth Kit) lists the partitions and the unallocated ranges. The last command shows how well the gap after the MBR compresses: a few kilobytes when it is empty, close to 1 MiB when it holds encrypted data. On disks partitioned before Windows Vista, the first partition usually starts at sector 63 rather than 2048, so use count=62.
Live hints are weak. A live read of sector 0 that differs from the offline image is itself evidence of filtering. On a BIOS machine with BitLocker and a TPM, an unexplained recovery prompt, or protection found suspended (manage-bde -status), deserves a raw disk check.
What the checker looks for
The bootsector module reads MBR disk images and disk heads. For this technique it raises:
- "MBR boot code does not match a known loader" (medium) when the first 440 bytes match none of the loaders it recognises: Windows, GRUB, Syslinux, ISOLINUX, LILO, GRUB4DOS and VeraCrypt. It names no family: the code is unknown, not identified.
- "Data before the first partition does not match a known loader" for non-zero data between the MBR and the first partition (medium when it is high-entropy), where ESPecter keeps its code, configuration and driver. GRUB's
core.imgis recognised there and left out. - "Copy of a boot sector before the first partition" for another sector in that gap that ends in
55 AAand carries a partition table, such as the original MBR an infected one chains to. - "Data after the last partition" when the image includes the end of the disk, where TDL4 and FinSpy keep their stages. A disk head alone does not reach it.
It cannot see what the code does or anything in memory, and it reads only the first 4 MiB and the last 1 MiB of large images. A disk head copied through the infected Windows shows the clean MBR the rootkit returns, as the module's own note warns. measuredboot does not compare the MBR measurements a BIOS with a TPM records, and esp does not apply. The sample walk-through reads a disk head with unknown MBR code; what an offline bootkit check can and cannot prove covers the limits.
Mitigation and recovery
Prevention.
- Boot through UEFI from a GPT disk with Secure Boot on, and turn off the firmware's legacy (CSM) boot where it offers one. Without Secure Boot, Microsoft notes, a PC runs whatever boot loader is on its disk. Microsoft's MBR2GPT converts a clean system disk; it is not a cleanup tool.
- On remaining BIOS machines, BitLocker with a TPM covers the MBR code (PCR 4) and partition table (PCR 5) by default, so a changed MBR triggers recovery. Treat unexplained recovery as a lead.
- Limit administrator rights: writing sector 0 needs them.
Recovery.
- Preserve a full image, or at least the first 4 MiB and the last MiB, taken from a clean environment.
bootrec /fixmbrfrom installation media rewrites the master boot code and, as Microsoft notes, leaves the partition table alone. It also leaves the hidden store on the disk, and says nothing about what the kernel code did.- Do not clean in place from the running system: the rootkit filters the writes that would remove it, and TDL4's driver re-infects a repaired MBR.
- Prefer a rebuild: wipe the whole disk, including the space outside the partitions (
diskpart,clean all), reinstall from trusted media, and rotate the credentials used on the host.
Sources
Primary public reports, newest first.
- 2022-01-15Destructive malware targeting Ukrainian organizationsMicrosoft
- 2021-10-05UEFI threats moving to the ESP: Introducing ESPecter bootkitESET
- 2021-09-28FinSpy: unseen findingsKaspersky
- 2018-05-31ProtectKeyWithTPM method of the Win32_EncryptableVolume classMicrosoft
- 2017-06-01Restricted Direct Disk Access and Volume Access in WindowsMicrosoft
- 2014-11-12Bootkits: past, present & futureVirus Bulletin (ESET)
- 2013-04-08Mind the Gapz: The most complex bootkit ever analyzed?ESET
- 2011-01-25TDSS. TDL-4Kaspersky
- 2009-10-08How Basic Disks and Volumes WorkMicrosoft
See also
Related techniques
- Infecting the VBR, its BPB or the IPLPersisting in the boot chain
- Hooking the BIOS disk service (int 13h)Taking over the boot
- Hidden storage outside the partitionsPersisting in the boot chain
- Hooking the disk stack to return clean sectorsHiding and protecting itself
- Turning off driver signing from the boot chainTaking over the boot
- Replacing or patching the ESP boot managerPersisting in the boot chain
Families that use it
- ESPecterUEFI bootkit
- FinSpy UEFI bootkitUEFI bootkit
- GapzVBR bootkit
- TDL4 (TDSS / Alureon)MBR bootkit