memaddr and module const cleanup.

This commit is contained in:
PixieCore 2022-07-15 19:43:29 +02:00
parent e0792af1ce
commit a8b74ec93a
2 changed files with 22 additions and 24 deletions

View File

@ -58,7 +58,7 @@ void CMemory::PatchString(const string& svString) const
DWORD oldProt = NULL;
SIZE_T dwSize = svString.size();
vector<char> bytes(svString.begin(), svString.end());
const vector<char> bytes(svString.begin(), svString.end());
VirtualProtect(reinterpret_cast<void*>(ptr), dwSize, PAGE_EXECUTE_READWRITE, &oldProt); // Patch page to be able to read and write to it.
@ -198,13 +198,13 @@ CMemory CMemory::FollowNearCallSelf(const ptrdiff_t opcodeOffset, const ptrdiff_
CMemory CMemory::ResolveRelativeAddress(const ptrdiff_t registerOffset, const ptrdiff_t nextInstructionOffset) const
{
// Skip register.
uintptr_t skipRegister = ptr + registerOffset;
const uintptr_t skipRegister = ptr + registerOffset;
// Get 4-byte long relative Address.
int32_t relativeAddress = *reinterpret_cast<int32_t*>(skipRegister);
const int32_t relativeAddress = *reinterpret_cast<int32_t*>(skipRegister);
// Get location of next instruction.
uintptr_t nextInstruction = ptr + nextInstructionOffset;
const uintptr_t nextInstruction = ptr + nextInstructionOffset;
// Get function location via adding relative Address to next instruction.
return CMemory(nextInstruction + relativeAddress);
@ -219,13 +219,13 @@ CMemory CMemory::ResolveRelativeAddress(const ptrdiff_t registerOffset, const pt
CMemory CMemory::ResolveRelativeAddressSelf(const ptrdiff_t registerOffset, const ptrdiff_t nextInstructionOffset)
{
// Skip register.
uintptr_t skipRegister = ptr + registerOffset;
const uintptr_t skipRegister = ptr + registerOffset;
// Get 4-byte long relative Address.
int32_t relativeAddress = *reinterpret_cast<int32_t*>(skipRegister);
const int32_t relativeAddress = *reinterpret_cast<int32_t*>(skipRegister);
// Get location of next instruction.
uintptr_t nextInstruction = ptr + nextInstructionOffset;
const uintptr_t nextInstruction = ptr + nextInstructionOffset;
// Get function location via adding relative Address to next instruction.
ptr = nextInstruction + relativeAddress;
@ -273,10 +273,10 @@ void CMemory::HookVirtualMethod(const uintptr_t virtualTable, const void* pHookM
DWORD oldProt = NULL;
// Calculate delta to next virtual method.
uintptr_t virtualMethod = virtualTable + (methodIndex * sizeof(ptrdiff_t));
const uintptr_t virtualMethod = virtualTable + (methodIndex * sizeof(ptrdiff_t));
// Preserve original function.
uintptr_t originalFunction = *reinterpret_cast<uintptr_t*>(virtualMethod);
const uintptr_t originalFunction = *reinterpret_cast<uintptr_t*>(virtualMethod);
// Set page for current virtual method to execute n read n write.
VirtualProtect(reinterpret_cast<void*>(virtualMethod), sizeof(virtualMethod), PAGE_EXECUTE_READWRITE, &oldProt);

View File

@ -44,18 +44,16 @@ CModule::CModule(const string& svModuleName) : m_svModuleName(svModuleName)
CMemory CModule::FindPatternSIMD(const uint8_t* szPattern, const char* szMask) const
{
if (!m_ExecutableCode.IsSectionValid())
{
return CMemory();
}
uint64_t nBase = static_cast<uint64_t>(m_ExecutableCode.m_pSectionBase);
uint64_t nSize = static_cast<uint64_t>(m_ExecutableCode.m_nSectionSize);
const uint64_t nBase = static_cast<uint64_t>(m_ExecutableCode.m_pSectionBase);
const uint64_t nSize = static_cast<uint64_t>(m_ExecutableCode.m_nSectionSize);
const uint8_t* pData = reinterpret_cast<uint8_t*>(nBase);
const uint8_t* pEnd = pData + static_cast<uint32_t>(nSize) - strlen(szMask);
int nMasks[64]; // 64*16 = enough masks for 1024 bytes.
int iNumMasks = static_cast<int>(ceil(static_cast<float>(strlen(szMask)) / 16.f));
const int iNumMasks = static_cast<int>(ceil(static_cast<float>(strlen(szMask)) / 16.f));
memset(nMasks, '\0', iNumMasks * sizeof(int));
for (intptr_t i = 0; i < iNumMasks; ++i)
@ -68,7 +66,7 @@ CMemory CModule::FindPatternSIMD(const uint8_t* szPattern, const char* szMask) c
}
}
}
__m128i xmm1 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(szPattern));
const __m128i xmm1 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(szPattern));
__m128i xmm2, xmm3, msks;
for (; pData != pEnd; _mm_prefetch(reinterpret_cast<const char*>(++pData + 64), _MM_HINT_NTA))
{
@ -125,7 +123,7 @@ CMemory CModule::FindStringReadOnly(const string& svString, bool bNullTerminator
if (!m_ReadOnlyData.IsSectionValid())
return CMemory();
vector<int> vBytes = StringToBytes(svString, bNullTerminator); // Convert our string to a byte array.
const vector<int> vBytes = StringToBytes(svString, bNullTerminator); // Convert our string to a byte array.
const pair bytesInfo = std::make_pair(vBytes.size(), vBytes.data()); // Get the size and data of our bytes.
uint8_t* pBase = reinterpret_cast<uint8_t*>(m_ReadOnlyData.m_pSectionBase); // Get start of .rdata section.
@ -165,7 +163,7 @@ CMemory CModule::FindString(const string& svString, const ptrdiff_t nOccurence,
if (!m_ExecutableCode.IsSectionValid())
return CMemory();
CMemory stringAddress = FindStringReadOnly(svString, bNullTerminator); // Get Address for the string in the .rdata section.
const CMemory stringAddress = FindStringReadOnly(svString, bNullTerminator); // Get Address for the string in the .rdata section.
if (!stringAddress)
return CMemory();
@ -178,10 +176,10 @@ CMemory CModule::FindString(const string& svString, const ptrdiff_t nOccurence,
byte byte = pTextStart[i];
if (byte == LEA)
{
CMemory skipOpCode = CMemory(reinterpret_cast<uintptr_t>(&pTextStart[i])).OffsetSelf(0x2); // Skip next 2 opcodes, those being the instruction and the register.
int32_t relativeAddress = skipOpCode.GetValue<int32_t>(); // Get 4-byte long string relative Address
uintptr_t nextInstruction = skipOpCode.Offset(0x4).GetPtr(); // Get location of next instruction.
CMemory potentialLocation = CMemory(nextInstruction + relativeAddress); // Get potential string location.
const CMemory skipOpCode = CMemory(reinterpret_cast<uintptr_t>(&pTextStart[i])).OffsetSelf(0x2); // Skip next 2 opcodes, those being the instruction and the register.
const int32_t relativeAddress = skipOpCode.GetValue<int32_t>(); // Get 4-byte long string relative Address
const uintptr_t nextInstruction = skipOpCode.Offset(0x4).GetPtr(); // Get location of next instruction.
const CMemory potentialLocation = CMemory(nextInstruction + relativeAddress); // Get potential string location.
if (potentialLocation == stringAddress)
{
@ -211,7 +209,7 @@ CMemory CModule::GetExportedFunction(const string& svFunctionName) const
return CMemory();
// Get the location of IMAGE_EXPORT_DIRECTORY for this module by adding the IMAGE_DIRECTORY_ENTRY_EXPORT relative virtual Address onto our module base Address.
IMAGE_EXPORT_DIRECTORY* pImageExportDirectory = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(m_pModuleBase + m_pNTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
const IMAGE_EXPORT_DIRECTORY* pImageExportDirectory = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(m_pModuleBase + m_pNTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
if (!pImageExportDirectory)
return CMemory();
@ -220,12 +218,12 @@ CMemory CModule::GetExportedFunction(const string& svFunctionName) const
return CMemory();
// Get the location of the functions via adding the relative virtual Address from the struct into our module base Address.
DWORD* pAddressOfFunctions = reinterpret_cast<DWORD*>(m_pModuleBase + pImageExportDirectory->AddressOfFunctions);
const DWORD* pAddressOfFunctions = reinterpret_cast<DWORD*>(m_pModuleBase + pImageExportDirectory->AddressOfFunctions);
if (!pAddressOfFunctions)
return CMemory();
// Get the names of the functions via adding the relative virtual Address from the struct into our module base Address.
DWORD* pAddressOfName = reinterpret_cast<DWORD*>(m_pModuleBase + pImageExportDirectory->AddressOfNames);
const DWORD* pAddressOfName = reinterpret_cast<DWORD*>(m_pModuleBase + pImageExportDirectory->AddressOfNames);
if (!pAddressOfName)
return CMemory();