From d8c16dfdf77bbacba13b4afedf743595163deb79 Mon Sep 17 00:00:00 2001 From: Mauro Leggieri Date: Fri, 27 Oct 2023 23:57:27 -0300 Subject: [PATCH] Fixed warnings and SAL --- .gitignore | 3 +- Console.cpp | 16 +- Console.h | 7 +- FileRoutines.cpp | 111 +++++++++-- FileRoutines.h | 13 +- LightJSonBuilder.cpp | 52 ++++-- MXCommonLibrariesHelpers.sln | 31 ++++ MXCommonLibrariesHelpers.vcxproj | 224 +++++++++++++++++++++++ MXCommonLibrariesHelpers.vcxproj.filters | 167 +++++++++++++++++ MemoryPackage.cpp | 14 +- Network.cpp | 10 +- Network.h | 23 ++- PeParser.cpp | 8 +- PeParser.h | 38 ++-- Process.cpp | 18 +- Process.h | 13 +- ResourceExtract.cpp | 2 +- ServiceManager.cpp | 47 ++--- ServiceManager.h | 32 ++-- Signatures.cpp | 6 +- Signatures.h | 4 + WinRegistry.cpp | 8 +- WinRegistry.h | 4 +- 23 files changed, 704 insertions(+), 147 deletions(-) create mode 100644 MXCommonLibrariesHelpers.sln create mode 100644 MXCommonLibrariesHelpers.vcxproj create mode 100644 MXCommonLibrariesHelpers.vcxproj.filters diff --git a/.gitignore b/.gitignore index 157632f..be64381 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .vs -.svn/ +obj/ +lib/ *.~* *.suo *.ncb diff --git a/Console.cpp b/Console.cpp index 727174c..e329211 100644 --- a/Console.cpp +++ b/Console.cpp @@ -53,21 +53,21 @@ VOID Print(_In_ Console::eColor nColor, _In_ LPCWSTR szFormatW, ...) CONSOLE_SCREEN_BUFFER_INFO sCsbi; va_list args; - if (nColor != ColorNormal) + if (nColor != Console::eColor::Normal) { ::GetConsoleScreenBufferInfo(hConsoleOut, &sCsbi); switch (nColor) { - case ColorError: + case Console::eColor::Error: ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_RED | FOREGROUND_INTENSITY); break; - case ColorSuccess: + case Console::eColor::Success: ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY); break; - case ColorYellow: + case Console::eColor::Yellow: ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); break; - case ColorBlue: + case Console::eColor::Blue: ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY); break; } @@ -77,7 +77,7 @@ VOID Print(_In_ Console::eColor nColor, _In_ LPCWSTR szFormatW, ...) vwprintf_s(szFormatW, args); va_end(args); - if (nColor != ColorNormal) + if (nColor != Console::eColor::Normal) { ::SetConsoleTextAttribute(hConsoleOut, sCsbi.wAttributes); } @@ -88,9 +88,9 @@ VOID Print(_In_ Console::eColor nColor, _In_ LPCWSTR szFormatW, ...) VOID PrintError(_In_ HRESULT hRes) { if (SUCCEEDED(hRes)) - Console::Print(ColorSuccess, L"OK"); + Console::Print(Console::eColor::Success, L"OK"); else - Console::Print(ColorError, L"ERROR: 0x%08X", hRes); + Console::Print(Console::eColor::Error, L"ERROR: 0x%08X", hRes); return; } diff --git a/Console.h b/Console.h index abb88a2..f3232ba 100644 --- a/Console.h +++ b/Console.h @@ -28,9 +28,10 @@ namespace MX { namespace Console { -typedef enum { - ColorNormal, ColorError, ColorSuccess, ColorYellow, ColorBlue -} eColor; +enum class eColor +{ + Normal, Error, Success, Yellow, Blue +} ; }; //namespace Console diff --git a/FileRoutines.cpp b/FileRoutines.cpp index 717b8d5..941207f 100644 --- a/FileRoutines.cpp +++ b/FileRoutines.cpp @@ -31,18 +31,21 @@ #define DELETE_RETRIES_COUNT 400 #define DELETE_RETRIES_DELAY_MS 15 +#ifndef FILE_SUPERSEDE + #define FILE_SUPERSEDE 0x00000000 +#endif //FILE_SUPERSEDE #ifndef FILE_OPEN #define FILE_OPEN 0x00000001 #endif //FILE_OPEN +#ifndef FILE_CREATE + #define FILE_CREATE 0x00000002 +#endif //FILE_CREATE #ifndef FILE_SYNCHRONOUS_IO_NONALERT #define FILE_SYNCHRONOUS_IO_NONALERT 0x00000020 #endif //FILE_SYNCHRONOUS_IO_NONALERT #ifndef FILE_NON_DIRECTORY_FILE #define FILE_NON_DIRECTORY_FILE 0x00000040 #endif //FILE_NON_DIRECTORY_FILE -#ifndef FILE_SEQUENTIAL_ONLY - #define FILE_SEQUENTIAL_ONLY 0x00000004 -#endif //FILE_SEQUENTIAL_ONLY #ifndef OBJ_CASE_INSENSITIVE #define OBJ_CASE_INSENSITIVE 0x00000040 @@ -471,7 +474,7 @@ HRESULT RemoveDirectoryRecursive(_In_ LPCWSTR szFolderNameW, _In_opt_ FileRoutin if (*szFolderNameW == 0) return E_INVALIDARG; - //trasverse folder + //transverse folder if (cStrTempW.Copy(szFolderNameW) == FALSE) return E_OUTOFMEMORY; nBaseLen = cStrTempW.GetLength(); @@ -519,7 +522,7 @@ HRESULT RemoveDirectoryRecursive(_In_ LPCWSTR szFolderNameW, _In_opt_ FileRoutin ::FindClose(hFind); //remove directory cStrTempW.Delete(nBaseLen-1, (SIZE_T)-1); //remove trailing slash - if (nDD == WaitUntilReboot) + if (nDD == eDelayedDelete::WaitUntilReboot) { if (::MoveFileExW((LPCWSTR)cStrTempW, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) == FALSE) return MX_HRESULT_FROM_LASTERROR(); @@ -545,7 +548,7 @@ HRESULT RemoveDirectoryRecursive(_In_ LPCWSTR szFolderNameW, _In_opt_ FileRoutin } if (i == 1) { - if (nDD == DeleteOnRebootOnFailure) + if (nDD == eDelayedDelete::DeleteOnRebootOnFailure) { if (::MoveFileExW((LPWSTR)cStrTempW, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) == FALSE) return MX_HRESULT_FROM_LASTERROR(); @@ -571,7 +574,7 @@ HRESULT _DeleteFile(_In_ LPCWSTR szFileNameW, _In_opt_ eDelayedDelete nDD) dw = ::GetFileAttributesW(szFileNameW); if (dw != INVALID_FILE_ATTRIBUTES && (dw & FILE_ATTRIBUTE_DIRECTORY) != 0) return S_OK; - if (nDD == WaitUntilReboot) + if (nDD == eDelayedDelete::WaitUntilReboot) { if (::MoveFileExW(szFileNameW, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) == FALSE) return MX_HRESULT_FROM_LASTERROR(); @@ -596,7 +599,7 @@ HRESULT _DeleteFile(_In_ LPCWSTR szFileNameW, _In_opt_ eDelayedDelete nDD) } if (dw == 1) { - if (nDD == DeleteOnRebootOnFailure) + if (nDD == eDelayedDelete::DeleteOnRebootOnFailure) { if (::MoveFileExW(szFileNameW, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) == FALSE) return MX_HRESULT_FROM_LASTERROR(); @@ -1177,7 +1180,7 @@ HRESULT GetFileNameFromHandle(_In_ HANDLE hFile, _Out_ CStringW &cStrFileNameW) HRESULT OpenFileWithEscalatingSharing(_In_z_ LPCWSTR szFileNameW, _Out_ HANDLE *lphFile) { - static const BYTE aSharingAccess[4] = { + static const BYTE aShareMode[4] = { FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_SHARE_READ | FILE_SHARE_DELETE, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SHARE_READ }; @@ -1206,8 +1209,8 @@ HRESULT OpenFileWithEscalatingSharing(_In_z_ LPCWSTR szFileNameW, _Out_ HANDLE * { ::MxMemSet(&sIoStatus, 0, sizeof(sIoStatus)); nNtStatus = ::MxNtCreateFile(lphFile, FILE_GENERIC_READ, &sObjAttrib, &sIoStatus, NULL, 0, - (ULONG)aSharingAccess[i], FILE_OPEN, FILE_NON_DIRECTORY_FILE | - FILE_SEQUENTIAL_ONLY | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); + (ULONG)aShareMode[i], FILE_OPEN, + FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if (NT_SUCCESS(nNtStatus)) return S_OK; hRes = MX_HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus)); @@ -1231,7 +1234,7 @@ HRESULT OpenFileWithEscalatingSharing(_In_z_ LPCWSTR szFileNameW, _Out_ HANDLE * ::Sleep(CREATE_RETRIES_DELAY_MS); for (i = 0; i < 4; i++) { - *lphFile = ::CreateFileW(szFileNameW, GENERIC_READ, (DWORD)aSharingAccess[i], NULL, OPEN_EXISTING, 0, NULL); + *lphFile = ::CreateFileW(szFileNameW, GENERIC_READ, (DWORD)aShareMode[i], NULL, OPEN_EXISTING, 0, NULL); if ((*lphFile) != NULL && (*lphFile) != INVALID_HANDLE_VALUE) return S_OK; hRes = MX_HRESULT_FROM_LASTERROR(); @@ -1246,6 +1249,90 @@ HRESULT OpenFileWithEscalatingSharing(_In_z_ LPCWSTR szFileNameW, _Out_ HANDLE * } } } + + //done + return hRes; +} + +HRESULT CreateFileWithOptions(_In_z_ LPCWSTR szFileNameW, _Out_ HANDLE *lphFile, _In_opt_ BOOL bCreateDirectory, + _In_opt_ BOOL bReplaceExisting, _In_opt_ DWORD dwSharedMode) +{ + DWORD dwRetry; + HRESULT hRes; + + if (bCreateDirectory != FALSE) + { + CStringW cStrTempW; + LPWSTR sW; + + if (cStrTempW.Copy(szFileNameW) == FALSE) + return E_OUTOFMEMORY; + MX::FileRoutines::NormalizePath(cStrTempW); + + sW = (LPWSTR)MX::StrChrW((LPCWSTR)cStrTempW, L'\\', TRUE); + if (sW != NULL) + *sW = 0; + MX::FileRoutines::CreateDirectoryRecursive((LPCWSTR)cStrTempW); //don't care about errors + } + + if (StrNCompareW(szFileNameW, L"\\??\\", 4) == 0 || StrNCompareW(szFileNameW, L"\\Device\\", 8, TRUE) == 0) + { + MX_OBJECT_ATTRIBUTES sObjAttrib; + MX_IO_STATUS_BLOCK sIoStatus; + MX_UNICODE_STRING usFileName; + NTSTATUS nNtStatus; + + ::MxMemSet(&sObjAttrib, 0, sizeof(sObjAttrib)); + sObjAttrib.Length = (ULONG)sizeof(sObjAttrib); + sObjAttrib.Attributes = OBJ_CASE_INSENSITIVE; + sObjAttrib.ObjectName = &usFileName; + usFileName.Buffer = (PWSTR)szFileNameW; + usFileName.Length = usFileName.MaximumLength = (USHORT)(StrLenW(szFileNameW) * 2); + for (dwRetry = CREATE_RETRIES_COUNT; dwRetry > 0; dwRetry--) + { + if (dwRetry != CREATE_RETRIES_COUNT) + ::Sleep(CREATE_RETRIES_DELAY_MS); + ::MxMemSet(&sIoStatus, 0, sizeof(sIoStatus)); + nNtStatus = ::MxNtCreateFile(lphFile, FILE_GENERIC_READ | FILE_GENERIC_WRITE, &sObjAttrib, &sIoStatus, + NULL, FILE_ATTRIBUTE_NORMAL, (ULONG)dwSharedMode, + ((bReplaceExisting != FALSE) ? FILE_SUPERSEDE : FILE_CREATE), + FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); + if (NT_SUCCESS(nNtStatus)) + return S_OK; + hRes = MX_HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus)); + *lphFile = NULL; + + if (hRes != HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION) && hRes != HRESULT_FROM_WIN32(ERROR_LOCK_VIOLATION) && + hRes != E_ACCESSDENIED) + { + break; + } + } + } + else + { + //NOTE: CreateFileW adds FILE_NON_DIRECTORY_FILE flag if FILE_FLAG_BACKUP_SEMANTICS is not specified + for (dwRetry = CREATE_RETRIES_COUNT; dwRetry > 0; dwRetry--) + { + if (dwRetry != CREATE_RETRIES_COUNT) + ::Sleep(CREATE_RETRIES_DELAY_MS); + + *lphFile = ::CreateFileW(szFileNameW, GENERIC_READ | GENERIC_WRITE, (DWORD)dwSharedMode, NULL, + ((bReplaceExisting != FALSE) ? CREATE_ALWAYS : CREATE_NEW), FILE_ATTRIBUTE_NORMAL, + NULL); + if ((*lphFile) != NULL && (*lphFile) != INVALID_HANDLE_VALUE) + return S_OK; + hRes = MX_HRESULT_FROM_LASTERROR(); + *lphFile = NULL; + if (hRes != HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION) && hRes != HRESULT_FROM_WIN32(ERROR_LOCK_VIOLATION) && + hRes != E_ACCESSDENIED) + { + break; + } + } + } + + //done return hRes; } diff --git a/FileRoutines.h b/FileRoutines.h index 35e6804..4b45494 100644 --- a/FileRoutines.h +++ b/FileRoutines.h @@ -30,11 +30,12 @@ namespace MX { namespace FileRoutines { -typedef enum { +enum class eDelayedDelete +{ DontTryDeleteOnReboot, DeleteOnRebootOnFailure, WaitUntilReboot -} eDelayedDelete; +} ; }; //namespace FileRoutines @@ -68,12 +69,12 @@ HRESULT _GetTempPath(_Out_ CStringW &cStrDestW); HRESULT CreateDirectoryRecursive(_In_ LPCWSTR szFolderNameW); HRESULT RemoveDirectoryRecursive(_In_ LPCWSTR szFolderNameW, - _In_opt_ FileRoutines::eDelayedDelete nDD = FileRoutines::DontTryDeleteOnReboot); + _In_opt_ FileRoutines::eDelayedDelete nDD = FileRoutines::eDelayedDelete::DontTryDeleteOnReboot); HRESULT _DeleteFile(_In_ LPCWSTR szFileNameW, - _In_opt_ FileRoutines::eDelayedDelete nDD = FileRoutines::DontTryDeleteOnReboot); + _In_opt_ FileRoutines::eDelayedDelete nDD = FileRoutines::eDelayedDelete::DontTryDeleteOnReboot); HRESULT DeleteDirectoryFiles(_In_ LPCWSTR szFolderNameW, - _In_opt_ FileRoutines::eDelayedDelete nDD = FileRoutines::DontTryDeleteOnReboot); + _In_opt_ FileRoutines::eDelayedDelete nDD = FileRoutines::eDelayedDelete::DontTryDeleteOnReboot); VOID NormalizePath(_Inout_ CStringW &cStrPathW); @@ -89,6 +90,8 @@ HRESULT ResolveSymbolicLink(_Inout_ CStringW &cStrPathW); HRESULT GetFileNameFromHandle(_In_ HANDLE hFile, _Out_ CStringW &cStrFileNameW); HRESULT OpenFileWithEscalatingSharing(_In_z_ LPCWSTR szFileNameW, _Out_ HANDLE *lphFile); +HRESULT CreateFileWithOptions(_In_z_ LPCWSTR szFileNameW, _Out_ HANDLE *lphFile, _In_opt_ BOOL bCreateDirectory = TRUE, + _In_opt_ BOOL bReplaceExisting = TRUE, _In_opt_ DWORD dwSharedMode = 0); }; //namespace FileRoutines diff --git a/LightJSonBuilder.cpp b/LightJSonBuilder.cpp index 92a4107..53c732f 100644 --- a/LightJSonBuilder.cpp +++ b/LightJSonBuilder.cpp @@ -74,7 +74,7 @@ BOOL CLightJSonBuilder::AddObject(_In_opt_z_ LPCSTR szNameA) { if (cStrJsonA.ConcatN("\"", 1) == FALSE) return FALSE; - if (cStrJsonA.Concat(szNameA) == FALSE) + if (EscapeString(cStrJsonA, szNameA, StrLenA(szNameA), TRUE) == FALSE) return FALSE; return cStrJsonA.ConcatN("\": { ", 5); } @@ -127,7 +127,7 @@ BOOL CLightJSonBuilder::AddArray(_In_opt_z_ LPCSTR szNameA) { if (cStrJsonA.ConcatN("\"", 1) == FALSE) return FALSE; - if (cStrJsonA.Concat(szNameA) == FALSE) + if (EscapeString(cStrJsonA, szNameA, StrLenA(szNameA), TRUE) == FALSE) return FALSE; return cStrJsonA.ConcatN("\": [ ", 5); } @@ -168,7 +168,7 @@ BOOL CLightJSonBuilder::AddObjectBoolean(_In_z_ LPCSTR szNameA, _In_ BOOL bValue //insert text if (cStrJsonA.ConcatN("\"", 1) == FALSE) return FALSE; - if (cStrJsonA.Concat(szNameA) == FALSE) + if (EscapeString(cStrJsonA, szNameA, StrLenA(szNameA), TRUE) == FALSE) return FALSE; if (bValue != FALSE) { @@ -208,7 +208,7 @@ BOOL CLightJSonBuilder::AddObjectString(_In_z_ LPCSTR szNameA, _In_ LPCSTR szVal //insert text if (cStrJsonA.ConcatN("\"", 1) == FALSE) return FALSE; - if (cStrJsonA.Concat(szNameA) == FALSE) + if (EscapeString(cStrJsonA, szNameA, StrLenA(szNameA), TRUE) == FALSE) return FALSE; if (cStrJsonA.ConcatN("\": \"", 4) == FALSE) return FALSE; @@ -258,7 +258,7 @@ BOOL CLightJSonBuilder::AddObjectString(_In_z_ LPCSTR szNameA, _In_ LPCWSTR szVa //insert text if (cStrJsonA.ConcatN("\"", 1) == FALSE) return FALSE; - if (cStrJsonA.Concat(szNameA) == FALSE) + if (EscapeString(cStrJsonA, szNameA, StrLenA(szNameA), TRUE) == FALSE) return FALSE; if (cStrJsonA.ConcatN("\": \"", 4) == FALSE) return FALSE; @@ -306,7 +306,7 @@ BOOL CLightJSonBuilder::AddObjectString(_In_z_ LPCSTR szNameA, _In_ PUNICODE_STR //insert text if (cStrJsonA.ConcatN("\"", 1) == FALSE) return FALSE; - if (cStrJsonA.Concat(szNameA) == FALSE) + if (EscapeString(cStrJsonA, szNameA, StrLenA(szNameA), TRUE) == FALSE) return FALSE; if (cStrJsonA.ConcatN("\": \"", 4) == FALSE) return FALSE; @@ -334,7 +334,11 @@ BOOL CLightJSonBuilder::AddObjectLong(_In_z_ LPCSTR szNameA, _In_ LONG nValue) } //insert text - return cStrJsonA.AppendFormat("\"%s\": %ld", szNameA, nValue); + if (cStrJsonA.ConcatN("\"", 1) == FALSE) + return FALSE; + if (EscapeString(cStrJsonA, szNameA, StrLenA(szNameA), TRUE) == FALSE) + return FALSE; + return cStrJsonA.AppendFormat("\": %ld", nValue); } BOOL CLightJSonBuilder::AddObjectULong(_In_z_ LPCSTR szNameA, _In_ ULONG nValue, _In_opt_ BOOL bAsHexa) @@ -356,7 +360,11 @@ BOOL CLightJSonBuilder::AddObjectULong(_In_z_ LPCSTR szNameA, _In_ ULONG nValue, } //insert text - return cStrJsonA.AppendFormat((bAsHexa == FALSE) ? "\"%s\": %lu" : "\"%s\": \"0x%08X\"", szNameA, nValue); + if (cStrJsonA.ConcatN("\"", 1) == FALSE) + return FALSE; + if (EscapeString(cStrJsonA, szNameA, StrLenA(szNameA), TRUE) == FALSE) + return FALSE; + return cStrJsonA.AppendFormat(((bAsHexa == FALSE) ? "\": %lu" : "\": \"0x%08X\""), nValue); } BOOL CLightJSonBuilder::AddObjectLongLong(_In_z_ LPCSTR szNameA, _In_ LONGLONG nValue) @@ -378,7 +386,11 @@ BOOL CLightJSonBuilder::AddObjectLongLong(_In_z_ LPCSTR szNameA, _In_ LONGLONG n } //insert text - return cStrJsonA.AppendFormat("\"%s\": \"%I64d\"", szNameA, nValue); + if (cStrJsonA.ConcatN("\"", 1) == FALSE) + return FALSE; + if (EscapeString(cStrJsonA, szNameA, StrLenA(szNameA), TRUE) == FALSE) + return FALSE; + return cStrJsonA.AppendFormat("\": \"%I64d\"", nValue); } BOOL CLightJSonBuilder::AddObjectULongLong(_In_z_ LPCSTR szNameA, _In_ ULONGLONG nValue, _In_opt_ BOOL bAsHexa) @@ -400,7 +412,11 @@ BOOL CLightJSonBuilder::AddObjectULongLong(_In_z_ LPCSTR szNameA, _In_ ULONGLONG } //insert text - return cStrJsonA.AppendFormat((bAsHexa == FALSE) ? "\"%s\": \"%I64u\"" : "\"%s\": \"0x%016I64X\"", szNameA, nValue); + if (cStrJsonA.ConcatN("\"", 1) == FALSE) + return FALSE; + if (EscapeString(cStrJsonA, szNameA, StrLenA(szNameA), TRUE) == FALSE) + return FALSE; + return cStrJsonA.AppendFormat(((bAsHexa == FALSE) ? "\": \"%I64u\"" : "\": \"0x%016I64X\""), nValue); } BOOL CLightJSonBuilder::AddObjectObject(_In_z_ LPCSTR szNameA, _In_ CLightJSonBuilder &cSrc) @@ -422,7 +438,13 @@ BOOL CLightJSonBuilder::AddObjectObject(_In_z_ LPCSTR szNameA, _In_ CLightJSonBu } //insert text - return cStrJsonA.AppendFormat("\"%s\": %s", szNameA, (LPCSTR)cSrc); + if (cStrJsonA.ConcatN("\"", 1) == FALSE) + return FALSE; + if (EscapeString(cStrJsonA, szNameA, StrLenA(szNameA), TRUE) == FALSE) + return FALSE; + if (cStrJsonA.ConcatN("\": ", 3) == FALSE) + return FALSE; + return cStrJsonA.ConcatN((LPCSTR)cSrc, cSrc.GetLength()); } BOOL CLightJSonBuilder::AddArrayBoolean(_In_ BOOL bValue) @@ -640,7 +662,7 @@ BOOL CLightJSonBuilder::AddArrayObject(_In_ CLightJSonBuilder &cSrc) } //insert text - return cStrJsonA.Concat((LPCSTR)cSrc); + return cStrJsonA.ConcatN((LPCSTR)cSrc, cSrc.GetLength()); } BOOL CLightJSonBuilder::AddRaw(_In_ LPCSTR szStrA, _In_opt_ SIZE_T nStrLen) @@ -663,8 +685,7 @@ BOOL CLightJSonBuilder::AddRaw(_In_ LPCWSTR szStrW, _In_opt_ SIZE_T nStrLen) return (SUCCEEDED(Utf8_Encode(cStrJsonA, szStrW, nStrLen, TRUE))) ? TRUE : FALSE; } -BOOL CLightJSonBuilder::EscapeString(_Inout_ CStringA &cStrA, _In_ LPCSTR szValueA, _In_ SIZE_T nValueLen, - _In_opt_ BOOL bAppend) +BOOL CLightJSonBuilder::EscapeString(_Inout_ CStringA &cStrA, _In_ LPCSTR szValueA, _In_ SIZE_T nValueLen, _In_opt_ BOOL bAppend) { LPCSTR szStartA, szValueEndA; @@ -736,8 +757,7 @@ BOOL CLightJSonBuilder::EscapeString(_Inout_ CStringA &cStrA, _In_ LPCSTR szValu return TRUE; } -BOOL CLightJSonBuilder::EscapeString(_Inout_ CStringA &cStrA, _In_ LPCWSTR szValueW, _In_ SIZE_T nValueLen, - _In_opt_ BOOL bAppend) +BOOL CLightJSonBuilder::EscapeString(_Inout_ CStringA &cStrA, _In_ LPCWSTR szValueW, _In_ SIZE_T nValueLen, _In_opt_ BOOL bAppend) { LPCWSTR szValueEndW; CHAR szDestA[6]; diff --git a/MXCommonLibrariesHelpers.sln b/MXCommonLibrariesHelpers.sln new file mode 100644 index 0000000..478a8b8 --- /dev/null +++ b/MXCommonLibrariesHelpers.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.34114.132 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MXCommonLibrariesHelpers", "MXCommonLibrariesHelpers.vcxproj", "{AB65FB1A-74DF-40F0-BB4B-B9C90383E41D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AB65FB1A-74DF-40F0-BB4B-B9C90383E41D}.Debug|x64.ActiveCfg = Debug|x64 + {AB65FB1A-74DF-40F0-BB4B-B9C90383E41D}.Debug|x64.Build.0 = Debug|x64 + {AB65FB1A-74DF-40F0-BB4B-B9C90383E41D}.Debug|x86.ActiveCfg = Debug|Win32 + {AB65FB1A-74DF-40F0-BB4B-B9C90383E41D}.Debug|x86.Build.0 = Debug|Win32 + {AB65FB1A-74DF-40F0-BB4B-B9C90383E41D}.Release|x64.ActiveCfg = Release|x64 + {AB65FB1A-74DF-40F0-BB4B-B9C90383E41D}.Release|x64.Build.0 = Release|x64 + {AB65FB1A-74DF-40F0-BB4B-B9C90383E41D}.Release|x86.ActiveCfg = Release|Win32 + {AB65FB1A-74DF-40F0-BB4B-B9C90383E41D}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {03FD83C3-F114-41F1-AEBC-63AB38EB8B01} + EndGlobalSection +EndGlobal diff --git a/MXCommonLibrariesHelpers.vcxproj b/MXCommonLibrariesHelpers.vcxproj new file mode 100644 index 0000000..61fda3e --- /dev/null +++ b/MXCommonLibrariesHelpers.vcxproj @@ -0,0 +1,224 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + + + + + + + + + + + + + + + + true + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + true + true + true + + + + + + + 16.0 + Win32Proj + {ab65fb1a-74df-40f0-bb4b-b9c90383e41d} + MXCommonLibrariesHelpers + 10.0 + + + + StaticLibrary + true + v142 + Unicode + + + StaticLibrary + false + v142 + true + Unicode + + + StaticLibrary + true + v142 + Unicode + + + StaticLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + lib\$(PlatformShortName)\$(Configuration)\ + obj\$(PlatformShortName)\$(Configuration)\ + + + false + lib\$(PlatformShortName)\$(Configuration)\ + obj\$(PlatformShortName)\$(Configuration)\ + + + true + lib\$(PlatformShortName)\$(Configuration)\ + obj\$(PlatformShortName)\$(Configuration)\ + + + false + lib\$(PlatformShortName)\$(Configuration)\ + obj\$(PlatformShortName)\$(Configuration)\ + + + + Level3 + true + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + ..\MXCommonLibraries\Include + + + + + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + ..\MXCommonLibraries\Include + + + + + true + true + true + + + + + Level3 + true + _DEBUG;_LIB;%(PreprocessorDefinitions) + true + ..\MXCommonLibraries\Include + + + + + true + + + + + Level3 + true + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + true + ..\MXCommonLibraries\Include + + + + + true + true + true + + + + + + \ No newline at end of file diff --git a/MXCommonLibrariesHelpers.vcxproj.filters b/MXCommonLibrariesHelpers.vcxproj.filters new file mode 100644 index 0000000..f5646d4 --- /dev/null +++ b/MXCommonLibrariesHelpers.vcxproj.filters @@ -0,0 +1,167 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + Header Files + + + \ No newline at end of file diff --git a/MemoryPackage.cpp b/MemoryPackage.cpp index fc88374..95eba42 100644 --- a/MemoryPackage.cpp +++ b/MemoryPackage.cpp @@ -42,7 +42,7 @@ class CFileStream : public CStream HRESULT Write(_In_reads_bytes_(nBytes) LPCVOID lpSrc, _In_ SIZE_T nBytes, _Out_ SIZE_T &nWritten, _In_opt_ ULONGLONG nStartOffset = ULONGLONG_MAX); - HRESULT Seek(_In_ ULONGLONG nPosition, _In_opt_ eSeekMethod nMethod = SeekStart); + HRESULT Seek(_In_ ULONGLONG nPosition, _In_opt_ eSeekMethod nMethod = eSeekMethod::Start); ULONGLONG GetLength() const; @@ -586,7 +586,7 @@ HRESULT CFileStream::Read(_Out_writes_bytes_(nRead) LPVOID lpDest, _In_ SIZE_T n { //NOTE: CFileStream::Read is only called during HTML output while sending data to socket so no problem to // modify the current offset with seek. - hRes = Seek(nStartOffset, MX::CStream::SeekStart); + hRes = Seek(nStartOffset, eSeekMethod::Start); if (FAILED(hRes)) return hRes; } @@ -614,7 +614,7 @@ HRESULT CFileStream::Read(_Out_writes_bytes_(nRead) LPVOID lpDest, _In_ SIZE_T n { if ((nAvailable = cDecompressor->GetAvailableData()) > 0) { - //process alredy decompressed data + //process already decompressed data if (nBytesToSkip > 0) { if ((ULONGLONG)nAvailable > nBytesToSkip) @@ -635,7 +635,7 @@ HRESULT CFileStream::Read(_Out_writes_bytes_(nRead) LPVOID lpDest, _In_ SIZE_T n } else { - //feed the decompressor with compressed data + //feed the decompresser with compressed data //calculate hash nCurrHash = (ULONGLONG)((SIZE_T)(sCompressedData.lpNext - sCompressedData.lpStart)); nCurrHash = fnv_64a_buf(&nCurrHash, sizeof(ULONGLONG), nFileHash); @@ -689,12 +689,12 @@ HRESULT CFileStream::Seek(_In_ ULONGLONG nPosition, _In_opt_ eSeekMethod nMethod { switch (nMethod) { - case SeekStart: + case eSeekMethod::Start: if (nPosition > nUncompressedSize) nPosition = nUncompressedSize; break; - case SeekCurrent: + case eSeekMethod::Current: if ((LONGLONG)nPosition >= 0) { if (nPosition > nUncompressedSize - nOffset) @@ -710,7 +710,7 @@ HRESULT CFileStream::Seek(_In_ ULONGLONG nPosition, _In_opt_ eSeekMethod nMethod } break; - case SeekEnd: + case eSeekMethod::End: if (nPosition > nUncompressedSize) nPosition = nUncompressedSize; nPosition = nUncompressedSize - nPosition; diff --git a/Network.cpp b/Network.cpp index dbbd569..5a5cc75 100644 --- a/Network.cpp +++ b/Network.cpp @@ -31,7 +31,7 @@ namespace MX { namespace Network { -HRESULT GetLocalIpAddresses(_Out_ TArrayListWithFree &cStrListW, _In_ int nFlags) +HRESULT GetLocalIpAddresses(_Out_ TArrayListWithFree &cStrListW, _In_ eLocalIpAddressesFlags nFlags) { TAutoFreePtr cIpAddrBuffer; PIP_ADAPTER_ADDRESSES lpCurrAdapter; @@ -83,10 +83,10 @@ HRESULT GetLocalIpAddresses(_Out_ TArrayListWithFree &cStrListW, _In_ i switch (lpCurrUnicastAddress->Address.lpSockaddr->sa_family) { case AF_INET: - if ((nFlags & LocalIpAddressesFlagsDontAddIpV4) != 0) + if ((nFlags & eLocalIpAddressesFlags::DontAddIpV4) != (eLocalIpAddressesFlags)0) break; u.lpAddrV4 = (sockaddr_in*)(lpCurrUnicastAddress->Address.lpSockaddr); - //ignore zero & localhost + //ignore zero & local host if (u.lpAddrV4->sin_addr.S_un.S_un_b.s_b2 == 0 && u.lpAddrV4->sin_addr.S_un.S_un_b.s_b3 == 0) { if ((u.lpAddrV4->sin_addr.S_un.S_un_b.s_b1 == 0 && u.lpAddrV4->sin_addr.S_un.S_un_b.s_b4 == 0) || @@ -106,7 +106,7 @@ HRESULT GetLocalIpAddresses(_Out_ TArrayListWithFree &cStrListW, _In_ i break; case AF_INET6: - if ((nFlags & LocalIpAddressesFlagsDontAddIpV6) != 0) + if ((nFlags & eLocalIpAddressesFlags::DontAddIpV6) != (eLocalIpAddressesFlags)0) break; u.lpAddrV6 = (SOCKADDR_IN6_W2KSP1*)(lpCurrUnicastAddress->Address.lpSockaddr); //ignore zero & localhost @@ -135,7 +135,7 @@ HRESULT GetLocalIpAddresses(_Out_ TArrayListWithFree &cStrListW, _In_ i } } - if ((nFlags & LocalIpAddressesFlagsDontAddNetbiosName) == 0) + if ((nFlags & eLocalIpAddressesFlags::DontAddNetbiosName) == (eLocalIpAddressesFlags)0) { hRes = System::_GetComputerNameEx(ComputerNameDnsFullyQualified, cStrTempW); if (FAILED(hRes)) diff --git a/Network.h b/Network.h index c666186..87443d0 100644 --- a/Network.h +++ b/Network.h @@ -32,11 +32,22 @@ namespace MX { namespace Network { -typedef enum { - LocalIpAddressesFlagsDontAddIpV4 = 1, - LocalIpAddressesFlagsDontAddIpV6 = 2, - LocalIpAddressesFlagsDontAddNetbiosName = 4 -} eGetLocalIpAddressesFlags; +enum class eLocalIpAddressesFlags +{ + DontAddIpV4 = 1, + DontAddIpV6 = 2, + DontAddNetbiosName = 4 +}; + +inline eLocalIpAddressesFlags operator|(eLocalIpAddressesFlags lhs, eLocalIpAddressesFlags rhs) +{ + return static_cast(static_cast(lhs) | static_cast(rhs)); +} + +inline eLocalIpAddressesFlags operator&(eLocalIpAddressesFlags lhs, eLocalIpAddressesFlags rhs) +{ + return static_cast(static_cast(lhs) & static_cast(rhs)); +} }; //namespace Network @@ -48,7 +59,7 @@ namespace MX { namespace Network { -HRESULT GetLocalIpAddresses(_Out_ TArrayListWithFree &cStrListW, _In_ int nFlags); +HRESULT GetLocalIpAddresses(_Out_ TArrayListWithFree &cStrListW, _In_ eLocalIpAddressesFlags nFlags); HRESULT FormatIpAddress(_Out_ CStringW &cStrW, _In_ PSOCKADDR_INET lpAddr); }; //namespace Network diff --git a/PeParser.cpp b/PeParser.cpp index a3b8786..fc9e223 100644 --- a/PeParser.cpp +++ b/PeParser.cpp @@ -44,7 +44,6 @@ namespace MX { CPEParser::CPEParser() : CBaseMemObj(), CNonCopyableObj() { - ClearVars(); return; } @@ -290,14 +289,15 @@ LPBYTE CPEParser::RvaToVa(_In_ DWORD dwVirtualAddress) if (dwVirtualAddress >= lpFileImgSect[i].VirtualAddress && dwVirtualAddress < lpFileImgSect[i].VirtualAddress + lpFileImgSect[i].Misc.VirtualSize) { - return lpBaseAddress + (SIZE_T)(dwVirtualAddress - lpFileImgSect[i].VirtualAddress + - lpFileImgSect[i].PointerToRawData); + return lpBaseAddress + (SIZE_T)dwVirtualAddress - (SIZE_T)(lpFileImgSect[i].VirtualAddress) + + (SIZE_T)(lpFileImgSect[i].PointerToRawData); } } return NULL; } -BOOL CPEParser::ReadRaw(_Out_writes_(nBytes) LPVOID lpDest, _In_ LPCVOID lpSrc, _In_ SIZE_T nBytes) +_Success_(return != FALSE) +BOOL CPEParser::ReadRaw(_Out_writes_bytes_(nBytes) LPVOID lpDest, _In_ LPCVOID lpSrc, _In_ SIZE_T nBytes) { SIZE_T nOffset; diff --git a/PeParser.h b/PeParser.h index db49e80..85c1ba9 100644 --- a/PeParser.h +++ b/PeParser.h @@ -143,7 +143,8 @@ class CPEParser : public CBaseMemObj, public CNonCopyableObj //NOTE: Returns NULL if invalid RVA LPBYTE RvaToVa(_In_ DWORD dwVirtualAddress); - BOOL ReadRaw(_Out_writes_(nBytes) LPVOID lpDest, _In_ LPCVOID lpSrc, _In_ SIZE_T nBytes); + _Success_(return != FALSE) + BOOL ReadRaw(_Out_writes_bytes_(nBytes) LPVOID lpDest, _In_ LPCVOID lpSrc, _In_ SIZE_T nBytes); HRESULT ReadAnsiString(_Out_ CStringA &cStrA, _In_ LPVOID lpNameAddress, _In_ SIZE_T nMaxLength); private: @@ -160,31 +161,32 @@ class CPEParser : public CBaseMemObj, public CNonCopyableObj _In_ LPCWSTR szKeyW, _Out_ PIMAGE_RESOURCE_DIRECTORY_ENTRY *lplpDirEntry); private: - HANDLE hFile; - HANDLE hProc; + HANDLE hFile{ NULL }; + HANDLE hProc{ NULL }; - LPBYTE lpBaseAddress; - SIZE_T nDataSize; - BOOL bImageIsMapped; + LPBYTE lpBaseAddress{ NULL }; + SIZE_T nDataSize{ 0 }; + BOOL bImageIsMapped{ FALSE }; struct { - BYTE aBuffer[8192]; - SIZE_T nOffset, nLength; + BYTE aBuffer[8192]{}; + SIZE_T nOffset{ 0 }, nLength{ 0 }; } sFileCache; - WORD wMachine; - LPVOID lpOriginalImageBaseAddress; + WORD wMachine{ 0 }; + LPVOID lpOriginalImageBaseAddress{ NULL }; - IMAGE_DOS_HEADER sDosHdr; + IMAGE_DOS_HEADER sDosHdr{}; union { IMAGE_NT_HEADERS32 s32; #if defined(_M_X64) IMAGE_NT_HEADERS64 s64; #endif //_M_X64 - } uNtHdr; + } uNtHdr{}; - SIZE_T nSectionsCount; + SIZE_T nSectionsCount{ 0 }; TAutoFreePtr cFileImgSect; +private: class CImportedDll : public CBaseMemObj { public: @@ -197,16 +199,16 @@ class CPEParser : public CBaseMemObj, public CNonCopyableObj } sImportsInfo; struct { - DWORD dwCharacteristics; - WORD wMajorVersion; - WORD wMinorVersion; + DWORD dwCharacteristics{ 0 }; + WORD wMajorVersion{ 0 }; + WORD wMinorVersion{ 0 }; TArrayListWithFree aEntries; } sExportsInfo; - PIMAGE_RESOURCE_DIRECTORY lpResourceDir; + PIMAGE_RESOURCE_DIRECTORY lpResourceDir{ NULL }; TAutoFreePtr cVersionInfo; - SIZE_T nVersionInfoSize; + SIZE_T nVersionInfoSize{ 0 }; }; }; //namespace MX diff --git a/Process.cpp b/Process.cpp index fe52511..8cba08a 100644 --- a/Process.cpp +++ b/Process.cpp @@ -297,7 +297,7 @@ HRESULT GetProcessMembershipType(_Out_ Process::eTokenGetMembershipType &nType) HANDLE hToken; HRESULT hRes; - nType = TokenMembershipTypeLimitedUser; + nType = eTokenGetMembershipType::LimitedUser; if (::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE, &hToken) != FALSE) { hRes = GetTokenMembershipType(hToken, nType); @@ -315,7 +315,7 @@ HRESULT GetThreadMembershipType(_Out_ Process::eTokenGetMembershipType &nType) HANDLE hToken; HRESULT hRes; - nType = TokenMembershipTypeLimitedUser; + nType = eTokenGetMembershipType::LimitedUser; if (::OpenThreadToken(::GetCurrentThread(), TOKEN_QUERY | TOKEN_DUPLICATE, TRUE, &hToken) != FALSE) { hRes = GetTokenMembershipType(hToken, nType); @@ -341,19 +341,19 @@ HRESULT GetTokenMembershipType(_In_ HANDLE hToken, _Out_ Process::eTokenGetMembe HANDLE hTokenToCheck = NULL; HRESULT hRes; + nType = Process::eTokenGetMembershipType::LimitedUser; if (::DuplicateToken(hToken, SecurityIdentification, &hTokenToCheck) == FALSE) - { - nType = Process::TokenMembershipTypeLimitedUser; return MX_HRESULT_FROM_LASTERROR(); - } + //check if system account b = FALSE; if (::CheckTokenMembership(hTokenToCheck, (PSID)&sLocalSystemSID, &b) != FALSE && b != FALSE) { - nType = TokenMembershipTypeRunningInSystemAccount; + nType = eTokenGetMembershipType::SystemAccount; hRes = S_OK; goto done; } + //on Vista+, check if we are elevated if (IsWinVistaPlus() != FALSE) { @@ -364,7 +364,7 @@ HRESULT GetTokenMembershipType(_In_ HANDLE hToken, _Out_ Process::eTokenGetMembe if (::GetTokenInformation(hTokenToCheck, TokenElevation, &sTokElev, (DWORD)sizeof(sTokElev), &dw) != FALSE && sTokElev.TokenIsElevated != 0) { - nType = TokenMembershipTypeRunningOnAdministratorsGroupAndElevated; + nType = eTokenGetMembershipType::AdministratorsGroupAndElevated; hRes = S_OK; goto done; } @@ -394,7 +394,7 @@ HRESULT GetTokenMembershipType(_In_ HANDLE hToken, _Out_ Process::eTokenGetMembe b = FALSE; if (::CheckTokenMembership(hTokenToCheck, (PSID)&sAdminsSID, &b) != FALSE && b != FALSE) { - nType = TokenMembershipTypeRunningOnAdministratorsGroup; + nType = eTokenGetMembershipType::AdministratorsGroup; hRes = S_OK; goto done; } @@ -405,7 +405,7 @@ HRESULT GetTokenMembershipType(_In_ HANDLE hToken, _Out_ Process::eTokenGetMembe b = FALSE; if (::CheckTokenMembership(hTokenToCheck, (PSID)&sAdminsSID, &b) != FALSE && b != FALSE) { - nType = TokenMembershipTypeRunningOnAdministratorsGroupAndElevated; + nType = eTokenGetMembershipType::AdministratorsGroupAndElevated; hRes = S_OK; goto done; } diff --git a/Process.h b/Process.h index 4cfaeed..9029c54 100644 --- a/Process.h +++ b/Process.h @@ -30,12 +30,13 @@ namespace MX { namespace Process { -typedef enum { - TokenMembershipTypeLimitedUser, - TokenMembershipTypeRunningInSystemAccount, - TokenMembershipTypeRunningOnAdministratorsGroup, - TokenMembershipTypeRunningOnAdministratorsGroupAndElevated -} eTokenGetMembershipType; +enum class eTokenGetMembershipType +{ + LimitedUser, + SystemAccount, + AdministratorsGroup, + AdministratorsGroupAndElevated +}; }; //Process diff --git a/ResourceExtract.cpp b/ResourceExtract.cpp index 199efc9..83b47ff 100644 --- a/ResourceExtract.cpp +++ b/ResourceExtract.cpp @@ -137,7 +137,7 @@ HRESULT ExtractToStream(_In_ HINSTANCE hInst, _In_ LPCWSTR szResNameW, _In_ LPCW if (SUCCEEDED(hRes) && (SIZE_T)dwResSize != nWritten) hRes = MX_E_WriteFault; if (SUCCEEDED(hRes)) - hRes = cStream->Seek(0, CStream::SeekStart); + hRes = cStream->Seek(0, CStream::eSeekMethod::Start); if (FAILED(hRes)) return hRes; //done diff --git a/ServiceManager.cpp b/ServiceManager.cpp index 1ee70b4..a78d33f 100644 --- a/ServiceManager.cpp +++ b/ServiceManager.cpp @@ -39,9 +39,9 @@ class CRegistryBackupValue : public MX::CBaseMemObj { public: CStringW cStrValueNameW; - DWORD dwType; + DWORD dwType{ 0 }; TAutoFreePtr cData; - SIZE_T nDataSize; + SIZE_T nDataSize{ 0 }; }; class CRegistryBackupKey : public MX::CBaseMemObj @@ -74,7 +74,6 @@ namespace MX { CServiceManager::CServiceManager() : CBaseMemObj(), CNonCopyableObj() { - hServMgr = hServ = NULL; return; } @@ -169,15 +168,15 @@ HRESULT CServiceManager::Create(_In_z_ LPCWSTR szServiceNameW, _In_ LPCREATEINFO dwServiceType = SERVICE_WIN32_OWN_PROCESS; switch (lpCreateInfo->nServiceType) { - case ServiceTypeLocalSystem: - case ServiceTypeNetworkService: + case eServiceType::LocalSystem: + case eServiceType::NetworkService: break; - case ServiceTypeKernelDriver: + case eServiceType::KernelDriver: dwServiceType = SERVICE_KERNEL_DRIVER; break; - case ServiceTypeFileSystemDriver: + case eServiceType::FileSystemDriver: dwServiceType = SERVICE_FILE_SYSTEM_DRIVER; break; @@ -199,7 +198,7 @@ HRESULT CServiceManager::Create(_In_z_ LPCWSTR szServiceNameW, _In_ LPCREATEINFO NULL, ((lpCreateInfo->szDependenciesW != NULL && *(lpCreateInfo->szDependenciesW) != 0) ? lpCreateInfo->szDependenciesW : NULL), - (lpCreateInfo->nServiceType != ServiceTypeNetworkService) ? NULL : szNetworkServiceAccountW, + (lpCreateInfo->nServiceType != eServiceType::NetworkService) ? NULL : szNetworkServiceAccountW, NULL); if (hServ == NULL) { @@ -219,8 +218,8 @@ HRESULT CServiceManager::Create(_In_z_ LPCWSTR szServiceNameW, _In_ LPCREATEINFO NULL, ((lpCreateInfo->szDependenciesW != NULL && *(lpCreateInfo->szDependenciesW) != 0) ? lpCreateInfo->szDependenciesW : L"\0"), - (lpCreateInfo->nServiceType != ServiceTypeNetworkService) ? NULL - : szNetworkServiceAccountW, + (lpCreateInfo->nServiceType != eServiceType::NetworkService) + ? NULL : szNetworkServiceAccountW, NULL, lpCreateInfo->szServiceDisplayNameW) == FALSE) { hRes = MX_HRESULT_FROM_LASTERROR(); @@ -250,9 +249,9 @@ HRESULT CServiceManager::Create(_In_z_ LPCWSTR szServiceNameW, _In_ LPCREATEINFO ? lpCreateInfo->szLoadOrderGroupW : NULL), NULL, ((lpCreateInfo->szDependenciesW != NULL && *(lpCreateInfo->szDependenciesW) != 0) - ? lpCreateInfo->szDependenciesW : NULL), - (lpCreateInfo->nServiceType != ServiceTypeNetworkService) - ? NULL : szNetworkServiceAccountW, + ? lpCreateInfo->szDependenciesW : NULL), + (lpCreateInfo->nServiceType != eServiceType::NetworkService) + ? NULL : szNetworkServiceAccountW, NULL); if (hServ != NULL) { @@ -276,7 +275,8 @@ HRESULT CServiceManager::Create(_In_z_ LPCWSTR szServiceNameW, _In_ LPCREATEINFO ? S_OK : MX_HRESULT_FROM_LASTERROR(); //setup required privileges if (SUCCEEDED(hRes) && bIsWindowsVistaOrLater != FALSE && - (lpCreateInfo->nServiceType == ServiceTypeLocalSystem || lpCreateInfo->nServiceType == ServiceTypeNetworkService)) + (lpCreateInfo->nServiceType == eServiceType::LocalSystem || + lpCreateInfo->nServiceType == eServiceType::NetworkService)) { SERVICE_REQUIRED_PRIVILEGES_INFOW sReqPrivInfoW; @@ -291,7 +291,8 @@ HRESULT CServiceManager::Create(_In_z_ LPCWSTR szServiceNameW, _In_ LPCREATEINFO } //setup sid info if (SUCCEEDED(hRes) && bIsWindowsVistaOrLater != FALSE && - (lpCreateInfo->nServiceType == ServiceTypeLocalSystem || lpCreateInfo->nServiceType == ServiceTypeNetworkService)) + (lpCreateInfo->nServiceType == eServiceType::LocalSystem || + lpCreateInfo->nServiceType == eServiceType::NetworkService)) { SERVICE_SID_INFO sServSidInfo; @@ -304,7 +305,8 @@ HRESULT CServiceManager::Create(_In_z_ LPCWSTR szServiceNameW, _In_ LPCREATEINFO } //setup restart time if (SUCCEEDED(hRes) && - (lpCreateInfo->nServiceType == ServiceTypeLocalSystem || lpCreateInfo->nServiceType == ServiceTypeNetworkService)) + (lpCreateInfo->nServiceType == eServiceType::LocalSystem || + lpCreateInfo->nServiceType == eServiceType::NetworkService)) { SERVICE_FAILURE_ACTIONSW sServFailActW; SC_ACTION aServActions[1]; @@ -341,7 +343,8 @@ HRESULT CServiceManager::Create(_In_z_ LPCWSTR szServiceNameW, _In_ LPCREATEINFO } } if (SUCCEEDED(hRes) && - (lpCreateInfo->nServiceType == ServiceTypeLocalSystem || lpCreateInfo->nServiceType == ServiceTypeNetworkService)) + (lpCreateInfo->nServiceType == eServiceType::LocalSystem || + lpCreateInfo->nServiceType == eServiceType::NetworkService)) { SERVICE_DESCRIPTIONW sServDescW; @@ -740,15 +743,15 @@ static DWORD GetServiceStartType(_In_ MX::CServiceManager::eStartMode nStartMode { switch (nStartMode) { - case MX::CServiceManager::StartModeAuto: + case MX::CServiceManager::eStartMode::Auto: return SERVICE_AUTO_START; - case MX::CServiceManager::StartModeBoot: + case MX::CServiceManager::eStartMode::Boot: return SERVICE_BOOT_START; - case MX::CServiceManager::StartModeSystem: + case MX::CServiceManager::eStartMode::System: return SERVICE_SYSTEM_START; - case MX::CServiceManager::StartModeManual: + case MX::CServiceManager::eStartMode::Manual: return SERVICE_DEMAND_START; - case MX::CServiceManager::StartModeDisabled: + case MX::CServiceManager::eStartMode::Disabled: return SERVICE_DISABLED; } return 0xFFFFFFFFUL; diff --git a/ServiceManager.h b/ServiceManager.h index b9a1b9e..a0063c1 100644 --- a/ServiceManager.h +++ b/ServiceManager.h @@ -30,20 +30,22 @@ namespace MX { class CServiceManager : public virtual CBaseMemObj, public CNonCopyableObj { public: - typedef enum { - ServiceTypeLocalSystem, - ServiceTypeNetworkService, - ServiceTypeKernelDriver, - ServiceTypeFileSystemDriver - } eServiceType; - - typedef enum { - StartModeAuto, - StartModeBoot, - StartModeSystem, - StartModeManual, - StartModeDisabled - } eStartMode; + enum class eServiceType + { + LocalSystem, + NetworkService, + KernelDriver, + FileSystemDriver + }; + + enum class eStartMode + { + Auto, + Boot, + System, + Manual, + Disabled + }; public: typedef struct tagCREATEINFO { @@ -88,7 +90,7 @@ class CServiceManager : public virtual CBaseMemObj, public CNonCopyableObj HRESULT ChangeStartMode(_In_ CServiceManager::eStartMode nStartMode); private: - SC_HANDLE hServMgr, hServ; + SC_HANDLE hServMgr{ NULL }, hServ{ NULL }; }; }; //namespace MX diff --git a/Signatures.cpp b/Signatures.cpp index 9be4de3..64ef146 100644 --- a/Signatures.cpp +++ b/Signatures.cpp @@ -939,12 +939,12 @@ HRESULT CalculateHashes(_In_z_ LPCWSTR szFileNameW, _In_opt_ HANDLE hFile, _In_o hFile = cFileH.Get(); } - hRes = cHashSha256.BeginDigest(CMessageDigest::AlgorithmSHA256); + hRes = cHashSha256.BeginDigest(CMessageDigest::eAlgorithm::SHA256); if (SUCCEEDED(hRes)) { - hRes = cHashSha1.BeginDigest(CMessageDigest::AlgorithmSHA1); + hRes = cHashSha1.BeginDigest(CMessageDigest::eAlgorithm::SHA1); if (SUCCEEDED(hRes)) - hRes = cHashMd5.BeginDigest(CMessageDigest::AlgorithmMD5); + hRes = cHashMd5.BeginDigest(CMessageDigest::eAlgorithm::MD5); } if (SUCCEEDED(hRes)) diff --git a/Signatures.h b/Signatures.h index 096bfc3..f487dba 100644 --- a/Signatures.h +++ b/Signatures.h @@ -32,6 +32,10 @@ namespace MX { namespace Signatures { +enum class eHashType { + HashTypeMD5 = 1, HashTypeSHA1, HashTypeSHA256 +}; + typedef struct tagHASHES { BYTE aSha256[32]; BYTE aSha1[20]; diff --git a/WinRegistry.cpp b/WinRegistry.cpp index d237879..5e021ab 100644 --- a/WinRegistry.cpp +++ b/WinRegistry.cpp @@ -33,7 +33,7 @@ static const MX_UNICODE_STRING usEmpty = { 0, 0, (PWSTR)L"" }; //----------------------------------------------------------- -static NTSTATUS OpenBaseKey(_In_ HKEY hKey, _In_ DWORD dwAccess, _Out_ PHANDLE lphBaseKey); +static NTSTATUS OpenBaseKey(_In_opt_ HKEY hKey, _In_ DWORD dwAccess, _Out_ PHANDLE lphBaseKey); static HRESULT RecursiveDeleteKey(_In_ HANDLE hKey, _In_opt_ PUNICODE_STRING SubKey); static NTSTATUS GetSubKeyName(_In_ HANDLE hKey, _In_ ULONG Index, _Out_ PUNICODE_STRING *pKeyName); @@ -927,12 +927,12 @@ HRESULT CWindowsRegistry::WriteMultiString(_In_z_ LPCWSTR szNameW, _In_ SIZE_T n return S_OK; } -HRESULT CWindowsRegistry::WriteBlob(_In_z_ LPCWSTR szNameW, _In_ LPCVOID lpValue, _In_ SIZE_T nValueLen) +HRESULT CWindowsRegistry::WriteBlob(_In_z_ LPCWSTR szNameW, _In_opt_ LPCVOID lpValue, _In_ SIZE_T nValueLen) { return WriteAny(szNameW, REG_BINARY, lpValue, nValueLen); } -HRESULT CWindowsRegistry::WriteAny(_In_z_ LPCWSTR szNameW, _In_ DWORD dwType, _In_ LPCVOID lpValue, +HRESULT CWindowsRegistry::WriteAny(_In_z_ LPCWSTR szNameW, _In_ DWORD dwType, _In_opt_ LPCVOID lpValue, _In_ SIZE_T nValueLen) { DWORD dwOsErr; @@ -1135,7 +1135,7 @@ HRESULT CWindowsRegistry::EnumerateValues(_In_ DWORD dwIndex, _Out_ PUNICODE_STR //----------------------------------------------------------- -static NTSTATUS OpenBaseKey(_In_ HKEY hKey, _In_ DWORD dwAccess, _Out_ PHANDLE lphBaseKey) +static NTSTATUS OpenBaseKey(_In_opt_ HKEY hKey, _In_ DWORD dwAccess, _Out_ PHANDLE lphBaseKey) { static const MX_UNICODE_STRING usMachine = { 34, 36, (PWSTR)L"\\REGISTRY\\MACHINE" }; static const MX_UNICODE_STRING usUser = { 28, 30, (PWSTR)L"\\REGISTRY\\USER" }; diff --git a/WinRegistry.h b/WinRegistry.h index 88ab91b..3e7e6d3 100644 --- a/WinRegistry.h +++ b/WinRegistry.h @@ -76,9 +76,9 @@ class CWindowsRegistry : public virtual CBaseMemObj, public CNonCopyableObj HRESULT WriteMultiString(_In_z_ LPCWSTR szNameW, _In_ SIZE_T nValuesCount, _In_ LPCWSTR *lpszValuesW); - HRESULT WriteBlob(_In_z_ LPCWSTR szNameW, _In_ LPCVOID lpValue, _In_ SIZE_T nValueLen); + HRESULT WriteBlob(_In_z_ LPCWSTR szNameW, _In_opt_ LPCVOID lpValue, _In_ SIZE_T nValueLen); - HRESULT WriteAny(_In_z_ LPCWSTR szNameW, _In_ DWORD dwType, _In_ LPCVOID lpValue, _In_ SIZE_T nValueLen); + HRESULT WriteAny(_In_z_ LPCWSTR szNameW, _In_ DWORD dwType, _In_opt_ LPCVOID lpValue, _In_ SIZE_T nValueLen); HRESULT WritePassword(_In_z_ LPCWSTR szNameW, _In_z_ LPCWSTR szPasswordW);