CVE-2023-38146: Arbitrary Code Execution via Windows Themes
A time-of-check-time-of-use flaw in Windows theme handling allows an attacker to replace a signed DLL with a malicious one after signature verification, leading to arbitrary code execution when a user opens a crafted theme file. The Mark-of-the-Web warning can be bypassed by packaging the theme in a .themepack file.
This is a fun bug I found while poking around at weird Windows file formats. It's a kind of classic Windows style vulnerability featuring broken signing, sketchy DLL loads, file races, cab files, and Mark-of-the-Web silliness.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Windows .theme files let a user customise the appearance of the operating system. They are ini files holding configuration details, and on Windows 11 clicking one invokes the following command:
"C:\WINDOWS\system32\rundll32.exe" C:\WINDOWS\system32\themecpl.dll,OpenThemeAction <theme file path>
The bug described here lives in the handling of .msstyles files. These are PE (DLL) files that carry resources such as icons for a theme, but should contain no code. A .theme file references one like this:
[VisualStyles]
Path=%SystemRoot%\resources\Themes\Aero\Aero.msstyles
When the .theme file is opened, the referenced .msstyles file is loaded as well. The researcher named the vulnerability ThemeBleed. It combines broken signing, a sketchy DLL load, a file race, cab files and Mark-of-the-Web behaviour, and it was the researcher's first submission to the MSRC Windows bug bounty after leaving Microsoft in April 2022.
Setup
The proof of concept needs two machines: an attacker machine running an SMB server, and a target machine that opens the .theme file.
Attacker machine requirements:
- Windows 10 or 11
- The "Server" service disabled to free up the SMB port (disable and restart, not just stop the service)
- An up to date .NET installation
- Reachable from the target machine on the network
Target machine requirements:
- Latest Windows 11
The PoC is published at https://github.com/exploits-forsale/themebleed and uses the SMBLibrary by Tal Aloni.
Finding it
The researcher was poking around at unusual Windows file formats. Two things in the theme loading path stood out.
First, when loading a .msstyles file, LoadThemeLibrary in uxtheme.dll reads the theme version from a resource named PACKTHEM_VERSION in the binary. If the version it reads is 999, it calls a second function, ReviseVersionIfNecessary. The decompiled function, with the relevant parts commented, is shown below:
__int64 __fastcall LoadThemeLibrary(const WCHAR *msstyles_path, HMODULE *out_module, int *out_version)
{
HMODULE module_handle;
signed int result;
int version;
signed int return_val;
unsigned int resource_size;
__int16 *version_ptr;
if ( out_version )
*out_version = 0;
module_handle = LoadLibraryExW(msstyles_path, 0, 2u);
if ( !module_handle )
return (unsigned int)MakeErrorLast();
result = GetPtrToResource(
module_handle,
L"PACKTHEM_VERSION",
(const unsigned __int16 *)1,
(void **)&version_ptr,
&resource_size); // !!! [1] version number is extracted from resource "PACKTHEM_VERSION"
if ( result < 0 || resource_size != 2 )
goto LABEL_22;
version = *version_ptr;
if ( out_version )
*out_version = version;
return_val = -2147467259;
if ( version >= 4 )
{
if ( version > 4 )
result = -2147467259;
return_val = result;
}
if ( return_val < 0 && (_WORD)version == 999 ) // !!! [2] special case for version 999
{
resource_size = 999;
return_val = ReviseVersionIfNecessary(msstyles_path, 999, (int *)&resource_size); // !!! [3] call to `ReviseVersionIfNecessary`
...
}
Second, the actions ReviseVersionIfNecessary performs on the given .msstyles path looked wrong:
- Create a new file path by appending
_vrf.dllto the.msstylesfile path. - Check if this new
_vrf.dllfile exists. If not, exit. - Open the
_vrf.dllfile. - Verify the signature on the
_vrf.dllfile. If the signature is invalid, exit. - Close the
_vrf.dllfile. - Load the
_vrf.dllfile as a DLL and call theVerifyThemeVersionfunction.
The intent appears to be to safely load a signed DLL and call a function in it. The implementation closes the DLL after verifying its signature in step 5 and re-opens it when loading it via LoadLibrary in step 6. That gap is a time-of-check-time-of-use window: an attacker can replace the verified _vrf.dll with an unsigned malicious one between the two steps, and the malicious DLL is then loaded and executed.
Exploitation
The PoC has two components: an SMB server executable run on the attacker's machine, and a .theme file opened on the target. The SMB server is used because a .theme file may point to a .msstyles path on a remote SMB share. Since the share is attacker controlled, it can exploit the TOCTOU bug by returning a validly signed file when the client first requests it for the signature check, and a malicious one when the client loads the DLL.
Step 1: Create the theme file
On the attacker machine, generate the .theme file pointing at the attacker's SMB server:
themebleed.exe make_theme <attacker machine ip> exploit.theme
Step 2: Start the SMB server
On the attacker machine, run the server component:
themebleed.exe server
Step 3: Open the theme on the target
On the target machine, open exploit.theme. The calculator opens on the target, demonstrating that arbitrary code has been executed.
Root cause
The core mistake is the check-then-use split in ReviseVersionIfNecessary: the _vrf.dll file is opened, its signature verified, and then closed, after which it is re-opened by LoadLibrary. Nothing ties the verified file to the file that is actually loaded, so a file swap in between defeats the signature check. The "version 999" special case in LoadThemeLibrary is what routes a .msstyles file into this code path at all.
Two further weaknesses make delivery easier. A .theme file may reference a .msstyles path on a remote SMB share, so the attacker controls the file the client reads at each step. And Mark-of-the-Web does not stop the attack: a downloaded .theme file triggers a security warning when launched, but packaging the .theme file inside a .themepack file bypasses it. A .themepack file is a cab file containing a .theme file; when a .themepack with Mark-of-the-Web is opened, the contained .theme file is loaded and no warning is displayed.
The researcher notes the vulnerability appeared to be new and present only in Windows 11, and that it goes from loading a theme to downloading and executing code without memory corruption.
Impact
When a user opens a crafted .theme file (or a .themepack containing one), arbitrary code runs on the target machine. Because the .themepack route suppresses the Mark-of-the-Web warning, opening a downloaded .themepack displays no warning.
Fix
Microsoft's fix removed the "version 999" functionality entirely. That mitigates this specific exploit but, in the researcher's analysis, does not address the TOCTOU issue in the signing of .msstyles files. Microsoft also did not add Mark-of-the-Web warnings to .themepack files.
The researcher recommended:
- Removing the "version 999" functionality altogether, while noting uncertainty about its intended use.
- Signing and verifying the
_vrf.dllbinary in the standard way Windows verifies other code, rather than the race-prone approach used here. - Disallowing loading resources from remote shares in theme files.
- Adding Mark-of-the-Web warnings to
.themepackfiles.
Reporting timeline
| Date | Event |
|---|---|
| 5/15/2023 | Report and PoC submitted to Microsoft |
| 5/16/2023 | Microsoft acknowledges the vulnerability |
| 5/17/2023 | $5,000 bounty awarded |
| 9/12/2023 | Fix released |
Variants to test
- Other version-gated code paths in theme or shell components. Look for special resource values that route loading into extra verification or helper-DLL logic. A positive result is a code path that opens a file for checking and then re-opens it by path for loading.
- Other
_vrf.dll-style sidecar files. Any component that appends a suffix to a user-controlled path, verifies the result, then loads it by name is a candidate. A positive result is a successful swap of the file between verification and load. - Remote-share resource loading in other file formats. Check whether other document or theme-like formats accept UNC paths for resources. A positive result is a client fetching attacker-controlled content from an SMB share during normal opening.
- Mark-of-the-Web handling in container formats. Test other archive or cab-based formats that embed files with their own handlers. A positive result is an embedded file being processed without the warning its standalone form would trigger.
Credits
The PoC makes use of SMBLibrary by Tal Aloni.
What happened, in plain words
A security researcher found a way to make a Windows 11 computer run code of an attacker's choosing when the user simply opens a theme file, the kind of file that changes how Windows looks. The trick does not rely on crashing anything or on a memory bug. It relies on the order in which Windows checks a file and then uses it, plus a couple of shortcuts in how Windows handles downloaded files.
The researcher named the vulnerability ThemeBleed. It was their first submission to the Microsoft bug bounty programme for Windows after leaving Microsoft in April 2022, and Microsoft paid a $5,000 bounty for it. The report below walks through the whole thing from zero: what a theme file is, what a DLL is, what a signature is, what a race condition is, and how all of those pieces combine into a working attack.
Background: what a theme file is
On Windows, a .theme file lets you customise the appearance of the operating system: colours, sounds, cursors, and the visual style of windows and buttons. A .theme file is not a program. It is an ini file, which is a very old and very simple text format made of sections in square brackets and key=value lines underneath. You can open one in Notepad and read it.
When you double-click a .theme file on Windows 11, Windows does not run it directly. It hands the file to a built-in Windows component, and the command it uses looks like this:
"C:\WINDOWS\system32\rundll32.exe" C:\WINDOWS\system32\themecpl.dll,OpenThemeAction <theme file path>
Line by line:
"C:\WINDOWS\system32\rundll32.exe"is a standard Windows program whose whole job is to load a DLL and call one specific function inside it. Think of it as a waiter who does not cook, but carries your order to the kitchen.C:\WINDOWS\system32\themecpl.dllis the DLL (a library of code) that knows how to apply themes. The.dllextension means it is a shared library, not a standalone program.,OpenThemeActionis the name of the function inside that DLL thatrundll32should call.<theme file path>is a placeholder for the path of the.themefile you clicked.
So clicking a theme file is really "ask a standard Windows helper to call a specific function in a specific Windows library, passing along my file". That matters later, because it means the file you clicked is treated as input to trusted Windows code.
The .msstyles file
A theme can also point at a .msstyles file. This is the file that actually holds the visual style: the pictures, icons and measurements used to draw windows and buttons. A .msstyles file is technically a PE file, which is the same family of file format as a .exe or a .dll. In other words, it is structured like a program, but by design it should only carry resources (pictures and data), not executable code.
A .theme file references one like this:
[VisualStyles]
Path=%SystemRoot%\resources\Themes\Aero\Aero.msstyles
Line by line:
[VisualStyles]starts a section of the ini file. Everything until the next section header belongs to it.Path=is the setting name. It tells Windows where to find the visual style file.%SystemRoot%\resources\Themes\Aero\Aero.msstylesis the value.%SystemRoot%is a Windows shorthand that expands to the Windows folder, usuallyC:\WINDOWS. So this points at the built-in Aero style.
When the .theme file is opened, the .msstyles file it names is loaded as well. And here is the important part for the attack: that path is just text in a file. Nothing stops a theme file from pointing somewhere else, including a shared folder on another computer.
Finding it: the "version 999" check
While poking around at unusual Windows file formats, the researcher looked at how Windows loads a .msstyles file. The loading happens in a function called LoadThemeLibrary, which lives in a Windows library called uxtheme.dll.
A function is a named block of code that does one job. LoadThemeLibrary does what its name says: it loads a theme library. Before doing so, it reads a version number stored inside the .msstyles file, in a resource named PACKTHEM_VERSION. A resource is a chunk of data embedded in a PE file, such as an icon, a string or, here, a small number.
If the version it reads is 999, the function takes a special branch and calls a second function, ReviseVersionIfNecessary. The decompiled code, with the researcher's comments, is below. Decompiled means it was turned from the machine code Windows actually runs back into readable C-like source.
__int64 __fastcall LoadThemeLibrary(const WCHAR *msstyles_path, HMODULE *out_module, int *out_version)
{
HMODULE module_handle;
signed int result;
int version;
signed int return_val;
unsigned int resource_size;
__int16 *version_ptr;
if ( out_version )
*out_version = 0;
module_handle = LoadLibraryExW(msstyles_path, 0, 2u);
if ( !module_handle )
return (unsigned int)MakeErrorLast();
result = GetPtrToResource(
module_handle,
L"PACKTHEM_VERSION",
(const unsigned __int16 *)1,
(void **)&version_ptr,
&resource_size); // !!! [1] version number is extracted from resource "PACKTHEM_VERSION"
if ( result < 0 || resource_size != 2 )
goto LABEL_22;
version = *version_ptr;
if ( out_version )
*out_version = version;
return_val = -2147467259;
if ( version >= 4 )
{
if ( version > 4 )
result = -2147467259;
return_val = result;
}
if ( return_val < 0 && (_WORD)version == 999 ) // !!! [2] special case for version 999
{
resource_size = 999;
return_val = ReviseVersionIfNecessary(msstyles_path, 999, (int *)&resource_size); // !!! [3] call to `ReviseVersionIfNecessary`
...
}
Line by line, in plain words:
__int64 __fastcall LoadThemeLibrary(...)declares the function. The parameters are the path to the.msstylesfile, a place to store the loaded module handle, and a place to store the version number.- The local variables are scratch space:
module_handlewill hold the loaded file,versionwill hold the number read from the file, and so on. if ( out_version ) *out_version = 0;sets the caller's version output to zero first, so it is never left uninitialised.module_handle = LoadLibraryExW(msstyles_path, 0, 2u);loads the file at the given path.LoadLibraryExWis a standard Windows function for loading a DLL. The2uflag tells Windows to load it as a data file rather than as an executable module.if ( !module_handle ) return ...;handles the case where loading failed, for example because the file does not exist.result = GetPtrToResource(module_handle, L"PACKTHEM_VERSION", ...)fetches the resource namedPACKTHEM_VERSIONfrom the loaded file and gives back a pointer to it plus its size. The comment[1]marks this as the point where the version number is extracted.if ( result < 0 || resource_size != 2 ) goto LABEL_22;bails out if the resource is missing or is not exactly two bytes long. Two bytes is the size of a small integer.version = *version_ptr;reads that two-byte number intoversion.- The block
if ( version >= 4 ) { ... }rejects versions above 4 by setting an error value. So normal, supported versions are small numbers. if ( return_val < 0 && (_WORD)version == 999 )is the special case, marked[2]. If the version is exactly 999, the code does something different from the normal path.return_val = ReviseVersionIfNecessary(msstyles_path, 999, ...);marked[3], calls the second function, passing the same path and the number 999.
Why does this matter? Because the version number is read from the file itself. The file is supplied by whoever made the theme. So an attacker can put 999 in their .msstyles file and steer Windows into the special code path on purpose. The researcher did not know what the 999 feature was originally for, and later recommended removing it.
The second problem: check, then use
The function called for version 999, ReviseVersionIfNecessary, does the following with the .msstyles path it is given:
- Create a new file path by appending
_vrf.dllto the.msstylesfile path. - Check if this new
_vrf.dllfile exists. If not, exit. - Open the
_vrf.dllfile. - Verify the signature on the
_vrf.dllfile. If the signature is invalid, exit. - Close the
_vrf.dllfile. - Load the
_vrf.dllfile as a DLL and call theVerifyThemeVersionfunction.
Two concepts need explaining here.
A DLL is a file full of code that other programs can load and call. Loading a DLL means asking Windows to map that file into memory and make its functions available. If an attacker can get their own DLL loaded, their code runs with the privileges of the program that loaded it.
A digital signature is a cryptographic stamp on a file. It lets Windows check two things: who produced the file, and whether the file has been altered since. If the signature is valid, Windows can be reasonably confident the file is the genuine article from a trusted publisher. Signature checks are how Windows decides whether to trust code.
The intent of these six steps is clear and sensible: only load a DLL that carries a valid signature, then call a function in it. The implementation, however, has a gap. Step 5 closes the file after checking it, and step 6 opens it again by path. Between those two moments, the file on disk can change. Nothing ties the file that was checked to the file that is loaded.
This class of bug is called time-of-check to time-of-use, or TOCTOU. The everyday comparison is a bouncer checking your ID at the door, handing it back, and then letting in whoever is standing there a moment later. The check happened, but it did not apply to the person who actually walked in. In software, an attacker who controls the file location can serve a properly signed file for the check and swap in an unsigned malicious file for the load.
Getting the file past the warning: Mark-of-the-Web
There is one more obstacle. When you download a file from the internet, Windows attaches a marker to it called Mark-of-the-Web. It is metadata, not part of the file's contents, and it tells Windows and other programs "this came from the internet, be careful". When you launch a downloaded .theme file, Windows shows a security warning because of that marker.
The researcher found this can be bypassed by packaging the .theme file inside a .themepack file. A .themepack is a cab file, which is a simple archive format (a container holding other files, like a zip). It contains a .theme file. When a .themepack file is opened, the .theme file inside it is loaded. When the .themepack itself carries Mark-of-the-Web, no warning is displayed. So the warning you would normally see for a downloaded theme never appears.
Building the proof of concept
The researcher built a proof of concept, usually shortened to PoC: a working demonstration that the bug is real and exploitable, not just theoretical. It has two parts:
- an SMB server program, run on the attacker's machine;
- a
.themefile, opened on the target's machine.
SMB stands for Server Message Block. It is the standard Windows protocol for sharing files over a network: when you open a folder on another computer, SMB is what carries the files to you. An SMB server is simply a program that serves files over that protocol.
Why use one? Because a .theme file can point its .msstyles path at a remote SMB share. If the attacker runs the SMB server, the attacker controls every file the target reads from that share. That makes the TOCTOU race easy to win: the server can return a validly signed file when the target first asks for it, to pass the signature check, and then return a malicious file when the target loads the DLL a moment later. Because the share is attacker controlled, it can easily exploit the race by answering the two requests differently.
The PoC is published at https://github.com/exploits-forsale/themebleed.
Setting up the two machines
You need two machines: one attacker machine running the SMB server, and one target machine that opens the .theme file.
Attacker machine requirements:
- Windows 10 or 11.
- The "Server" service disabled, to free up the SMB port. This is the built-in Windows service that shares your own files over SMB; it has to be disabled and the machine restarted, not just stopped, so the port is free for the PoC server.
- An up to date .NET installation.
- Reachable from the target machine on the network, so the target can actually connect to the fake share.
Target machine requirements:
- Latest Windows 11. The researcher notes the vulnerability appeared to be new and present only in Windows 11.
Running it
Step 1: Create the theme file
On the attacker machine, generate the .theme file pointing at the attacker's SMB server:
themebleed.exe make_theme <attacker machine ip> exploit.theme
Line by line:
themebleed.exeis the PoC program.make_themeis the subcommand that builds a theme file.<attacker machine ip>is a placeholder for the attacker machine's address on the network. The generated theme will point its.msstylespath at that address, so the target fetches the style file from the attacker's SMB server.exploit.themeis the name of the theme file to write.
This step exists because the theme file is the bait: it is the thing the victim opens, and it is what redirects Windows to the attacker's server.
Step 2: Start the SMB server
On the attacker machine, run the server component:
themebleed.exe server
Line by line:
themebleed.exeis the same PoC program.serveris the subcommand that starts the SMB server, which will serve the signed file for the check and the malicious file for the load.
Step 3: Open the theme on the target
On the target machine, open exploit.theme. The calculator opens on the target. That is the demonstration: the calculator is a harmless stand-in for arbitrary code, and its appearance proves that code chosen by the attacker ran on the target machine.
Why it works, and what it means
The core mistake is the check-then-use split in ReviseVersionIfNecessary. The _vrf.dll file is opened, its signature verified, and then closed, after which it is re-opened by LoadLibrary. Nothing ties the verified file to the file that is actually loaded, so swapping the file in between defeats the signature check. The "version 999" special case in LoadThemeLibrary is what routes a .msstyles file into this code path at all.
Two further weaknesses make delivery easier. A .theme file may reference a .msstyles path on a remote SMB share, so the attacker controls the file the client reads at each step. And Mark-of-the-Web does not stop the attack: a downloaded .theme file triggers a security warning when launched, but packaging the .theme file inside a .themepack file bypasses it.
What is the impact? When a user opens a crafted .theme file, or a .themepack containing one, arbitrary code runs on the target machine. Because the .themepack route suppresses the Mark-of-the-Web warning, opening a downloaded .themepack displays no warning. The researcher notes that the chain goes from loading a theme to downloading and executing code without any memory corruption, and calls it reliable.
The fix, and what it left behind
Microsoft's released fix removed the "version 999" functionality entirely. That mitigates this specific exploit. In the researcher's analysis, however, it does not address the TOCTOU issue in the signing of .msstyles files, and Microsoft did not add Mark-of-the-Web warnings to .themepack files.
The researcher recommended:
- Removing the "version 999" functionality altogether, while noting uncertainty about its intended use.
- Signing and verifying the
_vrf.dllbinary in the standard way Windows verifies other code, rather than the race-prone approach used here. - Disallowing loading resources from remote shares in theme files.
- Adding Mark-of-the-Web warnings to
.themepackfiles.
Reporting timeline
| Date | Event |
|---|---|
| 5/15/2023 | Report and PoC submitted to Microsoft |
| 5/16/2023 | Microsoft acknowledges the vulnerability |
| 5/17/2023 | $5,000 bounty awarded |
| 9/12/2023 | Fix released |
Where else to look
If you want to hunt for similar bugs, these are the directions the researcher suggests testing:
- Other version-gated code paths in theme or shell components. Look for special resource values that route loading into extra verification or helper-DLL logic. A positive result is a code path that opens a file for checking and then re-opens it by path for loading.
- Other
_vrf.dll-style sidecar files. Any component that appends a suffix to a user-controlled path, verifies the result, then loads it by name is a candidate. A positive result is a successful swap of the file between verification and load. - Remote-share resource loading in other file formats. Check whether other document or theme-like formats accept UNC paths for resources. A UNC path is a Windows network path of the form
\\server\share. A positive result is a client fetching attacker-controlled content from an SMB share during normal opening. - Mark-of-the-Web handling in container formats. Test other archive or cab-based formats that embed files with their own handlers. A positive result is an embedded file being processed without the warning its standalone form would trigger.
Credits
The PoC makes use of SMBLibrary by Tal Aloni.
Glossary
- API: an interface that one piece of software offers to another so it can ask it to do things, like a menu of functions a program can call.
- Bounty: a payment a company makes to a researcher who reports a security bug through its programme.
- Cab file: a Microsoft archive format that packs several files into one container, similar in purpose to a zip file.
- DLL: a file containing code that other programs can load and call, short for dynamic-link library.
- Decompiled: turned from machine code back into readable source-like code.
- Function: a named block of code that performs one job and can be called by name.
- Ini file: a simple text configuration format made of
[section]headers andkey=valuelines. - LoadLibrary: a standard Windows function that loads a DLL into a running program so its code can be called.
- Mark-of-the-Web: metadata Windows attaches to files downloaded from the internet, used to warn users and restrict what the file can do.
- Memory corruption: a class of bug where a program writes to memory it should not, often leading to crashes or code execution; this vulnerability does not involve it.
- MSRC: the Microsoft Security Response Center, the team that receives and handles vulnerability reports for Microsoft products.
- PE file: the file format Windows uses for executables and DLLs, short for Portable Executable.
- PoC: proof of concept, a working demonstration that a vulnerability can actually be exploited.
- Resource: a chunk of data embedded inside a PE file, such as an icon, a string or a number.
- Rundll32: a standard Windows program that loads a DLL and calls a named function inside it.
- Signature (digital signature): a cryptographic stamp on a file that lets Windows check who produced it and whether it has been altered.
- SMB: Server Message Block, the standard Windows protocol for sharing files over a network.
- SMB server: a program that serves files to other machines over SMB.
- Theme file (
.theme): an ini file that tells Windows how to customise the operating system's appearance. - Themepack (
.themepack): a cab archive containing a.themefile, which Windows opens as a packaged theme. - Time-of-check to time-of-use (TOCTOU): a bug where something is checked at one moment and used at a later moment, allowing it to be changed in between.
- UNC path: a Windows network path that points at a shared folder on another machine, written like
\\server\share. .msstylesfile: a PE file holding the visual style resources for a theme, which should contain no code._vrf.dll: the sidecar DLL thatReviseVersionIfNecessarybuilds by appending a suffix to the.msstylespath, verifies, and then loads.