Skip to content

Commit

Permalink
Address Thread Synchronization Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
NoMoreFood committed Jun 23, 2022
1 parent 08fceb9 commit 4699c1b
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 45 deletions.
Binary file modified Build/Release/x64/repacls.exe
Binary file not shown.
Binary file modified Build/Release/x86/repacls.exe
Binary file not shown.
Binary file modified Build/Repacls.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion Build/build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CLS
SET PATH=%WINDIR%\system32;%WINDIR%\system32\WindowsPowerShell\v1.0

:: cert info to use for signing
SET CERT=BC4F81C0B3B32755A8CC9A6B91713958294788F0
SET CERT=055E5F445405B24790B32F75FE9049884F2F3788
set TSAURL=http://time.certum.pl/
set LIBNAME=Repacls
set LIBURL=https://github.com/NoMoreFood/Repacls
Expand Down
12 changes: 3 additions & 9 deletions Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,15 @@ VOID BeginScan(Processor & oProcessor)
if (OperationPathMode::GetPathMode() == SE_REGISTRY_KEY) oObject = new ObjectRegistry(oProcessor);
if (OperationPathMode::GetPathMode() == SE_DS_OBJECT) oObject = new ObjectAds(oProcessor);

// to track threads that are running
bool ProcessingStarted = false;

// startup some threads for processing
std::vector<std::thread> oThreads;
oProcessor.GetQueue().SetWaiterCounter(InputOutput::MaxThreads());
for (USHORT iNum = 0; iNum < InputOutput::MaxThreads(); iNum++)
oThreads.push_back(std::thread([&ProcessingStarted,&oProcessor,oObject]() {
oThreads.push_back(std::thread([&oProcessor,oObject]() {
for (;;)
{
// fetch next entry
ObjectEntry oEntry = oProcessor.GetQueue().Pop();
ProcessingStarted = true;

// break out if entry flags a termination
if (oEntry.ObjectType == SE_UNKNOWN_OBJECT_TYPE) return;
Expand All @@ -64,10 +61,7 @@ VOID BeginScan(Processor & oProcessor)
}

// wait for queue to be completely empty
while (!ProcessingStarted)
{
oProcessor.GetQueue().WaitForEmptyQueues();
}
oProcessor.GetQueue().WaitForEmptyQueues();

// send in some empty entries to tell the thread to stop waiting
for (USHORT iNum = 0; iNum < InputOutput::MaxThreads(); iNum++)
Expand Down
2 changes: 1 addition & 1 deletion Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Object

public:

virtual void GetBaseObject(std::wstring_view sPath) = 0;
virtual void GetBaseObject(std::wstring sPath) = 0;
virtual void GetChildObjects(ObjectEntry& oObject) = 0;

Object(Processor& poProcessor) : oProcessor(poProcessor) {}
Expand Down
2 changes: 1 addition & 1 deletion ObjectAds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#pragma comment(lib,"activeds.lib")
#pragma comment(lib,"adsiid.lib")

void ObjectAds::GetBaseObject(std::wstring_view sPath)
void ObjectAds::GetBaseObject(std::wstring sPath)
{
// initialize com for this thread
InitThreadCom();
Expand Down
2 changes: 1 addition & 1 deletion ObjectAds.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ObjectAds : public Object
public:

// overrides
void GetBaseObject(std::wstring_view sPath) override;
void GetBaseObject(std::wstring sPath) override;
void GetChildObjects(ObjectEntry& oEntry) override;

// constructors
Expand Down
2 changes: 1 addition & 1 deletion ObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "DriverKitPartial.h"
#include "OperationDepth.h"

void ObjectFile::GetBaseObject(std::wstring_view sPath)
void ObjectFile::GetBaseObject(std::wstring sPath)
{
// make a local copy of the path since we may have to alter it
// handle special case where a drive root is specified
Expand Down
2 changes: 1 addition & 1 deletion ObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ObjectFile : public Object
public:

// overrides
void GetBaseObject(std::wstring_view sPath) override;
void GetBaseObject(std::wstring sPath) override;
void GetChildObjects(ObjectEntry& oEntry) override;

// constructors
Expand Down
2 changes: 1 addition & 1 deletion ObjectRegistry.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "InputOutput.h"
#include "ObjectRegistry.h"

void ObjectRegistry::GetBaseObject(std::wstring_view sPath)
void ObjectRegistry::GetBaseObject(std::wstring sPath)
{
const static std::map<std::wstring_view, std::pair<HKEY, std::wstring>> oRegMap = {
{ L"HKLM", { HKEY_LOCAL_MACHINE, L"MACHINE" }},
Expand Down
2 changes: 1 addition & 1 deletion ObjectRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ObjectRegistry : public Object
public:

// overrides
void GetBaseObject(std::wstring_view sPath) override;
void GetBaseObject(std::wstring sPath) override;
void GetChildObjects(ObjectEntry& oEntry) override;

// constructors
Expand Down
2 changes: 1 addition & 1 deletion Operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Operation
virtual bool ProcessSidAction(const WCHAR * const sSdPart, ObjectEntry & tObjectEntry, PSID & tCurrentSid, bool & bSidReplacement);
virtual SidActionResult DetermineSid(const WCHAR * const sSdPart, ObjectEntry & tObjectEntry, PSID const tCurrentSid, PSID & tResultantSid) { return SidActionResult::Nothing; }
virtual void ProcessObjectAction(ObjectEntry & tObjectEntry) { return; }
static PSID Operation::GetSidFromAce(PACE_ACCESS_HEADER tAce) noexcept;
static PSID GetSidFromAce(PACE_ACCESS_HEADER tAce) noexcept;

Operation(std::queue<std::wstring> & oArgList);
virtual ~Operation() = default;
Expand Down
38 changes: 15 additions & 23 deletions OperationLocateHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ OperationLocateHash::OperationLocateHash(std::queue<std::wstring> & oArgList, co
{
// exit if there are not enough arguments to parse
std::vector<std::wstring> sReportFile = ProcessAndCheckArgs(1, oArgList, L"\\0");
std::vector<std::wstring> sMatchAndArgs = ProcessAndCheckArgs(1, oArgList);
std::vector<std::wstring> sMatchAndArgs = ProcessAndCheckArgs(2, oArgList);

// fetch params
HANDLE hFile = CreateFile(sReportFile.at(0).c_str(), GENERIC_WRITE,
Expand Down Expand Up @@ -70,18 +70,14 @@ OperationLocateHash::OperationLocateHash(std::queue<std::wstring> & oArgList, co
std::exit(-1);
}

// record specific hash if specified
if (sMatchAndArgs.size() > 1)
// determine hash to match
aHashToMatch = new BYTE[HASH_IN_BYTES];
DWORD iBytesRead = HASH_IN_BYTES;
if (CryptStringToBinary(sMatchAndArgs.at(1).c_str(), (DWORD) sMatchAndArgs.at(1).size(),
CRYPT_STRING_HEX_ANY, aHashToMatch, &iBytesRead, NULL, NULL) == FALSE || iBytesRead != HASH_IN_BYTES)
{
// convert the sha1 string from hex to binary
aHashToMatch = new BYTE[HASH_IN_BYTES];
DWORD iBytesRead = HASH_IN_BYTES;
if (CryptStringToBinary(sMatchAndArgs.at(1).c_str(), (DWORD) sMatchAndArgs.at(1).size(),
CRYPT_STRING_HEX_ANY, aHashToMatch, &iBytesRead, NULL, NULL) == FALSE || iBytesRead != HASH_IN_BYTES)
{
wprintf(L"ERROR: Invalid hash '%s' specified for parameter '%s'.\n", sMatchAndArgs.at(1).c_str(), GetCommand().c_str());
std::exit(-1);
}
wprintf(L"ERROR: Invalid hash '%s' specified for parameter '%s'.\n", sMatchAndArgs.at(1).c_str(), GetCommand().c_str());
std::exit(-1);
}

// record specific size if specified
Expand Down Expand Up @@ -148,7 +144,7 @@ void OperationLocateHash::ProcessObjectAction(ObjectEntry & tObjectEntry)
// complete hash data
if (BCryptFinishHash(HashHandle, Hash, HashLength, 0) != 0)
{
InputOutput::AddError(L"Could not finalize file data.");
InputOutput::AddError(L"Could not finalize file data for hashing.");
std::exit(-1);
}

Expand Down Expand Up @@ -177,16 +173,12 @@ void OperationLocateHash::ProcessObjectAction(ObjectEntry & tObjectEntry)
const std::wstring sModifiedTime = FileTimeToString(tObjectEntry.ModifiedTime);
const std::wstring sCreationTime = FileTimeToString(tObjectEntry.CreationTime);

// check if the target path matches out regex filter
if (true)
// write output to file
std::wstring sToWrite = std::wstring(L"") + Q(tObjectEntry.Name) + L"," +
Q(sCreationTime) + L"," + Q(sModifiedTime) + L"," +
Q(sSize) + L"," + Q(sAttributes) + L"," + Q(sHash) + L"," + L"\r\n";
if (WriteToFile(sToWrite, hReportFile) == 0)
{
// write output to file
std::wstring sToWrite = std::wstring(L"") + Q(tObjectEntry.Name) + L"," +
Q(sCreationTime) + L"," + Q(sModifiedTime) + L"," +
Q(sSize) + L"," + Q(sAttributes) + L"," + Q(sHash) + L"," + L"\r\n";
if (WriteToFile(sToWrite, hReportFile) == 0)
{
InputOutput::AddError(L"Unable to write information to report file.");
}
InputOutput::AddError(L"Unable to write information to report file.");
}
}
Binary file modified Resource.rc
Binary file not shown.
8 changes: 4 additions & 4 deletions repacls.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
Expand All @@ -121,7 +121,7 @@
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<DisableSpecificWarnings>4100</DisableSpecificWarnings>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
Expand Down Expand Up @@ -151,7 +151,7 @@
<ShowIncludes />
<StringPooling>true</StringPooling>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
Expand Down Expand Up @@ -185,7 +185,7 @@
<OmitFramePointers>true</OmitFramePointers>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<StringPooling>true</StringPooling>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
Expand Down

0 comments on commit 4699c1b

Please sign in to comment.