Created
August 3, 2024 14:08
-
-
Save rbmm/919676c171bcab1a9eb4faf51c552f71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Strsafe.h " | |
struct ResultStatus | |
{ | |
HRESULT hr; | |
NTSTATUS status; | |
}; | |
enum class ReportFailureOptions { NtStatus, HResult }; | |
// wil::details:: | |
void ReportFailure_Base(PVOID callerAddress, | |
ULONG Line, | |
PCSTR pcszFileName, | |
PCSTR , | |
PCSTR , | |
PVOID Address, | |
ResultStatus const & rs, | |
PCWSTR Msg, | |
ReportFailureOptions opt) | |
{ | |
_LDR_DATA_TABLE_ENTRY* ldte; | |
PUNICODE_STRING BaseDllName = 0; | |
if (0 <= LdrFindEntryForAddress(Address, &ldte)) | |
{ | |
BaseDllName = &ldte->BaseDllName; | |
} | |
ULONG tid = GetCurrentThreadId(); | |
PWSTR lpOutputString = 0; | |
LONG cch = 0; | |
LONG r = 0; | |
switch (opt) | |
{ | |
case ReportFailureOptions::NtStatus: | |
r = rs.status; | |
break; | |
case ReportFailureOptions::HResult: | |
r = rs.hr; | |
break; | |
default: | |
__debugbreak(); | |
} | |
while (0 < (cch = _snwprintf(lpOutputString, cch, | |
L"%hs(%u)\\%wZ!%p: (caller: %p) LogHr(%x) tid(%x) %08x Msg:[%ls]\r\n", | |
pcszFileName, Line, BaseDllName, Address, callerAddress, opt, tid, r, Msg))) | |
{ | |
if (lpOutputString) | |
{ | |
OutputDebugStringW(lpOutputString); | |
break; | |
} | |
lpOutputString = (PWSTR)alloca(++cch * sizeof(WCHAR)); | |
} | |
} | |
void ReportFailure_Msg(PVOID callerAddress, | |
ULONG Line, | |
PCSTR pcszFileName, | |
PCSTR , | |
PCSTR , | |
PVOID Address, | |
ResultStatus const & rs, | |
PCSTR formatString, | |
va_list argList) | |
{ | |
WCHAR fmt[0x800], Msg[0x800]; | |
if (formatString) | |
{ | |
if (argList) | |
{ | |
StringCchPrintfW(fmt, _countof(fmt), L"%hs", formatString); // fmt = L"%s" | |
StringCchVPrintfW(Msg, _countof(Msg), fmt, argList); | |
} | |
else | |
{ | |
StringCchPrintfW(Msg, _countof(Msg), L"%hs", formatString); | |
} | |
} | |
else | |
{ | |
*Msg = 0; | |
} | |
ReportFailure_Base(callerAddress, Line, pcszFileName, 0, 0, Address, rs, Msg, ReportFailureOptions::HResult); | |
} | |
NTSTATUS HrToNtStatus(HRESULT hr) | |
{ | |
return hr; | |
} | |
void ReportFailure_HrMsg( | |
PVOID callerAddress, | |
ULONG Line, | |
PCSTR pcszFileName, | |
PCSTR , | |
PCSTR , | |
PVOID Address, | |
HRESULT hr, | |
PCSTR formatString, | |
va_list argList ) | |
{ | |
ResultStatus rs { hr, HrToNtStatus(hr) }; | |
ReportFailure_Msg(callerAddress, Line, pcszFileName, 0, 0, Address, rs, formatString, argList); | |
} | |
HRESULT Log_HrMsg(PVOID callerAddress, ULONG Line, PCSTR pcszFileName, HRESULT hr, PCSTR formatString, ...) | |
{ | |
va_list argList; | |
va_start(argList, formatString); | |
ReportFailure_HrMsg(callerAddress, Line, pcszFileName, 0, 0, _ReturnAddress(), hr, formatString, argList); | |
va_end(argList); | |
return hr; | |
} | |
NTSTATUS OpenDevice(PCSTR formatString) | |
{ | |
UNICODE_STRING ObjectName; | |
OBJECT_ATTRIBUTES attributes = { sizeof(attributes), 0, &ObjectName, OBJ_CASE_INSENSITIVE }; | |
RtlInitUnicodeString(&ObjectName, L"\\Device\\P9Rdr"); | |
IO_STATUS_BLOCK ioStatus; | |
HANDLE device; | |
NTSTATUS status = NtCreateFile(&device, SYNCHRONIZE, &attributes, &ioStatus, 0, 0, | |
FILE_SHARE_VALID_FLAGS, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, 0, 0); | |
if (0 > status) | |
{ | |
return Log_HrMsg(_ReturnAddress(), __LINE__, __FILE__, status, formatString, | |
"NtCreateFile(&device, SYNCHRONIZE, &attributes, &ioStatus, nullptr, " | |
"FILE_ATTRIBUTE_NORMAL, (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE), " | |
"FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, nullptr, 0)"); | |
} | |
NtClose(device); | |
return STATUS_SUCCESS; | |
} | |
EXTERN_C_START | |
WINBASEAPI | |
DWORD APIENTRY | |
NPOpenEnum ( | |
_In_ DWORD dwScope, | |
_In_ DWORD dwType, | |
_In_ DWORD dwUsage, | |
_In_opt_ LPNETRESOURCEW lpNetResource, | |
_Out_ LPHANDLE lphEnum | |
); | |
WINBASEAPI | |
DWORD APIENTRY | |
NPEnumResource ( | |
_In_ HANDLE hEnum, | |
_Inout_ LPDWORD lpcCount, | |
_Out_writes_bytes_(*lpBufferSize) LPVOID lpBuffer, | |
_Inout_ LPDWORD lpBufferSize | |
); | |
WINBASEAPI | |
DWORD APIENTRY | |
NPCloseEnum ( | |
_In_ HANDLE hEnum | |
); | |
PVOID __imp_NPOpenEnum = 0, __imp_NPEnumResource = 0, __imp_NPCloseEnum = 0; | |
EXTERN_C_END | |
void p9np() | |
{ | |
if (HMODULE hmod = LoadLibraryW(L"p9np")) | |
{ | |
if ((__imp_NPCloseEnum = GetProcAddress(hmod, "NPCloseEnum")) && | |
(__imp_NPOpenEnum = GetProcAddress(hmod, "NPOpenEnum")) && | |
(__imp_NPEnumResource = GetProcAddress(hmod, "NPEnumResource"))) | |
{ | |
HANDLE hEnum = 0; | |
if (WN_SUCCESS == NPOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_DISK, 0, 0, &hEnum)) | |
{ | |
DWORD cCount = MAXULONG; | |
ULONG BufferSize = 0x1000; | |
PVOID lpBuffer = alloca(BufferSize); | |
NPEnumResource(hEnum, &cCount, lpBuffer, &BufferSize); | |
NPCloseEnum(hEnum); | |
} | |
} | |
FreeLibrary(hmod); | |
} | |
OpenDevice("%hs"); | |
OpenDevice("%s"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment