Giter Site home page Giter Site logo

uefi-bootkit-rs's Introduction

A UEFI Bootkit in Rust

Note: This project is incomplete and work is in progress (W.I.P).

While it's possible to use this for advanced adversary simulation or emulation (red teaming), it's unlikely to be used in most engagements. This tool can also be used for game hacking and is a side project for those interested in fun, learning, malware research, and spreading security awareness. It also demonstrates that Rust can handle both low-level and high-level tasks. One important capability of this tool is its ability to load a kernel driver before the operating system, or even execute shellcode in the kernel to bypass Windows security protections. It's important to recognize the potential of Rust and not underestimate its power.

Feel free to check out my Windows Kernel Rootkit and Blue Pill Hypervisor in pure Rust:

This project is mostly inspired by:

Features

TODO

Description

A kernel driver can be loaded during boot using a bootkit. A bootkit can run code before the operating system and potentially inject malicious code into the kernel or load a malicious kernel driver by infecting the boot process and taking over the system's firmware or bootloader.

The image below shows how Legacy and UEFI boot works.

Legacy-and-UEFI-Boot Figure 1. Comparison of the Legacy Boot flow (left) and UEFI boot flow (right) on Windows (Vista and newer) systems (Full Credits: WeLiveSecurity)

  1. There are a few ways to achieve the same objective as shown below:

    • Hook/detour Archpx64TransferTo64BitApplicationAsm in bootmgfw.efi (Windows OS loader), which transfers execution to the OS loader (winload.efi) or

    • ImgArchStartBootApplication to catch the moment when the Windows OS loader (winload.efi) is loaded in the memory but still has not been executed or

    • Hook/Detour ExitBootServices, which is UEFI firmware service that signals the end of the boot process and transitions the system from the firmware environment to the operating system environment.

      1.1. The following is required if UEFI Secure Boot is enabled:

      • Patch BmFwVerifySelfIntegrity to bypass self integrity checks.
      • Execute bcdedit /set {bootmgr} nointegritychecks on to skip the integrity checks.
      • Inject bcdedit /set {bootmgr} nointegritychecks on option dynamically by modifying the LoadOptions.

      1.2. The following is required to allocate an additional memory buffer for the malicious kernel driver, because as a UEFI Application it will be unloaded from memory after returning from its entry point function.

      • BlImgAllocateImageBuffer or BlMmAllocateVirtualPages in the Windows OS loader (winload.efi).
  2. Hook/detour OslArchTransferToKernel in winload.efi (Windows OS loader), which transfers execution to the Windows Kernel (ntoskrnl.exe) to catch the moment when the OS kernel and some of the system drivers are already loaded in the memory, but still not been executed, which is a perfect moment to perform more in-memory patching.

    • Patch SepInitializeCodeIntegrity, a parameter to CiInitialize in ntoskrnl.exe to disable Driver Signature Enforcement (DSE).
    • Patch KeInitAmd64SpecificState in ntoskrnl.exe to disable PatchGuard.

Usage

The UEFI Bootkit works under one or more of the following conditions:

a) Turn off secure boot

b) Install your own secure boot keys and keep secure boot on

c) Bring your vulnerable binary (BYOVB) that is not in the "deny list" to exploit a 1-day and bypass secure boot.

d) Exploit a 0-day to bypass secure boot.

Usage 1: Infect Windows Boot Manager bootmgfw.efi on Disk (Unsupported)

Typically UEFI Bootkits infect the Windows Boot Manager bootmgfw.efi located in EFI partition \EFI\Microsoft\Boot\bootmgfw.efi or C:\Windows\Boot\EFI\bootmgfw.efi as shown below:

  • Convert our bootkit to shellcode
  • Find bootmgfw.efi (Windows Boot Manager)
  • Add .efi section to bootmgfw.efi (Windows Boot Manager)
  • Inject/copy bootkit shellcode to the .efi section in bootmgfw.efi
  • Change entry point of the bootmgfw.efi (Windows Boot Manager) to .efi bootkit shellcode
  • Reboot

Usage 2: Execute UEFI Bootkit via UEFI Shell (Supported)

  1. Compile the project
cargo build --target x86_64-unknown-uefi

Download EDK2 efi shell or UEFI-Shell and follow these steps:

  1. Extract downloaded efi shell and rename file Shell.efi (should be in folder UefiShell/X64) to bootx64.efi

  2. Format some USB drive to FAT32

  3. Create following folder structure:

USB:.
 │   bootkit.efi
 │
 └───EFI
      └───Boot
              bootx64.efi
  1. Boot from the USB drive

    • VMware Workstation: VM -> Settings -> Hardware -> Add -> Hard Disk -> Next -> SCSI or NVMe (Recommended) -> Next -> Use a physical disk (for advanced users) -> Next -> Device: PhysicalDrive1 and Usage: Use entire disk -> Next -> Finish.

    • Start VM by clicking Power On to Firmware

    • Select Internal Shell (Unsupported option) or EFI Vmware Virtual SCSI Hard Drive (1.0)

  2. An UEFI shell should start, change directory to your USB (FS1 should be the USB since we are booting from it) and list files:

FS1:
ls
  1. You should see file bootkit.efi, if you do, load it:
bootkit.efi
  1. Now you should see output from the bootkit.efi application. If it is successful, Windows should boot automatically otherwise, exit and boot into Windows (change to Windows boot media - usually FS0 - and run \EFI\Microsoft\Boot\bootmgfw.efi or \EFI\Boot\bootx64.efi)

Credits / References / Thanks / Motivation

Special thanks to btbd, ajkhoury, Mattiwatti, mrexodia, SamuelTulach, realoriginal, Cr4sh, matrosov, not-matthias and welivesecurity

uefi-bootkit-rs's People

Contributors

memn0ps avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.