Skip to content

Commit

Permalink
Pattern Added, Check Ocurrences in Both Files
Browse files Browse the repository at this point in the history
  • Loading branch information
pinwhell committed Jun 28, 2022
1 parent ac1d0ec commit 943ce66
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 4 deletions.
23 changes: 23 additions & 0 deletions FileAOBDiff/FileAOBDiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <conio.h>
#include "Pattern.h"
#include "DiffHelper.h"
#include "PatternEngine.h"
#include <functional>
#include <thread>


FileAOBDiff::FileAOBDiff()
Expand Down Expand Up @@ -75,6 +78,26 @@ void FileAOBDiff::Run()
DiffHelper::MakeDiff(pFile1, pFile2, file1Off, file2Off, uint64_t(diffingSize), resultDiff);

std::cout << "Result: " << resultDiff.toString() << std::endl;
std::vector<uintptr_t> resultDiffFile1Ocurrences;
std::vector<uintptr_t> resultDiffFile2Ocurrences;

std::thread t1([&] {
PatternEngine::FindPattern(pFile1, resultDiff, resultDiffFile1Ocurrences);
});

std::thread t2([&] {
PatternEngine::FindPattern(pFile2, resultDiff, resultDiffFile2Ocurrences);
});

t1.join();
t2.join();

if (resultDiffFile1Ocurrences.size() != resultDiffFile2Ocurrences.size())
{
std::cout << file1Name << ": " << resultDiffFile1Ocurrences.size() << " Ocurrences\n";
std::cout << file2Name << ": " << resultDiffFile2Ocurrences.size() << " Ocurrences\n";
}
else std::cout << "Pattern Unique!\n";
}
else std::cout << "Invalid Offset\n";
}
Expand Down
3 changes: 0 additions & 3 deletions FileAOBDiff/FileAOBDiff.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#pragma once

class FilesEngine;
class PatternEngine;


class FileAOBDiff
{
private:
FilesEngine* pFilesEngine;
PatternEngine* pPatternEngine;

public:
FileAOBDiff();
Expand Down
4 changes: 4 additions & 0 deletions FileAOBDiff/FileAOBDiff.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -130,6 +132,8 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down
10 changes: 10 additions & 0 deletions FileAOBDiff/Pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ void Pattern::AddWildCard()
{
AddByte(0x0, true);
}

std::vector<unsigned char> Pattern::getBytes() const
{
std::vector<unsigned char> bytes;

for (const PatternByte& uc : aob)
bytes.push_back(uc.byteType == ByteType::BYTE ? uc.byte : '?');

return bytes;
}
1 change: 1 addition & 0 deletions FileAOBDiff/Pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ struct Pattern
std::string toString();
void AddByte(unsigned char byte, bool bIsWildCard = false);
void AddWildCard();
std::vector<unsigned char> getBytes() const;
};

31 changes: 31 additions & 0 deletions FileAOBDiff/PatternEngine.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
#include "PatternEngine.h"
#include "File.h"
#include "Pattern.h"
#include "Buffer.h"

bool PatternEngine::CompareMem(const unsigned char* memChunk, const unsigned char* mask, const size_t len)
{
for (size_t i = 0; i < len; i++)
{
if (mask[i] != memChunk[i] && mask[i] != '?')
return false;
}

return true;
}

void PatternEngine::FindPattern(const File* pFile, const Pattern& patternMask, std::vector<uintptr_t>& outResults)
{
Buffer* pBuff = pFile->pBuff;
const auto patternMaskBuff = patternMask.getBytes();

if (pBuff) FindPattern(pBuff->mBuff, pBuff->mBuff + pBuff->mSize, patternMaskBuff.data(), patternMaskBuff.size(), outResults);
}

void PatternEngine::FindPattern(const unsigned char* pStartAddr, const unsigned char* pEndAddr, const unsigned char* pMask, size_t maskLen, std::vector<uintptr_t>& outResults)
{
for (const unsigned char* pCurr = pStartAddr; (pCurr + maskLen) < pEndAddr; pCurr++)
{
if (CompareMem(pCurr, pMask, maskLen))
outResults.push_back((uintptr_t)(pStartAddr + (pEndAddr - pCurr)));
}
}
10 changes: 9 additions & 1 deletion FileAOBDiff/PatternEngine.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#pragma once

#include <vector>

struct File;
struct Pattern;

class PatternEngine
{

public:
static bool CompareMem(const unsigned char* memChunk, const unsigned char* mask, const size_t len);
static void FindPattern(const File* pFile, const Pattern& pPatternMask, std::vector<uintptr_t>& outResults);
static void FindPattern(const unsigned char* pStartAddr, const unsigned char* pEndAddr, const unsigned char* pMask, size_t maskLen, std::vector<uintptr_t>& outResults);
};

0 comments on commit 943ce66

Please sign in to comment.