-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added /Locate operation. This non-security operation performs a file name search and records the name and file metadata to a CSV file. - Changed all output files to be UTF-8 since it is smaller in most cases and allows Excel to recognize CSV file formats automatically. - Corrected issue where /RestoreSecurity was prematurely freeing security descriptor memory. - Changed /BackupSecurity and /RestoreSecurity to use a pipe '|' delimiter character since '=' was an allowed character in file names.
- Loading branch information
1 parent
c08256a
commit 193758d
Showing
16 changed files
with
256 additions
and
53 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include "OperationLocate.h" | ||
#include "InputOutput.h" | ||
#include "Functions.h" | ||
|
||
ClassFactory<OperationLocate> * OperationLocate::RegisteredFactory = | ||
new ClassFactory<OperationLocate>(GetCommand()); | ||
|
||
#define Q(x) L"\"" + x + L"\"" | ||
|
||
OperationLocate::OperationLocate(std::queue<std::wstring> & oArgList) : Operation(oArgList) | ||
{ | ||
// exit if there are not enough arguments to part | ||
std::vector<std::wstring> sReportFile = ProcessAndCheckArgs(1, oArgList, L"\\0"); | ||
std::vector<std::wstring> sMatchAndArgs = ProcessAndCheckArgs(1, oArgList, L"\\0"); | ||
|
||
// fetch params | ||
HANDLE hFile = CreateFile(sReportFile[0].c_str(), GENERIC_WRITE, | ||
FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | ||
|
||
// see if names could be resolved | ||
if (hFile == INVALID_HANDLE_VALUE) | ||
{ | ||
// complain | ||
wprintf(L"ERROR: Could not create file '%s' specified for parameter '%s'.\n", sReportFile[0].c_str(), GetCommand().c_str()); | ||
exit(-1); | ||
} | ||
|
||
// register the file handle | ||
hReportFile = RegisterFileHandle(hFile, GetCommand()); | ||
|
||
// if this is the first handle using this file, write out a header | ||
if (hFile == hReportFile) | ||
{ | ||
// write out the file type marker | ||
BYTE hHeader[] = { 0xEF,0xBB,0xBF }; | ||
DWORD iBytes = 0; | ||
if (WriteFile(hFile, &hHeader, _countof(hHeader), &iBytes, NULL) == 0) | ||
{ | ||
wprintf(L"ERROR: Could not write out file type marker '%s'.\n", GetCommand().c_str()); | ||
exit(-1); | ||
} | ||
|
||
// write out the header | ||
std::wstring sToWrite = std::wstring(L"") + Q(L"Path") + L"," + Q(L"Creation Time") + L"," + | ||
Q(L"Modified Time") + L"," + Q(L"Size") + L"," + Q(L"Attributes") + L"\r\n"; | ||
if (WriteToFile(sToWrite, hReportFile) == 0) | ||
{ | ||
wprintf(L"ERROR: Could not write header to report file for parameter '%s'.\n", GetCommand().c_str()); | ||
exit(-1); | ||
} | ||
} | ||
|
||
// only flag this to apply to the core object with the file name | ||
AppliesToObject = true; | ||
|
||
// compile the regular expression | ||
try | ||
{ | ||
tRegex = std::wregex(sMatchAndArgs[0], std::wregex::icase | std::wregex::optimize); | ||
} | ||
catch (const std::regex_error &) | ||
{ | ||
wprintf(L"ERROR: Invalid regular expression '%s' specified for parameter '%s'.\n", sMatchAndArgs[0].c_str(), GetCommand().c_str()); | ||
exit(-1); | ||
} | ||
} | ||
|
||
void OperationLocate::ProcessObjectAction(ObjectEntry & tObjectEntry) | ||
{ | ||
// skip any file names that do not match the regex | ||
const WCHAR * sFileName = tObjectEntry.Name.c_str(); | ||
if (wcschr(sFileName, '\\') != NULL) sFileName = wcschr(sFileName, '\\'); | ||
if (!std::regex_match(sFileName, tRegex)) return; | ||
|
||
// fetch file attribute data | ||
WIN32_FILE_ATTRIBUTE_DATA tData; | ||
GetFileAttributesExW(tObjectEntry.Name.c_str(), GetFileExInfoStandard, &tData); | ||
|
||
// convert the file size to a string | ||
WCHAR sSize[32]; | ||
ULARGE_INTEGER iFileSize; | ||
iFileSize.LowPart = tData.nFileSizeLow; | ||
iFileSize.HighPart = tData.nFileSizeHigh; | ||
setlocale(LC_NUMERIC, ""); | ||
wsprintf(sSize, L"%I64u", iFileSize.QuadPart); | ||
|
||
// decode attributes | ||
std::wstring sAttributes = L""; | ||
if (tData.dwFileAttributes & FILE_ATTRIBUTE_READONLY) sAttributes += L"R"; | ||
if (tData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) sAttributes += L"H"; | ||
if (tData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) sAttributes += L"S"; | ||
if (tData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) sAttributes += L"D"; | ||
if (tData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) sAttributes += L"A"; | ||
if (tData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) sAttributes += L"T"; | ||
if (tData.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) sAttributes += L"C"; | ||
if (tData.dwFileAttributes & FILE_ATTRIBUTE_OFFLINE) sAttributes += L"O"; | ||
if (tData.dwFileAttributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) sAttributes += L"N"; | ||
if (tData.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED) sAttributes += L"E"; | ||
|
||
// write the string to a file | ||
std::wstring sToWrite = std::wstring(L"") + Q(tObjectEntry.Name) + L"," + | ||
Q(FileTimeToString(&tData.ftCreationTime)) + L"," + Q(FileTimeToString(&tData.ftLastWriteTime)) + | ||
L"," + Q(sSize) + L"," + Q(sAttributes) + L"\r\n"; | ||
if (WriteToFile(sToWrite, hReportFile) == 0) | ||
{ | ||
InputOutput::AddError(L"ERROR: Unable to write security information to report file."); | ||
} | ||
} |
Oops, something went wrong.