450 lines
15 KiB
C++
450 lines
15 KiB
C++
#include "StdAfx.h"
|
|
#include "ThreadEnumerator.h"
|
|
#include "AnticheatManager.h"
|
|
#include "CheatQueueManager.h"
|
|
#include "ProcessNameHelper.h"
|
|
|
|
typedef DWORD(NTAPI* TRtlComputeCrc32)(DWORD dwInitial, const BYTE* pData, int iLen);
|
|
static TRtlComputeCrc32 RtlComputeCrc32 = nullptr;
|
|
|
|
float GetEntropy(BYTE* byBuffer, DWORD dwLength)
|
|
{
|
|
DWORD dwSize = 0;
|
|
long lBuff[0xFF + 1] = { 0 };
|
|
float fTemp, fEntropy = 0;
|
|
|
|
for (DWORD i = 0; i < dwLength; i++)
|
|
{
|
|
lBuff[byBuffer[i]]++;
|
|
dwSize++;
|
|
}
|
|
|
|
for (DWORD i = 0; i < 256; i++)
|
|
{
|
|
if (lBuff[i])
|
|
{
|
|
fTemp = (float)lBuff[i] / (float)dwSize;
|
|
fEntropy += (-fTemp * log2(fTemp));
|
|
}
|
|
}
|
|
|
|
return fEntropy;
|
|
}
|
|
|
|
bool IsPackedExecutable(const std::string & szName)
|
|
{
|
|
bool bIsPacked = false;
|
|
|
|
auto hFile = CreateFileA(szName.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
|
|
if (!hFile || hFile == INVALID_HANDLE_VALUE)
|
|
{
|
|
return bIsPacked;
|
|
}
|
|
|
|
auto dwFileLen = GetFileSize(hFile, NULL);
|
|
if (!dwFileLen || dwFileLen == INVALID_FILE_SIZE)
|
|
{
|
|
CloseHandle(hFile);
|
|
return bIsPacked;
|
|
}
|
|
|
|
auto pImage = reinterpret_cast<BYTE*>(malloc(dwFileLen));
|
|
if (!pImage)
|
|
{
|
|
CloseHandle(hFile);
|
|
return bIsPacked;
|
|
}
|
|
|
|
DWORD dwReadedBytes;
|
|
BOOL readRet = ReadFile(hFile, pImage, dwFileLen, &dwReadedBytes, NULL);
|
|
if (!readRet || dwReadedBytes != dwFileLen)
|
|
{
|
|
CloseHandle(hFile);
|
|
free(pImage);
|
|
return bIsPacked;
|
|
}
|
|
CloseHandle(hFile);
|
|
|
|
float fEntropy = GetEntropy(pImage, dwFileLen);
|
|
|
|
free(pImage);
|
|
return (fEntropy > 7.5f);
|
|
}
|
|
|
|
|
|
std::string GetModuleOwnerName(LPVOID pModuleBase)
|
|
{
|
|
char szFileName[2048] = { 0 };
|
|
if (!GetMappedFileNameA(GetCurrentProcess(), pModuleBase, szFileName, sizeof(szFileName)))
|
|
return std::string("");
|
|
|
|
std::string strFileName = szFileName;
|
|
auto szRealName = DosDevicePath2LogicalPath(szFileName);
|
|
if (szRealName.empty())
|
|
return std::string("");
|
|
|
|
std::transform(strFileName.begin(), strFileName.end(), strFileName.begin(), tolower);
|
|
return strFileName;
|
|
}
|
|
|
|
bool GetSectionInformation(const std::string & szSectionName, DWORD dwBaseAddress, LPDWORD dwOffset, LPDWORD dwLen)
|
|
{
|
|
auto pImageDosHeader = (PIMAGE_DOS_HEADER)dwBaseAddress;
|
|
if (!pImageDosHeader || pImageDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
|
|
return false;
|
|
|
|
auto pImageNtHeaders = (PIMAGE_NT_HEADERS)(dwBaseAddress + pImageDosHeader->e_lfanew);
|
|
if (!pImageNtHeaders || pImageNtHeaders->Signature != IMAGE_NT_SIGNATURE)
|
|
return false;
|
|
|
|
auto pImageSectionHeader = IMAGE_FIRST_SECTION(pImageNtHeaders);
|
|
if (!pImageSectionHeader)
|
|
return false;
|
|
|
|
for (std::size_t i = 0; i < pImageNtHeaders->FileHeader.NumberOfSections; ++i)
|
|
{
|
|
if (!strcmp(szSectionName.c_str(), (char*)pImageSectionHeader[i].Name))
|
|
{
|
|
*dwOffset = DWORD(dwBaseAddress + pImageSectionHeader[i].VirtualAddress);
|
|
*dwLen = pImageSectionHeader[i].SizeOfRawData;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
DWORD CalculateChecksum(DWORD dwSectionStart, DWORD dwSectionLen)
|
|
{
|
|
auto dwChecksumResult = 0UL;
|
|
__try
|
|
{
|
|
dwChecksumResult = RtlComputeCrc32(0, (const BYTE*)dwSectionStart, dwSectionLen);
|
|
}
|
|
__except (1) { }
|
|
|
|
return dwChecksumResult;
|
|
}
|
|
|
|
|
|
inline bool GetSectionHash(DWORD dwModuleBase, const std::string & szSectionName, LPDWORD pdwSectionHash)
|
|
{
|
|
auto dwSectionBase = 0UL, dwSectionSize = 0UL;
|
|
if (szSectionName == XOR("PEHEADER"))
|
|
{
|
|
dwSectionBase = dwModuleBase;
|
|
dwSectionSize = 0x1000;
|
|
}
|
|
else
|
|
{
|
|
bool bFindSection = GetSectionInformation(szSectionName, dwModuleBase, &dwSectionBase, &dwSectionSize);
|
|
if (!bFindSection)
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("Section: %s can not found for: %p", szSectionName.c_str(), dwModuleBase);
|
|
#endif
|
|
return false;
|
|
}
|
|
}
|
|
|
|
auto szModuleOwner = GetModuleOwnerName(reinterpret_cast<LPVOID>(dwModuleBase));
|
|
if (szModuleOwner.empty())
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("Module: %p informations not found! Last error: %u", dwModuleBase, GetLastError());
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
if (!dwSectionBase || !dwSectionSize)
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("Section base address or size is null! Target Module base: %p Section: %s Module: %s IsPacked result: %d",
|
|
dwModuleBase, szSectionName.c_str(), szModuleOwner.c_str(), IsPackedExecutable(szModuleOwner));
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
if (IsBadReadPtr((LPCVOID)dwSectionBase, dwSectionSize))
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("Section is not at readable area! Base: %p Size: %p", dwSectionBase, dwSectionSize);
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
if (!VirtualQuery((LPCVOID)dwSectionBase, &mbi, sizeof(mbi)))
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("VirtualQuery fail! Last error: %u", GetLastError());
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
if (!(mbi.State & MEM_COMMIT) || (mbi.State & MEM_RELEASE) || (mbi.Protect == PAGE_NOACCESS) || (mbi.Protect & PAGE_GUARD))
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("Section is not a accesible memory!");
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
auto dwOldProtect = 0UL;
|
|
if (!VirtualProtect((LPVOID)dwSectionBase, dwSectionSize, PAGE_EXECUTE_READWRITE, &dwOldProtect))
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("VirtualProtect(pre) fail! Last error: %u", GetLastError());
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
auto dwChecksum = CalculateChecksum(dwSectionBase, dwSectionSize);
|
|
if (!dwChecksum)
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("Section checksum can not calculated! Last error: %u", GetLastError());
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
if (!VirtualProtect((LPVOID)dwSectionBase, dwSectionSize, dwOldProtect, &dwOldProtect))
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("VirtualProtect(post) fail! Last error: %u", GetLastError());
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
// TraceError("Module: %p Section: %s checksum calculated: %p - %p -> (%p)", dwModuleBase, szSectionName.c_str(), dwSectionBase, dwSectionSize, dwChecksum);
|
|
if (pdwSectionHash) *pdwSectionHash = dwChecksum;
|
|
return true;
|
|
}
|
|
|
|
|
|
bool InitSectionHash(DWORD dwModuleBase, std::shared_ptr < SModuleSectionsHashCtx> & pModuleSectionHashContainer)
|
|
{
|
|
// TraceError("Section checksum create init started! Target module: %p", dwModuleBase);
|
|
|
|
auto szModuleOwner = GetModuleOwnerName(reinterpret_cast<LPVOID>(dwModuleBase));
|
|
if (szModuleOwner.empty() || IsPackedExecutable(szModuleOwner))
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("Section checksum fail! Executable: %s is probably packed!", szModuleOwner.c_str());
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
if (!GetSectionHash(dwModuleBase, XOR("PEHEADER"), &pModuleSectionHashContainer->dwPeHeaderHash))
|
|
{
|
|
// TraceError("GetSectionHash(PEHEADER) fail!");
|
|
if (pModuleSectionHashContainer->dwPeHeaderHash) pModuleSectionHashContainer->dwPeHeaderHash = 0;
|
|
}
|
|
if (!GetSectionHash(dwModuleBase, XOR(".text"), &pModuleSectionHashContainer->dwTextHash))
|
|
{
|
|
// TraceError("GetSectionHash(.text) fail!");
|
|
if (pModuleSectionHashContainer->dwTextHash) pModuleSectionHashContainer->dwTextHash = 0;
|
|
}
|
|
if (!GetSectionHash(dwModuleBase, XOR(".rdata"), &pModuleSectionHashContainer->dwRDataHash))
|
|
{
|
|
// TraceError("GetSectionHash(.rdata) fail!");
|
|
if (pModuleSectionHashContainer->dwRDataHash) pModuleSectionHashContainer->dwRDataHash = 0;
|
|
}
|
|
if (!GetSectionHash(dwModuleBase, XOR(".edata"), &pModuleSectionHashContainer->dwEDataHash))
|
|
{
|
|
// TraceError("GetSectionHash(.edata) fail!");
|
|
if (pModuleSectionHashContainer->dwEDataHash) pModuleSectionHashContainer->dwEDataHash = 0;
|
|
}
|
|
if (!GetSectionHash(dwModuleBase, XOR(".rsrc"), &pModuleSectionHashContainer->dwRsrcHash))
|
|
{
|
|
// TraceError("GetSectionHash(.rsrc) fail!");
|
|
if (pModuleSectionHashContainer->dwRsrcHash) pModuleSectionHashContainer->dwRsrcHash = 0;
|
|
}
|
|
if (!GetSectionHash(dwModuleBase, XOR(".reloc"), &pModuleSectionHashContainer->dwRelocHash))
|
|
{
|
|
// TraceError("GetSectionHash(.reloc) fail!");
|
|
if (pModuleSectionHashContainer->dwRelocHash) pModuleSectionHashContainer->dwRelocHash = 0;
|
|
}
|
|
|
|
#if 0
|
|
TraceError("Section checksum calculate completed! result: PE(%p)Text(%p)Rdata(%p)Edata(%p)Rsrc(%p)Reloc(%p)",
|
|
pModuleSectionHashContainer->dwPeHeaderHash,
|
|
pModuleSectionHashContainer->dwTextHash,
|
|
pModuleSectionHashContainer->dwRDataHash,
|
|
pModuleSectionHashContainer->dwEDataHash,
|
|
pModuleSectionHashContainer->dwRsrcHash,
|
|
pModuleSectionHashContainer->dwRelocHash
|
|
);
|
|
#endif
|
|
|
|
return true;
|
|
}
|
|
|
|
typedef struct _ldr_enum_context
|
|
{
|
|
bool bCheck;
|
|
int iViolationIndex;
|
|
WCHAR wszViolationOwnModule[1024];
|
|
} SLdrEnumContext, *PLdrEnumContext;
|
|
VOID NTAPI LdrEnumCallBack(IN PLDR_DATA_TABLE_ENTRY_MY DataTableEntry, IN PVOID Context, IN OUT BOOLEAN *StopEnumeration)
|
|
{
|
|
auto pldrContext = (PLdrEnumContext)Context;
|
|
if (!pldrContext)
|
|
{
|
|
*StopEnumeration = TRUE;
|
|
return;
|
|
}
|
|
|
|
if (pldrContext->bCheck == false)
|
|
{
|
|
auto pModuleSectionHashContainer = std::make_shared<SModuleSectionsHashCtx>();
|
|
if (InitSectionHash((DWORD)DataTableEntry->DllBase, pModuleSectionHashContainer))
|
|
{
|
|
// TraceError("Module: %ls(%p) section hash list succesfully generated!", DataTableEntry->FullDllName.Buffer, DataTableEntry->DllBase);
|
|
|
|
pModuleSectionHashContainer->dwModuleBase = (DWORD)DataTableEntry->DllBase;
|
|
wcscpy_s(pModuleSectionHashContainer->wszModuleName, DataTableEntry->FullDllName.Buffer);
|
|
|
|
CAnticheatManager::Instance().SaveModuleSectionHashList(pModuleSectionHashContainer);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
auto pCurrentHashList = std::make_shared<SModuleSectionsHashCtx>();
|
|
if (InitSectionHash((DWORD)DataTableEntry->DllBase, pCurrentHashList))
|
|
{
|
|
auto pCorrectHashContainer = CAnticheatManager::Instance().GetModuleSectionHashContainer();
|
|
for (const auto & pCorrectHashList : pCorrectHashContainer)
|
|
{
|
|
if (pCorrectHashList->dwModuleBase == (DWORD)DataTableEntry->DllBase)
|
|
{
|
|
if (pCurrentHashList->dwPeHeaderHash && pCorrectHashList->dwPeHeaderHash && pCurrentHashList->dwPeHeaderHash != pCorrectHashList->dwPeHeaderHash)
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("PE Header checksum violation! Curr: %p - Corr: %p", pCurrentHashList->dwPeHeaderHash, pCorrectHashList->dwPeHeaderHash);
|
|
#endif
|
|
pldrContext->iViolationIndex = 1;
|
|
wcscpy_s(pldrContext->wszViolationOwnModule, pCorrectHashList->wszModuleName);
|
|
}
|
|
|
|
else if (pCurrentHashList->dwTextHash && pCorrectHashList->dwTextHash && pCurrentHashList->dwTextHash != pCorrectHashList->dwTextHash)
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("Text section checksum violation! Curr: %p - Corr: %p", pCurrentHashList->dwTextHash, pCorrectHashList->dwTextHash);
|
|
#endif
|
|
pldrContext->iViolationIndex = 2;
|
|
wcscpy_s(pldrContext->wszViolationOwnModule, pCorrectHashList->wszModuleName);
|
|
}
|
|
|
|
else if (pCurrentHashList->dwRDataHash && pCorrectHashList->dwRDataHash && pCurrentHashList->dwRDataHash != pCorrectHashList->dwRDataHash)
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("rdata section checksum violation! Curr: %p - Corr: %p", pCurrentHashList->dwRDataHash, pCorrectHashList->dwRDataHash);
|
|
#endif
|
|
pldrContext->iViolationIndex = 3;
|
|
wcscpy_s(pldrContext->wszViolationOwnModule, pCorrectHashList->wszModuleName);
|
|
}
|
|
|
|
else if (pCurrentHashList->dwEDataHash && pCorrectHashList->dwEDataHash && pCurrentHashList->dwEDataHash != pCorrectHashList->dwEDataHash)
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("edata section checksum violation! Curr: %p - Corr: %p", pCurrentHashList->dwEDataHash, pCorrectHashList->dwEDataHash);
|
|
#endif
|
|
pldrContext->iViolationIndex = 4;
|
|
wcscpy_s(pldrContext->wszViolationOwnModule, pCorrectHashList->wszModuleName);
|
|
}
|
|
|
|
else if (pCurrentHashList->dwRsrcHash && pCorrectHashList->dwRsrcHash && pCurrentHashList->dwRsrcHash != pCorrectHashList->dwRsrcHash)
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("rsrc section checksum violation! Curr: %p - Corr: %p", pCurrentHashList->dwRsrcHash, pCorrectHashList->dwRsrcHash);
|
|
#endif
|
|
pldrContext->iViolationIndex = 5;
|
|
wcscpy_s(pldrContext->wszViolationOwnModule, pCorrectHashList->wszModuleName);
|
|
}
|
|
|
|
else if (pCurrentHashList->dwRelocHash && pCorrectHashList->dwRelocHash && pCurrentHashList->dwRelocHash != pCorrectHashList->dwRelocHash)
|
|
{
|
|
#ifdef _DEBUG
|
|
TraceError("reloc section checksum violation! Curr: %p - Corr: %p", pCurrentHashList->dwRelocHash, pCorrectHashList->dwRelocHash);
|
|
#endif
|
|
pldrContext->iViolationIndex = 6;
|
|
wcscpy_s(pldrContext->wszViolationOwnModule, pCorrectHashList->wszModuleName);
|
|
}
|
|
|
|
else
|
|
{
|
|
pldrContext->iViolationIndex = 0;
|
|
}
|
|
}
|
|
|
|
// TraceError("Section integrity scan completed for: %p(%ls)", pCorrectHashList->dwModuleBase, pCorrectHashList->wszModuleName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void CAnticheatManager::InitSectionHashes()
|
|
{
|
|
auto hNtdll = LoadLibraryA(XOR("ntdll.dll"));
|
|
if (!hNtdll)
|
|
return;
|
|
|
|
if (!RtlComputeCrc32)
|
|
{
|
|
auto pRtlComputeCrc32 = GetProcAddress(hNtdll, XOR("RtlComputeCrc32"));
|
|
if (pRtlComputeCrc32)
|
|
RtlComputeCrc32 = (TRtlComputeCrc32)pRtlComputeCrc32;
|
|
}
|
|
if (!RtlComputeCrc32)
|
|
return;
|
|
|
|
typedef VOID(NTAPI LDR_ENUM_CALLBACK)(_In_ PLDR_DATA_TABLE_ENTRY_MY ModuleInformation, _In_ PVOID Parameter, _Out_ BOOLEAN *Stop);
|
|
typedef NTSTATUS(NTAPI* TLdrEnumerateLoadedModules)(IN BOOLEAN ReservedFlag, IN LDR_ENUM_CALLBACK* EnumProc, IN PVOID Context);
|
|
|
|
|
|
auto pLdrEnumerateLoadedModules = GetProcAddress(hNtdll, XOR("LdrEnumerateLoadedModules"));
|
|
if (!pLdrEnumerateLoadedModules)
|
|
return;
|
|
auto LdrEnumerateLoadedModules = (TLdrEnumerateLoadedModules)pLdrEnumerateLoadedModules;
|
|
|
|
SLdrEnumContext ldrEnumContext;
|
|
ldrEnumContext.bCheck = false;
|
|
LdrEnumerateLoadedModules(0, LdrEnumCallBack, &ldrEnumContext);
|
|
}
|
|
|
|
bool CAnticheatManager::HasCorruptedModuleSection()
|
|
{
|
|
typedef VOID(NTAPI LDR_ENUM_CALLBACK)(_In_ PLDR_DATA_TABLE_ENTRY_MY ModuleInformation, _In_ PVOID Parameter, _Out_ BOOLEAN *Stop);
|
|
typedef NTSTATUS(NTAPI* TLdrEnumerateLoadedModules)(IN BOOLEAN ReservedFlag, IN LDR_ENUM_CALLBACK* EnumProc, IN PVOID Context);
|
|
|
|
static TLdrEnumerateLoadedModules LdrEnumerateLoadedModules = nullptr;
|
|
if (!LdrEnumerateLoadedModules)
|
|
{
|
|
auto hNtdll = LoadLibraryA(XOR("ntdll.dll"));
|
|
if (hNtdll)
|
|
{
|
|
auto pLdrEnumerateLoadedModules = GetProcAddress(hNtdll, XOR("LdrEnumerateLoadedModules"));
|
|
if (pLdrEnumerateLoadedModules)
|
|
LdrEnumerateLoadedModules = (TLdrEnumerateLoadedModules)pLdrEnumerateLoadedModules;
|
|
}
|
|
}
|
|
if (!LdrEnumerateLoadedModules)
|
|
return false;
|
|
|
|
SLdrEnumContext ldrEnumContext;
|
|
ldrEnumContext.bCheck = true;
|
|
LdrEnumerateLoadedModules(0, LdrEnumCallBack, &ldrEnumContext);
|
|
|
|
if (ldrEnumContext.iViolationIndex > 0)
|
|
{
|
|
auto wstrViolationOwner = std::wstring(ldrEnumContext.wszViolationOwnModule);
|
|
auto strViolationOwner = std::string(wstrViolationOwner.begin(), wstrViolationOwner.end());
|
|
|
|
CCheatDetectQueueMgr::Instance().AppendDetection(MODULE_SECTION_INTEGRITY_CORRUPTED, ldrEnumContext.iViolationIndex, strViolationOwner);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|