#pragma once namespace Utils { constexpr static uint32_t MaxFileBuffer{ 0x8000 }; // 32kib #pragma warning(push) #pragma warning(disable : 4634) // xmldoc: discarding XML document comment for invalid target /// /// Removes all occurrences of a set of values from an object. /// /// /// Object type to remove elements of. Must implement erase, be forward /// iterate-able, and contained value type must be move assignable. /// /// /// Object to erase elements from. /// /// /// Values to remove. /// template void EraseAll( _Inout_ T& Object, _In_ const std::initializer_list& Values) { for (const auto& value : Values) { Object.erase(std::remove(Object.begin(), Object.end(), value), Object.end()); } } #pragma warning(pop) /// /// Formats an error code as a string. /// /// /// Error code to format as a string. /// /// /// Human readable string for the error code if the error is unknown a /// string is returned formatted as "[number] - Unknown Error". /// std::wstring FormatError(_In_ uint32_t Error); /// /// Generates a buffer of a given length containing a supplied pattern. /// /// /// Buffer to fill with the patter, must not be empty. /// /// /// Pattern to write into the buffer. /// /// /// Success when the buffer is filled with the pattern. Failure if Buffer /// is empty. /// _Must_inspect_result_ HRESULT FillBufferWithPattern( _Inout_ std::vector& Buffer, _In_ std::span Pattern); /// /// Gets a file size. /// /// /// File to get the size of. /// /// /// Set to the size of the file on success. /// /// /// Success if the file size of retrieved. /// _Must_inspect_result_ HRESULT GetFileSize( _In_ handle_t FileHandle, _Out_ uint64_t& FileSize); /// /// Sets a file pointer. /// /// /// File to set the pointer of. /// /// /// Distance to move the file pointer. /// /// /// Move method to use (FILE_BEGIN, FILE_CURRENT, FILE_END). /// /// /// Success if the file pointer was set (or was already set). /// _Must_inspect_result_ HRESULT SetFilePointer( _In_ handle_t FileHandle, _In_ int64_t DistanceToMove, _In_ uint32_t MoveMethod); /// /// Copies the contents for a source file to the target by handle. /// /// /// Source file handle. /// /// /// Target file handle. /// /// /// Success if the source file has been copied to the target. /// _Must_inspect_result_ HRESULT CopyFileByHandle( _In_ handle_t SourceHandle, _In_ handle_t TargetHandle); /// /// Overwrites the contents of a file with a pattern. /// /// /// Target file to overwrite. /// /// /// Pattern write over the file content. /// /// /// Length of Pattern buffer. /// /// /// Success if the file content was overwritten. /// _Must_inspect_result_ HRESULT OverwriteFileContentsWithPattern( _In_ handle_t FileHandle, _In_ std::span Pattern); /// /// Overwrites a file from a given offset with a pattern. /// /// /// Target file to overwrite. /// /// /// Offset to begin writing from. /// /// /// Pattern to use to extend the target file with. /// /// /// Number of bytes written. /// /// /// Success if the file was overwritten. /// _Must_inspect_result_ HRESULT OverwriteFileAfterWithPattern( _In_ handle_t FileHandle, _In_ uint64_t FileOffset, _In_ std::span Pattern, _Out_ uint32_t& WrittenBytes); /// /// Extends a PE file security directory by a number of bytes. /// /// /// Target file handle. /// /// /// Number of bytes to extend the security directory by. /// /// /// Success if the security directory was extended. Failure if the file is /// not a PE file or does not have a security directory. /// _Must_inspect_result_ HRESULT ExtendFileSecurityDirectory( _In_ handle_t FileHandle, _In_ uint32_t ExtendedBy); /// /// Retrieves the image entry point RVA from a file. /// /// /// File to parse for the entry point RVA. /// /// /// Set to the entry point RVA on success. /// /// /// Success if the PE image entry RVA is located. /// _Must_inspect_result_ HRESULT GetImageEntryPointRva( _In_ handle_t FileHandle, _Out_ uint32_t& EntryPointRva); /// /// Writes remote process parameters into target process. /// /// /// Process to write parameters into. /// /// /// Dll path to write into the parameters, optional. /// /// /// Image file name to write into the parameters. /// /// /// Current directory to write into the parameters, optional. /// /// /// Command line to write into the parameters, optional. /// /// /// Environment block to write into the parameters, optional. /// /// /// Window title to write into the parameters, optional. /// /// /// Desktop info to write into the parameters, optional. /// /// /// ShellInfo to write into the parameters, optional. /// /// /// Runtime data to write into the parameters, optional. /// /// /// Success if the remote process parameters are written. /// _Must_inspect_result_ HRESULT WriteRemoteProcessParameters( _In_ handle_t ProcessHandle, _In_ const std::wstring ImageFileName, _In_opt_ const std::optional& DllPath, _In_opt_ const std::optional& CurrentDirectory, _In_opt_ const std::optional& CommandLine, _In_opt_ void* EnvironmentBlock, _In_opt_ const std::optional& WindowTitle, _In_opt_ const std::optional& DesktopInfo, _In_opt_ const std::optional& ShellInfo, _In_opt_ const std::optional& RuntimeData); _Must_inspect_result_ BOOL ShouldReplaceWithFile( _In_ const char* fileName); _Must_inspect_result_ HRESULT GetFileName( _In_ const char* sourceFileName, _Out_ std::wstring& finalFileName); #ifndef _WIN64 // // Only needed for 32-bit Windows // typedef struct _FILE_VERSION { WORD MajorVersion; WORD MinorVersion; WORD BuildVersion; WORD RevisionVersion; } FILE_VERSION, * PFILE_VERSION; _Must_inspect_result_ HRESULT GetFileVersion( _In_ LPCWSTR lptstrFilename, _Out_ PFILE_VERSION ver); _Must_inspect_result_ HRESULT IsBuggyKernel(); #endif }