This repository was archived by the owner on Apr 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathintegrity_check.h
More file actions
63 lines (52 loc) · 1.93 KB
/
Copy pathintegrity_check.h
File metadata and controls
63 lines (52 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once
#include <iostream>
#include <Windows.h>
#include <nmmintrin.h>
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<std::uintptr_t>(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<std::uint8_t*>(data)[index]);
return result;
}
section get_text_section(std::uintptr_t module)
{
section text_section = {};
PIMAGE_DOS_HEADER dosheader = reinterpret_cast<PIMAGE_DOS_HEADER>(module);
PIMAGE_NT_HEADERS nt_headers = reinterpret_cast<PIMAGE_NT_HEADERS>(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<char const*>(section->Name));
if (name != ".text")
continue;
void* address = reinterpret_cast<void*>(module + section->VirtualAddress);
text_section = { section->Name, address, crc32(address, section->Misc.VirtualSize) };
}
return text_section;
}
/// <summary>
/// Checks .text integrity.
/// </summary>
/// <returns>Returns true if it has been changed.</returns>
bool check_integrity()
{
section section2 = get_text_section(reinterpret_cast<std::uintptr_t>(GetModuleHandle(nullptr)));
return (!(_cached == section2));
}
};