From 1a9c1d989a39e18a135a93a95583af19b4b5b09f Mon Sep 17 00:00:00 2001 From: autumn <102363146+autumnlikescode@users.noreply.github.com> Date: Fri, 19 Apr 2024 11:49:55 +1000 Subject: [PATCH 1/2] Added x86 Integrity Check --- x86/example.vcxproj | 1 + x86/example.vcxproj.filters | 3 +++ x86/lib/auth.cpp | 20 +++++++++++++++----- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/x86/example.vcxproj b/x86/example.vcxproj index 95a006d..e48e181 100644 --- a/x86/example.vcxproj +++ b/x86/example.vcxproj @@ -169,6 +169,7 @@ + diff --git a/x86/example.vcxproj.filters b/x86/example.vcxproj.filters index 16397e1..2c25b2d 100644 --- a/x86/example.vcxproj.filters +++ b/x86/example.vcxproj.filters @@ -200,6 +200,9 @@ Header Files + + Header Files + diff --git a/x86/lib/auth.cpp b/x86/lib/auth.cpp index 4eaf90d..e44e57e 100644 --- a/x86/lib/auth.cpp +++ b/x86/lib/auth.cpp @@ -55,6 +55,7 @@ #include #include "Security.hpp" +#include "integrity.h" #define SHA256_HASH_SIZE 32 @@ -71,9 +72,7 @@ bool initalized; void KeyAuth::api::init() { - #if defined(__x86_64__) || defined(_M_X64) - CreateThread(0, 0, (LPTHREAD_START_ROUTINE)modify, 0, 0, 0); - #endif + CreateThread(0, 0, (LPTHREAD_START_ROUTINE)modify, 0, 0, 0); if (ownerid.length() != 10 || secret.length() != 64) { @@ -1454,7 +1453,11 @@ void error(std::string message) { return patched; } #elif defined(__i386) || defined(_M_IX86) - + // code submitted in pull request from https://github.com/autumnlikescode authored by https://github.com/Vasie1337/integrity-check + auto check_section_integrity() { + _integrity_check check; + return check.check_integrity(); + } #endif std::string checksum() @@ -1691,5 +1694,12 @@ DWORD64 FindPattern(BYTE* bMask, const char* szMask) } } #elif defined(__i386) || defined(_M_IX86) - +// code submitted in pull request from https://github.com/autumnlikescode authored by https://github.com/Vasie1337/integrity-check + void modify() { + while (true) { + if (check_section_integrity()) { + error(XorStr("check_section_integrity() failed, don't tamper with the program.")); + } + } + } #endif From 6734f52a4fc60070ab1b7517fd70557762ec9a43 Mon Sep 17 00:00:00 2001 From: autumn <102363146+autumnlikescode@users.noreply.github.com> Date: Fri, 19 Apr 2024 11:51:47 +1000 Subject: [PATCH 2/2] Add files via upload --- x86/lib/integrity.h | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 x86/lib/integrity.h diff --git a/x86/lib/integrity.h b/x86/lib/integrity.h new file mode 100644 index 0000000..511a572 --- /dev/null +++ b/x86/lib/integrity.h @@ -0,0 +1,63 @@ +#pragma once +#include +#include +#include + +typedef struct _integrity_check +{ + struct section { + std::uint8_t* name = {}; + void* address = {}; + std::uint32_t checksum = {}; + + bool operator==(section& other) + { + return checksum == other.checksum; + } + }; section _cached; + + _integrity_check() + { + _cached = get_text_section(reinterpret_cast(GetModuleHandle(nullptr))); + } + + std::uint32_t crc32(void* data, std::size_t size) + { + std::uint32_t result = {}; + + for (std::size_t index = {}; index < size; ++index) + result = _mm_crc32_u32(result, reinterpret_cast(data)[index]); + + return result; + } + + section get_text_section(std::uintptr_t module) + { + section text_section = {}; + + PIMAGE_DOS_HEADER dosheader = reinterpret_cast(module); + PIMAGE_NT_HEADERS nt_headers = reinterpret_cast(module + dosheader->e_lfanew); + + PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(nt_headers); + + for (int i = 0; i < nt_headers->FileHeader.NumberOfSections; i++, section++) + { + std::string name(reinterpret_cast(section->Name)); + if (name != ".text") + continue; + + void* address = reinterpret_cast(module + section->VirtualAddress); + text_section = { section->Name, address, crc32(address, section->Misc.VirtualSize) }; + } + return text_section; + } + /// + /// Checks .text integrity. + /// + /// Returns true if it has been changed. + bool check_integrity() + { + section section2 = get_text_section(reinterpret_cast(GetModuleHandle(nullptr))); + return (!(_cached == section2)); + } +}; \ No newline at end of file